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" |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "chrome/common/extensions/extension_messages.h" | 24 #include "chrome/common/extensions/extension_messages.h" |
25 #include "content/public/browser/render_view_host.h" | 25 #include "content/public/browser/render_view_host.h" |
26 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
27 | 27 |
28 using content::BrowserThread; | 28 using content::BrowserThread; |
29 | 29 |
30 namespace keys = extension_tabs_module_constants; | 30 namespace keys = extension_tabs_module_constants; |
31 | 31 |
32 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() | 32 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() |
33 : execute_tab_id_(-1), | 33 : execute_tab_id_(-1), |
34 all_frames_(false) { | 34 all_frames_(false), |
| 35 run_at_(UserScript::DOCUMENT_IDLE) { |
35 } | 36 } |
36 | 37 |
37 ExecuteCodeInTabFunction::~ExecuteCodeInTabFunction() { | 38 ExecuteCodeInTabFunction::~ExecuteCodeInTabFunction() { |
38 } | 39 } |
39 | 40 |
40 bool ExecuteCodeInTabFunction::RunImpl() { | 41 bool ExecuteCodeInTabFunction::RunImpl() { |
41 DictionaryValue* script_info; | 42 DictionaryValue* script_info; |
42 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &script_info)); | 43 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &script_info)); |
43 size_t number_of_value = script_info->size(); | 44 size_t number_of_value = script_info->size(); |
44 if (number_of_value == 0) { | 45 if (number_of_value == 0) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 if (!GetExtension()->CanExecuteScriptOnPage( | 89 if (!GetExtension()->CanExecuteScriptOnPage( |
89 contents->web_contents()->GetURL(), NULL, &error_)) { | 90 contents->web_contents()->GetURL(), NULL, &error_)) { |
90 return false; | 91 return false; |
91 } | 92 } |
92 | 93 |
93 if (script_info->HasKey(keys::kAllFramesKey)) { | 94 if (script_info->HasKey(keys::kAllFramesKey)) { |
94 if (!script_info->GetBoolean(keys::kAllFramesKey, &all_frames_)) | 95 if (!script_info->GetBoolean(keys::kAllFramesKey, &all_frames_)) |
95 return false; | 96 return false; |
96 } | 97 } |
97 | 98 |
| 99 if (script_info->HasKey(keys::kRunAtKey)) { |
| 100 std::string run_string; |
| 101 EXTENSION_FUNCTION_VALIDATE(script_info->GetString( |
| 102 keys::kRunAtKey, &run_string)); |
| 103 |
| 104 if (run_string == extension_manifest_values::kRunAtDocumentStart) |
| 105 run_at_ = UserScript::DOCUMENT_START; |
| 106 else if (run_string == extension_manifest_values::kRunAtDocumentEnd) |
| 107 run_at_ = UserScript::DOCUMENT_END; |
| 108 else if (run_string == extension_manifest_values::kRunAtDocumentIdle) |
| 109 run_at_ = UserScript::DOCUMENT_IDLE; |
| 110 else |
| 111 EXTENSION_FUNCTION_VALIDATE(false); |
| 112 } |
| 113 |
98 std::string code_string; | 114 std::string code_string; |
99 if (script_info->HasKey(keys::kCodeKey)) { | 115 if (script_info->HasKey(keys::kCodeKey)) { |
100 if (!script_info->GetString(keys::kCodeKey, &code_string)) | 116 if (!script_info->GetString(keys::kCodeKey, &code_string)) |
101 return false; | 117 return false; |
102 } | 118 } |
103 | 119 |
104 if (!code_string.empty()) { | 120 if (!code_string.empty()) { |
105 if (!Execute(code_string)) | 121 if (!Execute(code_string)) |
106 return false; | 122 return false; |
107 return true; | 123 return true; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 } else if (function_name != TabsExecuteScriptFunction::function_name()) { | 231 } else if (function_name != TabsExecuteScriptFunction::function_name()) { |
216 DCHECK(false); | 232 DCHECK(false); |
217 } | 233 } |
218 | 234 |
219 ExtensionMsg_ExecuteCode_Params params; | 235 ExtensionMsg_ExecuteCode_Params params; |
220 params.request_id = request_id(); | 236 params.request_id = request_id(); |
221 params.extension_id = extension->id(); | 237 params.extension_id = extension->id(); |
222 params.is_javascript = is_js_code; | 238 params.is_javascript = is_js_code; |
223 params.code = code_string; | 239 params.code = code_string; |
224 params.all_frames = all_frames_; | 240 params.all_frames = all_frames_; |
| 241 params.run_at = run_at_; |
225 params.in_main_world = false; | 242 params.in_main_world = false; |
226 contents->web_contents()->GetRenderViewHost()->Send( | 243 contents->web_contents()->GetRenderViewHost()->Send( |
227 new ExtensionMsg_ExecuteCode( | 244 new ExtensionMsg_ExecuteCode( |
228 contents->web_contents()->GetRenderViewHost()->GetRoutingID(), | 245 contents->web_contents()->GetRenderViewHost()->GetRoutingID(), |
229 params)); | 246 params)); |
230 | 247 |
231 Observe(contents->web_contents()); | 248 Observe(contents->web_contents()); |
232 AddRef(); // balanced in OnExecuteCodeFinished() | 249 AddRef(); // balanced in OnExecuteCodeFinished() |
233 return true; | 250 return true; |
234 } | 251 } |
(...skipping 25 matching lines...) Expand all Loading... |
260 if (!error.empty()) { | 277 if (!error.empty()) { |
261 CHECK(!success); | 278 CHECK(!success); |
262 error_ = error; | 279 error_ = error; |
263 } | 280 } |
264 | 281 |
265 SendResponse(success); | 282 SendResponse(success); |
266 | 283 |
267 Observe(NULL); | 284 Observe(NULL); |
268 Release(); // balanced in Execute() | 285 Release(); // balanced in Execute() |
269 } | 286 } |
OLD | NEW |