OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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. | |
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" | |
Devlin
2015/01/20 17:51:05
nit: prune includes.
Xi Han
2015/01/21 21:30:16
Why I cannot find these unnecessary includes:(
| |
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; | |
Devlin
2015/01/20 17:51:05
nit: Even though you have this line, you specify "
Xi Han
2015/01/21 21:30:16
Removed.
| |
33 | |
34 namespace extensions { | |
35 | |
36 namespace { | |
37 | |
38 // Verifies file contents as they are read. | |
39 void VerifyContent(scoped_refptr<ContentVerifier> verifier, | |
40 const ConsumerID& consumer_id, | |
41 const base::FilePath& extension_root, | |
42 const base::FilePath& relative_path, | |
43 const std::string& content) { | |
44 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
45 scoped_refptr<ContentVerifyJob> job(verifier->CreateJobFor( | |
46 consumer_id.host_id(), extension_root, relative_path)); | |
Devlin
2015/01/20 17:51:05
nit: since this is in the ExtensionUserScriptLoade
Xi Han
2015/01/21 21:30:16
Done.
| |
47 if (job.get()) { | |
48 job->Start(); | |
49 job->BytesRead(content.size(), content.data()); | |
50 job->DoneReading(); | |
51 } | |
52 } | |
53 | |
54 } // namespace | |
55 | |
56 ExtensionUserScriptLoader::ExtensionUserScriptLoader( | |
57 Profile* profile, | |
58 const ConsumerID& consumer_id, | |
59 bool listen_for_extension_system_loaded) | |
60 : UserScriptLoader(profile, | |
61 consumer_id, | |
62 ExtensionSystem::Get(profile)->content_verifier()), | |
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 SetReady(false); | |
Devlin
2015/01/20 17:51:05
UserScriptLoader is, by default, set to false. Yo
Xi Han
2015/01/21 21:30:16
Done.
| |
68 ExtensionSystem::Get(profile)->ready().Post( | |
69 FROM_HERE, | |
70 base::Bind(&ExtensionUserScriptLoader::OnExtensionSystemReady, | |
71 weak_factory_.GetWeakPtr())); | |
72 } else { | |
73 SetReady(true); | |
74 } | |
75 } | |
76 | |
77 ExtensionUserScriptLoader::~ExtensionUserScriptLoader() { | |
78 } | |
79 | |
80 void ExtensionUserScriptLoader::UpdateConsumersInfo( | |
81 const ConsumerIDSet& changed_consumers) { | |
82 ExtensionRegistry* registry = ExtensionRegistry::Get(profile()); | |
83 for (const ConsumerID& consumer_id : changed_consumers) { | |
84 const Extension* extension = registry->GetExtensionById( | |
85 consumer_id.host_id(), ExtensionRegistry::ENABLED); | |
86 // |changed_consumers_| may include consumers that have been removed, | |
87 // which leads to the above lookup failing. In this case, just continue. | |
88 if (!extension) | |
89 continue; | |
90 AddConsumerInfo(consumer_id, ExtensionSet::ExtensionPathAndDefaultLocale( | |
91 extension->path(), | |
92 LocaleInfo::GetDefaultLocale(extension))); | |
93 } | |
94 } | |
95 | |
96 void ExtensionUserScriptLoader::SendUpdate( | |
97 content::RenderProcessHost* process, | |
98 base::SharedMemoryHandle handle_for_process, | |
99 const std::set<ConsumerID>& changed_consumers) { | |
100 // TODO(hanxi): update the IPC message to send a set of ConsumerID to render. | |
101 // Also, remove this function when the refactor is done on render side. | |
102 std::set<std::string> changed_extensions; | |
103 for (std::set<ConsumerID>::iterator it = changed_consumers.begin(); | |
Devlin
2015/01/20 17:51:05
nit: shorter:
for (const ConsumerID& id : changed_
Xi Han
2015/01/21 21:30:16
Done.
| |
104 it != changed_consumers.end(); ++it) { | |
105 changed_extensions.insert(it->host_id()); | |
106 } | |
107 | |
108 if (base::SharedMemory::IsHandleValid(handle_for_process)) { | |
109 process->Send(new ExtensionMsg_UpdateUserScripts( | |
110 handle_for_process, consumer_id().host_id(), changed_extensions)); | |
111 } | |
112 } | |
113 | |
114 UserScriptLoader::LoadUserScriptsFunctionCallback | |
115 ExtensionUserScriptLoader::GetLoadUserScriptsFunction() { | |
116 return base::Bind(&ExtensionUserScriptLoader::LoadScriptContent); | |
117 } | |
118 | |
119 // static | |
120 bool ExtensionUserScriptLoader::LoadScriptContent( | |
121 const ConsumerID& consumer_id, | |
122 UserScript::File* script_file, | |
123 const SubstitutionMap* localization_messages, | |
124 scoped_refptr<ContentVerifier> verifier) { | |
125 DCHECK(script_file); | |
126 std::string content; | |
127 const base::FilePath& path = ExtensionResource::GetFilePath( | |
128 script_file->extension_root(), script_file->relative_path(), | |
129 ExtensionResource::SYMLINKS_MUST_RESOLVE_WITHIN_ROOT); | |
130 if (path.empty()) { | |
131 int resource_id = 0; | |
132 if (ExtensionsBrowserClient::Get() | |
133 ->GetComponentExtensionResourceManager() | |
134 ->IsComponentExtensionResource(script_file->extension_root(), | |
135 script_file->relative_path(), | |
136 &resource_id)) { | |
137 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
138 content = rb.GetRawDataResource(resource_id).as_string(); | |
139 } else { | |
140 LOG(WARNING) << "Failed to get file path to " | |
141 << script_file->relative_path().value() << " from " | |
142 << script_file->extension_root().value(); | |
143 return false; | |
144 } | |
145 } else { | |
146 if (!base::ReadFileToString(path, &content)) { | |
147 LOG(WARNING) << "Failed to load user script file: " << path.value(); | |
148 return false; | |
149 } | |
150 if (verifier.get()) { | |
151 content::BrowserThread::PostTask( | |
152 content::BrowserThread::IO, FROM_HERE, | |
153 base::Bind(&VerifyContent, verifier, consumer_id, | |
154 script_file->extension_root(), | |
155 script_file->relative_path(), content)); | |
156 } | |
157 } | |
158 | |
159 // Localize the content. | |
160 if (localization_messages) { | |
161 std::string error; | |
162 MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages, | |
163 &content, &error); | |
164 if (!error.empty()) | |
165 LOG(WARNING) << "Failed to replace messages in script: " << error; | |
166 } | |
167 | |
168 // Remove BOM from the content. | |
169 std::string::size_type index = content.find(base::kUtf8ByteOrderMark); | |
170 if (index == 0) { | |
Devlin
2015/01/20 17:51:05
nit: no brackets.
Xi Han
2015/01/21 21:30:16
Done.
| |
171 script_file->set_content(content.substr(strlen(base::kUtf8ByteOrderMark))); | |
172 } else { | |
173 script_file->set_content(content); | |
174 } | |
175 | |
176 return true; | |
177 } | |
178 | |
179 void ExtensionUserScriptLoader::OnExtensionUnloaded( | |
180 content::BrowserContext* browser_context, | |
181 const Extension* extension, | |
182 UnloadedExtensionInfo::Reason reason) { | |
183 RemoveConsumerInfo(extension->id()); | |
184 } | |
185 | |
186 void ExtensionUserScriptLoader::OnExtensionSystemReady() { | |
187 SetReady(true); | |
188 } | |
189 | |
190 } // namespace extensions | |
OLD | NEW |