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