[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 1 | // Copyright (c) 2012 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 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 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/callback_forward.h" |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 11 | #include "base/observer_list.h" |
[email protected] | 09ae70d4 | 2012-11-07 00:42:09 | [diff] [blame] | 12 | #include "chrome/browser/extensions/tab_helper.h" |
[email protected] | 49d9b14 | 2013-07-19 08:50:27 | [diff] [blame^] | 13 | #include "extensions/common/user_script.h" |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 14 | |
[email protected] | 7f3b91e | 2012-08-07 08:05:03 | [diff] [blame] | 15 | class GURL; |
| 16 | |
[email protected] | cab8cd98 | 2012-07-20 20:57:03 | [diff] [blame] | 17 | namespace base { |
| 18 | class ListValue; |
| 19 | } // namespace base |
| 20 | |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 21 | namespace content { |
| 22 | class WebContents; |
| 23 | } |
| 24 | |
| 25 | namespace extensions { |
| 26 | |
| 27 | // Interface for executing extension content scripts (e.g. executeScript) as |
| 28 | // described by the ExtensionMsg_ExecuteCode_Params IPC, and notifying the |
| 29 | // caller when responded with ExtensionHostMsg_ExecuteCodeFinished. |
| 30 | class ScriptExecutor { |
| 31 | public: |
[email protected] | 09ae70d4 | 2012-11-07 00:42:09 | [diff] [blame] | 32 | ScriptExecutor( |
| 33 | content::WebContents* web_contents, |
| 34 | // |script_observers| is assumed to be owned by |this|'s owner, and in |
| 35 | // such a way that |this| is destroyed first. |
| 36 | ObserverList<TabHelper::ScriptExecutionObserver>* script_observers); |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 37 | |
| 38 | ~ScriptExecutor(); |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 39 | |
| 40 | // The type of script being injected. |
| 41 | enum ScriptType { |
| 42 | JAVASCRIPT, |
| 43 | CSS, |
| 44 | }; |
| 45 | |
| 46 | // The scope of the script injection across the frames. |
| 47 | enum FrameScope { |
| 48 | TOP_FRAME, |
| 49 | ALL_FRAMES, |
| 50 | }; |
| 51 | |
| 52 | // The type of world to inject into (main world, or its own isolated world). |
| 53 | enum WorldType { |
| 54 | MAIN_WORLD, |
| 55 | ISOLATED_WORLD, |
| 56 | }; |
| 57 | |
[email protected] | 7f3b91e | 2012-08-07 08:05:03 | [diff] [blame] | 58 | // Callback from ExecuteScript. The arguments are (error, on_page_id, on_url, |
| 59 | // result). Success is implied by an empty error. |
| 60 | typedef base::Callback<void(const std::string&, int32, const GURL&, |
[email protected] | cab8cd98 | 2012-07-20 20:57:03 | [diff] [blame] | 61 | const base::ListValue&)> |
[email protected] | 28a69d3 | 2012-05-30 07:58:18 | [diff] [blame] | 62 | ExecuteScriptCallback; |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 63 | |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 64 | // Executes a script. The arguments match ExtensionMsg_ExecuteCode_Params in |
| 65 | // extension_messages.h (request_id is populated automatically). |
| 66 | // |
| 67 | // |callback| will always be called even if the IPC'd renderer is destroyed |
| 68 | // before a response is received (in this case the callback will be with a |
| 69 | // failure and appropriate error message). |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 70 | void ExecuteScript(const std::string& extension_id, |
| 71 | ScriptType script_type, |
| 72 | const std::string& code, |
| 73 | FrameScope frame_scope, |
| 74 | UserScript::RunLocation run_at, |
| 75 | WorldType world_type, |
[email protected] | 44703cc7 | 2013-01-24 04:56:06 | [diff] [blame] | 76 | bool is_web_view, |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 77 | const ExecuteScriptCallback& callback); |
| 78 | |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 79 | private: |
| 80 | // The next value to use for request_id in ExtensionMsg_ExecuteCode_Params. |
| 81 | int next_request_id_; |
| 82 | |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 83 | content::WebContents* web_contents_; |
| 84 | |
[email protected] | 09ae70d4 | 2012-11-07 00:42:09 | [diff] [blame] | 85 | ObserverList<TabHelper::ScriptExecutionObserver>* script_observers_; |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | } // namespace extensions |
| 89 | |
| 90 | #endif // CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_ |