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 #include "chrome/browser/extensions/script_executor_impl.h" | 5 #include "chrome/browser/extensions/script_executor.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/pickle.h" | 9 #include "base/pickle.h" |
10 #include "chrome/common/extensions/extension_messages.h" | 10 #include "chrome/common/extensions/extension_messages.h" |
11 #include "content/public/browser/render_view_host.h" | 11 #include "content/public/browser/render_view_host.h" |
12 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
13 #include "content/public/browser/web_contents_observer.h" | 13 #include "content/public/browser/web_contents_observer.h" |
14 #include "ipc/ipc_message.h" | 14 #include "ipc/ipc_message.h" |
15 #include "ipc/ipc_message_macros.h" | 15 #include "ipc/ipc_message_macros.h" |
16 | 16 |
17 namespace extensions { | 17 namespace extensions { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 const char* kRendererDestroyed = "The tab was closed."; | 21 const char* kRendererDestroyed = "The tab was closed."; |
22 | 22 |
23 // A handler for a single injection request. On creation this will send the | 23 // A handler for a single injection request. On creation this will send the |
24 // injection request to the renderer, and it will be destroyed after either the | 24 // injection request to the renderer, and it will be destroyed after either the |
25 // corresponding response comes from the renderer, or the renderer is destroyed. | 25 // corresponding response comes from the renderer, or the renderer is destroyed. |
26 class Handler : public content::WebContentsObserver { | 26 class Handler : public content::WebContentsObserver { |
27 public: | 27 public: |
28 Handler(content::WebContents* web_contents, | 28 Handler(ObserverList<ScriptExecutor::Observer>* observer_list, |
29 content::WebContents* web_contents, | |
29 const ExtensionMsg_ExecuteCode_Params& params, | 30 const ExtensionMsg_ExecuteCode_Params& params, |
30 const ScriptExecutor::ExecuteScriptCallback& callback) | 31 const ScriptExecutor::ExecuteScriptCallback& callback) |
31 : content::WebContentsObserver(web_contents), | 32 : content::WebContentsObserver(web_contents), |
33 observer_list_(AsWeakPtr(observer_list)), | |
34 extension_id_(params.extension_id), | |
32 request_id_(params.request_id), | 35 request_id_(params.request_id), |
33 callback_(callback) { | 36 callback_(callback) { |
34 content::RenderViewHost* rvh = web_contents->GetRenderViewHost(); | 37 content::RenderViewHost* rvh = web_contents->GetRenderViewHost(); |
35 rvh->Send(new ExtensionMsg_ExecuteCode(rvh->GetRoutingID(), params)); | 38 rvh->Send(new ExtensionMsg_ExecuteCode(rvh->GetRoutingID(), params)); |
36 } | 39 } |
37 | 40 |
38 virtual ~Handler() {} | 41 virtual ~Handler() {} |
39 | 42 |
40 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | 43 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
41 // Unpack by hand to check the request_id, since there may be multiple | 44 // Unpack by hand to check the request_id, since there may be multiple |
(...skipping 18 matching lines...) Expand all Loading... | |
60 virtual void WebContentsDestroyed(content::WebContents* tab) OVERRIDE { | 63 virtual void WebContentsDestroyed(content::WebContents* tab) OVERRIDE { |
61 callback_.Run(false, -1, kRendererDestroyed); | 64 callback_.Run(false, -1, kRendererDestroyed); |
62 delete this; | 65 delete this; |
63 } | 66 } |
64 | 67 |
65 private: | 68 private: |
66 void OnExecuteCodeFinished(int request_id, | 69 void OnExecuteCodeFinished(int request_id, |
67 bool success, | 70 bool success, |
68 int32 page_id, | 71 int32 page_id, |
69 const std::string& error) { | 72 const std::string& error) { |
73 if (observer_list_) | |
74 FOR_EACH_OBSERVER(ScriptExecutor::Observer, *observer_list_, | |
75 OnExecuteScriptFinished(extension_id_, success, | |
76 page_id, error)); | |
not at google - send to devlin
2012/07/10 01:28:15
nit: surround multi-line condition bodies in {}
Jeffrey Yasskin
2012/07/10 18:34:58
Oops, done.
| |
77 | |
70 callback_.Run(success, page_id, error); | 78 callback_.Run(success, page_id, error); |
71 delete this; | 79 delete this; |
72 } | 80 } |
73 | 81 |
82 base::WeakPtr<ObserverList<ScriptExecutor::Observer> > observer_list_; | |
83 std::string extension_id_; | |
74 int request_id_; | 84 int request_id_; |
75 ScriptExecutor::ExecuteScriptCallback callback_; | 85 ScriptExecutor::ExecuteScriptCallback callback_; |
76 }; | 86 }; |
77 | 87 |
78 } // namespace | 88 } // namespace |
79 | 89 |
80 ScriptExecutorImpl::ScriptExecutorImpl( | 90 ScriptExecutor::Observer::Observer(ScriptExecutor* script_executor) |
91 : script_executor_(*script_executor){ | |
92 script_executor_.AddObserver(this); | |
93 } | |
94 | |
95 ScriptExecutor::Observer::~Observer() { | |
96 script_executor_.RemoveObserver(this); | |
97 } | |
98 | |
99 ScriptExecutor::ScriptExecutor( | |
81 content::WebContents* web_contents) | 100 content::WebContents* web_contents) |
not at google - send to devlin
2012/07/10 01:28:15
nit: 1 line
Jeffrey Yasskin
2012/07/10 18:34:58
Oops again. Oh wait, this one was like that when I
| |
82 : next_request_id_(0), | 101 : next_request_id_(0), |
83 web_contents_(web_contents) {} | 102 web_contents_(web_contents) {} |
84 | 103 |
85 ScriptExecutorImpl::~ScriptExecutorImpl() {} | 104 ScriptExecutor::~ScriptExecutor() {} |
86 | 105 |
87 void ScriptExecutorImpl::ExecuteScript( | 106 void ScriptExecutor::ExecuteScript( |
88 const std::string& extension_id, | 107 const std::string& extension_id, |
89 ScriptExecutor::ScriptType script_type, | 108 ScriptExecutor::ScriptType script_type, |
90 const std::string& code, | 109 const std::string& code, |
91 ScriptExecutor::FrameScope frame_scope, | 110 ScriptExecutor::FrameScope frame_scope, |
92 UserScript::RunLocation run_at, | 111 UserScript::RunLocation run_at, |
93 ScriptExecutor::WorldType world_type, | 112 ScriptExecutor::WorldType world_type, |
94 const ExecuteScriptCallback& callback) { | 113 const ExecuteScriptCallback& callback) { |
95 ExtensionMsg_ExecuteCode_Params params; | 114 ExtensionMsg_ExecuteCode_Params params; |
96 params.request_id = next_request_id_++; | 115 params.request_id = next_request_id_++; |
97 params.extension_id = extension_id; | 116 params.extension_id = extension_id; |
98 params.is_javascript = (script_type == JAVASCRIPT); | 117 params.is_javascript = (script_type == JAVASCRIPT); |
99 params.code = code; | 118 params.code = code; |
100 params.all_frames = (frame_scope == ALL_FRAMES); | 119 params.all_frames = (frame_scope == ALL_FRAMES); |
101 params.run_at = (int) run_at; | 120 params.run_at = (int) run_at; |
102 params.in_main_world = (world_type == MAIN_WORLD); | 121 params.in_main_world = (world_type == MAIN_WORLD); |
103 | 122 |
104 // Handler handles IPCs and deletes itself on completion. | 123 // Handler handles IPCs and deletes itself on completion. |
105 new Handler(web_contents_, params, callback); | 124 new Handler(&observer_list_, web_contents_, params, callback); |
106 } | 125 } |
107 | 126 |
108 } // namespace extensions | 127 } // namespace extensions |
OLD | NEW |