| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/files/file_enumerator.h" | 7 #include "base/files/file_enumerator.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/location.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" |
| 11 #include "base/sequenced_task_runner.h" |
| 12 #include "base/single_thread_task_runner.h" |
| 9 #include "base/values.h" | 13 #include "base/values.h" |
| 10 #include "base/version.h" | 14 #include "base/version.h" |
| 11 // TODO(ddorwin): Find a better place for ReadManifest. | 15 // TODO(ddorwin): Find a better place for ReadManifest. |
| 12 #include "chrome/browser/component_updater/component_unpacker.h" | 16 #include "chrome/browser/component_updater/component_unpacker.h" |
| 13 #include "chrome/browser/component_updater/default_component_installer.h" | 17 #include "chrome/browser/component_updater/default_component_installer.h" |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 | 18 |
| 16 namespace component_updater { | 19 namespace component_updater { |
| 17 | 20 |
| 18 namespace { | 21 namespace { |
| 19 // Version "0" corresponds to no installed version. By the server's conventions, | 22 // Version "0" corresponds to no installed version. By the server's conventions, |
| 20 // we represent it as a dotted quad. | 23 // we represent it as a dotted quad. |
| 21 const char kNullVersion[] = "0.0.0.0"; | 24 const char kNullVersion[] = "0.0.0.0"; |
| 22 } // namespace | 25 } // namespace |
| 23 | 26 |
| 24 ComponentInstallerTraits::~ComponentInstallerTraits() { | 27 ComponentInstallerTraits::~ComponentInstallerTraits() { |
| 25 } | 28 } |
| 26 | 29 |
| 27 DefaultComponentInstaller::DefaultComponentInstaller( | 30 DefaultComponentInstaller::DefaultComponentInstaller( |
| 28 scoped_ptr<ComponentInstallerTraits> installer_traits) | 31 scoped_ptr<ComponentInstallerTraits> installer_traits, |
| 29 : current_version_(kNullVersion) { | 32 scoped_refptr<base::SequencedTaskRunner> task_runner) |
| 33 : current_version_(kNullVersion), |
| 34 task_runner_(task_runner), |
| 35 main_task_runner_(base::MessageLoopProxy::current()) { |
| 30 installer_traits_ = installer_traits.Pass(); | 36 installer_traits_ = installer_traits.Pass(); |
| 31 } | 37 } |
| 32 | 38 |
| 33 DefaultComponentInstaller::~DefaultComponentInstaller() { | 39 DefaultComponentInstaller::~DefaultComponentInstaller() { |
| 40 DCHECK(thread_checker_.CalledOnValidThread()); |
| 34 } | 41 } |
| 35 | 42 |
| 36 void DefaultComponentInstaller::Register(ComponentUpdateService* cus) { | 43 void DefaultComponentInstaller::Register(ComponentUpdateService* cus) { |
| 44 DCHECK(thread_checker_.CalledOnValidThread()); |
| 37 if (!installer_traits_) { | 45 if (!installer_traits_) { |
| 38 NOTREACHED() << "A DefaultComponentInstaller has been created but " | 46 NOTREACHED() << "A DefaultComponentInstaller has been created but " |
| 39 << "has no installer traits."; | 47 << "has no installer traits."; |
| 40 return; | 48 return; |
| 41 } | 49 } |
| 42 content::BrowserThread::PostBlockingPoolTask( | 50 task_runner_->PostTask( |
| 43 FROM_HERE, | 51 FROM_HERE, |
| 44 base::Bind(&DefaultComponentInstaller::StartRegistration, | 52 base::Bind(&DefaultComponentInstaller::StartRegistration, |
| 45 base::Unretained(this), | 53 base::Unretained(this), |
| 46 cus)); | 54 cus)); |
| 47 } | 55 } |
| 48 | 56 |
| 49 void DefaultComponentInstaller::OnUpdateError(int error) { | 57 void DefaultComponentInstaller::OnUpdateError(int error) { |
| 50 NOTREACHED() << "Component update error: " << error; | 58 NOTREACHED() << "Component update error: " << error; |
| 51 } | 59 } |
| 52 | 60 |
| 53 bool DefaultComponentInstaller::InstallHelper( | 61 bool DefaultComponentInstaller::InstallHelper( |
| 54 const base::DictionaryValue& manifest, | 62 const base::DictionaryValue& manifest, |
| 55 const base::FilePath& unpack_path, | 63 const base::FilePath& unpack_path, |
| 56 const base::FilePath& install_path) { | 64 const base::FilePath& install_path) { |
| 57 if (!base::Move(unpack_path, install_path)) | 65 if (!base::Move(unpack_path, install_path)) |
| 58 return false; | 66 return false; |
| 59 if (!installer_traits_->OnCustomInstall(manifest, install_path)) | 67 if (!installer_traits_->OnCustomInstall(manifest, install_path)) |
| 60 return false; | 68 return false; |
| 61 if (!installer_traits_->VerifyInstallation(install_path)) | 69 if (!installer_traits_->VerifyInstallation(install_path)) |
| 62 return false; | 70 return false; |
| 63 return true; | 71 return true; |
| 64 } | 72 } |
| 65 | 73 |
| 66 bool DefaultComponentInstaller::Install(const base::DictionaryValue& manifest, | 74 bool DefaultComponentInstaller::Install(const base::DictionaryValue& manifest, |
| 67 const base::FilePath& unpack_path) { | 75 const base::FilePath& unpack_path) { |
| 76 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 68 std::string manifest_version; | 77 std::string manifest_version; |
| 69 manifest.GetStringASCII("version", &manifest_version); | 78 manifest.GetStringASCII("version", &manifest_version); |
| 70 base::Version version(manifest_version.c_str()); | 79 base::Version version(manifest_version.c_str()); |
| 71 if (!version.IsValid()) | 80 if (!version.IsValid()) |
| 72 return false; | 81 return false; |
| 73 if (current_version_.CompareTo(version) > 0) | 82 if (current_version_.CompareTo(version) > 0) |
| 74 return false; | 83 return false; |
| 75 base::FilePath install_path = | 84 base::FilePath install_path = |
| 76 installer_traits_->GetBaseDirectory().AppendASCII(version.GetString()); | 85 installer_traits_->GetBaseDirectory().AppendASCII(version.GetString()); |
| 77 if (base::PathExists(install_path)) { | 86 if (base::PathExists(install_path)) { |
| 78 if (!base::DeleteFile(install_path, true)) | 87 if (!base::DeleteFile(install_path, true)) |
| 79 return false; | 88 return false; |
| 80 } | 89 } |
| 81 if (!InstallHelper(manifest, unpack_path, install_path)) { | 90 if (!InstallHelper(manifest, unpack_path, install_path)) { |
| 82 base::DeleteFile(install_path, true); | 91 base::DeleteFile(install_path, true); |
| 83 return false; | 92 return false; |
| 84 } | 93 } |
| 85 current_version_ = version; | 94 current_version_ = version; |
| 86 // TODO(ddorwin): Change the parameter to scoped_ptr<base::DictionaryValue> | 95 // TODO(ddorwin): Change the parameter to scoped_ptr<base::DictionaryValue> |
| 87 // so we can avoid this DeepCopy. | 96 // so we can avoid this DeepCopy. |
| 88 current_manifest_.reset(manifest.DeepCopy()); | 97 current_manifest_.reset(manifest.DeepCopy()); |
| 89 scoped_ptr<base::DictionaryValue> manifest_copy( | 98 scoped_ptr<base::DictionaryValue> manifest_copy( |
| 90 current_manifest_->DeepCopy()); | 99 current_manifest_->DeepCopy()); |
| 91 content::BrowserThread::PostTask( | 100 main_task_runner_->PostTask(FROM_HERE, |
| 92 content::BrowserThread::UI, | 101 base::Bind(&ComponentInstallerTraits::ComponentReady, |
| 93 FROM_HERE, | 102 base::Unretained(installer_traits_.get()), |
| 94 base::Bind(&ComponentInstallerTraits::ComponentReady, | 103 current_version_, |
| 95 base::Unretained(installer_traits_.get()), | 104 GetInstallDirectory(), |
| 96 current_version_, | 105 base::Passed(&manifest_copy))); |
| 97 GetInstallDirectory(), | |
| 98 base::Passed(&manifest_copy))); | |
| 99 return true; | 106 return true; |
| 100 } | 107 } |
| 101 | 108 |
| 102 bool DefaultComponentInstaller::GetInstalledFile( | 109 bool DefaultComponentInstaller::GetInstalledFile( |
| 103 const std::string& file, | 110 const std::string& file, |
| 104 base::FilePath* installed_file) { | 111 base::FilePath* installed_file) { |
| 105 if (current_version_.Equals(base::Version(kNullVersion))) | 112 if (current_version_.Equals(base::Version(kNullVersion))) |
| 106 return false; // No component has been installed yet. | 113 return false; // No component has been installed yet. |
| 107 | 114 |
| 108 *installed_file = installer_traits_->GetBaseDirectory() | 115 *installed_file = installer_traits_->GetBaseDirectory() |
| 109 .AppendASCII(current_version_.GetString()) | 116 .AppendASCII(current_version_.GetString()) |
| 110 .AppendASCII(file); | 117 .AppendASCII(file); |
| 111 return true; | 118 return true; |
| 112 } | 119 } |
| 113 | 120 |
| 114 void DefaultComponentInstaller::StartRegistration(ComponentUpdateService* cus) { | 121 void DefaultComponentInstaller::StartRegistration(ComponentUpdateService* cus) { |
| 122 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 115 base::FilePath base_dir = installer_traits_->GetBaseDirectory(); | 123 base::FilePath base_dir = installer_traits_->GetBaseDirectory(); |
| 116 if (!base::PathExists(base_dir) && !base::CreateDirectory(base_dir)) { | 124 if (!base::PathExists(base_dir) && !base::CreateDirectory(base_dir)) { |
| 117 NOTREACHED() << "Could not create the base directory for " | 125 NOTREACHED() << "Could not create the base directory for " |
| 118 << installer_traits_->GetName() << " (" | 126 << installer_traits_->GetName() << " (" |
| 119 << base_dir.MaybeAsASCII() << ")."; | 127 << base_dir.MaybeAsASCII() << ")."; |
| 120 return; | 128 return; |
| 121 } | 129 } |
| 122 | 130 |
| 123 base::FilePath latest_dir; | 131 base::FilePath latest_dir; |
| 124 base::Version latest_version(kNullVersion); | 132 base::Version latest_version(kNullVersion); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 } | 177 } |
| 170 | 178 |
| 171 // Remove older versions of the component. None should be in use during | 179 // Remove older versions of the component. None should be in use during |
| 172 // browser startup. | 180 // browser startup. |
| 173 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin(); | 181 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin(); |
| 174 iter != older_dirs.end(); | 182 iter != older_dirs.end(); |
| 175 ++iter) { | 183 ++iter) { |
| 176 base::DeleteFile(*iter, true); | 184 base::DeleteFile(*iter, true); |
| 177 } | 185 } |
| 178 | 186 |
| 179 content::BrowserThread::PostTask( | 187 main_task_runner_->PostTask( |
| 180 content::BrowserThread::UI, | |
| 181 FROM_HERE, | 188 FROM_HERE, |
| 182 base::Bind(&DefaultComponentInstaller::FinishRegistration, | 189 base::Bind(&DefaultComponentInstaller::FinishRegistration, |
| 183 base::Unretained(this), | 190 base::Unretained(this), |
| 184 cus)); | 191 cus)); |
| 185 } | 192 } |
| 186 | 193 |
| 187 base::FilePath DefaultComponentInstaller::GetInstallDirectory() { | 194 base::FilePath DefaultComponentInstaller::GetInstallDirectory() { |
| 188 return installer_traits_->GetBaseDirectory() | 195 return installer_traits_->GetBaseDirectory() |
| 189 .AppendASCII(current_version_.GetString()); | 196 .AppendASCII(current_version_.GetString()); |
| 190 } | 197 } |
| 191 | 198 |
| 192 void DefaultComponentInstaller::FinishRegistration( | 199 void DefaultComponentInstaller::FinishRegistration( |
| 193 ComponentUpdateService* cus) { | 200 ComponentUpdateService* cus) { |
| 194 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 201 DCHECK(thread_checker_.CalledOnValidThread()); |
| 195 if (installer_traits_->CanAutoUpdate()) { | 202 if (installer_traits_->CanAutoUpdate()) { |
| 196 CrxComponent crx; | 203 CrxComponent crx; |
| 197 crx.name = installer_traits_->GetName(); | 204 crx.name = installer_traits_->GetName(); |
| 198 crx.installer = this; | 205 crx.installer = this; |
| 199 crx.version = current_version_; | 206 crx.version = current_version_; |
| 200 crx.fingerprint = current_fingerprint_; | 207 crx.fingerprint = current_fingerprint_; |
| 201 installer_traits_->GetHash(&crx.pk_hash); | 208 installer_traits_->GetHash(&crx.pk_hash); |
| 202 ComponentUpdateService::Status status = cus->RegisterComponent(crx); | 209 ComponentUpdateService::Status status = cus->RegisterComponent(crx); |
| 203 if (status != ComponentUpdateService::kOk && | 210 if (status != ComponentUpdateService::kOk && |
| 204 status != ComponentUpdateService::kReplaced) { | 211 status != ComponentUpdateService::kReplaced) { |
| 205 NOTREACHED() << "Component registration failed for " | 212 NOTREACHED() << "Component registration failed for " |
| 206 << installer_traits_->GetName(); | 213 << installer_traits_->GetName(); |
| 207 return; | 214 return; |
| 208 } | 215 } |
| 209 } | 216 } |
| 210 | 217 |
| 211 if (current_version_.CompareTo(base::Version(kNullVersion)) > 0) { | 218 if (current_version_.CompareTo(base::Version(kNullVersion)) > 0) { |
| 212 scoped_ptr<base::DictionaryValue> manifest_copy( | 219 scoped_ptr<base::DictionaryValue> manifest_copy( |
| 213 current_manifest_->DeepCopy()); | 220 current_manifest_->DeepCopy()); |
| 214 installer_traits_->ComponentReady( | 221 installer_traits_->ComponentReady( |
| 215 current_version_, GetInstallDirectory(), manifest_copy.Pass()); | 222 current_version_, GetInstallDirectory(), manifest_copy.Pass()); |
| 216 } | 223 } |
| 217 } | 224 } |
| 218 | 225 |
| 219 } // namespace component_updater | 226 } // namespace component_updater |
| OLD | NEW |