blob: 4820992c71aa127995e0ec3701a8e5fd4b5b71b9 [file] [log] [blame]
[email protected]c11e6592014-06-27 17:07:341// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef EXTENSIONS_RENDERER_PROGRAMMATIC_SCRIPT_INJECTOR_H_
6#define EXTENSIONS_RENDERER_PROGRAMMATIC_SCRIPT_INJECTOR_H_
7
dchengf6f80662016-04-20 20:26:048#include <memory>
9
[email protected]c11e6592014-06-27 17:07:3410#include "base/macros.h"
rdevlin.cronin4bb32d72015-06-02 21:55:0111#include "base/values.h"
[email protected]c11e6592014-06-27 17:07:3412#include "extensions/renderer/script_injection.h"
[email protected]cde18a2d2014-07-01 03:13:0413#include "url/gurl.h"
[email protected]c11e6592014-06-27 17:07:3414
15struct ExtensionMsg_ExecuteCode_Params;
16
[email protected]cde18a2d2014-07-01 03:13:0417namespace content {
rdevlin.cronin3e11c9862015-06-04 19:54:2518class RenderFrame;
[email protected]cde18a2d2014-07-01 03:13:0419}
20
[email protected]c11e6592014-06-27 17:07:3421namespace extensions {
[email protected]c11e6592014-06-27 17:07:3422
23// A ScriptInjector to handle tabs.executeScript().
24class ProgrammaticScriptInjector : public ScriptInjector {
25 public:
jam69e71152016-11-02 01:15:4326 explicit ProgrammaticScriptInjector(
27 const ExtensionMsg_ExecuteCode_Params& params);
dcheng9168b2f2014-10-21 12:38:2428 ~ProgrammaticScriptInjector() override;
[email protected]c11e6592014-06-27 17:07:3429
30 private:
31 // ScriptInjector implementation.
dcheng9168b2f2014-10-21 12:38:2432 UserScript::InjectionType script_type() const override;
dcheng9168b2f2014-10-21 12:38:2433 bool ShouldExecuteInMainWorld() const override;
34 bool IsUserGesture() const override;
Manish Jethani9494d722018-01-20 00:28:4735 base::Optional<CSSOrigin> GetCssOrigin() const override;
Manish Jethaniff6ff852018-02-23 07:24:5536 const base::Optional<std::string> GetInjectionKey() const override;
dcheng9168b2f2014-10-21 12:38:2437 bool ExpectsResults() const override;
catmullingsd4faad4f2016-09-08 19:55:3038 bool ShouldInjectJs(
39 UserScript::RunLocation run_location,
40 const std::set<std::string>& executing_scripts) const override;
41 bool ShouldInjectCss(
42 UserScript::RunLocation run_location,
43 const std::set<std::string>& injected_stylesheets) const override;
Devlin Cronin3e532b82018-05-03 21:27:1944 PermissionsData::PageAccess CanExecuteOnFrame(
hanxia5c856cf2015-02-13 20:51:5845 const InjectionHost* injection_host,
rdevlin.cronin3e11c9862015-06-04 19:54:2546 blink::WebLocalFrame* web_frame,
jam69e71152016-11-02 01:15:4347 int tab_id) override;
dcheng9168b2f2014-10-21 12:38:2448 std::vector<blink::WebScriptSource> GetJsSources(
catmullingsd4faad4f2016-09-08 19:55:3049 UserScript::RunLocation run_location,
50 std::set<std::string>* executing_scripts,
51 size_t* num_injected_js_scripts) const override;
lazyboy49cc0b3a2016-08-18 21:55:1252 std::vector<blink::WebString> GetCssSources(
catmullingsd4faad4f2016-09-08 19:55:3053 UserScript::RunLocation run_location,
54 std::set<std::string>* injected_stylesheets,
55 size_t* num_injected_stylesheets) const override;
dchengf6f80662016-04-20 20:26:0456 void OnInjectionComplete(std::unique_ptr<base::Value> execution_result,
rdevlin.cronind533be962015-10-02 17:01:1857 UserScript::RunLocation run_location,
58 content::RenderFrame* render_frame) override;
59 void OnWillNotInject(InjectFailureReason reason,
60 content::RenderFrame* render_frame) override;
[email protected]c11e6592014-06-27 17:07:3461
rob52277c82016-02-07 17:28:5762 // Whether it is safe to include information about the URL in error messages.
63 bool CanShowUrlInError() const;
64
[email protected]c11e6592014-06-27 17:07:3465 // Notify the browser that the script was injected (or never will be), and
66 // send along any results or errors.
rdevlin.cronind533be962015-10-02 17:01:1867 void Finish(const std::string& error, content::RenderFrame* render_frame);
[email protected]c11e6592014-06-27 17:07:3468
69 // The parameters for injecting the script.
dchengf6f80662016-04-20 20:26:0470 std::unique_ptr<ExtensionMsg_ExecuteCode_Params> params_;
[email protected]c11e6592014-06-27 17:07:3471
[email protected]cde18a2d2014-07-01 03:13:0472 // The url of the frame into which we are injecting.
73 GURL url_;
74
rob52277c82016-02-07 17:28:5775 // The serialization of the frame's origin if the frame is an about:-URL. This
76 // is used to provide user-friendly messages.
77 std::string origin_for_about_error_;
roba9e6e6422015-03-30 21:14:0278
[email protected]c11e6592014-06-27 17:07:3479 // The results of the script execution.
rdevlin.cronin4bb32d72015-06-02 21:55:0180 base::ListValue results_;
[email protected]c11e6592014-06-27 17:07:3481
82 // Whether or not this script injection has finished.
83 bool finished_;
84
85 DISALLOW_COPY_AND_ASSIGN(ProgrammaticScriptInjector);
86};
87
88} // namespace extensions
89
90#endif // EXTENSIONS_RENDERER_PROGRAMMATIC_SCRIPT_INJECTOR_H_