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

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: Introduce HostID for DeclarativeUserScriptMaser assignment, remove ConsumerID. 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 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/profiles/profile.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/browser/render_process_host.h"
19 #include "extensions/browser/component_extension_resource_manager.h"
20 #include "extensions/browser/content_verifier.h"
21 #include "extensions/browser/extension_registry.h"
22 #include "extensions/browser/extension_system.h"
23 #include "extensions/browser/extensions_browser_client.h"
24 #include "extensions/common/file_util.h"
25 #include "extensions/common/manifest_handlers/default_locale_handler.h"
26 #include "extensions/common/message_bundle.h"
27 #include "extensions/common/one_shot_event.h"
28 #include "ui/base/resource/resource_bundle.h"
29
30 namespace extensions {
31
32 namespace {
33
34 // Verifies file contents as they are read.
35 void VerifyContent(const scoped_refptr<ContentVerifier>& verifier,
36 const std::string& extension_id,
37 const base::FilePath& extension_root,
38 const base::FilePath& relative_path,
39 const std::string& content) {
40 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
41 scoped_refptr<ContentVerifyJob> job(
42 verifier->CreateJobFor(extension_id, extension_root, relative_path));
43 if (job.get()) {
44 job->Start();
45 job->BytesRead(content.size(), content.data());
46 job->DoneReading();
47 }
48 }
49
50 // Loads user scripts from the extension who owns these scripts.
51 bool ExtensionLoadScriptContent(
52 const HostID& host_id,
53 UserScript::File* script_file,
54 const UserScriptLoader::SubstitutionMap* localization_messages,
55 const scoped_refptr<ContentVerifier>& verifier) {
56 DCHECK(script_file);
57 std::string content;
58 const base::FilePath& path = ExtensionResource::GetFilePath(
59 script_file->extension_root(), script_file->relative_path(),
60 ExtensionResource::SYMLINKS_MUST_RESOLVE_WITHIN_ROOT);
61 if (path.empty()) {
62 int resource_id = 0;
63 if (ExtensionsBrowserClient::Get()
64 ->GetComponentExtensionResourceManager()
65 ->IsComponentExtensionResource(script_file->extension_root(),
66 script_file->relative_path(),
67 &resource_id)) {
68 const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
69 content = rb.GetRawDataResource(resource_id).as_string();
70 } else {
71 LOG(WARNING) << "Failed to get file path to "
72 << script_file->relative_path().value() << " from "
73 << script_file->extension_root().value();
74 return false;
75 }
76 } else {
77 if (!base::ReadFileToString(path, &content)) {
78 LOG(WARNING) << "Failed to load user script file: " << path.value();
79 return false;
80 }
81 if (verifier.get()) {
82 content::BrowserThread::PostTask(
83 content::BrowserThread::IO, FROM_HERE,
84 base::Bind(&VerifyContent, verifier, host_id.ID(),
85 script_file->extension_root(),
86 script_file->relative_path(), content));
87 }
88 }
89
90 // Localize the content.
91 if (localization_messages) {
92 std::string error;
93 MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages,
94 &content, &error);
95 if (!error.empty())
96 LOG(WARNING) << "Failed to replace messages in script: " << error;
97 }
98
99 // Remove BOM from the content.
100 std::string::size_type index = content.find(base::kUtf8ByteOrderMark);
101 if (index == 0)
102 script_file->set_content(content.substr(strlen(base::kUtf8ByteOrderMark)));
103 else
104 script_file->set_content(content);
105
106 return true;
107 }
Devlin 2015/01/26 20:12:08 nit: newline
Xi Han 2015/01/26 23:27:53 Done.
108 } // namespace
109
110 ExtensionUserScriptLoader::ExtensionUserScriptLoader(
111 Profile* profile,
112 const HostID& host_id,
113 bool listen_for_extension_system_loaded)
114 : UserScriptLoader(profile,
115 host_id,
116 ExtensionSystem::Get(profile)->content_verifier()),
117 extension_registry_observer_(this),
118 weak_factory_(this) {
119 extension_registry_observer_.Add(ExtensionRegistry::Get(profile));
120 if (listen_for_extension_system_loaded) {
121 ExtensionSystem::Get(profile)->ready().Post(
122 FROM_HERE,
123 base::Bind(&ExtensionUserScriptLoader::OnExtensionSystemReady,
124 weak_factory_.GetWeakPtr()));
125 } else {
126 SetReady(true);
127 }
128 }
129
130 ExtensionUserScriptLoader::~ExtensionUserScriptLoader() {
131 }
132
133 void ExtensionUserScriptLoader::UpdateHostsInfo(
134 const std::set<HostID>& changed_hosts) {
135 ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
136 for (const HostID& host_id : changed_hosts) {
137 const Extension* extension =
138 registry->GetExtensionById(host_id.ID(), ExtensionRegistry::ENABLED);
139 // |changed_hosts_| may include hosts that have been removed,
140 // which leads to the above lookup failing. In this case, just continue.
141 if (!extension)
142 continue;
143 AddHostInfo(host_id, ExtensionSet::ExtensionPathAndDefaultLocale(
144 extension->path(),
145 LocaleInfo::GetDefaultLocale(extension)));
146 }
147 }
148
149 UserScriptLoader::LoadUserScriptsContentFunction
150 ExtensionUserScriptLoader::GetLoadUserScriptsFunction() {
151 return base::Bind(&ExtensionLoadScriptContent);
152 }
153
154 void ExtensionUserScriptLoader::OnExtensionUnloaded(
155 content::BrowserContext* browser_context,
156 const Extension* extension,
157 UnloadedExtensionInfo::Reason reason) {
158 RemoveHostInfo(HostID(HostID::EXTENSIONS, extension->id()));
159 }
160
161 void ExtensionUserScriptLoader::OnExtensionSystemReady() {
162 SetReady(true);
163 }
164
165 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698