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

Side by Side Diff: chrome/browser/extensions/extension_user_script_loader.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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
Devlin 2015/01/14 16:45:09 check year (everywhere)
Xi Han 2015/01/14 23:46:03 Thanks. I didn't realize there are so many new fil
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/extension_user_script_loader.h"
6
7 #include <set>
8 #include <string>
9
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h"
14 #include "base/version.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/render_process_host.h"
20 #include "extensions/browser/component_extension_resource_manager.h"
21 #include "extensions/browser/content_verifier.h"
22 #include "extensions/browser/extension_registry.h"
23 #include "extensions/browser/extension_system.h"
24 #include "extensions/browser/extensions_browser_client.h"
25 #include "extensions/common/extension_messages.h"
26 #include "extensions/common/file_util.h"
27 #include "extensions/common/manifest_handlers/default_locale_handler.h"
28 #include "extensions/common/message_bundle.h"
29 #include "extensions/common/one_shot_event.h"
30 #include "ui/base/resource/resource_bundle.h"
31
32 using content::BrowserThread;
33
34 namespace extensions {
35 class ExtensionsBrowserClient;
Devlin 2015/01/14 16:45:09 why do you need this?
Xi Han 2015/01/14 23:46:03 Removed.
36
37 namespace {
38
39 // Verifies file contents as they are read.
40 void VerifyContent(scoped_refptr<ContentVerifier> verifier,
41 const ConsumerID& consumer_id,
42 const base::FilePath& extension_root,
43 const base::FilePath& relative_path,
44 const std::string& content) {
45 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
46 scoped_refptr<ContentVerifyJob> job(verifier->CreateJobFor(
47 consumer_id.host_id(), extension_root, relative_path));
48 if (job.get()) {
49 job->Start();
50 job->BytesRead(content.size(), content.data());
51 job->DoneReading();
52 }
53 }
54
55 } // namespace
56
57 ExtensionUserScriptLoader::ExtensionUserScriptLoader(
58 Profile* profile,
59 const ConsumerID& consumer_id,
60 bool listen_for_extension_system_loaded)
61 : UserScriptLoader(profile, consumer_id),
62 extension_system_ready_(false),
63 extension_registry_observer_(this),
64 weak_factory_(this) {
65 extension_registry_observer_.Add(ExtensionRegistry::Get(profile));
66 if (listen_for_extension_system_loaded) {
67 ExtensionSystem::Get(profile)->ready().Post(
68 FROM_HERE,
69 base::Bind(&ExtensionUserScriptLoader::OnExtensionSystemReady,
70 weak_factory_.GetWeakPtr()));
Devlin 2015/01/14 16:45:09 indentation
Xi Han 2015/01/14 23:46:03 Done.
71 } else
Devlin 2015/01/14 16:45:09 our bracketing rules are complex. if (one_line_if_
Xi Han 2015/01/14 23:46:03 I must misunderstood your last round of comments a
72 extension_system_ready_ = true;
73 }
74
75 ExtensionUserScriptLoader::~ExtensionUserScriptLoader() {
76 }
77
78 void ExtensionUserScriptLoader::UpdateConsumersInfo() {
79 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_);
80 for (std::set<ConsumerID>::const_iterator it = changed_consumers_.begin();
Devlin 2015/01/14 16:45:09 Let's C++11 this: for (const ConsumerID& consumer_
Xi Han 2015/01/14 23:46:03 Done.
81 it != changed_consumers_.end(); ++it) {
82 if (consumers_info_.find(*it) == consumers_info_.end()) {
83 const Extension* extension = registry->GetExtensionById(
84 it->host_id(), ExtensionRegistry::ENABLED);
85 // |changed_consumers_| may include consumers that have been removed,
86 // which leads to the above lookup failing. In this case, just continue.
87 if (!extension)
88 continue;
89 consumers_info_[*it] = ExtensionSet::ExtensionPathAndDefaultLocale(
90 extension->path(), LocaleInfo::GetDefaultLocale(extension));
91 }
92 }
93 }
94
95 bool ExtensionUserScriptLoader::Ready() {
96 return extension_system_ready_;
97 }
98
99 ContentVerifier* ExtensionUserScriptLoader::GetContentVerifier() {
100 return ExtensionSystem::Get(profile_)->content_verifier();
101 }
102
103 void ExtensionUserScriptLoader::SendUpdate(
104 content::RenderProcessHost* process,
105 base::SharedMemoryHandle handle_for_process,
106 const std::set<ConsumerID>& changed_consumers) {
107 // TODO(hanxi): update the IPC message to send a set of ConsumerID to render.
108 // Also, remove this function when the refactor is done on render side.
109 std::set<std::string> changed_extensions;
110 for (std::set<ConsumerID>::iterator it = changed_consumers.begin();
111 it != changed_consumers.end(); ++it) {
112 changed_extensions.insert(it->host_id());
113 }
114
115 if (base::SharedMemory::IsHandleValid(handle_for_process)) {
116 process->Send(new ExtensionMsg_UpdateUserScripts(
117 handle_for_process, consumer_id_.host_id(), changed_extensions));
118 }
119 }
120
121 UserScriptLoader::LoadUserScriptsFunctionCallback
122 ExtensionUserScriptLoader::GetLoadUserScriptsFunction() {
123 return base::Bind(&ExtensionUserScriptLoader::LoadScriptContent);
124 }
125
126 // static
127 bool ExtensionUserScriptLoader::LoadScriptContent(
128 const ConsumerID& consumer_id,
129 UserScript::File* script_file,
130 const SubstitutionMap* localization_messages,
131 scoped_refptr<ContentVerifier> verifier) {
132 DCHECK(script_file);
133 std::string content;
134 const base::FilePath& path = ExtensionResource::GetFilePath(
135 script_file->extension_root(), script_file->relative_path(),
136 ExtensionResource::SYMLINKS_MUST_RESOLVE_WITHIN_ROOT);
137 if (path.empty()) {
138 int resource_id = 0;
139 if (ExtensionsBrowserClient::Get()
140 ->GetComponentExtensionResourceManager()
141 ->IsComponentExtensionResource(script_file->extension_root(),
142 script_file->relative_path(),
143 &resource_id)) {
144 const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
145 content = rb.GetRawDataResource(resource_id).as_string();
146 } else {
147 LOG(WARNING) << "Failed to get file path to "
148 << script_file->relative_path().value() << " from "
149 << script_file->extension_root().value();
150 return false;
151 }
152 } else {
153 if (!base::ReadFileToString(path, &content)) {
154 LOG(WARNING) << "Failed to load user script file: " << path.value();
155 return false;
156 }
157 if (verifier.get()) {
158 content::BrowserThread::PostTask(
159 content::BrowserThread::IO, FROM_HERE,
160 base::Bind(&VerifyContent, verifier, consumer_id,
161 script_file->extension_root(),
162 script_file->relative_path(), content));
163 }
164 }
165
166 // Localize the content.
167 if (localization_messages) {
168 std::string error;
169 MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages,
170 &content, &error);
171 if (!error.empty())
172 LOG(WARNING) << "Failed to replace messages in script: " << error;
173 }
174
175 // Remove BOM from the content.
176 std::string::size_type index = content.find(base::kUtf8ByteOrderMark);
177 if (index == 0) {
178 script_file->set_content(content.substr(strlen(base::kUtf8ByteOrderMark)));
179 } else
180 script_file->set_content(content);
181
182 return true;
183 }
184
185 void ExtensionUserScriptLoader::OnExtensionUnloaded(
186 content::BrowserContext* browser_context,
187 const Extension* extension,
188 UnloadedExtensionInfo::Reason reason) {
189 std::set<ConsumerID> ids_to_delete;
190
191 for (ConsumersInfo::iterator it = consumers_info_.begin();
192 it != consumers_info_.end(); ++it) {
193 const ConsumerID& id = it->first;
194 if (id.host_id() == extension->id())
195 ids_to_delete.insert(id);
196 }
197
198 for (std::set<ConsumerID>::iterator it = ids_to_delete.begin();
199 it != ids_to_delete.end(); ++it)
200 consumers_info_.erase(*it);
201 }
202
203 void ExtensionUserScriptLoader::OnExtensionSystemReady() {
204 extension_system_ready_ = true;
205 AttemptLoad();
206 }
207
208 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698