[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] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 12 | #include "chrome/common/extensions/user_script.h" |
| 13 | |
[email protected] | 7f3b91e | 2012-08-07 08:05:03 | [diff] [blame^] | 14 | class GURL; |
| 15 | |
[email protected] | cab8cd98 | 2012-07-20 20:57:03 | [diff] [blame] | 16 | namespace base { |
| 17 | class ListValue; |
| 18 | } // namespace base |
| 19 | |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 20 | namespace content { |
| 21 | class WebContents; |
| 22 | } |
| 23 | |
| 24 | namespace extensions { |
| 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] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 31 | explicit ScriptExecutor(content::WebContents* web_contents); |
| 32 | |
| 33 | ~ScriptExecutor(); |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 34 | |
| 35 | // The type of script being injected. |
| 36 | enum ScriptType { |
| 37 | JAVASCRIPT, |
| 38 | CSS, |
| 39 | }; |
| 40 | |
| 41 | // The scope of the script injection across the frames. |
| 42 | enum FrameScope { |
| 43 | TOP_FRAME, |
| 44 | ALL_FRAMES, |
| 45 | }; |
| 46 | |
| 47 | // The type of world to inject into (main world, or its own isolated world). |
| 48 | enum WorldType { |
| 49 | MAIN_WORLD, |
| 50 | ISOLATED_WORLD, |
| 51 | }; |
| 52 | |
[email protected] | 7f3b91e | 2012-08-07 08:05:03 | [diff] [blame^] | 53 | // Callback from ExecuteScript. The arguments are (error, on_page_id, on_url, |
| 54 | // result). Success is implied by an empty error. |
| 55 | typedef base::Callback<void(const std::string&, int32, const GURL&, |
[email protected] | cab8cd98 | 2012-07-20 20:57:03 | [diff] [blame] | 56 | const base::ListValue&)> |
[email protected] | 28a69d3 | 2012-05-30 07:58:18 | [diff] [blame] | 57 | ExecuteScriptCallback; |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 58 | |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 59 | class Observer { |
| 60 | public: |
| 61 | // Automatically observes and unobserves *script_executor on construction |
| 62 | // and destruction. *script_executor must outlive *this. |
| 63 | explicit Observer(ScriptExecutor* script_executor); |
| 64 | virtual ~Observer(); |
| 65 | |
| 66 | virtual void OnExecuteScriptFinished(const std::string& extension_id, |
[email protected] | cab8cd98 | 2012-07-20 20:57:03 | [diff] [blame] | 67 | const std::string& error, |
[email protected] | 7f3b91e | 2012-08-07 08:05:03 | [diff] [blame^] | 68 | int32 on_page_id, |
| 69 | const GURL& on_url, |
[email protected] | cab8cd98 | 2012-07-20 20:57:03 | [diff] [blame] | 70 | const base::ListValue&) = 0; |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 71 | private: |
| 72 | ScriptExecutor& script_executor_; |
| 73 | }; |
| 74 | |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 75 | // Executes a script. The arguments match ExtensionMsg_ExecuteCode_Params in |
| 76 | // extension_messages.h (request_id is populated automatically). |
| 77 | // |
| 78 | // |callback| will always be called even if the IPC'd renderer is destroyed |
| 79 | // before a response is received (in this case the callback will be with a |
| 80 | // failure and appropriate error message). |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 81 | void ExecuteScript(const std::string& extension_id, |
| 82 | ScriptType script_type, |
| 83 | const std::string& code, |
| 84 | FrameScope frame_scope, |
| 85 | UserScript::RunLocation run_at, |
| 86 | WorldType world_type, |
| 87 | const ExecuteScriptCallback& callback); |
| 88 | |
| 89 | void AddObserver(Observer* obs) { |
| 90 | observer_list_.AddObserver(obs); |
| 91 | } |
| 92 | |
| 93 | void RemoveObserver(Observer* obs) { |
| 94 | observer_list_.RemoveObserver(obs); |
| 95 | } |
| 96 | |
| 97 | private: |
| 98 | // The next value to use for request_id in ExtensionMsg_ExecuteCode_Params. |
| 99 | int next_request_id_; |
| 100 | |
| 101 | // The WebContents this is bound to. |
| 102 | content::WebContents* web_contents_; |
| 103 | |
| 104 | ObserverList<Observer> observer_list_; |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | } // namespace extensions |
| 108 | |
| 109 | #endif // CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_ |