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/execute_code_in_tab_function.h" | 5 #include "chrome/browser/extensions/execute_code_in_tab_function.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
11 #include "chrome/browser/extensions/extension_tab_util.h" | 11 #include "chrome/browser/extensions/extension_tab_util.h" |
12 #include "chrome/browser/extensions/extension_tabs_module.h" | 12 #include "chrome/browser/extensions/extension_tabs_module.h" |
13 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 13 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
14 #include "chrome/browser/extensions/file_reader.h" | 14 #include "chrome/browser/extensions/file_reader.h" |
| 15 #include "chrome/browser/extensions/script_executor.h" |
15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
18 #include "chrome/common/extensions/extension.h" | 19 #include "chrome/common/extensions/extension.h" |
19 #include "chrome/common/extensions/extension_constants.h" | 20 #include "chrome/common/extensions/extension_constants.h" |
20 #include "chrome/common/extensions/extension_error_utils.h" | 21 #include "chrome/common/extensions/extension_error_utils.h" |
21 #include "chrome/common/extensions/extension_file_util.h" | 22 #include "chrome/common/extensions/extension_file_util.h" |
22 #include "chrome/common/extensions/extension_l10n_util.h" | 23 #include "chrome/common/extensions/extension_l10n_util.h" |
23 #include "chrome/common/extensions/extension_manifest_constants.h" | 24 #include "chrome/common/extensions/extension_manifest_constants.h" |
24 #include "chrome/common/extensions/extension_message_bundle.h" | 25 #include "chrome/common/extensions/extension_message_bundle.h" |
25 #include "chrome/common/extensions/extension_messages.h" | 26 #include "chrome/common/extensions/extension_messages.h" |
26 #include "content/public/browser/render_view_host.h" | 27 #include "content/public/browser/render_view_host.h" |
27 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
28 | 29 |
29 using content::BrowserThread; | 30 using content::BrowserThread; |
| 31 using extensions::ScriptExecutor; |
30 | 32 |
31 namespace keys = extension_tabs_module_constants; | 33 namespace keys = extension_tabs_module_constants; |
32 | 34 |
33 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() | 35 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() |
34 : execute_tab_id_(-1), | 36 : execute_tab_id_(-1), |
35 all_frames_(false), | 37 all_frames_(false), |
36 run_at_(UserScript::DOCUMENT_IDLE) { | 38 run_at_(UserScript::DOCUMENT_IDLE) { |
37 } | 39 } |
38 | 40 |
39 ExecuteCodeInTabFunction::~ExecuteCodeInTabFunction() {} | 41 ExecuteCodeInTabFunction::~ExecuteCodeInTabFunction() {} |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 return false; | 136 return false; |
135 } | 137 } |
136 | 138 |
137 scoped_refptr<FileReader> file_reader(new FileReader( | 139 scoped_refptr<FileReader> file_reader(new FileReader( |
138 resource_, base::Bind(&ExecuteCodeInTabFunction::DidLoadFile, this))); | 140 resource_, base::Bind(&ExecuteCodeInTabFunction::DidLoadFile, this))); |
139 file_reader->Start(); | 141 file_reader->Start(); |
140 | 142 |
141 return true; | 143 return true; |
142 } | 144 } |
143 | 145 |
144 bool ExecuteCodeInTabFunction::OnMessageReceived(const IPC::Message& message) { | 146 void ExecuteCodeInTabFunction::OnExecuteCodeFinished(bool success, |
145 if (message.type() != ExtensionHostMsg_ExecuteCodeFinished::ID) | |
146 return false; | |
147 | |
148 int message_request_id; | |
149 PickleIterator iter(message); | |
150 if (!message.ReadInt(&iter, &message_request_id)) { | |
151 NOTREACHED() << "malformed extension message"; | |
152 return true; | |
153 } | |
154 | |
155 if (message_request_id != request_id()) | |
156 return false; | |
157 | |
158 IPC_BEGIN_MESSAGE_MAP(ExecuteCodeInTabFunction, message) | |
159 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ExecuteCodeFinished, | |
160 OnExecuteCodeFinished) | |
161 IPC_END_MESSAGE_MAP() | |
162 return true; | |
163 } | |
164 | |
165 void ExecuteCodeInTabFunction::OnExecuteCodeFinished(int request_id, | |
166 bool success, | |
167 const std::string& error) { | 147 const std::string& error) { |
168 if (!error.empty()) { | 148 if (!error.empty()) { |
169 CHECK(!success); | 149 CHECK(!success); |
170 error_ = error; | 150 error_ = error; |
171 } | 151 } |
172 | 152 |
173 SendResponse(success); | 153 SendResponse(success); |
174 | |
175 Observe(NULL); | |
176 Release(); // balanced in Execute() | |
177 } | 154 } |
178 | 155 |
179 void ExecuteCodeInTabFunction::DidLoadFile(bool success, | 156 void ExecuteCodeInTabFunction::DidLoadFile(bool success, |
180 const std::string& data) { | 157 const std::string& data) { |
181 std::string function_name = name(); | 158 std::string function_name = name(); |
182 const Extension* extension = GetExtension(); | 159 const Extension* extension = GetExtension(); |
183 | 160 |
184 // Check if the file is CSS and needs localization. | 161 // Check if the file is CSS and needs localization. |
185 if (success && | 162 if (success && |
186 function_name == TabsInsertCSSFunction::function_name() && | 163 function_name == TabsInsertCSSFunction::function_name() && |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 SendResponse(false); | 229 SendResponse(false); |
253 return false; | 230 return false; |
254 } | 231 } |
255 | 232 |
256 const Extension* extension = GetExtension(); | 233 const Extension* extension = GetExtension(); |
257 if (!extension) { | 234 if (!extension) { |
258 SendResponse(false); | 235 SendResponse(false); |
259 return false; | 236 return false; |
260 } | 237 } |
261 | 238 |
262 bool is_js_code = true; | 239 ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT; |
263 std::string function_name = name(); | 240 std::string function_name = name(); |
264 if (function_name == TabsInsertCSSFunction::function_name()) { | 241 if (function_name == TabsInsertCSSFunction::function_name()) { |
265 is_js_code = false; | 242 script_type = ScriptExecutor::CSS; |
266 } else if (function_name != TabsExecuteScriptFunction::function_name()) { | 243 } else if (function_name != TabsExecuteScriptFunction::function_name()) { |
267 DCHECK(false); | 244 NOTREACHED(); |
268 } | 245 } |
269 | 246 |
270 ExtensionMsg_ExecuteCode_Params params; | 247 contents->extension_script_executor()->ExecuteScript( |
271 params.request_id = request_id(); | 248 extension->id(), |
272 params.extension_id = extension->id(); | 249 script_type, |
273 params.is_javascript = is_js_code; | 250 code_string, |
274 params.code = code_string; | 251 all_frames_ ? ScriptExecutor::ALL_FRAMES : ScriptExecutor::TOP_FRAME, |
275 params.all_frames = all_frames_; | 252 run_at_, |
276 params.run_at = run_at_; | 253 ScriptExecutor::ISOLATED_WORLD, |
277 params.in_main_world = false; | 254 base::Bind(&ExecuteCodeInTabFunction::OnExecuteCodeFinished, this)); |
278 contents->web_contents()->GetRenderViewHost()->Send( | |
279 new ExtensionMsg_ExecuteCode( | |
280 contents->web_contents()->GetRenderViewHost()->GetRoutingID(), | |
281 params)); | |
282 | |
283 Observe(contents->web_contents()); | |
284 AddRef(); // balanced in OnExecuteCodeFinished() | |
285 return true; | 255 return true; |
286 } | 256 } |
OLD | NEW |