| 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" | 19 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 20 #include "chrome/common/extensions/api/tabs.h" |
| 20 #include "chrome/common/extensions/extension.h" | 21 #include "chrome/common/extensions/extension.h" |
| 21 #include "chrome/common/extensions/extension_constants.h" | 22 #include "chrome/common/extensions/extension_constants.h" |
| 22 #include "chrome/common/extensions/extension_error_utils.h" | 23 #include "chrome/common/extensions/extension_error_utils.h" |
| 23 #include "chrome/common/extensions/extension_file_util.h" | 24 #include "chrome/common/extensions/extension_file_util.h" |
| 24 #include "chrome/common/extensions/extension_l10n_util.h" | 25 #include "chrome/common/extensions/extension_l10n_util.h" |
| 25 #include "chrome/common/extensions/extension_manifest_constants.h" | 26 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 26 #include "chrome/common/extensions/extension_messages.h" | 27 #include "chrome/common/extensions/extension_messages.h" |
| 27 #include "chrome/common/extensions/message_bundle.h" | 28 #include "chrome/common/extensions/message_bundle.h" |
| 28 #include "content/public/browser/render_view_host.h" | 29 #include "content/public/browser/render_view_host.h" |
| 29 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
| 30 | 31 |
| 31 using content::BrowserThread; | 32 using content::BrowserThread; |
| 33 using extensions::api::tabs::InjectDetails; |
| 32 using extensions::ScriptExecutor; | 34 using extensions::ScriptExecutor; |
| 35 using extensions::UserScript; |
| 33 | 36 |
| 34 namespace keys = extensions::tabs_constants; | 37 namespace keys = extensions::tabs_constants; |
| 35 | 38 |
| 36 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() | 39 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() |
| 37 : execute_tab_id_(-1), | 40 : execute_tab_id_(-1) { |
| 38 all_frames_(false), | |
| 39 run_at_(extensions::UserScript::DOCUMENT_IDLE) { | |
| 40 } | 41 } |
| 41 | 42 |
| 42 ExecuteCodeInTabFunction::~ExecuteCodeInTabFunction() {} | 43 ExecuteCodeInTabFunction::~ExecuteCodeInTabFunction() {} |
| 43 | 44 |
| 45 bool ExecuteCodeInTabFunction::HasPermission() { |
| 46 if (Init() && |
| 47 execute_tab_id_ >= 0 && |
| 48 extension_->HasAPIPermissionForTab(execute_tab_id_, |
| 49 extensions::APIPermission::kTab)) { |
| 50 return true; |
| 51 } |
| 52 return ExtensionFunction::HasPermission(); |
| 53 } |
| 54 |
| 44 bool ExecuteCodeInTabFunction::RunImpl() { | 55 bool ExecuteCodeInTabFunction::RunImpl() { |
| 45 DictionaryValue* script_info; | 56 EXTENSION_FUNCTION_VALIDATE(Init()); |
| 46 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &script_info)); | 57 |
| 47 size_t number_of_value = script_info->size(); | 58 if (!details_->code.get() && !details_->file.get()) { |
| 48 if (number_of_value == 0) { | |
| 49 error_ = keys::kNoCodeOrFileToExecuteError; | 59 error_ = keys::kNoCodeOrFileToExecuteError; |
| 50 return false; | 60 return false; |
| 51 } else { | 61 } |
| 52 bool has_code = script_info->HasKey(keys::kCodeKey); | 62 if (details_->code.get() && details_->file.get()) { |
| 53 bool has_file = script_info->HasKey(keys::kFileKey); | 63 error_ = keys::kMoreThanOneValuesError; |
| 54 if (has_code && has_file) { | 64 return false; |
| 55 error_ = keys::kMoreThanOneValuesError; | |
| 56 return false; | |
| 57 } else if (!has_code && !has_file) { | |
| 58 error_ = keys::kNoCodeOrFileToExecuteError; | |
| 59 return false; | |
| 60 } | |
| 61 } | 65 } |
| 62 | 66 |
| 63 execute_tab_id_ = -1; | |
| 64 Browser* browser = NULL; | 67 Browser* browser = NULL; |
| 65 TabContents* contents = NULL; | 68 TabContents* contents = NULL; |
| 66 | 69 |
| 67 // If |tab_id| is specified, look for it. Otherwise default to selected tab | 70 // If |tab_id| is specified, look for the tab. Otherwise default to selected |
| 68 // in the current window. | 71 // tab in the current window. |
| 69 Value* tab_value = NULL; | 72 if (execute_tab_id_ == -1) { |
| 70 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); | |
| 71 if (tab_value->IsType(Value::TYPE_NULL)) { | |
| 72 browser = GetCurrentBrowser(); | 73 browser = GetCurrentBrowser(); |
| 73 if (!browser) { | 74 if (!browser) { |
| 74 error_ = keys::kNoCurrentWindowError; | 75 error_ = keys::kNoCurrentWindowError; |
| 75 return false; | 76 return false; |
| 76 } | 77 } |
| 77 if (!ExtensionTabUtil::GetDefaultTab(browser, &contents, &execute_tab_id_)) | 78 if (!ExtensionTabUtil::GetDefaultTab(browser, &contents, &execute_tab_id_)) |
| 78 return false; | 79 return false; |
| 79 } else { | 80 } else { |
| 80 EXTENSION_FUNCTION_VALIDATE(tab_value->GetAsInteger(&execute_tab_id_)); | |
| 81 if (!ExtensionTabUtil::GetTabById(execute_tab_id_, profile(), | 81 if (!ExtensionTabUtil::GetTabById(execute_tab_id_, profile(), |
| 82 include_incognito(), | 82 include_incognito(), |
| 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(), execute_tab_id_, 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 (details_->code.get()) |
| 98 if (!script_info->GetBoolean(keys::kAllFramesKey, &all_frames_)) | 98 return Execute(*details_->code); |
| 99 return false; | |
| 100 } | |
| 101 | 99 |
| 102 if (script_info->HasKey(keys::kRunAtKey)) { | 100 CHECK(details_->file.get()); |
| 103 std::string run_string; | 101 resource_ = GetExtension()->GetResource(*details_->file); |
| 104 EXTENSION_FUNCTION_VALIDATE(script_info->GetString( | |
| 105 keys::kRunAtKey, &run_string)); | |
| 106 | 102 |
| 107 if (run_string == extension_manifest_values::kRunAtDocumentStart) | |
| 108 run_at_ = extensions::UserScript::DOCUMENT_START; | |
| 109 else if (run_string == extension_manifest_values::kRunAtDocumentEnd) | |
| 110 run_at_ = extensions::UserScript::DOCUMENT_END; | |
| 111 else if (run_string == extension_manifest_values::kRunAtDocumentIdle) | |
| 112 run_at_ = extensions::UserScript::DOCUMENT_IDLE; | |
| 113 else | |
| 114 EXTENSION_FUNCTION_VALIDATE(false); | |
| 115 } | |
| 116 | |
| 117 std::string code_string; | |
| 118 if (script_info->HasKey(keys::kCodeKey)) { | |
| 119 if (!script_info->GetString(keys::kCodeKey, &code_string)) | |
| 120 return false; | |
| 121 } | |
| 122 | |
| 123 if (!code_string.empty()) | |
| 124 return Execute(code_string); | |
| 125 | |
| 126 std::string relative_path; | |
| 127 if (script_info->HasKey(keys::kFileKey)) { | |
| 128 if (!script_info->GetString(keys::kFileKey, &relative_path)) | |
| 129 return false; | |
| 130 resource_ = GetExtension()->GetResource(relative_path); | |
| 131 } | |
| 132 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { | 103 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { |
| 133 error_ = keys::kNoCodeOrFileToExecuteError; | 104 error_ = keys::kNoCodeOrFileToExecuteError; |
| 134 return false; | 105 return false; |
| 135 } | 106 } |
| 136 | 107 |
| 137 scoped_refptr<FileReader> file_reader(new FileReader( | 108 scoped_refptr<FileReader> file_reader(new FileReader( |
| 138 resource_, base::Bind(&ExecuteCodeInTabFunction::DidLoadFile, this))); | 109 resource_, base::Bind(&ExecuteCodeInTabFunction::DidLoadFile, this))); |
| 139 file_reader->Start(); | 110 file_reader->Start(); |
| 140 | 111 |
| 141 return true; | 112 return true; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 156 void TabsExecuteScriptFunction::OnExecuteCodeFinished(bool success, | 127 void TabsExecuteScriptFunction::OnExecuteCodeFinished(bool success, |
| 157 int32 page_id, | 128 int32 page_id, |
| 158 const std::string& error, | 129 const std::string& error, |
| 159 const ListValue& result) { | 130 const ListValue& result) { |
| 160 if (error.empty()) | 131 if (error.empty()) |
| 161 SetResult(result.DeepCopy()); | 132 SetResult(result.DeepCopy()); |
| 162 ExecuteCodeInTabFunction::OnExecuteCodeFinished(success, page_id, error, | 133 ExecuteCodeInTabFunction::OnExecuteCodeFinished(success, page_id, error, |
| 163 result); | 134 result); |
| 164 } | 135 } |
| 165 | 136 |
| 137 bool ExecuteCodeInTabFunction::Init() { |
| 138 if (details_.get()) |
| 139 return true; |
| 140 |
| 141 // tab_id is optional so it's ok if it's not there. |
| 142 int tab_id = -1; |
| 143 args_->GetInteger(0, &tab_id); |
| 144 |
| 145 // details are not optional. |
| 146 DictionaryValue* details_value = NULL; |
| 147 if (!args_->GetDictionary(1, &details_value)) |
| 148 return false; |
| 149 scoped_ptr<InjectDetails> details(new InjectDetails()); |
| 150 if (!InjectDetails::Populate(*details_value, details.get())) |
| 151 return false; |
| 152 |
| 153 execute_tab_id_ = tab_id; |
| 154 details_ = details.Pass(); |
| 155 return true; |
| 156 } |
| 157 |
| 166 void ExecuteCodeInTabFunction::DidLoadFile(bool success, | 158 void ExecuteCodeInTabFunction::DidLoadFile(bool success, |
| 167 const std::string& data) { | 159 const std::string& data) { |
| 168 std::string function_name = name(); | 160 std::string function_name = name(); |
| 169 const extensions::Extension* extension = GetExtension(); | 161 const extensions::Extension* extension = GetExtension(); |
| 170 | 162 |
| 171 // Check if the file is CSS and needs localization. | 163 // Check if the file is CSS and needs localization. |
| 172 if (success && | 164 if (success && |
| 173 function_name == TabsInsertCSSFunction::function_name() && | 165 function_name == TabsInsertCSSFunction::function_name() && |
| 174 extension != NULL && | 166 extension != NULL && |
| 175 data.find( | 167 data.find( |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 return false; | 237 return false; |
| 246 | 238 |
| 247 ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT; | 239 ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT; |
| 248 std::string function_name = name(); | 240 std::string function_name = name(); |
| 249 if (function_name == TabsInsertCSSFunction::function_name()) { | 241 if (function_name == TabsInsertCSSFunction::function_name()) { |
| 250 script_type = ScriptExecutor::CSS; | 242 script_type = ScriptExecutor::CSS; |
| 251 } else if (function_name != TabsExecuteScriptFunction::function_name()) { | 243 } else if (function_name != TabsExecuteScriptFunction::function_name()) { |
| 252 NOTREACHED(); | 244 NOTREACHED(); |
| 253 } | 245 } |
| 254 | 246 |
| 247 ScriptExecutor::FrameScope frame_scope = |
| 248 details_->all_frames.get() && *details_->all_frames ? |
| 249 ScriptExecutor::ALL_FRAMES : |
| 250 ScriptExecutor::TOP_FRAME; |
| 251 |
| 252 UserScript::RunLocation run_at = UserScript::UNDEFINED; |
| 253 switch (details_->run_at) { |
| 254 case InjectDetails::RUN_AT_NONE: |
| 255 case InjectDetails::RUN_AT_DOCUMENT_IDLE: |
| 256 run_at = UserScript::DOCUMENT_IDLE; |
| 257 break; |
| 258 case InjectDetails::RUN_AT_DOCUMENT_START: |
| 259 run_at = UserScript::DOCUMENT_START; |
| 260 break; |
| 261 case InjectDetails::RUN_AT_DOCUMENT_END: |
| 262 run_at = UserScript::DOCUMENT_END; |
| 263 break; |
| 264 } |
| 265 CHECK_NE(UserScript::UNDEFINED, run_at); |
| 266 |
| 255 contents->extension_tab_helper()->script_executor()->ExecuteScript( | 267 contents->extension_tab_helper()->script_executor()->ExecuteScript( |
| 256 extension->id(), | 268 extension->id(), |
| 257 script_type, | 269 script_type, |
| 258 code_string, | 270 code_string, |
| 259 all_frames_ ? ScriptExecutor::ALL_FRAMES : ScriptExecutor::TOP_FRAME, | 271 frame_scope, |
| 260 run_at_, | 272 run_at, |
| 261 ScriptExecutor::ISOLATED_WORLD, | 273 ScriptExecutor::ISOLATED_WORLD, |
| 262 base::Bind(&ExecuteCodeInTabFunction::OnExecuteCodeFinished, this)); | 274 base::Bind(&ExecuteCodeInTabFunction::OnExecuteCodeFinished, this)); |
| 263 return true; | 275 return true; |
| 264 } | 276 } |
| OLD | NEW |