Chromium Code Reviews| 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/declarative_content/content_action.h" | 5 #include "chrome/browser/extensions/api/declarative_content/content_action.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/api/declarative_content/content_constants.h" | 12 #include "chrome/browser/extensions/api/declarative_content/content_constants.h" |
| 13 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 13 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| 14 #include "chrome/browser/extensions/declarative_user_script_manager.h" | 14 #include "chrome/browser/extensions/declarative_user_script_manager.h" |
| 15 #include "chrome/browser/extensions/extension_action.h" | 15 #include "chrome/browser/extensions/extension_action.h" |
| 16 #include "chrome/browser/extensions/extension_action_manager.h" | 16 #include "chrome/browser/extensions/extension_action_manager.h" |
| 17 #include "chrome/browser/extensions/extension_tab_util.h" | 17 #include "chrome/browser/extensions/extension_tab_util.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/sessions/session_tab_helper.h" | 19 #include "chrome/browser/sessions/session_tab_helper.h" |
| 20 #include "content/public/browser/invalidate_type.h" | 20 #include "content/public/browser/invalidate_type.h" |
| 21 #include "content/public/browser/render_view_host.h" | 21 #include "content/public/browser/render_view_host.h" |
| 22 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 23 #include "extensions/browser/extension_registry.h" | 23 #include "extensions/browser/extension_registry.h" |
| 24 #include "extensions/browser/extension_system.h" | 24 #include "extensions/browser/extension_system.h" |
| 25 #include "extensions/common/consumer_id.h" | |
| 25 #include "extensions/common/extension.h" | 26 #include "extensions/common/extension.h" |
| 26 #include "extensions/common/extension_messages.h" | 27 #include "extensions/common/extension_messages.h" |
| 27 #include "ui/gfx/image/image.h" | 28 #include "ui/gfx/image/image.h" |
| 28 #include "ui/gfx/image/image_skia.h" | 29 #include "ui/gfx/image/image_skia.h" |
| 29 | 30 |
| 30 namespace extensions { | 31 namespace extensions { |
| 31 | 32 |
| 32 namespace keys = declarative_content_constants; | 33 namespace keys = declarative_content_constants; |
| 33 | 34 |
| 34 namespace { | 35 namespace { |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 factory_methods[keys::kRequestContentScript] = | 233 factory_methods[keys::kRequestContentScript] = |
| 233 &RequestContentScript::Create; | 234 &RequestContentScript::Create; |
| 234 factory_methods[keys::kSetIcon] = | 235 factory_methods[keys::kSetIcon] = |
| 235 &SetIcon::Create; | 236 &SetIcon::Create; |
| 236 } | 237 } |
| 237 }; | 238 }; |
| 238 | 239 |
| 239 base::LazyInstance<ContentActionFactory>::Leaky | 240 base::LazyInstance<ContentActionFactory>::Leaky |
| 240 g_content_action_factory = LAZY_INSTANCE_INITIALIZER; | 241 g_content_action_factory = LAZY_INSTANCE_INITIALIZER; |
| 241 | 242 |
| 243 using RequestContentScriptKey = RequestContentScript::RequestKey; | |
| 244 | |
| 245 using RequestContentScriptKeyToIDMap = | |
| 246 std::map<RequestContentScriptKey, ConsumerID>; | |
| 247 | |
| 248 // A map of ConsumerIDs for the given RequestContentScript ids. | |
| 249 // Different types of input ids need to define their own maps to store the | |
| 250 // mappings from ids to ConsumerIDs. | |
| 251 base::LazyInstance<RequestContentScriptKeyToIDMap> | |
| 252 request_content_script_key_to_id_map_ = LAZY_INSTANCE_INITIALIZER; | |
| 253 | |
| 254 // Looks up and returns the ConsumerID by the given RequestContentScriptKey; | |
| 255 // if one does not exist, a new object will be created and returned. | |
| 256 const ConsumerID& GetConsumerID(const RequestContentScriptKey& key, | |
| 257 ConsumerID::HostType host_type, | |
| 258 ConsumerID::InstanceType instance_type) { | |
| 259 RequestContentScriptKeyToIDMap& map = | |
| 260 request_content_script_key_to_id_map_.Get(); | |
| 261 auto it = map.find(key); | |
| 262 if (it == map.end()) { | |
| 263 it = map.insert(std::make_pair(key, | |
| 264 ConsumerID(host_type, key, instance_type, | |
| 265 true /* is_declarative*/))).first; | |
| 266 } | |
| 267 return it->second; | |
| 268 } | |
| 269 | |
| 242 } // namespace | 270 } // namespace |
| 243 | 271 |
| 244 // | 272 // |
| 245 // RequestContentScript | 273 // RequestContentScript |
| 246 // | 274 // |
| 247 | 275 |
| 248 struct RequestContentScript::ScriptData { | 276 struct RequestContentScript::ScriptData { |
| 249 ScriptData(); | 277 ScriptData(); |
| 250 ~ScriptData(); | 278 ~ScriptData(); |
| 251 | 279 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 &script_data->match_about_blank)); | 371 &script_data->match_about_blank)); |
| 344 } | 372 } |
| 345 | 373 |
| 346 return true; | 374 return true; |
| 347 } | 375 } |
| 348 | 376 |
| 349 RequestContentScript::RequestContentScript( | 377 RequestContentScript::RequestContentScript( |
| 350 content::BrowserContext* browser_context, | 378 content::BrowserContext* browser_context, |
| 351 const Extension* extension, | 379 const Extension* extension, |
| 352 const ScriptData& script_data) { | 380 const ScriptData& script_data) { |
| 353 InitScript(extension, script_data); | 381 const RequestKey key(extension->id()); |
|
Devlin
2015/01/21 23:25:20
I think the typedef to RequestKey actually makes i
Xi Han
2015/01/22 17:19:36
Sorry, but no. To support <webview>, we need to ex
| |
| 354 | 382 const ConsumerID& consumer_id = |
| 383 GetConsumerID(key, ConsumerID::EXTENSIONS, ConsumerID::TAB); | |
| 355 master_ = ExtensionSystem::Get(browser_context) | 384 master_ = ExtensionSystem::Get(browser_context) |
| 356 ->declarative_user_script_manager() | 385 ->declarative_user_script_manager() |
| 357 ->GetDeclarativeUserScriptMasterByID(extension->id()); | 386 ->GetDeclarativeUserScriptMasterByID(consumer_id); |
| 387 InitScript(extension, script_data); | |
| 358 AddScript(); | 388 AddScript(); |
| 359 } | 389 } |
| 360 | 390 |
| 361 RequestContentScript::RequestContentScript( | 391 RequestContentScript::RequestContentScript( |
| 362 DeclarativeUserScriptMaster* master, | 392 DeclarativeUserScriptMaster* master, |
| 363 const Extension* extension, | 393 const Extension* extension, |
| 364 const ScriptData& script_data) { | 394 const ScriptData& script_data) { |
| 395 master_ = master; | |
| 365 InitScript(extension, script_data); | 396 InitScript(extension, script_data); |
| 366 | |
| 367 master_ = master; | |
| 368 AddScript(); | 397 AddScript(); |
| 369 } | 398 } |
| 370 | 399 |
| 371 RequestContentScript::~RequestContentScript() { | 400 RequestContentScript::~RequestContentScript() { |
| 372 DCHECK(master_); | 401 DCHECK(master_); |
| 373 master_->RemoveScript(script_); | 402 master_->RemoveScript(script_); |
| 374 } | 403 } |
| 375 | 404 |
| 376 void RequestContentScript::InitScript(const Extension* extension, | 405 void RequestContentScript::InitScript(const Extension* extension, |
| 377 const ScriptData& script_data) { | 406 const ScriptData& script_data) { |
| 378 script_.set_id(UserScript::GenerateUserScriptID()); | 407 script_.set_id(UserScript::GenerateUserScriptID()); |
| 379 script_.set_extension_id(extension->id()); | 408 script_.set_consumer_id(master_->consumer_id()); |
| 380 script_.set_run_location(UserScript::BROWSER_DRIVEN); | 409 script_.set_run_location(UserScript::BROWSER_DRIVEN); |
| 381 script_.set_match_all_frames(script_data.all_frames); | 410 script_.set_match_all_frames(script_data.all_frames); |
| 382 script_.set_match_about_blank(script_data.match_about_blank); | 411 script_.set_match_about_blank(script_data.match_about_blank); |
| 383 for (std::vector<std::string>::const_iterator it = | 412 for (std::vector<std::string>::const_iterator it = |
| 384 script_data.css_file_names.begin(); | 413 script_data.css_file_names.begin(); |
| 385 it != script_data.css_file_names.end(); ++it) { | 414 it != script_data.css_file_names.end(); ++it) { |
| 386 GURL url = extension->GetResourceURL(*it); | 415 GURL url = extension->GetResourceURL(*it); |
| 387 ExtensionResource resource = extension->GetResource(*it); | 416 ExtensionResource resource = extension->GetResource(*it); |
| 388 script_.css_scripts().push_back(UserScript::File( | 417 script_.css_scripts().push_back(UserScript::File( |
| 389 resource.extension_root(), resource.relative_path(), url)); | 418 resource.extension_root(), resource.relative_path(), url)); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 496 bool* bad_message, | 525 bool* bad_message, |
| 497 const base::DictionaryValue** action_dict, | 526 const base::DictionaryValue** action_dict, |
| 498 std::string* instance_type) { | 527 std::string* instance_type) { |
| 499 INPUT_FORMAT_VALIDATE(json_action.GetAsDictionary(action_dict)); | 528 INPUT_FORMAT_VALIDATE(json_action.GetAsDictionary(action_dict)); |
| 500 INPUT_FORMAT_VALIDATE( | 529 INPUT_FORMAT_VALIDATE( |
| 501 (*action_dict)->GetString(keys::kInstanceType, instance_type)); | 530 (*action_dict)->GetString(keys::kInstanceType, instance_type)); |
| 502 return true; | 531 return true; |
| 503 } | 532 } |
| 504 | 533 |
| 505 } // namespace extensions | 534 } // namespace extensions |
| OLD | NEW |