Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(824)

Side by Side Diff: chrome/browser/extensions/api/tabs/execute_code_in_tab_function.cc

Issue 10443105: Take 2 at implementing activeTab. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: empty -> is_empty Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/api/tabs/execute_code_in_tab_function.h" 5 #include "chrome/browser/extensions/api/tabs/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/api/tabs/tabs.h" 10 #include "chrome/browser/extensions/api/tabs/tabs.h"
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
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);
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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 // anything to localize. 194 // anything to localize.
198 BrowserThread::PostTask( 195 BrowserThread::PostTask(
199 BrowserThread::UI, FROM_HERE, 196 BrowserThread::UI, FROM_HERE,
200 base::Bind(&ExecuteCodeInTabFunction::DidLoadAndLocalizeFile, this, 197 base::Bind(&ExecuteCodeInTabFunction::DidLoadAndLocalizeFile, this,
201 true, css_data)); 198 true, css_data));
202 } 199 }
203 200
204 void ExecuteCodeInTabFunction::DidLoadAndLocalizeFile(bool success, 201 void ExecuteCodeInTabFunction::DidLoadAndLocalizeFile(bool success,
205 const std::string& data) { 202 const std::string& data) {
206 if (success) { 203 if (success) {
207 Execute(data); 204 if (!Execute(data))
205 SendResponse(false);
208 } else { 206 } else {
209 #if defined(OS_POSIX) 207 #if defined(OS_POSIX)
210 // TODO(viettrungluu): bug: there's no particular reason the path should be 208 // TODO(viettrungluu): bug: there's no particular reason the path should be
211 // UTF-8, in which case this may fail. 209 // UTF-8, in which case this may fail.
212 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, 210 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError,
213 resource_.relative_path().value()); 211 resource_.relative_path().value());
214 #elif defined(OS_WIN) 212 #elif defined(OS_WIN)
215 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, 213 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError,
216 WideToUTF8(resource_.relative_path().value())); 214 WideToUTF8(resource_.relative_path().value()));
217 #endif // OS_WIN 215 #endif // OS_WIN
218 SendResponse(false); 216 SendResponse(false);
219 } 217 }
220 } 218 }
221 219
222 bool ExecuteCodeInTabFunction::Execute(const std::string& code_string) { 220 bool ExecuteCodeInTabFunction::Execute(const std::string& code_string) {
223 TabContents* contents = NULL; 221 TabContents* contents = NULL;
224 Browser* browser = NULL; 222 Browser* browser = NULL;
225 223
226 bool success = ExtensionTabUtil::GetTabById( 224 bool success = ExtensionTabUtil::GetTabById(
227 execute_tab_id_, profile(), include_incognito(), &browser, NULL, 225 execute_tab_id_, profile(), include_incognito(), &browser, NULL,
228 &contents, NULL) && contents && browser; 226 &contents, NULL) && contents && browser;
229 227
230 if (!success) { 228 if (!success)
231 SendResponse(false);
232 return false; 229 return false;
233 }
234 230
235 const extensions::Extension* extension = GetExtension(); 231 const extensions::Extension* extension = GetExtension();
236 if (!extension) { 232 if (!extension)
237 SendResponse(false);
238 return false; 233 return false;
239 }
240 234
241 ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT; 235 ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT;
242 std::string function_name = name(); 236 std::string function_name = name();
243 if (function_name == TabsInsertCSSFunction::function_name()) { 237 if (function_name == TabsInsertCSSFunction::function_name()) {
244 script_type = ScriptExecutor::CSS; 238 script_type = ScriptExecutor::CSS;
245 } else if (function_name != TabsExecuteScriptFunction::function_name()) { 239 } else if (function_name != TabsExecuteScriptFunction::function_name()) {
246 NOTREACHED(); 240 NOTREACHED();
247 } 241 }
248 242
249 contents->extension_tab_helper()->script_executor()->ExecuteScript( 243 contents->extension_tab_helper()->script_executor()->ExecuteScript(
250 extension->id(), 244 extension->id(),
251 script_type, 245 script_type,
252 code_string, 246 code_string,
253 all_frames_ ? ScriptExecutor::ALL_FRAMES : ScriptExecutor::TOP_FRAME, 247 all_frames_ ? ScriptExecutor::ALL_FRAMES : ScriptExecutor::TOP_FRAME,
254 run_at_, 248 run_at_,
255 ScriptExecutor::ISOLATED_WORLD, 249 ScriptExecutor::ISOLATED_WORLD,
256 base::Bind(&ExecuteCodeInTabFunction::OnExecuteCodeFinished, this)); 250 base::Bind(&ExecuteCodeInTabFunction::OnExecuteCodeFinished, this));
257 return true; 251 return true;
258 } 252 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/offscreen_tabs/offscreen_tabs_api.cc ('k') | chrome/browser/extensions/api/tabs/tabs.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698