Add metrics for tracking LoginDatabase::Init failure.
[chromium-blink-merge.git] / extensions / renderer / user_script_set.cc
blob63634e0f04cfc919571deae1c22137374217b530
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.h"
7 #include "base/memory/ref_counted.h"
8 #include "content/public/common/url_constants.h"
9 #include "content/public/renderer/render_frame.h"
10 #include "content/public/renderer/render_thread.h"
11 #include "extensions/common/extension.h"
12 #include "extensions/common/extension_set.h"
13 #include "extensions/common/extensions_client.h"
14 #include "extensions/common/permissions/permissions_data.h"
15 #include "extensions/renderer/extension_injection_host.h"
16 #include "extensions/renderer/extensions_renderer_client.h"
17 #include "extensions/renderer/injection_host.h"
18 #include "extensions/renderer/script_context.h"
19 #include "extensions/renderer/script_injection.h"
20 #include "extensions/renderer/user_script_injector.h"
21 #include "extensions/renderer/web_ui_injection_host.h"
22 #include "third_party/WebKit/public/web/WebDocument.h"
23 #include "third_party/WebKit/public/web/WebLocalFrame.h"
24 #include "url/gurl.h"
26 namespace extensions {
28 namespace {
30 GURL GetDocumentUrlForFrame(blink::WebLocalFrame* frame) {
31 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame);
32 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) {
33 data_source_url = GURL(content::kViewSourceScheme + std::string(":") +
34 data_source_url.spec());
37 return data_source_url;
40 } // namespace
42 UserScriptSet::UserScriptSet(const ExtensionSet* extensions)
43 : extensions_(extensions) {
46 UserScriptSet::~UserScriptSet() {
49 void UserScriptSet::AddObserver(Observer* observer) {
50 observers_.AddObserver(observer);
53 void UserScriptSet::RemoveObserver(Observer* observer) {
54 observers_.RemoveObserver(observer);
57 void UserScriptSet::GetActiveExtensionIds(
58 std::set<std::string>* ids) const {
59 for (const UserScript* script : scripts_) {
60 if (script->host_id().type() != HostID::EXTENSIONS)
61 continue;
62 DCHECK(!script->extension_id().empty());
63 ids->insert(script->extension_id());
67 void UserScriptSet::GetInjections(
68 ScopedVector<ScriptInjection>* injections,
69 content::RenderFrame* render_frame,
70 int tab_id,
71 UserScript::RunLocation run_location) {
72 GURL document_url = GetDocumentUrlForFrame(render_frame->GetWebFrame());
73 for (const UserScript* script : scripts_) {
74 scoped_ptr<ScriptInjection> injection = GetInjectionForScript(
75 script,
76 render_frame,
77 tab_id,
78 run_location,
79 document_url,
80 false /* is_declarative */);
81 if (injection.get())
82 injections->push_back(injection.Pass());
86 bool UserScriptSet::UpdateUserScripts(base::SharedMemoryHandle shared_memory,
87 const std::set<HostID>& changed_hosts,
88 bool whitelisted_only) {
89 bool only_inject_incognito =
90 ExtensionsRendererClient::Get()->IsIncognitoProcess();
92 // Create the shared memory object (read only).
93 shared_memory_.reset(new base::SharedMemory(shared_memory, true));
94 if (!shared_memory_.get())
95 return false;
97 // First get the size of the memory block.
98 if (!shared_memory_->Map(sizeof(base::Pickle::Header)))
99 return false;
100 base::Pickle::Header* pickle_header =
101 reinterpret_cast<base::Pickle::Header*>(shared_memory_->memory());
103 // Now map in the rest of the block.
104 int pickle_size = sizeof(base::Pickle::Header) + pickle_header->payload_size;
105 shared_memory_->Unmap();
106 if (!shared_memory_->Map(pickle_size))
107 return false;
109 // Unpickle scripts.
110 size_t num_scripts = 0;
111 base::Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()),
112 pickle_size);
113 base::PickleIterator iter(pickle);
114 CHECK(iter.ReadSizeT(&num_scripts));
116 scripts_.clear();
117 scripts_.reserve(num_scripts);
118 for (size_t i = 0; i < num_scripts; ++i) {
119 scoped_ptr<UserScript> script(new UserScript());
120 script->Unpickle(pickle, &iter);
122 // Note that this is a pointer into shared memory. We don't own it. It gets
123 // cleared up when the last renderer or browser process drops their
124 // reference to the shared memory.
125 for (size_t j = 0; j < script->js_scripts().size(); ++j) {
126 const char* body = NULL;
127 int body_length = 0;
128 CHECK(iter.ReadData(&body, &body_length));
129 script->js_scripts()[j].set_external_content(
130 base::StringPiece(body, body_length));
132 for (size_t j = 0; j < script->css_scripts().size(); ++j) {
133 const char* body = NULL;
134 int body_length = 0;
135 CHECK(iter.ReadData(&body, &body_length));
136 script->css_scripts()[j].set_external_content(
137 base::StringPiece(body, body_length));
140 if (only_inject_incognito && !script->is_incognito_enabled())
141 continue; // This script shouldn't run in an incognito tab.
143 const Extension* extension = extensions_->GetByID(script->extension_id());
144 if (whitelisted_only &&
145 (!extension ||
146 !PermissionsData::CanExecuteScriptEverywhere(extension))) {
147 continue;
150 scripts_.push_back(script.Pass());
153 FOR_EACH_OBSERVER(Observer,
154 observers_,
155 OnUserScriptsUpdated(changed_hosts, scripts_.get()));
156 return true;
159 scoped_ptr<ScriptInjection> UserScriptSet::GetDeclarativeScriptInjection(
160 int script_id,
161 content::RenderFrame* render_frame,
162 int tab_id,
163 UserScript::RunLocation run_location,
164 const GURL& document_url) {
165 for (const UserScript* script : scripts_) {
166 if (script->id() == script_id) {
167 return GetInjectionForScript(script,
168 render_frame,
169 tab_id,
170 run_location,
171 document_url,
172 true /* is_declarative */);
175 return scoped_ptr<ScriptInjection>();
178 scoped_ptr<ScriptInjection> UserScriptSet::GetInjectionForScript(
179 const UserScript* script,
180 content::RenderFrame* render_frame,
181 int tab_id,
182 UserScript::RunLocation run_location,
183 const GURL& document_url,
184 bool is_declarative) {
185 scoped_ptr<ScriptInjection> injection;
186 scoped_ptr<const InjectionHost> injection_host;
187 blink::WebLocalFrame* web_frame = render_frame->GetWebFrame();
189 const HostID& host_id = script->host_id();
190 if (host_id.type() == HostID::EXTENSIONS) {
191 injection_host = ExtensionInjectionHost::Create(host_id.id(), extensions_);
192 if (!injection_host)
193 return injection.Pass();
194 } else {
195 DCHECK_EQ(host_id.type(), HostID::WEBUI);
196 injection_host.reset(new WebUIInjectionHost(host_id));
199 if (web_frame->parent() && !script->match_all_frames())
200 return injection.Pass(); // Only match subframes if the script declared it.
202 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL(
203 web_frame, document_url, script->match_about_blank());
205 if (!script->MatchesURL(effective_document_url))
206 return injection.Pass();
208 scoped_ptr<ScriptInjector> injector(new UserScriptInjector(script,
209 this,
210 is_declarative));
212 if (injector->CanExecuteOnFrame(
213 injection_host.get(),
214 web_frame,
215 -1 /* Content scripts are not tab-specific. */) ==
216 PermissionsData::ACCESS_DENIED) {
217 return injection.Pass();
220 bool inject_css = !script->css_scripts().empty() &&
221 run_location == UserScript::DOCUMENT_START;
222 bool inject_js =
223 !script->js_scripts().empty() && script->run_location() == run_location;
224 if (inject_css || inject_js) {
225 injection.reset(new ScriptInjection(
226 injector.Pass(),
227 render_frame,
228 injection_host.Pass(),
229 run_location,
230 tab_id));
232 return injection.Pass();
235 } // namespace extensions