| 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 18 matching lines...) Expand all Loading... |
| 29 using content::BrowserThread; | 29 using content::BrowserThread; |
| 30 | 30 |
| 31 namespace keys = extension_tabs_module_constants; | 31 namespace keys = extension_tabs_module_constants; |
| 32 | 32 |
| 33 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() | 33 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() |
| 34 : execute_tab_id_(-1), | 34 : execute_tab_id_(-1), |
| 35 all_frames_(false), | 35 all_frames_(false), |
| 36 run_at_(UserScript::DOCUMENT_IDLE) { | 36 run_at_(UserScript::DOCUMENT_IDLE) { |
| 37 } | 37 } |
| 38 | 38 |
| 39 ExecuteCodeInTabFunction::~ExecuteCodeInTabFunction() { | 39 ExecuteCodeInTabFunction::~ExecuteCodeInTabFunction() {} |
| 40 } | |
| 41 | 40 |
| 42 bool ExecuteCodeInTabFunction::RunImpl() { | 41 bool ExecuteCodeInTabFunction::RunImpl() { |
| 43 DictionaryValue* script_info; | 42 DictionaryValue* script_info; |
| 44 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &script_info)); | 43 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &script_info)); |
| 45 size_t number_of_value = script_info->size(); | 44 size_t number_of_value = script_info->size(); |
| 46 if (number_of_value == 0) { | 45 if (number_of_value == 0) { |
| 47 error_ = keys::kNoCodeOrFileToExecuteError; | 46 error_ = keys::kNoCodeOrFileToExecuteError; |
| 48 return false; | 47 return false; |
| 49 } else { | 48 } else { |
| 50 bool has_code = script_info->HasKey(keys::kCodeKey); | 49 bool has_code = script_info->HasKey(keys::kCodeKey); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 return false; | 134 return false; |
| 136 } | 135 } |
| 137 | 136 |
| 138 scoped_refptr<FileReader> file_reader(new FileReader( | 137 scoped_refptr<FileReader> file_reader(new FileReader( |
| 139 resource_, base::Bind(&ExecuteCodeInTabFunction::DidLoadFile, this))); | 138 resource_, base::Bind(&ExecuteCodeInTabFunction::DidLoadFile, this))); |
| 140 file_reader->Start(); | 139 file_reader->Start(); |
| 141 | 140 |
| 142 return true; | 141 return true; |
| 143 } | 142 } |
| 144 | 143 |
| 144 bool ExecuteCodeInTabFunction::OnMessageReceived(const IPC::Message& message) { |
| 145 if (message.type() != ExtensionHostMsg_ExecuteCodeFinished::ID) |
| 146 return false; |
| 147 |
| 148 int message_request_id; |
| 149 PickleIterator iter(message); |
| 150 if (!message.ReadInt(&iter, &message_request_id)) { |
| 151 NOTREACHED() << "malformed extension message"; |
| 152 return true; |
| 153 } |
| 154 |
| 155 if (message_request_id != request_id()) |
| 156 return false; |
| 157 |
| 158 IPC_BEGIN_MESSAGE_MAP(ExecuteCodeInTabFunction, message) |
| 159 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ExecuteCodeFinished, |
| 160 OnExecuteCodeFinished) |
| 161 IPC_END_MESSAGE_MAP() |
| 162 return true; |
| 163 } |
| 164 |
| 165 void ExecuteCodeInTabFunction::OnExecuteCodeFinished(int request_id, |
| 166 bool success, |
| 167 const std::string& error) { |
| 168 if (!error.empty()) { |
| 169 CHECK(!success); |
| 170 error_ = error; |
| 171 } |
| 172 |
| 173 SendResponse(success); |
| 174 |
| 175 Observe(NULL); |
| 176 Release(); // balanced in Execute() |
| 177 } |
| 178 |
| 145 void ExecuteCodeInTabFunction::DidLoadFile(bool success, | 179 void ExecuteCodeInTabFunction::DidLoadFile(bool success, |
| 146 const std::string& data) { | 180 const std::string& data) { |
| 147 std::string function_name = name(); | 181 std::string function_name = name(); |
| 148 const Extension* extension = GetExtension(); | 182 const Extension* extension = GetExtension(); |
| 149 | 183 |
| 150 // Check if the file is CSS and needs localization. | 184 // Check if the file is CSS and needs localization. |
| 151 if (success && | 185 if (success && |
| 152 function_name == TabsInsertCSSFunction::function_name() && | 186 function_name == TabsInsertCSSFunction::function_name() && |
| 153 extension != NULL && | 187 extension != NULL && |
| 154 data.find(ExtensionMessageBundle::kMessageBegin) != std::string::npos) { | 188 data.find(ExtensionMessageBundle::kMessageBegin) != std::string::npos) { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 params.in_main_world = false; | 277 params.in_main_world = false; |
| 244 contents->web_contents()->GetRenderViewHost()->Send( | 278 contents->web_contents()->GetRenderViewHost()->Send( |
| 245 new ExtensionMsg_ExecuteCode( | 279 new ExtensionMsg_ExecuteCode( |
| 246 contents->web_contents()->GetRenderViewHost()->GetRoutingID(), | 280 contents->web_contents()->GetRenderViewHost()->GetRoutingID(), |
| 247 params)); | 281 params)); |
| 248 | 282 |
| 249 Observe(contents->web_contents()); | 283 Observe(contents->web_contents()); |
| 250 AddRef(); // balanced in OnExecuteCodeFinished() | 284 AddRef(); // balanced in OnExecuteCodeFinished() |
| 251 return true; | 285 return true; |
| 252 } | 286 } |
| 253 | |
| 254 bool ExecuteCodeInTabFunction::OnMessageReceived(const IPC::Message& message) { | |
| 255 if (message.type() != ExtensionHostMsg_ExecuteCodeFinished::ID) | |
| 256 return false; | |
| 257 | |
| 258 int message_request_id; | |
| 259 PickleIterator iter(message); | |
| 260 if (!message.ReadInt(&iter, &message_request_id)) { | |
| 261 NOTREACHED() << "malformed extension message"; | |
| 262 return true; | |
| 263 } | |
| 264 | |
| 265 if (message_request_id != request_id()) | |
| 266 return false; | |
| 267 | |
| 268 IPC_BEGIN_MESSAGE_MAP(ExecuteCodeInTabFunction, message) | |
| 269 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ExecuteCodeFinished, | |
| 270 OnExecuteCodeFinished) | |
| 271 IPC_END_MESSAGE_MAP() | |
| 272 return true; | |
| 273 } | |
| 274 | |
| 275 void ExecuteCodeInTabFunction::OnExecuteCodeFinished(int request_id, | |
| 276 bool success, | |
| 277 const std::string& error) { | |
| 278 if (!error.empty()) { | |
| 279 CHECK(!success); | |
| 280 error_ = error; | |
| 281 } | |
| 282 | |
| 283 SendResponse(success); | |
| 284 | |
| 285 Observe(NULL); | |
| 286 Release(); // balanced in Execute() | |
| 287 } | |
| OLD | NEW |