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/script_executor.h" |
10 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
11 #include "chrome/browser/extensions/extension_tab_util.h" | 12 #include "chrome/browser/extensions/extension_tab_util.h" |
12 #include "chrome/browser/extensions/extension_tabs_module.h" | 13 #include "chrome/browser/extensions/extension_tabs_module.h" |
13 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 14 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
14 #include "chrome/browser/extensions/file_reader.h" | 15 #include "chrome/browser/extensions/file_reader.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" |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 return false; | 135 return false; |
135 } | 136 } |
136 | 137 |
137 scoped_refptr<FileReader> file_reader(new FileReader( | 138 scoped_refptr<FileReader> file_reader(new FileReader( |
138 resource_, base::Bind(&ExecuteCodeInTabFunction::DidLoadFile, this))); | 139 resource_, base::Bind(&ExecuteCodeInTabFunction::DidLoadFile, this))); |
139 file_reader->Start(); | 140 file_reader->Start(); |
140 | 141 |
141 return true; | 142 return true; |
142 } | 143 } |
143 | 144 |
144 bool ExecuteCodeInTabFunction::OnMessageReceived(const IPC::Message& message) { | 145 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) { | 146 const std::string& error) { |
168 if (!error.empty()) { | 147 if (!error.empty()) { |
169 CHECK(!success); | 148 CHECK(!success); |
170 error_ = error; | 149 error_ = error; |
171 } | 150 } |
172 | 151 |
173 SendResponse(success); | 152 SendResponse(success); |
174 | |
175 Observe(NULL); | |
176 Release(); // balanced in Execute() | |
177 } | 153 } |
178 | 154 |
179 void ExecuteCodeInTabFunction::DidLoadFile(bool success, | 155 void ExecuteCodeInTabFunction::DidLoadFile(bool success, |
180 const std::string& data) { | 156 const std::string& data) { |
181 std::string function_name = name(); | 157 std::string function_name = name(); |
182 const Extension* extension = GetExtension(); | 158 const Extension* extension = GetExtension(); |
183 | 159 |
184 // Check if the file is CSS and needs localization. | 160 // Check if the file is CSS and needs localization. |
185 if (success && | 161 if (success && |
186 function_name == TabsInsertCSSFunction::function_name() && | 162 function_name == TabsInsertCSSFunction::function_name() && |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 } | 236 } |
261 | 237 |
262 bool is_js_code = true; | 238 bool is_js_code = true; |
263 std::string function_name = name(); | 239 std::string function_name = name(); |
264 if (function_name == TabsInsertCSSFunction::function_name()) { | 240 if (function_name == TabsInsertCSSFunction::function_name()) { |
265 is_js_code = false; | 241 is_js_code = false; |
266 } else if (function_name != TabsExecuteScriptFunction::function_name()) { | 242 } else if (function_name != TabsExecuteScriptFunction::function_name()) { |
267 DCHECK(false); | 243 DCHECK(false); |
268 } | 244 } |
269 | 245 |
270 ExtensionMsg_ExecuteCode_Params params; | 246 contents->extension_script_executor()->ExecuteScript( |
271 params.request_id = request_id(); | 247 extension->id(), |
272 params.extension_id = extension->id(); | 248 is_js_code, |
273 params.is_javascript = is_js_code; | 249 code_string, |
274 params.code = code_string; | 250 all_frames_, |
275 params.all_frames = all_frames_; | 251 run_at_, |
276 params.run_at = run_at_; | 252 false, |
277 params.in_main_world = false; | 253 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; | 254 return true; |
286 } | 255 } |
OLD | NEW |