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/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" |
11 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 11 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
13 #include "chrome/browser/extensions/extension_tab_util.h" | 13 #include "chrome/browser/extensions/extension_tab_util.h" |
14 #include "chrome/browser/extensions/tab_helper.h" | 14 #include "chrome/browser/extensions/tab_helper.h" |
15 #include "chrome/browser/extensions/file_reader.h" | 15 #include "chrome/browser/extensions/file_reader.h" |
16 #include "chrome/browser/extensions/script_executor.h" | 16 #include "chrome/browser/extensions/script_executor.h" |
17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
19 #include "chrome/browser/ui/tab_contents/tab_contents.h" | |
20 #include "chrome/common/extensions/api/tabs.h" | 19 #include "chrome/common/extensions/api/tabs.h" |
21 #include "chrome/common/extensions/extension.h" | 20 #include "chrome/common/extensions/extension.h" |
22 #include "chrome/common/extensions/extension_constants.h" | 21 #include "chrome/common/extensions/extension_constants.h" |
23 #include "chrome/common/extensions/extension_error_utils.h" | 22 #include "chrome/common/extensions/extension_error_utils.h" |
24 #include "chrome/common/extensions/extension_file_util.h" | 23 #include "chrome/common/extensions/extension_file_util.h" |
25 #include "chrome/common/extensions/extension_l10n_util.h" | 24 #include "chrome/common/extensions/extension_l10n_util.h" |
26 #include "chrome/common/extensions/extension_manifest_constants.h" | 25 #include "chrome/common/extensions/extension_manifest_constants.h" |
27 #include "chrome/common/extensions/extension_messages.h" | 26 #include "chrome/common/extensions/extension_messages.h" |
28 #include "chrome/common/extensions/message_bundle.h" | 27 #include "chrome/common/extensions/message_bundle.h" |
29 #include "content/public/browser/render_view_host.h" | 28 #include "content/public/browser/render_view_host.h" |
(...skipping 26 matching lines...) Expand all Loading... |
56 | 55 |
57 if (!details_->code.get() && !details_->file.get()) { | 56 if (!details_->code.get() && !details_->file.get()) { |
58 error_ = keys::kNoCodeOrFileToExecuteError; | 57 error_ = keys::kNoCodeOrFileToExecuteError; |
59 return false; | 58 return false; |
60 } | 59 } |
61 if (details_->code.get() && details_->file.get()) { | 60 if (details_->code.get() && details_->file.get()) { |
62 error_ = keys::kMoreThanOneValuesError; | 61 error_ = keys::kMoreThanOneValuesError; |
63 return false; | 62 return false; |
64 } | 63 } |
65 | 64 |
66 TabContents* contents = NULL; | 65 content::WebContents* contents = NULL; |
67 | 66 |
68 // If |tab_id| is specified, look for the tab. Otherwise default to selected | 67 // If |tab_id| is specified, look for the tab. Otherwise default to selected |
69 // tab in the current window. | 68 // tab in the current window. |
70 CHECK_GE(execute_tab_id_, 0); | 69 CHECK_GE(execute_tab_id_, 0); |
71 if (!ExtensionTabUtil::GetTabById(execute_tab_id_, profile(), | 70 if (!ExtensionTabUtil::GetTabById(execute_tab_id_, profile(), |
72 include_incognito(), | 71 include_incognito(), |
73 NULL, NULL, &contents, NULL)) { | 72 NULL, NULL, &contents, NULL)) { |
74 return false; | 73 return false; |
75 } | 74 } |
76 | 75 |
77 // NOTE: This can give the wrong answer due to race conditions, but it is OK, | 76 // NOTE: This can give the wrong answer due to race conditions, but it is OK, |
78 // we check again in the renderer. | 77 // we check again in the renderer. |
79 CHECK(contents); | 78 CHECK(contents); |
80 if (!GetExtension()->CanExecuteScriptOnPage( | 79 if (!GetExtension()->CanExecuteScriptOnPage(contents->GetURL(), |
81 contents->web_contents()->GetURL(), | 80 contents->GetURL(), |
82 contents->web_contents()->GetURL(), | 81 execute_tab_id_, |
83 execute_tab_id_, | 82 NULL, |
84 NULL, | 83 &error_)) { |
85 &error_)) { | |
86 return false; | 84 return false; |
87 } | 85 } |
88 | 86 |
89 if (details_->code.get()) | 87 if (details_->code.get()) |
90 return Execute(*details_->code); | 88 return Execute(*details_->code); |
91 | 89 |
92 CHECK(details_->file.get()); | 90 CHECK(details_->file.get()); |
93 resource_ = GetExtension()->GetResource(*details_->file); | 91 resource_ = GetExtension()->GetResource(*details_->file); |
94 | 92 |
95 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { | 93 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 scoped_ptr<InjectDetails> details(new InjectDetails()); | 137 scoped_ptr<InjectDetails> details(new InjectDetails()); |
140 if (!InjectDetails::Populate(*details_value, details.get())) | 138 if (!InjectDetails::Populate(*details_value, details.get())) |
141 return false; | 139 return false; |
142 | 140 |
143 // If the tab ID is -1 then it needs to be converted to the currently active | 141 // If the tab ID is -1 then it needs to be converted to the currently active |
144 // tab's ID. | 142 // tab's ID. |
145 if (tab_id == -1) { | 143 if (tab_id == -1) { |
146 Browser* browser = GetCurrentBrowser(); | 144 Browser* browser = GetCurrentBrowser(); |
147 if (!browser) | 145 if (!browser) |
148 return false; | 146 return false; |
149 TabContents* tab_contents = NULL; | 147 content::WebContents* web_contents = NULL; |
150 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) | 148 if (!ExtensionTabUtil::GetDefaultTab(browser, &web_contents, &tab_id)) |
151 return false; | 149 return false; |
152 } | 150 } |
153 | 151 |
154 execute_tab_id_ = tab_id; | 152 execute_tab_id_ = tab_id; |
155 details_ = details.Pass(); | 153 details_ = details.Pass(); |
156 return true; | 154 return true; |
157 } | 155 } |
158 | 156 |
159 void ExecuteCodeInTabFunction::DidLoadFile(bool success, | 157 void ExecuteCodeInTabFunction::DidLoadFile(bool success, |
160 const std::string& data) { | 158 const std::string& data) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 resource_.relative_path().value()); | 214 resource_.relative_path().value()); |
217 #elif defined(OS_WIN) | 215 #elif defined(OS_WIN) |
218 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, | 216 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, |
219 WideToUTF8(resource_.relative_path().value())); | 217 WideToUTF8(resource_.relative_path().value())); |
220 #endif // OS_WIN | 218 #endif // OS_WIN |
221 SendResponse(false); | 219 SendResponse(false); |
222 } | 220 } |
223 } | 221 } |
224 | 222 |
225 bool ExecuteCodeInTabFunction::Execute(const std::string& code_string) { | 223 bool ExecuteCodeInTabFunction::Execute(const std::string& code_string) { |
226 TabContents* contents = NULL; | 224 content::WebContents* contents = NULL; |
227 Browser* browser = NULL; | 225 Browser* browser = NULL; |
228 | 226 |
229 bool success = ExtensionTabUtil::GetTabById( | 227 bool success = ExtensionTabUtil::GetTabById( |
230 execute_tab_id_, profile(), include_incognito(), &browser, NULL, | 228 execute_tab_id_, profile(), include_incognito(), &browser, NULL, |
231 &contents, NULL) && contents && browser; | 229 &contents, NULL) && contents && browser; |
232 | 230 |
233 if (!success) | 231 if (!success) |
234 return false; | 232 return false; |
235 | 233 |
236 const extensions::Extension* extension = GetExtension(); | 234 const extensions::Extension* extension = GetExtension(); |
(...skipping 21 matching lines...) Expand all Loading... |
258 break; | 256 break; |
259 case InjectDetails::RUN_AT_DOCUMENT_START: | 257 case InjectDetails::RUN_AT_DOCUMENT_START: |
260 run_at = UserScript::DOCUMENT_START; | 258 run_at = UserScript::DOCUMENT_START; |
261 break; | 259 break; |
262 case InjectDetails::RUN_AT_DOCUMENT_END: | 260 case InjectDetails::RUN_AT_DOCUMENT_END: |
263 run_at = UserScript::DOCUMENT_END; | 261 run_at = UserScript::DOCUMENT_END; |
264 break; | 262 break; |
265 } | 263 } |
266 CHECK_NE(UserScript::UNDEFINED, run_at); | 264 CHECK_NE(UserScript::UNDEFINED, run_at); |
267 | 265 |
268 extensions::TabHelper::FromWebContents(contents->web_contents())-> | 266 extensions::TabHelper::FromWebContents(contents)-> |
269 script_executor()->ExecuteScript( | 267 script_executor()->ExecuteScript( |
270 extension->id(), | 268 extension->id(), |
271 script_type, | 269 script_type, |
272 code_string, | 270 code_string, |
273 frame_scope, | 271 frame_scope, |
274 run_at, | 272 run_at, |
275 ScriptExecutor::ISOLATED_WORLD, | 273 ScriptExecutor::ISOLATED_WORLD, |
276 base::Bind(&ExecuteCodeInTabFunction::OnExecuteCodeFinished, this)); | 274 base::Bind(&ExecuteCodeInTabFunction::OnExecuteCodeFinished, this)); |
277 return true; | 275 return true; |
278 } | 276 } |
OLD | NEW |