Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / extensions / renderer / user_script_set_manager.cc
blob1c8f72d47c1fe1b8061b40e1b7a53511dd927588
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),
21 extensions_(extensions) {
22 content::RenderThread::Get()->AddObserver(this);
25 UserScriptSetManager::~UserScriptSetManager() {
28 void UserScriptSetManager::AddObserver(Observer* observer) {
29 observers_.AddObserver(observer);
32 void UserScriptSetManager::RemoveObserver(Observer* observer) {
33 observers_.RemoveObserver(observer);
36 scoped_ptr<ScriptInjection>
37 UserScriptSetManager::GetInjectionForDeclarativeScript(
38 int script_id,
39 blink::WebFrame* web_frame,
40 int tab_id,
41 const GURL& url,
42 const std::string& extension_id) {
43 UserScriptSet* user_script_set =
44 GetProgrammaticScriptsByHostID(HostID(HostID::EXTENSIONS, extension_id));
45 if (!user_script_set)
46 return scoped_ptr<ScriptInjection>();
48 return user_script_set->GetDeclarativeScriptInjection(
49 script_id,
50 web_frame,
51 tab_id,
52 UserScript::BROWSER_DRIVEN,
53 url);
56 bool UserScriptSetManager::OnControlMessageReceived(
57 const IPC::Message& message) {
58 bool handled = true;
59 IPC_BEGIN_MESSAGE_MAP(UserScriptSetManager, message)
60 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateUserScripts, OnUpdateUserScripts)
61 IPC_MESSAGE_UNHANDLED(handled = false)
62 IPC_END_MESSAGE_MAP()
63 return handled;
66 void UserScriptSetManager::GetAllInjections(
67 ScopedVector<ScriptInjection>* injections,
68 blink::WebFrame* web_frame,
69 int tab_id,
70 UserScript::RunLocation run_location) {
71 static_scripts_.GetInjections(injections, web_frame, tab_id, run_location);
72 for (UserScriptSetMap::iterator it = programmatic_scripts_.begin();
73 it != programmatic_scripts_.end();
74 ++it) {
75 it->second->GetInjections(injections, web_frame, tab_id, run_location);
79 void UserScriptSetManager::GetAllActiveExtensionIds(
80 std::set<std::string>* ids) const {
81 DCHECK(ids);
82 static_scripts_.GetActiveExtensionIds(ids);
83 for (UserScriptSetMap::const_iterator it = programmatic_scripts_.begin();
84 it != programmatic_scripts_.end();
85 ++it) {
86 it->second->GetActiveExtensionIds(ids);
90 UserScriptSet* UserScriptSetManager::GetProgrammaticScriptsByHostID(
91 const HostID& host_id) {
92 UserScriptSetMap::const_iterator it = programmatic_scripts_.find(host_id);
93 return it != programmatic_scripts_.end() ? it->second.get() : NULL;
96 void UserScriptSetManager::OnUpdateUserScripts(
97 base::SharedMemoryHandle shared_memory,
98 const HostID& host_id,
99 const std::set<HostID>& changed_hosts,
100 bool whitelisted_only) {
101 if (!base::SharedMemory::IsHandleValid(shared_memory)) {
102 NOTREACHED() << "Bad scripts handle";
103 return;
106 for (const HostID& host_id : changed_hosts) {
107 if (host_id.type() == HostID::EXTENSIONS &&
108 !crx_file::id_util::IdIsValid(host_id.id())) {
109 NOTREACHED() << "Invalid extension id: " << host_id.id();
110 return;
114 UserScriptSet* scripts = NULL;
115 if (!host_id.id().empty()) {
116 // The expectation when there is a host that "owns" this shared
117 // memory region is that the |changed_hosts| is either the empty list
118 // or just the owner.
119 CHECK(changed_hosts.size() <= 1);
120 if (programmatic_scripts_.find(host_id) == programmatic_scripts_.end()) {
121 scripts = new UserScriptSet(extensions_);
122 programmatic_scripts_[host_id] = make_linked_ptr(scripts);
123 } else {
124 scripts = programmatic_scripts_[host_id].get();
126 } else {
127 scripts = &static_scripts_;
129 DCHECK(scripts);
131 // If no hosts are included in the set, that indicates that all
132 // hosts were updated. Add them all to the set so that observers and
133 // individual UserScriptSets don't need to know this detail.
134 const std::set<HostID>* effective_hosts = &changed_hosts;
135 std::set<HostID> all_hosts;
136 if (changed_hosts.empty()) {
137 // The meaning of "all hosts(extensions)" varies, depending on whether some
138 // host "owns" this shared memory region.
139 // No owner => all known hosts.
140 // Owner => just the owner host.
141 if (host_id.id().empty()) {
142 std::set<std::string> extension_ids = extensions_->GetIDs();
143 for (const std::string& extension_id : extension_ids)
144 all_hosts.insert(HostID(HostID::EXTENSIONS, extension_id));
145 } else {
146 all_hosts.insert(host_id);
148 effective_hosts = &all_hosts;
151 if (scripts->UpdateUserScripts(shared_memory,
152 *effective_hosts,
153 whitelisted_only)) {
154 FOR_EACH_OBSERVER(
155 Observer,
156 observers_,
157 OnUserScriptsUpdated(*effective_hosts, scripts->scripts()));
161 } // namespace extensions