Adding Peter Thatcher to the owners file.
[chromium-blink-merge.git] / extensions / renderer / user_script_set_manager.cc
blobfee2f724977c988002cee1ef85597d9e8e2998f8
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/renderer/user_script_set_manager.h"
7 #include "components/crx_file/id_util.h"
8 #include "content/public/renderer/render_thread.h"
9 #include "extensions/common/extension_messages.h"
10 #include "extensions/renderer/dispatcher.h"
11 #include "extensions/renderer/script_injection.h"
12 #include "extensions/renderer/user_script_set.h"
13 #include "ipc/ipc_message.h"
14 #include "ipc/ipc_message_macros.h"
15 #include "third_party/WebKit/public/web/WebFrame.h"
17 namespace extensions {
19 UserScriptSetManager::UserScriptSetManager(const ExtensionSet* extensions)
20 : static_scripts_(extensions), extensions_(extensions) {
21 content::RenderThread::Get()->AddObserver(this);
24 UserScriptSetManager::~UserScriptSetManager() {
27 void UserScriptSetManager::AddObserver(Observer* observer) {
28 observers_.AddObserver(observer);
31 void UserScriptSetManager::RemoveObserver(Observer* observer) {
32 observers_.RemoveObserver(observer);
35 scoped_ptr<ScriptInjection>
36 UserScriptSetManager::GetInjectionForDeclarativeScript(
37 int script_id,
38 blink::WebFrame* web_frame,
39 int tab_id,
40 const GURL& url,
41 const std::string& extension_id) {
42 UserScriptSet* user_script_set =
43 GetProgrammaticScriptsByHostID(HostID(HostID::EXTENSIONS, extension_id));
44 if (!user_script_set)
45 return scoped_ptr<ScriptInjection>();
47 return user_script_set->GetDeclarativeScriptInjection(
48 script_id,
49 web_frame,
50 tab_id,
51 UserScript::BROWSER_DRIVEN,
52 url);
55 bool UserScriptSetManager::OnControlMessageReceived(
56 const IPC::Message& message) {
57 bool handled = true;
58 IPC_BEGIN_MESSAGE_MAP(UserScriptSetManager, message)
59 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateUserScripts, OnUpdateUserScripts)
60 IPC_MESSAGE_UNHANDLED(handled = false)
61 IPC_END_MESSAGE_MAP()
62 return handled;
65 void UserScriptSetManager::GetAllInjections(
66 ScopedVector<ScriptInjection>* injections,
67 blink::WebFrame* web_frame,
68 int tab_id,
69 UserScript::RunLocation run_location) {
70 static_scripts_.GetInjections(injections, web_frame, tab_id, run_location);
71 for (UserScriptSetMap::iterator it = programmatic_scripts_.begin();
72 it != programmatic_scripts_.end();
73 ++it) {
74 it->second->GetInjections(injections, web_frame, tab_id, run_location);
78 void UserScriptSetManager::GetAllActiveExtensionIds(
79 std::set<std::string>* ids) const {
80 DCHECK(ids);
81 static_scripts_.GetActiveExtensionIds(ids);
82 for (UserScriptSetMap::const_iterator it = programmatic_scripts_.begin();
83 it != programmatic_scripts_.end();
84 ++it) {
85 it->second->GetActiveExtensionIds(ids);
89 UserScriptSet* UserScriptSetManager::GetProgrammaticScriptsByHostID(
90 const HostID& host_id) {
91 UserScriptSetMap::const_iterator it = programmatic_scripts_.find(host_id);
92 return it != programmatic_scripts_.end() ? it->second.get() : NULL;
95 void UserScriptSetManager::OnUpdateUserScripts(
96 base::SharedMemoryHandle shared_memory,
97 const HostID& host_id,
98 const std::set<HostID>& changed_hosts) {
99 if (!base::SharedMemory::IsHandleValid(shared_memory)) {
100 NOTREACHED() << "Bad scripts handle";
101 return;
104 for (const HostID& host_id : changed_hosts) {
105 if (host_id.type() == HostID::EXTENSIONS &&
106 !crx_file::id_util::IdIsValid(host_id.id())) {
107 NOTREACHED() << "Invalid extension id: " << host_id.id();
108 return;
112 UserScriptSet* scripts = NULL;
113 if (!host_id.id().empty()) {
114 // The expectation when there is a host that "owns" this shared
115 // memory region is that the |changed_hosts| is either the empty list
116 // or just the owner.
117 CHECK(changed_hosts.size() <= 1);
118 if (programmatic_scripts_.find(host_id) == programmatic_scripts_.end()) {
119 scripts = new UserScriptSet(extensions_);
120 programmatic_scripts_[host_id] = make_linked_ptr(scripts);
121 } else {
122 scripts = programmatic_scripts_[host_id].get();
124 } else {
125 scripts = &static_scripts_;
127 DCHECK(scripts);
129 // If no hosts are included in the set, that indicates that all
130 // hosts were updated. Add them all to the set so that observers and
131 // individual UserScriptSets don't need to know this detail.
132 const std::set<HostID>* effective_hosts = &changed_hosts;
133 std::set<HostID> all_hosts;
134 if (changed_hosts.empty()) {
135 // The meaning of "all hosts(extensions)" varies, depending on whether some
136 // host "owns" this shared memory region.
137 // No owner => all known hosts.
138 // Owner => just the owner host.
139 if (host_id.id().empty()) {
140 std::set<std::string> extension_ids = extensions_->GetIDs();
141 for (const std::string& extension_id : extension_ids)
142 all_hosts.insert(HostID(HostID::EXTENSIONS, extension_id));
143 } else {
144 all_hosts.insert(host_id);
146 effective_hosts = &all_hosts;
149 if (scripts->UpdateUserScripts(shared_memory, *effective_hosts)) {
150 FOR_EACH_OBSERVER(
151 Observer,
152 observers_,
153 OnUserScriptsUpdated(*effective_hosts, scripts->scripts()));
157 } // namespace extensions