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" | |
12 #include "chrome/common/extensions/user_script.h" | 13 #include "chrome/common/extensions/user_script.h" |
13 | 14 |
14 namespace content { | 15 namespace content { |
15 class WebContents; | 16 class WebContents; |
16 } | 17 } |
17 | 18 |
18 namespace extensions { | 19 namespace extensions { |
19 | 20 |
20 // Interface for executing extension content scripts (e.g. executeScript) as | 21 // Interface for executing extension content scripts (e.g. executeScript) as |
21 // described by the ExtensionMsg_ExecuteCode_Params IPC, and notifying the | 22 // described by the ExtensionMsg_ExecuteCode_Params IPC, and notifying the |
22 // caller when responded with ExtensionHostMsg_ExecuteCodeFinished. | 23 // caller when responded with ExtensionHostMsg_ExecuteCodeFinished. |
23 class ScriptExecutor { | 24 class ScriptExecutor { |
24 public: | 25 public: |
25 virtual ~ScriptExecutor() {} | 26 explicit ScriptExecutor(content::WebContents* web_contents); |
27 | |
28 ~ScriptExecutor(); | |
26 | 29 |
27 // The type of script being injected. | 30 // The type of script being injected. |
28 enum ScriptType { | 31 enum ScriptType { |
29 JAVASCRIPT, | 32 JAVASCRIPT, |
30 CSS, | 33 CSS, |
31 }; | 34 }; |
32 | 35 |
33 // The scope of the script injection across the frames. | 36 // The scope of the script injection across the frames. |
34 enum FrameScope { | 37 enum FrameScope { |
35 TOP_FRAME, | 38 TOP_FRAME, |
36 ALL_FRAMES, | 39 ALL_FRAMES, |
37 }; | 40 }; |
38 | 41 |
39 // The type of world to inject into (main world, or its own isolated world). | 42 // The type of world to inject into (main world, or its own isolated world). |
40 enum WorldType { | 43 enum WorldType { |
41 MAIN_WORLD, | 44 MAIN_WORLD, |
42 ISOLATED_WORLD, | 45 ISOLATED_WORLD, |
43 }; | 46 }; |
44 | 47 |
45 // Callback from ExecuteScript. The arguments are (success, page_id, error). | 48 // Callback from ExecuteScript. The arguments are (success, page_id, error). |
46 // page_id is only valid on success, error is only valid on !success. | 49 // page_id is only valid on success, error is only valid on !success. |
47 typedef base::Callback<void(bool, int32, const std::string&)> | 50 typedef base::Callback<void(bool, int32, const std::string&)> |
48 ExecuteScriptCallback; | 51 ExecuteScriptCallback; |
49 | 52 |
53 class Observer { | |
54 public: | |
55 // Automatically observes and unobserves *script_executor on construction | |
56 // and destruction. *script_executor must outlive *this. | |
not at google - send to devlin
2012/07/10 01:28:15
nit: 1 space not 2
Jeffrey Yasskin
2012/07/10 18:34:58
Done. We're so inconsistent on this, but of course
| |
57 explicit Observer(ScriptExecutor* script_executor); | |
not at google - send to devlin
2012/07/10 01:28:15
That lifetime thing is neat... I wonder why we don
Jeffrey Yasskin
2012/07/10 18:34:58
Several observers do. I was imitating WebContentsO
Jeffrey Yasskin
2012/07/10 19:14:44
By 3 lines, which I think isn't worth it. If Scope
| |
58 virtual ~Observer(); | |
59 | |
60 virtual void OnExecuteScriptFinished(const std::string& extension_id, | |
61 bool success, | |
62 int32 page_id, | |
63 const std::string& error) = 0; | |
64 private: | |
65 ScriptExecutor& script_executor_; | |
not at google - send to devlin
2012/07/10 01:28:15
Being passed into here as a pointer, why not hold
Jeffrey Yasskin
2012/07/10 18:34:58
It can't be NULL, and it's constant through the li
| |
66 }; | |
67 | |
50 // Executes a script. The arguments match ExtensionMsg_ExecuteCode_Params in | 68 // Executes a script. The arguments match ExtensionMsg_ExecuteCode_Params in |
51 // extension_messages.h (request_id is populated automatically). | 69 // extension_messages.h (request_id is populated automatically). |
52 // | 70 // |
53 // |callback| will always be called even if the IPC'd renderer is destroyed | 71 // |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 | 72 // before a response is received (in this case the callback will be with a |
55 // failure and appropriate error message). | 73 // failure and appropriate error message). |
56 virtual void ExecuteScript(const std::string& extension_id, | 74 void ExecuteScript(const std::string& extension_id, |
57 ScriptType script_type, | 75 ScriptType script_type, |
58 const std::string& code, | 76 const std::string& code, |
59 FrameScope frame_scope, | 77 FrameScope frame_scope, |
60 UserScript::RunLocation run_at, | 78 UserScript::RunLocation run_at, |
61 WorldType world_type, | 79 WorldType world_type, |
62 const ExecuteScriptCallback& callback) = 0; | 80 const ExecuteScriptCallback& callback); |
81 | |
82 void AddObserver(Observer* obs) { | |
83 observer_list_.AddObserver(obs); | |
84 } | |
85 | |
86 void RemoveObserver(Observer* obs) { | |
87 observer_list_.RemoveObserver(obs); | |
88 } | |
89 | |
90 private: | |
91 // The next value to use for request_id in ExtensionMsg_ExecuteCode_Params. | |
92 int next_request_id_; | |
93 | |
94 // The WebContents this is bound to. | |
95 content::WebContents* web_contents_; | |
96 | |
97 ObserverList<Observer> observer_list_; | |
63 }; | 98 }; |
64 | 99 |
65 } // namespace extensions | 100 } // namespace extensions |
66 | 101 |
67 #endif // CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_ | 102 #endif // CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_ |
OLD | NEW |