[email protected] | 16a4206f | 2014-08-15 09:44:43 | [diff] [blame^] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 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 CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_ |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 7 | |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 8 | #include "base/callback_forward.h" |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 9 | #include "base/observer_list.h" |
[email protected] | 49d9b14 | 2013-07-19 08:50:27 | [diff] [blame] | 10 | #include "extensions/common/user_script.h" |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 11 | |
[email protected] | 7f3b91e | 2012-08-07 08:05:03 | [diff] [blame] | 12 | class GURL; |
[email protected] | ac02ac5 | 2014-05-20 01:11:26 | [diff] [blame] | 13 | struct ExtensionMsg_ExecuteCode_Params; |
[email protected] | 7f3b91e | 2012-08-07 08:05:03 | [diff] [blame] | 14 | |
[email protected] | cab8cd98 | 2012-07-20 20:57:03 | [diff] [blame] | 15 | namespace base { |
| 16 | class ListValue; |
| 17 | } // namespace base |
| 18 | |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 19 | namespace content { |
| 20 | class WebContents; |
| 21 | } |
| 22 | |
| 23 | namespace extensions { |
[email protected] | 16a4206f | 2014-08-15 09:44:43 | [diff] [blame^] | 24 | class ScriptExecutionObserver; |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 25 | |
| 26 | // Interface for executing extension content scripts (e.g. executeScript) as |
| 27 | // described by the ExtensionMsg_ExecuteCode_Params IPC, and notifying the |
| 28 | // caller when responded with ExtensionHostMsg_ExecuteCodeFinished. |
| 29 | class ScriptExecutor { |
| 30 | public: |
[email protected] | 09ae70d4 | 2012-11-07 00:42:09 | [diff] [blame] | 31 | ScriptExecutor( |
| 32 | content::WebContents* web_contents, |
| 33 | // |script_observers| is assumed to be owned by |this|'s owner, and in |
| 34 | // such a way that |this| is destroyed first. |
[email protected] | 16a4206f | 2014-08-15 09:44:43 | [diff] [blame^] | 35 | ObserverList<ScriptExecutionObserver>* script_observers); |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 36 | |
| 37 | ~ScriptExecutor(); |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 38 | |
| 39 | // The type of script being injected. |
| 40 | enum ScriptType { |
| 41 | JAVASCRIPT, |
| 42 | CSS, |
| 43 | }; |
| 44 | |
| 45 | // The scope of the script injection across the frames. |
| 46 | enum FrameScope { |
| 47 | TOP_FRAME, |
| 48 | ALL_FRAMES, |
| 49 | }; |
| 50 | |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 51 | // Whether to insert the script in about: frames when its origin matches |
| 52 | // the extension's host permissions. |
| 53 | enum MatchAboutBlank { |
| 54 | DONT_MATCH_ABOUT_BLANK, |
| 55 | MATCH_ABOUT_BLANK, |
| 56 | }; |
| 57 | |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 58 | // The type of world to inject into (main world, or its own isolated world). |
| 59 | enum WorldType { |
| 60 | MAIN_WORLD, |
| 61 | ISOLATED_WORLD, |
| 62 | }; |
| 63 | |
[email protected] | 88c6f5c | 2013-08-28 04:08:41 | [diff] [blame] | 64 | // The type of process the target is. |
| 65 | enum ProcessType { |
| 66 | DEFAULT_PROCESS, |
| 67 | WEB_VIEW_PROCESS, |
| 68 | }; |
| 69 | |
| 70 | // The type of result the caller is interested in. |
| 71 | enum ResultType { |
| 72 | NO_RESULT, |
| 73 | JSON_SERIALIZED_RESULT, |
| 74 | }; |
| 75 | |
[email protected] | 0f8bf4f | 2014-07-02 19:26:53 | [diff] [blame] | 76 | // Callback from ExecuteScript. The arguments are (error, on_url, result). |
| 77 | // Success is implied by an empty error. |
[email protected] | 16a4206f | 2014-08-15 09:44:43 | [diff] [blame^] | 78 | typedef base::Callback< |
| 79 | void(const std::string&, const GURL&, const base::ListValue&)> |
[email protected] | 28a69d3 | 2012-05-30 07:58:18 | [diff] [blame] | 80 | ExecuteScriptCallback; |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 81 | |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 82 | // Executes a script. The arguments match ExtensionMsg_ExecuteCode_Params in |
| 83 | // extension_messages.h (request_id is populated automatically). |
| 84 | // |
| 85 | // |callback| will always be called even if the IPC'd renderer is destroyed |
| 86 | // before a response is received (in this case the callback will be with a |
| 87 | // failure and appropriate error message). |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 88 | void ExecuteScript(const std::string& extension_id, |
| 89 | ScriptType script_type, |
| 90 | const std::string& code, |
| 91 | FrameScope frame_scope, |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 92 | MatchAboutBlank match_about_blank, |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 93 | UserScript::RunLocation run_at, |
| 94 | WorldType world_type, |
[email protected] | 88c6f5c | 2013-08-28 04:08:41 | [diff] [blame] | 95 | ProcessType process_type, |
[email protected] | 6f451a4 | 2014-04-10 17:12:47 | [diff] [blame] | 96 | const GURL& webview_src, |
[email protected] | a7074d1 | 2013-10-03 23:09:30 | [diff] [blame] | 97 | const GURL& file_url, |
[email protected] | 0df4943 | 2014-03-04 01:02:50 | [diff] [blame] | 98 | bool user_gesture, |
[email protected] | 88c6f5c | 2013-08-28 04:08:41 | [diff] [blame] | 99 | ResultType result_type, |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 100 | const ExecuteScriptCallback& callback); |
| 101 | |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 102 | private: |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 103 | // The next value to use for request_id in ExtensionMsg_ExecuteCode_Params. |
| 104 | int next_request_id_; |
| 105 | |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 106 | content::WebContents* web_contents_; |
| 107 | |
[email protected] | 16a4206f | 2014-08-15 09:44:43 | [diff] [blame^] | 108 | ObserverList<ScriptExecutionObserver>* script_observers_; |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | } // namespace extensions |
| 112 | |
| 113 | #endif // CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_ |