OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
12 #include "base/observer_list.h" | |
13 #include "base/scoped_observer.h" | |
12 #include "chrome/common/extensions/user_script.h" | 14 #include "chrome/common/extensions/user_script.h" |
13 | 15 |
14 namespace content { | 16 namespace content { |
15 class WebContents; | 17 class WebContents; |
16 } | 18 } |
17 | 19 |
18 namespace extensions { | 20 namespace extensions { |
19 | 21 |
20 // Interface for executing extension content scripts (e.g. executeScript) as | 22 // Interface for executing extension content scripts (e.g. executeScript) as |
21 // described by the ExtensionMsg_ExecuteCode_Params IPC, and notifying the | 23 // described by the ExtensionMsg_ExecuteCode_Params IPC, and notifying the |
22 // caller when responded with ExtensionHostMsg_ExecuteCodeFinished. | 24 // caller when responded with ExtensionHostMsg_ExecuteCodeFinished. |
23 class ScriptExecutor { | 25 class ScriptExecutor { |
24 public: | 26 public: |
25 virtual ~ScriptExecutor() {} | 27 explicit ScriptExecutor(content::WebContents* web_contents); |
28 | |
29 ~ScriptExecutor(); | |
26 | 30 |
27 // The type of script being injected. | 31 // The type of script being injected. |
28 enum ScriptType { | 32 enum ScriptType { |
29 JAVASCRIPT, | 33 JAVASCRIPT, |
30 CSS, | 34 CSS, |
31 }; | 35 }; |
32 | 36 |
33 // The scope of the script injection across the frames. | 37 // The scope of the script injection across the frames. |
34 enum FrameScope { | 38 enum FrameScope { |
35 TOP_FRAME, | 39 TOP_FRAME, |
36 ALL_FRAMES, | 40 ALL_FRAMES, |
37 }; | 41 }; |
38 | 42 |
39 // The type of world to inject into (main world, or its own isolated world). | 43 // The type of world to inject into (main world, or its own isolated world). |
40 enum WorldType { | 44 enum WorldType { |
41 MAIN_WORLD, | 45 MAIN_WORLD, |
42 ISOLATED_WORLD, | 46 ISOLATED_WORLD, |
43 }; | 47 }; |
44 | 48 |
45 // Callback from ExecuteScript. The arguments are (success, page_id, error). | 49 // Callback from ExecuteScript. The arguments are (success, page_id, error). |
46 // page_id is only valid on success, error is only valid on !success. | 50 // page_id is only valid on success, error is only valid on !success. |
47 typedef base::Callback<void(bool, int32, const std::string&)> | 51 typedef base::Callback<void(bool, int32, const std::string&)> |
48 ExecuteScriptCallback; | 52 ExecuteScriptCallback; |
49 | 53 |
54 class Observer { | |
55 public: | |
56 // Automatically observes and unobserves *script_executor on construction | |
57 // and destruction. *script_executor must outlive *this. | |
58 explicit Observer(ScriptExecutor* script_executor); | |
59 ~Observer() {} | |
Jeffrey Yasskin
2012/07/10 19:14:44
This explicit destructor is required by chromium's
| |
60 | |
61 virtual void OnExecuteScriptFinished(const std::string& extension_id, | |
62 bool success, | |
63 int32 page_id, | |
64 const std::string& error) = 0; | |
65 private: | |
66 ScopedObserver<ScriptExecutor, Observer> scoped_observer_; | |
67 }; | |
68 | |
50 // Executes a script. The arguments match ExtensionMsg_ExecuteCode_Params in | 69 // Executes a script. The arguments match ExtensionMsg_ExecuteCode_Params in |
51 // extension_messages.h (request_id is populated automatically). | 70 // extension_messages.h (request_id is populated automatically). |
52 // | 71 // |
53 // |callback| will always be called even if the IPC'd renderer is destroyed | 72 // |callback| will always be called even if the IPC'd renderer is destroyed |
54 // before a response is received (in this case the callback will be with a | 73 // before a response is received (in this case the callback will be with a |
55 // failure and appropriate error message). | 74 // failure and appropriate error message). |
56 virtual void ExecuteScript(const std::string& extension_id, | 75 void ExecuteScript(const std::string& extension_id, |
57 ScriptType script_type, | 76 ScriptType script_type, |
58 const std::string& code, | 77 const std::string& code, |
59 FrameScope frame_scope, | 78 FrameScope frame_scope, |
60 UserScript::RunLocation run_at, | 79 UserScript::RunLocation run_at, |
61 WorldType world_type, | 80 WorldType world_type, |
62 const ExecuteScriptCallback& callback) = 0; | 81 const ExecuteScriptCallback& callback); |
82 | |
83 void AddObserver(Observer* obs) { | |
84 observer_list_.AddObserver(obs); | |
85 } | |
86 | |
87 void RemoveObserver(Observer* obs) { | |
88 observer_list_.RemoveObserver(obs); | |
89 } | |
90 | |
91 private: | |
92 // The next value to use for request_id in ExtensionMsg_ExecuteCode_Params. | |
93 int next_request_id_; | |
94 | |
95 // The WebContents this is bound to. | |
96 content::WebContents* web_contents_; | |
97 | |
98 ObserverList<Observer> observer_list_; | |
63 }; | 99 }; |
64 | 100 |
65 } // namespace extensions | 101 } // namespace extensions |
66 | 102 |
67 #endif // CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_ | 103 #endif // CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_ |
OLD | NEW |