[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 1 | // 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 | |
| 8 | #include "base/macros.h" |
| 9 | #include "base/memory/scoped_ptr.h" |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 10 | #include "base/values.h" |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 11 | #include "extensions/renderer/script_injection.h" |
[email protected] | cde18a2d | 2014-07-01 03:13:04 | [diff] [blame] | 12 | #include "url/gurl.h" |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 13 | |
| 14 | struct ExtensionMsg_ExecuteCode_Params; |
| 15 | |
[email protected] | cde18a2d | 2014-07-01 03:13:04 | [diff] [blame] | 16 | namespace content { |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 17 | class RenderFrame; |
[email protected] | cde18a2d | 2014-07-01 03:13:04 | [diff] [blame] | 18 | } |
| 19 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 20 | namespace extensions { |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 21 | |
| 22 | // A ScriptInjector to handle tabs.executeScript(). |
| 23 | class ProgrammaticScriptInjector : public ScriptInjector { |
| 24 | public: |
| 25 | ProgrammaticScriptInjector(const ExtensionMsg_ExecuteCode_Params& params, |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 26 | content::RenderFrame* render_frame); |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 27 | ~ProgrammaticScriptInjector() override; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 28 | |
| 29 | private: |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 30 | // ScriptInjector implementation. |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 31 | UserScript::InjectionType script_type() const override; |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 32 | bool ShouldExecuteInMainWorld() const override; |
| 33 | bool IsUserGesture() const override; |
| 34 | bool ExpectsResults() const override; |
| 35 | bool ShouldInjectJs(UserScript::RunLocation run_location) const override; |
| 36 | bool ShouldInjectCss(UserScript::RunLocation run_location) const override; |
| 37 | PermissionsData::AccessType CanExecuteOnFrame( |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 38 | const InjectionHost* injection_host, |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 39 | blink::WebLocalFrame* web_frame, |
rdevlin.cronin | f994d1e | 2015-06-03 22:28:19 | [diff] [blame] | 40 | int tab_id) const override; |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 41 | std::vector<blink::WebScriptSource> GetJsSources( |
mostynb | 0eac4e1b | 2014-10-03 16:32:19 | [diff] [blame] | 42 | UserScript::RunLocation run_location) const override; |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 43 | std::vector<std::string> GetCssSources( |
mostynb | 0eac4e1b | 2014-10-03 16:32:19 | [diff] [blame] | 44 | UserScript::RunLocation run_location) const override; |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 45 | void GetRunInfo(ScriptsRunInfo* scripts_run_info, |
| 46 | UserScript::RunLocation run_location) const override; |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 47 | void OnInjectionComplete(scoped_ptr<base::Value> execution_result, |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame^] | 48 | UserScript::RunLocation run_location, |
| 49 | content::RenderFrame* render_frame) override; |
| 50 | void OnWillNotInject(InjectFailureReason reason, |
| 51 | content::RenderFrame* render_frame) override; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 52 | |
| 53 | // Return the run location for this injector. |
| 54 | UserScript::RunLocation GetRunLocation() const; |
| 55 | |
| 56 | // Notify the browser that the script was injected (or never will be), and |
| 57 | // send along any results or errors. |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame^] | 58 | void Finish(const std::string& error, content::RenderFrame* render_frame); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 59 | |
| 60 | // The parameters for injecting the script. |
| 61 | scoped_ptr<ExtensionMsg_ExecuteCode_Params> params_; |
| 62 | |
[email protected] | cde18a2d | 2014-07-01 03:13:04 | [diff] [blame] | 63 | // The url of the frame into which we are injecting. |
| 64 | GURL url_; |
| 65 | |
rob | a9e6e642 | 2015-03-30 21:14:02 | [diff] [blame] | 66 | // The URL of the frame's origin. This is usually identical to |url_|, but |
| 67 | // could be different for e.g. about:blank URLs. Do not use this value to make |
| 68 | // security decisions, to avoid race conditions (e.g. due to navigation). |
| 69 | GURL effective_url_; |
| 70 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 71 | // The results of the script execution. |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 72 | base::ListValue results_; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 73 | |
| 74 | // Whether or not this script injection has finished. |
| 75 | bool finished_; |
| 76 | |
| 77 | DISALLOW_COPY_AND_ASSIGN(ProgrammaticScriptInjector); |
| 78 | }; |
| 79 | |
| 80 | } // namespace extensions |
| 81 | |
| 82 | #endif // EXTENSIONS_RENDERER_PROGRAMMATIC_SCRIPT_INJECTOR_H_ |