OLD | NEW |
(Empty) | |
| 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. |
| 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 using extensions::ExtensionsBrowserClient; |
| 34 |
| 35 namespace extensions { |
| 36 |
| 37 namespace { |
| 38 |
| 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)); |
| 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, consumer_id), |
| 61 extension_system_ready_(false), |
| 62 extension_registry_observer_(this), |
| 63 weak_factory_(this) { |
| 64 extension_registry_observer_.Add(ExtensionRegistry::Get(profile)); |
| 65 if (listen_for_extension_system_loaded) { |
| 66 ExtensionSystem::Get(profile)->ready().Post( |
| 67 FROM_HERE, |
| 68 base::Bind(&ExtensionUserScriptLoader::OnExtensionSystemReady, |
| 69 weak_factory_.GetWeakPtr())); |
| 70 } else { |
| 71 extension_system_ready_ = true; |
| 72 } |
| 73 } |
| 74 |
| 75 ExtensionUserScriptLoader::~ExtensionUserScriptLoader() { |
| 76 } |
| 77 |
| 78 bool ExtensionUserScriptLoader::ready() { |
| 79 return extension_system_ready_; |
| 80 } |
| 81 |
| 82 ContentVerifier* ExtensionUserScriptLoader::GetContentVerifier() { |
| 83 return ExtensionSystem::Get(profile_)->content_verifier(); |
| 84 } |
| 85 |
| 86 void ExtensionUserScriptLoader::SendUpdate( |
| 87 content::RenderProcessHost* process, |
| 88 base::SharedMemoryHandle handle_for_process, |
| 89 const std::set<ConsumerID>& changed_consumers) { |
| 90 // TODO(hanxi): update the IPC message to send a set of ConsumerID to render. |
| 91 // Also, remove this function when the refactor is done on render side. |
| 92 std::set<std::string> changed_extensions; |
| 93 for (std::set<ConsumerID>::iterator it = changed_consumers.begin(); |
| 94 it != changed_consumers.end(); ++it) { |
| 95 changed_extensions.insert(it->host_id); |
| 96 } |
| 97 |
| 98 if (base::SharedMemory::IsHandleValid(handle_for_process)) { |
| 99 process->Send(new ExtensionMsg_UpdateUserScripts( |
| 100 handle_for_process, consumer_id_.host_id, changed_extensions)); |
| 101 } |
| 102 } |
| 103 |
| 104 UserScriptLoader::LoadUserScriptsFunctionCallback |
| 105 ExtensionUserScriptLoader::GetLoadUserScriptsFunction() { |
| 106 return base::Bind(&ExtensionUserScriptLoader::LoadScriptContent); |
| 107 } |
| 108 |
| 109 // static |
| 110 bool ExtensionUserScriptLoader::LoadScriptContent( |
| 111 const ConsumerID& consumer_id, |
| 112 UserScript::File* script_file, |
| 113 const SubstitutionMap* localization_messages, |
| 114 scoped_refptr<ContentVerifier> verifier) { |
| 115 std::string content; |
| 116 const base::FilePath& path = ExtensionResource::GetFilePath( |
| 117 script_file->extension_root(), script_file->relative_path(), |
| 118 ExtensionResource::SYMLINKS_MUST_RESOLVE_WITHIN_ROOT); |
| 119 if (path.empty()) { |
| 120 int resource_id; |
| 121 if (ExtensionsBrowserClient::Get() |
| 122 ->GetComponentExtensionResourceManager() |
| 123 ->IsComponentExtensionResource(script_file->extension_root(), |
| 124 script_file->relative_path(), |
| 125 &resource_id)) { |
| 126 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 127 content = rb.GetRawDataResource(resource_id).as_string(); |
| 128 } else { |
| 129 LOG(WARNING) << "Failed to get file path to " |
| 130 << script_file->relative_path().value() << " from " |
| 131 << script_file->extension_root().value(); |
| 132 return false; |
| 133 } |
| 134 } else { |
| 135 if (!base::ReadFileToString(path, &content)) { |
| 136 LOG(WARNING) << "Failed to load user script file: " << path.value(); |
| 137 return false; |
| 138 } |
| 139 if (verifier.get()) { |
| 140 content::BrowserThread::PostTask( |
| 141 content::BrowserThread::IO, FROM_HERE, |
| 142 base::Bind(&VerifyContent, verifier, consumer_id, |
| 143 script_file->extension_root(), |
| 144 script_file->relative_path(), content)); |
| 145 } |
| 146 } |
| 147 |
| 148 // Localize the content. |
| 149 if (localization_messages) { |
| 150 std::string error; |
| 151 MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages, |
| 152 &content, &error); |
| 153 if (!error.empty()) { |
| 154 LOG(WARNING) << "Failed to replace messages in script: " << error; |
| 155 } |
| 156 } |
| 157 |
| 158 // Remove BOM from the content. |
| 159 std::string::size_type index = content.find(base::kUtf8ByteOrderMark); |
| 160 if (index == 0) { |
| 161 script_file->set_content(content.substr(strlen(base::kUtf8ByteOrderMark))); |
| 162 } else { |
| 163 script_file->set_content(content); |
| 164 } |
| 165 |
| 166 return true; |
| 167 } |
| 168 |
| 169 void ExtensionUserScriptLoader::OnExtensionUnloaded( |
| 170 content::BrowserContext* browser_context, |
| 171 const Extension* extension, |
| 172 UnloadedExtensionInfo::Reason reason) { |
| 173 std::set<ConsumerID> ids_to_delete; |
| 174 |
| 175 for (ConsumersInfo::iterator it = consumers_info_.begin(); |
| 176 it != consumers_info_.end(); ++it) { |
| 177 ConsumerID id = it->first; |
| 178 if (id.host_id.compare(extension->id()) == 0) |
| 179 ids_to_delete.insert(id); |
| 180 } |
| 181 |
| 182 for (std::set<ConsumerID>::iterator it = ids_to_delete.begin(); |
| 183 it != ids_to_delete.end(); ++it) |
| 184 consumers_info_.erase(*it); |
| 185 } |
| 186 |
| 187 void ExtensionUserScriptLoader::OnExtensionSystemReady() { |
| 188 extension_system_ready_ = true; |
| 189 AttemptLoad(); |
| 190 } |
| 191 |
| 192 void ExtensionUserScriptLoader::UpdateConsumersInfo() { |
| 193 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); |
| 194 for (std::set<ConsumerID>::const_iterator it = changed_consumers_.begin(); |
| 195 it != changed_consumers_.end(); ++it) { |
| 196 if (consumers_info_.find(*it) == consumers_info_.end()) { |
| 197 const Extension* extension = registry->GetExtensionById( |
| 198 it->host_id, ExtensionRegistry::EVERYTHING); |
| 199 // |changed_consumers_| may include consumers that have been removed, |
| 200 // which leads to the above lookup failing. In this case, just continue. |
| 201 if (!extension) |
| 202 continue; |
| 203 consumers_info_[*it] = ExtensionSet::ExtensionPathAndDefaultLocale( |
| 204 extension->path(), LocaleInfo::GetDefaultLocale(extension)); |
| 205 } |
| 206 } |
| 207 } |
| 208 |
| 209 } // namespace extensions |
OLD | NEW |