| OLD | NEW | 
|    1 // Copyright 2014 The Chromium Authors. All rights reserved. |    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 |    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 "components/component_updater/default_component_installer.h" |    5 #include "components/component_updater/default_component_installer.h" | 
|    6  |    6  | 
|    7 #include <utility> |    7 #include <utility> | 
|    8  |    8  | 
|    9 #include "base/bind.h" |    9 #include "base/bind.h" | 
|   10 #include "base/bind_helpers.h" |   10 #include "base/bind_helpers.h" | 
|   11 #include "base/files/file_enumerator.h" |   11 #include "base/files/file_enumerator.h" | 
|   12 #include "base/files/file_path.h" |   12 #include "base/files/file_path.h" | 
|   13 #include "base/files/file_util.h" |   13 #include "base/files/file_util.h" | 
|   14 #include "base/location.h" |   14 #include "base/location.h" | 
|   15 #include "base/path_service.h" |   15 #include "base/path_service.h" | 
|   16 #include "base/sequenced_task_runner.h" |   16 #include "base/sequenced_task_runner.h" | 
|   17 #include "base/single_thread_task_runner.h" |   17 #include "base/single_thread_task_runner.h" | 
|   18 #include "base/threading/thread_task_runner_handle.h" |   18 #include "base/threading/thread_task_runner_handle.h" | 
|   19 #include "base/values.h" |   19 #include "base/values.h" | 
|   20 #include "base/version.h" |   20 #include "base/version.h" | 
|   21 #include "components/component_updater/component_updater_paths.h" |   21 #include "components/component_updater/component_updater_paths.h" | 
|   22 // TODO(ddorwin): Find a better place for ReadManifest. |   22 // TODO(ddorwin): Find a better place for ReadManifest. | 
|   23 #include "components/component_updater/component_updater_service.h" |   23 #include "components/component_updater/component_updater_service.h" | 
|   24 #include "components/update_client/component_unpacker.h" |   24 #include "components/update_client/component_unpacker.h" | 
|   25 #include "components/update_client/update_client.h" |  | 
|   26 #include "components/update_client/update_client_errors.h" |  | 
|   27 #include "components/update_client/utils.h" |   25 #include "components/update_client/utils.h" | 
|   28  |   26  | 
 |   27 using update_client::CrxComponent; | 
 |   28  | 
|   29 namespace component_updater { |   29 namespace component_updater { | 
|   30  |   30  | 
|   31 namespace { |   31 namespace { | 
|   32  |   32  | 
|   33 // Version "0" corresponds to no installed version. By the server's conventions, |   33 // Version "0" corresponds to no installed version. By the server's conventions, | 
|   34 // we represent it as a dotted quad. |   34 // we represent it as a dotted quad. | 
|   35 const char kNullVersion[] = "0.0.0.0"; |   35 const char kNullVersion[] = "0.0.0.0"; | 
|   36  |   36  | 
|   37 using Result = update_client::CrxInstaller::Result; |  | 
|   38 using InstallError = update_client::InstallError; |  | 
|   39  |  | 
|   40 }  // namespace |   37 }  // namespace | 
|   41  |   38  | 
|   42 ComponentInstallerTraits::~ComponentInstallerTraits() { |   39 ComponentInstallerTraits::~ComponentInstallerTraits() { | 
|   43 } |   40 } | 
|   44  |   41  | 
|   45 DefaultComponentInstaller::DefaultComponentInstaller( |   42 DefaultComponentInstaller::DefaultComponentInstaller( | 
|   46     std::unique_ptr<ComponentInstallerTraits> installer_traits) |   43     std::unique_ptr<ComponentInstallerTraits> installer_traits) | 
|   47     : current_version_(kNullVersion), |   44     : current_version_(kNullVersion), | 
|   48       main_task_runner_(base::ThreadTaskRunnerHandle::Get()) { |   45       main_task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 
|   49   installer_traits_ = std::move(installer_traits); |   46   installer_traits_ = std::move(installer_traits); | 
| (...skipping 18 matching lines...) Expand all  Loading... | 
|   68       base::Bind(&DefaultComponentInstaller::StartRegistration, |   65       base::Bind(&DefaultComponentInstaller::StartRegistration, | 
|   69                  this, cus), |   66                  this, cus), | 
|   70       base::Bind(&DefaultComponentInstaller::FinishRegistration, |   67       base::Bind(&DefaultComponentInstaller::FinishRegistration, | 
|   71                  this, cus, callback)); |   68                  this, cus, callback)); | 
|   72 } |   69 } | 
|   73  |   70  | 
|   74 void DefaultComponentInstaller::OnUpdateError(int error) { |   71 void DefaultComponentInstaller::OnUpdateError(int error) { | 
|   75   LOG(ERROR) << "Component update error: " << error; |   72   LOG(ERROR) << "Component update error: " << error; | 
|   76 } |   73 } | 
|   77  |   74  | 
|   78 Result DefaultComponentInstaller::InstallHelper( |   75 bool DefaultComponentInstaller::InstallHelper( | 
|   79     const base::DictionaryValue& manifest, |   76     const base::DictionaryValue& manifest, | 
|   80     const base::FilePath& unpack_path, |   77     const base::FilePath& unpack_path, | 
|   81     const base::FilePath& install_path) { |   78     const base::FilePath& install_path) { | 
|   82   VLOG(1) << "InstallHelper: unpack_path=" << unpack_path.AsUTF8Unsafe() |   79   VLOG(1) << "InstallHelper: unpack_path=" << unpack_path.AsUTF8Unsafe() | 
|   83           << " install_path=" << install_path.AsUTF8Unsafe(); |   80           << " install_path=" << install_path.AsUTF8Unsafe(); | 
|   84  |   81  | 
|   85   if (!base::Move(unpack_path, install_path)) { |   82   if (!base::Move(unpack_path, install_path)) { | 
|   86     PLOG(ERROR) << "Move failed."; |   83     PLOG(ERROR) << "Move failed."; | 
|   87     return Result(InstallError::GENERIC_ERROR); |   84     return false; | 
|   88   } |   85   } | 
|   89   const auto result = |   86   if (!installer_traits_->OnCustomInstall(manifest, install_path)) { | 
|   90       installer_traits_->OnCustomInstall(manifest, install_path); |  | 
|   91   if (result.error) { |  | 
|   92     PLOG(ERROR) << "CustomInstall failed."; |   87     PLOG(ERROR) << "CustomInstall failed."; | 
|   93     return result; |   88     return false; | 
|   94   } |   89   } | 
|   95   if (!installer_traits_->VerifyInstallation(manifest, install_path)) { |   90   if (!installer_traits_->VerifyInstallation(manifest, install_path)) { | 
|   96     PLOG(ERROR) << "VerifyInstallation failed."; |   91     PLOG(ERROR) << "VerifyInstallation failed."; | 
|   97     return Result(InstallError::GENERIC_ERROR); |   92     return false; | 
|   98   } |   93   } | 
|   99  |   94  | 
|  100   return Result(InstallError::NONE); |   95   return true; | 
|  101 } |   96 } | 
|  102  |   97  | 
|  103 Result DefaultComponentInstaller::Install(const base::DictionaryValue& manifest, |   98 bool DefaultComponentInstaller::Install(const base::DictionaryValue& manifest, | 
|  104                                           const base::FilePath& unpack_path) { |   99                                         const base::FilePath& unpack_path) { | 
|  105   std::string manifest_version; |  100   std::string manifest_version; | 
|  106   manifest.GetStringASCII("version", &manifest_version); |  101   manifest.GetStringASCII("version", &manifest_version); | 
|  107   base::Version version(manifest_version); |  102   base::Version version(manifest_version); | 
|  108  |  103  | 
|  109   VLOG(1) << "Install: version=" << version.GetString() |  104   VLOG(1) << "Install: version=" << version.GetString() | 
|  110           << " current version=" << current_version_.GetString(); |  105           << " current version=" << current_version_.GetString(); | 
|  111  |  106  | 
|  112   if (!version.IsValid()) |  107   if (!version.IsValid()) | 
|  113     return Result(InstallError::GENERIC_ERROR); |  108     return false; | 
|  114   if (current_version_.CompareTo(version) > 0) |  109   if (current_version_.CompareTo(version) > 0) | 
|  115     return Result(InstallError::GENERIC_ERROR); |  110     return false; | 
|  116   base::FilePath install_path; |  111   base::FilePath install_path; | 
|  117   if (!PathService::Get(DIR_COMPONENT_USER, &install_path)) |  112   if (!PathService::Get(DIR_COMPONENT_USER, &install_path)) | 
|  118     return Result(InstallError::GENERIC_ERROR); |  113     return false; | 
|  119   install_path = install_path.Append(installer_traits_->GetRelativeInstallDir()) |  114   install_path = install_path.Append(installer_traits_->GetRelativeInstallDir()) | 
|  120                      .AppendASCII(version.GetString()); |  115                      .AppendASCII(version.GetString()); | 
|  121   if (base::PathExists(install_path)) { |  116   if (base::PathExists(install_path)) { | 
|  122     if (!base::DeleteFile(install_path, true)) |  117     if (!base::DeleteFile(install_path, true)) | 
|  123       return Result(InstallError::GENERIC_ERROR); |  118       return false; | 
|  124   } |  119   } | 
|  125   const auto result = InstallHelper(manifest, unpack_path, install_path); |  120   if (!InstallHelper(manifest, unpack_path, install_path)) { | 
|  126   if (result.error) { |  | 
|  127     base::DeleteFile(install_path, true); |  121     base::DeleteFile(install_path, true); | 
|  128     return result; |  122     return false; | 
|  129   } |  123   } | 
|  130   current_version_ = version; |  124   current_version_ = version; | 
|  131   current_install_dir_ = install_path; |  125   current_install_dir_ = install_path; | 
|  132   // TODO(ddorwin): Change parameter to std::unique_ptr<base::DictionaryValue> |  126   // TODO(ddorwin): Change parameter to std::unique_ptr<base::DictionaryValue> | 
|  133   // so we can avoid this DeepCopy. |  127   // so we can avoid this DeepCopy. | 
|  134   current_manifest_.reset(manifest.DeepCopy()); |  128   current_manifest_.reset(manifest.DeepCopy()); | 
|  135   std::unique_ptr<base::DictionaryValue> manifest_copy( |  129   std::unique_ptr<base::DictionaryValue> manifest_copy( | 
|  136       current_manifest_->DeepCopy()); |  130       current_manifest_->DeepCopy()); | 
|  137   main_task_runner_->PostTask( |  131   main_task_runner_->PostTask( | 
|  138       FROM_HERE, |  132       FROM_HERE, | 
|  139       base::Bind(&DefaultComponentInstaller::ComponentReady, |  133       base::Bind(&DefaultComponentInstaller::ComponentReady, | 
|  140                  this, base::Passed(&manifest_copy))); |  134                  this, base::Passed(&manifest_copy))); | 
|  141   return result; |  135   return true; | 
|  142 } |  136 } | 
|  143  |  137  | 
|  144 bool DefaultComponentInstaller::GetInstalledFile( |  138 bool DefaultComponentInstaller::GetInstalledFile( | 
|  145     const std::string& file, |  139     const std::string& file, | 
|  146     base::FilePath* installed_file) { |  140     base::FilePath* installed_file) { | 
|  147   if (current_version_ == base::Version(kNullVersion)) |  141   if (current_version_ == base::Version(kNullVersion)) | 
|  148     return false;  // No component has been installed yet. |  142     return false;  // No component has been installed yet. | 
|  149   *installed_file = current_install_dir_.AppendASCII(file); |  143   *installed_file = current_install_dir_.AppendASCII(file); | 
|  150   return true; |  144   return true; | 
|  151 } |  145 } | 
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  324       DLOG(ERROR) << "Couldn't delete " << base_dir.value(); |  318       DLOG(ERROR) << "Couldn't delete " << base_dir.value(); | 
|  325   } |  319   } | 
|  326 } |  320 } | 
|  327  |  321  | 
|  328 void DefaultComponentInstaller::FinishRegistration( |  322 void DefaultComponentInstaller::FinishRegistration( | 
|  329     ComponentUpdateService* cus, |  323     ComponentUpdateService* cus, | 
|  330     const base::Closure& callback) { |  324     const base::Closure& callback) { | 
|  331   VLOG(1) << __func__ << " for " << installer_traits_->GetName(); |  325   VLOG(1) << __func__ << " for " << installer_traits_->GetName(); | 
|  332   DCHECK(thread_checker_.CalledOnValidThread()); |  326   DCHECK(thread_checker_.CalledOnValidThread()); | 
|  333  |  327  | 
|  334   update_client::CrxComponent crx; |  328   CrxComponent crx; | 
|  335   installer_traits_->GetHash(&crx.pk_hash); |  329   installer_traits_->GetHash(&crx.pk_hash); | 
|  336   crx.installer = this; |  330   crx.installer = this; | 
|  337   crx.version = current_version_; |  331   crx.version = current_version_; | 
|  338   crx.fingerprint = current_fingerprint_; |  332   crx.fingerprint = current_fingerprint_; | 
|  339   crx.name = installer_traits_->GetName(); |  333   crx.name = installer_traits_->GetName(); | 
|  340   crx.installer_attributes = installer_traits_->GetInstallerAttributes(); |  334   crx.installer_attributes = installer_traits_->GetInstallerAttributes(); | 
|  341   crx.requires_network_encryption = |  335   crx.requires_network_encryption = | 
|  342       installer_traits_->RequiresNetworkEncryption(); |  336       installer_traits_->RequiresNetworkEncryption(); | 
|  343   crx.handled_mime_types = installer_traits_->GetMimeTypes(); |  337   crx.handled_mime_types = installer_traits_->GetMimeTypes(); | 
|  344   crx.supports_group_policy_enable_component_updates = |  338   crx.supports_group_policy_enable_component_updates = | 
| (...skipping 20 matching lines...) Expand all  Loading... | 
|  365  |  359  | 
|  366 void DefaultComponentInstaller::ComponentReady( |  360 void DefaultComponentInstaller::ComponentReady( | 
|  367     std::unique_ptr<base::DictionaryValue> manifest) { |  361     std::unique_ptr<base::DictionaryValue> manifest) { | 
|  368   VLOG(1) << "Component ready, version " << current_version_.GetString() |  362   VLOG(1) << "Component ready, version " << current_version_.GetString() | 
|  369           << " in " << current_install_dir_.value(); |  363           << " in " << current_install_dir_.value(); | 
|  370   installer_traits_->ComponentReady(current_version_, current_install_dir_, |  364   installer_traits_->ComponentReady(current_version_, current_install_dir_, | 
|  371                                     std::move(manifest)); |  365                                     std::move(manifest)); | 
|  372 } |  366 } | 
|  373  |  367  | 
|  374 }  // namespace component_updater |  368 }  // namespace component_updater | 
| OLD | NEW |