Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2251)

Unified Diff: chrome/browser/extensions/api/declarative_content/content_action.cc

Issue 822453002: Introduce HostID and de-couple Extensions from "script injection System" [browser side] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix the test failures. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/declarative_content/content_action.cc
diff --git a/chrome/browser/extensions/api/declarative_content/content_action.cc b/chrome/browser/extensions/api/declarative_content/content_action.cc
index e0ff006e16d05bc8418a6014f331bb6300debe48..56afe2d011f1bc41531a44d0266e62d239bc4a30 100644
--- a/chrome/browser/extensions/api/declarative_content/content_action.cc
+++ b/chrome/browser/extensions/api/declarative_content/content_action.cc
@@ -7,6 +7,7 @@
#include <map>
#include "base/lazy_instance.h"
+#include "base/memory/linked_ptr.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/declarative_content/content_constants.h"
@@ -22,6 +23,7 @@
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
+#include "extensions/common/consumer.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_messages.h"
#include "ui/gfx/image/image.h"
@@ -239,6 +241,37 @@ struct ContentActionFactory {
base::LazyInstance<ContentActionFactory>::Leaky
g_content_action_factory = LAZY_INSTANCE_INITIALIZER;
+using RequestContentScriptKey = RequestContentScript::RequestKey;
+
+using RequestContentScriptKeyToIDMap =
+ 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.
+
+// A map of ConsumerIDs for the given RequestContentScript ids.
+// Different types of input ids need to define their own maps to store the
+// mappings from ids to ConsumerIDs.
+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.
+ request_content_script_key_to_id_map_ = LAZY_INSTANCE_INITIALIZER;
+
Devlin 2015/01/14 16:45:08 extra newline.
Xi Han 2015/01/14 23:46:02 Oops, removed.
+
+// Looks up and returns the ConsumerID by the given RequestContentScriptKey;
+// if one does not exist, a new object will be created and returned.
+const ConsumerID& GetConsumerID(
+ const RequestContentScriptKey& key,
+ ConsumerID::HostType host_type,
+ ConsumerID::InstanceType instance_type) {
+ RequestContentScriptKeyToIDMap& map =
+ request_content_script_key_to_id_map_.Get();
+ RequestContentScriptKeyToIDMap::iterator it =
+ map.find(key);
+ if (it != map.end())
+ return *(it->second.get());
+
+ linked_ptr<ConsumerID> consumer_id(new ConsumerID(
+ host_type, key.host_id, instance_type, ConsumerID::GetNextID()));
+ map[key] = consumer_id;
+ return *consumer_id;
+}
+
} // namespace
//
@@ -350,11 +383,13 @@ RequestContentScript::RequestContentScript(
content::BrowserContext* browser_context,
const Extension* extension,
const ScriptData& script_data) {
- InitScript(extension, script_data);
-
+ const RequestKey key(extension->id());
+ const ConsumerID& consumer_id = GetConsumerID(
+ key, ConsumerID::EXTENSIONS, ConsumerID::TAB);
master_ = ExtensionSystem::Get(browser_context)
->declarative_user_script_manager()
- ->GetDeclarativeUserScriptMasterByID(extension->id());
+ ->GetDeclarativeUserScriptMasterByID(consumer_id);
+ InitScript(extension, script_data);
AddScript();
}
@@ -362,21 +397,20 @@ RequestContentScript::RequestContentScript(
DeclarativeUserScriptMaster* master,
const Extension* extension,
const ScriptData& script_data) {
+ master_.reset(master);
InitScript(extension, script_data);
-
- master_ = master;
AddScript();
}
RequestContentScript::~RequestContentScript() {
- DCHECK(master_);
+ DCHECK(master_.get());
master_->RemoveScript(script_);
}
void RequestContentScript::InitScript(const Extension* extension,
const ScriptData& script_data) {
script_.set_id(UserScript::GenerateUserScriptID());
- script_.set_extension_id(extension->id());
+ script_.set_consumer_id(master_->consumer_id());
script_.set_run_location(UserScript::BROWSER_DRIVEN);
script_.set_match_all_frames(script_data.all_frames);
script_.set_match_about_blank(script_data.match_about_blank);

Powered by Google App Engine
This is Rietveld 408576698