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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 &browser, NULL, &contents, NULL)) { | 83 &browser, NULL, &contents, NULL)) { |
84 return false; | 84 return false; |
85 } | 85 } |
86 } | 86 } |
87 | 87 |
88 // NOTE: This can give the wrong answer due to race conditions, but it is OK, | 88 // NOTE: This can give the wrong answer due to race conditions, but it is OK, |
89 // we check again in the renderer. | 89 // we check again in the renderer. |
90 CHECK(browser); | 90 CHECK(browser); |
91 CHECK(contents); | 91 CHECK(contents); |
92 if (!GetExtension()->CanExecuteScriptOnPage( | 92 if (!GetExtension()->CanExecuteScriptOnPage( |
93 contents->web_contents()->GetURL(), NULL, &error_)) { | 93 contents->web_contents()->GetURL(), execute_tab_id_, NULL, &error_)) { |
94 return false; | 94 return false; |
95 } | 95 } |
96 | 96 |
97 if (script_info->HasKey(keys::kAllFramesKey)) { | 97 if (script_info->HasKey(keys::kAllFramesKey)) { |
98 if (!script_info->GetBoolean(keys::kAllFramesKey, &all_frames_)) | 98 if (!script_info->GetBoolean(keys::kAllFramesKey, &all_frames_)) |
99 return false; | 99 return false; |
100 } | 100 } |
101 | 101 |
102 if (script_info->HasKey(keys::kRunAtKey)) { | 102 if (script_info->HasKey(keys::kRunAtKey)) { |
103 std::string run_string; | 103 std::string run_string; |
104 EXTENSION_FUNCTION_VALIDATE(script_info->GetString( | 104 EXTENSION_FUNCTION_VALIDATE(script_info->GetString( |
105 keys::kRunAtKey, &run_string)); | 105 keys::kRunAtKey, &run_string)); |
106 | 106 |
107 if (run_string == extension_manifest_values::kRunAtDocumentStart) | 107 if (run_string == extension_manifest_values::kRunAtDocumentStart) |
108 run_at_ = UserScript::DOCUMENT_START; | 108 run_at_ = UserScript::DOCUMENT_START; |
109 else if (run_string == extension_manifest_values::kRunAtDocumentEnd) | 109 else if (run_string == extension_manifest_values::kRunAtDocumentEnd) |
110 run_at_ = UserScript::DOCUMENT_END; | 110 run_at_ = UserScript::DOCUMENT_END; |
111 else if (run_string == extension_manifest_values::kRunAtDocumentIdle) | 111 else if (run_string == extension_manifest_values::kRunAtDocumentIdle) |
112 run_at_ = UserScript::DOCUMENT_IDLE; | 112 run_at_ = UserScript::DOCUMENT_IDLE; |
113 else | 113 else |
114 EXTENSION_FUNCTION_VALIDATE(false); | 114 EXTENSION_FUNCTION_VALIDATE(false); |
115 } | 115 } |
116 | 116 |
117 std::string code_string; | 117 std::string code_string; |
118 if (script_info->HasKey(keys::kCodeKey)) { | 118 if (script_info->HasKey(keys::kCodeKey)) { |
119 if (!script_info->GetString(keys::kCodeKey, &code_string)) | 119 if (!script_info->GetString(keys::kCodeKey, &code_string)) |
120 return false; | 120 return false; |
121 } | 121 } |
122 | 122 |
123 if (!code_string.empty()) { | 123 if (!code_string.empty()) |
124 if (!Execute(code_string)) | 124 return Execute(code_string); |
Aaron Boodman
2012/06/06 01:03:13
heh.
| |
125 return false; | |
126 return true; | |
127 } | |
128 | 125 |
129 std::string relative_path; | 126 std::string relative_path; |
130 if (script_info->HasKey(keys::kFileKey)) { | 127 if (script_info->HasKey(keys::kFileKey)) { |
131 if (!script_info->GetString(keys::kFileKey, &relative_path)) | 128 if (!script_info->GetString(keys::kFileKey, &relative_path)) |
132 return false; | 129 return false; |
133 resource_ = GetExtension()->GetResource(relative_path); | 130 resource_ = GetExtension()->GetResource(relative_path); |
134 } | 131 } |
135 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { | 132 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { |
136 error_ = keys::kNoCodeOrFileToExecuteError; | 133 error_ = keys::kNoCodeOrFileToExecuteError; |
137 return false; | 134 return false; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 } | 217 } |
221 | 218 |
222 bool ExecuteCodeInTabFunction::Execute(const std::string& code_string) { | 219 bool ExecuteCodeInTabFunction::Execute(const std::string& code_string) { |
223 TabContentsWrapper* contents = NULL; | 220 TabContentsWrapper* contents = NULL; |
224 Browser* browser = NULL; | 221 Browser* browser = NULL; |
225 | 222 |
226 bool success = ExtensionTabUtil::GetTabById( | 223 bool success = ExtensionTabUtil::GetTabById( |
227 execute_tab_id_, profile(), include_incognito(), &browser, NULL, | 224 execute_tab_id_, profile(), include_incognito(), &browser, NULL, |
228 &contents, NULL) && contents && browser; | 225 &contents, NULL) && contents && browser; |
229 | 226 |
230 if (!success) { | 227 if (!success) |
231 SendResponse(false); | |
Aaron Boodman
2012/06/06 01:03:13
This seems like it would be a behavior change.
not at google - send to devlin
2012/06/06 07:38:40
Don't think so*. It think it's an existing bug in
| |
232 return false; | 228 return false; |
233 } | |
234 | 229 |
235 const extensions::Extension* extension = GetExtension(); | 230 const extensions::Extension* extension = GetExtension(); |
236 if (!extension) { | 231 if (!extension) |
237 SendResponse(false); | |
238 return false; | 232 return false; |
239 } | |
240 | 233 |
241 ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT; | 234 ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT; |
242 std::string function_name = name(); | 235 std::string function_name = name(); |
243 if (function_name == TabsInsertCSSFunction::function_name()) { | 236 if (function_name == TabsInsertCSSFunction::function_name()) { |
244 script_type = ScriptExecutor::CSS; | 237 script_type = ScriptExecutor::CSS; |
245 } else if (function_name != TabsExecuteScriptFunction::function_name()) { | 238 } else if (function_name != TabsExecuteScriptFunction::function_name()) { |
246 NOTREACHED(); | 239 NOTREACHED(); |
247 } | 240 } |
248 | 241 |
249 contents->extension_tab_helper()->script_executor()->ExecuteScript( | 242 contents->extension_tab_helper()->script_executor()->ExecuteScript( |
250 extension->id(), | 243 extension->id(), |
251 script_type, | 244 script_type, |
252 code_string, | 245 code_string, |
253 all_frames_ ? ScriptExecutor::ALL_FRAMES : ScriptExecutor::TOP_FRAME, | 246 all_frames_ ? ScriptExecutor::ALL_FRAMES : ScriptExecutor::TOP_FRAME, |
254 run_at_, | 247 run_at_, |
255 ScriptExecutor::ISOLATED_WORLD, | 248 ScriptExecutor::ISOLATED_WORLD, |
256 base::Bind(&ExecuteCodeInTabFunction::OnExecuteCodeFinished, this)); | 249 base::Bind(&ExecuteCodeInTabFunction::OnExecuteCodeFinished, this)); |
257 return true; | 250 return true; |
258 } | 251 } |
OLD | NEW |