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/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 } |
| 108 |
| 109 } // namespace |
| 110 |
| 111 ExtensionUserScriptLoader::ExtensionUserScriptLoader( |
| 112 Profile* profile, |
| 113 const HostID& host_id, |
| 114 bool listen_for_extension_system_loaded) |
| 115 : UserScriptLoader(profile, |
| 116 host_id, |
| 117 ExtensionSystem::Get(profile)->content_verifier()), |
| 118 extension_registry_observer_(this), |
| 119 weak_factory_(this) { |
| 120 extension_registry_observer_.Add(ExtensionRegistry::Get(profile)); |
| 121 if (listen_for_extension_system_loaded) { |
| 122 ExtensionSystem::Get(profile)->ready().Post( |
| 123 FROM_HERE, |
| 124 base::Bind(&ExtensionUserScriptLoader::OnExtensionSystemReady, |
| 125 weak_factory_.GetWeakPtr())); |
| 126 } else { |
| 127 SetReady(true); |
| 128 } |
| 129 } |
| 130 |
| 131 ExtensionUserScriptLoader::~ExtensionUserScriptLoader() { |
| 132 } |
| 133 |
| 134 void ExtensionUserScriptLoader::UpdateHostsInfo( |
| 135 const std::set<HostID>& changed_hosts) { |
| 136 ExtensionRegistry* registry = ExtensionRegistry::Get(profile()); |
| 137 for (const HostID& host_id : changed_hosts) { |
| 138 const Extension* extension = |
| 139 registry->GetExtensionById(host_id.id(), ExtensionRegistry::ENABLED); |
| 140 // |changed_hosts_| may include hosts that have been removed, |
| 141 // which leads to the above lookup failing. In this case, just continue. |
| 142 if (!extension) |
| 143 continue; |
| 144 AddHostInfo(host_id, ExtensionSet::ExtensionPathAndDefaultLocale( |
| 145 extension->path(), |
| 146 LocaleInfo::GetDefaultLocale(extension))); |
| 147 } |
| 148 } |
| 149 |
| 150 UserScriptLoader::LoadUserScriptsContentFunction |
| 151 ExtensionUserScriptLoader::GetLoadUserScriptsFunction() { |
| 152 return base::Bind(&ExtensionLoadScriptContent); |
| 153 } |
| 154 |
| 155 void ExtensionUserScriptLoader::OnExtensionUnloaded( |
| 156 content::BrowserContext* browser_context, |
| 157 const Extension* extension, |
| 158 UnloadedExtensionInfo::Reason reason) { |
| 159 RemoveHostInfo(HostID(HostID::EXTENSIONS, extension->id())); |
| 160 } |
| 161 |
| 162 void ExtensionUserScriptLoader::OnExtensionSystemReady() { |
| 163 SetReady(true); |
| 164 } |
| 165 |
| 166 } // namespace extensions |
OLD | NEW |