OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/renderer/user_script_set_manager.h" | 5 #include "extensions/renderer/user_script_set_manager.h" |
6 | 6 |
7 #include "components/crx_file/id_util.h" | 7 #include "components/crx_file/id_util.h" |
8 #include "content/public/renderer/render_thread.h" | 8 #include "content/public/renderer/render_thread.h" |
9 #include "extensions/common/extension_messages.h" | 9 #include "extensions/common/extension_messages.h" |
10 #include "extensions/renderer/dispatcher.h" | 10 #include "extensions/renderer/dispatcher.h" |
(...skipping 22 matching lines...) Expand all Loading... | |
33 } | 33 } |
34 | 34 |
35 scoped_ptr<ScriptInjection> | 35 scoped_ptr<ScriptInjection> |
36 UserScriptSetManager::GetInjectionForDeclarativeScript( | 36 UserScriptSetManager::GetInjectionForDeclarativeScript( |
37 int script_id, | 37 int script_id, |
38 blink::WebFrame* web_frame, | 38 blink::WebFrame* web_frame, |
39 int tab_id, | 39 int tab_id, |
40 const GURL& url, | 40 const GURL& url, |
41 const std::string& extension_id) { | 41 const std::string& extension_id) { |
42 UserScriptSet* user_script_set = | 42 UserScriptSet* user_script_set = |
43 GetProgrammaticScriptsByExtension(extension_id); | 43 GetProgrammaticScriptsByHostID(HostID(HostID::EXTENSIONS, extension_id)); |
44 if (!user_script_set) | 44 if (!user_script_set) |
45 return scoped_ptr<ScriptInjection>(); | 45 return scoped_ptr<ScriptInjection>(); |
46 | 46 |
47 return user_script_set->GetDeclarativeScriptInjection( | 47 return user_script_set->GetDeclarativeScriptInjection( |
48 script_id, | 48 script_id, |
49 web_frame, | 49 web_frame, |
50 tab_id, | 50 tab_id, |
51 UserScript::BROWSER_DRIVEN, | 51 UserScript::BROWSER_DRIVEN, |
52 url); | 52 url); |
53 } | 53 } |
(...skipping 25 matching lines...) Expand all Loading... | |
79 std::set<std::string>* ids) const { | 79 std::set<std::string>* ids) const { |
80 DCHECK(ids); | 80 DCHECK(ids); |
81 static_scripts_.GetActiveExtensionIds(ids); | 81 static_scripts_.GetActiveExtensionIds(ids); |
82 for (UserScriptSetMap::const_iterator it = programmatic_scripts_.begin(); | 82 for (UserScriptSetMap::const_iterator it = programmatic_scripts_.begin(); |
83 it != programmatic_scripts_.end(); | 83 it != programmatic_scripts_.end(); |
84 ++it) { | 84 ++it) { |
85 it->second->GetActiveExtensionIds(ids); | 85 it->second->GetActiveExtensionIds(ids); |
86 } | 86 } |
87 } | 87 } |
88 | 88 |
89 UserScriptSet* UserScriptSetManager::GetProgrammaticScriptsByExtension( | 89 UserScriptSet* UserScriptSetManager::GetProgrammaticScriptsByHostID( |
90 const ExtensionId& extension_id) { | 90 const HostID& host_id) { |
91 UserScriptSetMap::const_iterator it = | 91 UserScriptSetMap::const_iterator it = programmatic_scripts_.find(host_id); |
92 programmatic_scripts_.find(extension_id); | |
93 return it != programmatic_scripts_.end() ? it->second.get() : NULL; | 92 return it != programmatic_scripts_.end() ? it->second.get() : NULL; |
94 } | 93 } |
95 | 94 |
96 void UserScriptSetManager::OnUpdateUserScripts( | 95 void UserScriptSetManager::OnUpdateUserScripts( |
97 base::SharedMemoryHandle shared_memory, | 96 base::SharedMemoryHandle shared_memory, |
98 const ExtensionId& extension_id, | 97 const HostID& host_id, |
99 const std::set<std::string>& changed_extensions) { | 98 const std::set<HostID>& changed_hosts) { |
100 if (!base::SharedMemory::IsHandleValid(shared_memory)) { | 99 if (!base::SharedMemory::IsHandleValid(shared_memory)) { |
101 NOTREACHED() << "Bad scripts handle"; | 100 NOTREACHED() << "Bad scripts handle"; |
102 return; | 101 return; |
103 } | 102 } |
104 | 103 |
105 for (std::set<std::string>::const_iterator iter = changed_extensions.begin(); | 104 for (const HostID& host_id : changed_hosts) { |
106 iter != changed_extensions.end(); | 105 if (host_id.type() == HostID::EXTENSIONS && |
107 ++iter) { | 106 !crx_file::id_util::IdIsValid(host_id.id())) { |
108 if (!crx_file::id_util::IdIsValid(*iter)) { | 107 NOTREACHED() << "Invalid extension id: " << host_id.id(); |
109 NOTREACHED() << "Invalid extension id: " << *iter; | |
110 return; | 108 return; |
111 } | 109 } |
112 } | 110 } |
113 | 111 |
114 UserScriptSet* scripts = NULL; | 112 UserScriptSet* scripts = NULL; |
115 if (!extension_id.empty()) { | 113 if (!host_id.id().empty()) { |
116 // The expectation when there is an extension that "owns" this shared | 114 // The expectation when there is a host that "owns" this shared |
117 // memory region is that the |changed_extensions| is either the empty list | 115 // memory region is that the |changed_hosts| is either the empty list |
118 // or just the owner. | 116 // or just the owner. |
119 CHECK(changed_extensions.size() <= 1); | 117 CHECK(changed_hosts.size() <= 1); |
120 if (programmatic_scripts_.find(extension_id) == | 118 if (programmatic_scripts_.find(host_id) == programmatic_scripts_.end()) { |
121 programmatic_scripts_.end()) { | |
122 scripts = new UserScriptSet(extensions_); | 119 scripts = new UserScriptSet(extensions_); |
123 programmatic_scripts_[extension_id] = make_linked_ptr(scripts); | 120 programmatic_scripts_[host_id] = make_linked_ptr(scripts); |
124 } else { | 121 } else { |
125 scripts = programmatic_scripts_[extension_id].get(); | 122 scripts = programmatic_scripts_[host_id].get(); |
126 } | 123 } |
127 } else { | 124 } else { |
128 scripts = &static_scripts_; | 125 scripts = &static_scripts_; |
129 } | 126 } |
130 DCHECK(scripts); | 127 DCHECK(scripts); |
131 | 128 |
132 // If no extensions are included in the set, that indicates that all | 129 // If no hosts are included in the set, that indicates that all |
133 // extensions were updated. Add them all to the set so that observers and | 130 // hosts were updated. Add them all to the set so that observers and |
134 // individual UserScriptSets don't need to know this detail. | 131 // individual UserScriptSets don't need to know this detail. |
135 const std::set<std::string>* effective_extensions = &changed_extensions; | 132 const std::set<HostID>* effective_hosts = &changed_hosts; |
136 std::set<std::string> all_extensions; | 133 std::set<HostID> all_hosts; |
137 if (changed_extensions.empty()) { | 134 if (changed_hosts.empty()) { |
138 // The meaning of "all extensions" varies, depending on whether some | 135 // The meaning of "all hosts(extensions)" varies, depending on whether some |
139 // extension "owns" this shared memory region. | 136 // host "owns" this shared memory region. |
140 // No owner => all known extensions. | 137 // No owner => all known hosts. |
141 // Owner => just the owner extension. | 138 // Owner => just the owner host. |
142 if (extension_id.empty()) | 139 if (host_id.id().empty()) { |
143 all_extensions = extensions_->GetIDs(); | 140 std::set<std::string> extension_ids = extensions_->GetIDs(); |
144 else | 141 for (std::string extension_id : extension_ids) |
Devlin
2015/03/11 18:12:20
const std::string&
| |
145 all_extensions.insert(extension_id); | 142 all_hosts.insert(HostID(HostID::EXTENSIONS, extension_id)); |
146 effective_extensions = &all_extensions; | 143 } else { |
144 all_hosts.insert(host_id); | |
145 } | |
146 effective_hosts = &all_hosts; | |
147 } | 147 } |
148 | 148 |
149 if (scripts->UpdateUserScripts(shared_memory, *effective_extensions)) { | 149 if (scripts->UpdateUserScripts(shared_memory, *effective_hosts)) { |
150 FOR_EACH_OBSERVER( | 150 FOR_EACH_OBSERVER( |
151 Observer, | 151 Observer, |
152 observers_, | 152 observers_, |
153 OnUserScriptsUpdated(*effective_extensions, scripts->scripts())); | 153 OnUserScriptsUpdated(*effective_hosts, scripts->scripts())); |
154 } | 154 } |
155 } | 155 } |
156 | 156 |
157 } // namespace extensions | 157 } // namespace extensions |
OLD | NEW |