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/component_updater/cld_component_installer.h" 6 7 #include <string> 8 #include <vector> 9 10 #include "base/bind.h" 11 #include "base/files/file_path.h" 12 #include "base/files/file_util.h" 13 #include "base/lazy_instance.h" 14 #include "base/logging.h" 15 #include "base/path_service.h" 16 #include "components/component_updater/component_updater_paths.h" 17 #include "components/translate/content/browser/browser_cld_data_provider.h" 18 #include "components/translate/content/common/cld_data_source.h" 19 #include "content/public/browser/browser_thread.h" 20 #include "net/ssl/ssl_config_service.h" 21 22 using component_updater::ComponentUpdateService; 23 24 namespace { 25 // TODO(andrewhayden): Make the data file path into a gyp/gn define 26 // If you change this, also update component_cld_data_harness.cc 27 // and cld_component_installer_unittest.cc accordingly! 28 const base::FilePath::CharType kCldDataFileName[] = 29 FILE_PATH_LITERAL("cld2_data.bin"); 30 31 // Tracks the last value seen in SetLatestCldDataFile. 32 base::LazyInstance<base::FilePath>::Leaky g_latest_cld_data_file = 33 LAZY_INSTANCE_INITIALIZER; 34 } // namespace 35 36 namespace component_updater { 37 38 // The SHA256 of the SubjectPublicKeyInfo used to sign the extension. 39 // The extension id is: dpedmmgabcgnikllifiidmijgoiihfgf 40 const uint8_t kPublicKeySHA256[32] = { 41 0x3f, 0x43, 0xcc, 0x60, 0x12, 0x6d, 0x8a, 0xbb, 42 0x85, 0x88, 0x3c, 0x89, 0x6e, 0x88, 0x75, 0x65, 43 0xb9, 0x46, 0x09, 0xe8, 0xca, 0x92, 0xdd, 0x82, 44 0x4e, 0x6d, 0x0e, 0xe6, 0x79, 0x8a, 0x87, 0xf5 45 }; 46 47 const char kCldManifestName[] = "CLD2 Data"; 48 49 CldComponentInstallerTraits::CldComponentInstallerTraits() { 50 } 51 52 bool CldComponentInstallerTraits::CanAutoUpdate() const { 53 return true; 54 } 55 56 bool CldComponentInstallerTraits::OnCustomInstall( 57 const base::DictionaryValue& manifest, 58 const base::FilePath& install_dir) { 59 return true; // Nothing custom here. 60 } 61 62 base::FilePath CldComponentInstallerTraits::GetInstalledPath( 63 const base::FilePath& base) { 64 // Currently, all platforms have the file at the same location because there 65 // is no binary difference in the generated file on any supported platform. 66 // NB: This may change when 64-bit is officially supported. 67 return base.Append(FILE_PATH_LITERAL("_platform_specific")) 68 .Append(FILE_PATH_LITERAL("all")) 69 .Append(kCldDataFileName); 70 } 71 72 void CldComponentInstallerTraits::ComponentReady( 73 const base::Version& version, 74 const base::FilePath& path, 75 scoped_ptr<base::DictionaryValue> manifest) { 76 VLOG(1) << "Component ready, version " << version.GetString() << " in " 77 << path.value(); 78 SetLatestCldDataFile(GetInstalledPath(path)); 79 } 80 81 bool CldComponentInstallerTraits::VerifyInstallation( 82 const base::FilePath& install_dir) const { 83 // We can't really do much to verify the CLD2 data file. In theory we could 84 // read the headers, but that won't do much other than tell us whether or 85 // not the headers are valid. So just check if the file exists. 86 const base::FilePath expected_file = GetInstalledPath(install_dir); 87 VLOG(1) << "Verifying install: " << expected_file.value(); 88 const bool result = base::PathExists(expected_file); 89 VLOG(1) << "Verification result: " << (result ? "valid" : "invalid"); 90 return result; 91 } 92 93 base::FilePath CldComponentInstallerTraits::GetBaseDirectory() const { 94 base::FilePath result; 95 PathService::Get(DIR_COMPONENT_CLD2, &result); 96 return result; 97 } 98 99 void CldComponentInstallerTraits::GetHash(std::vector<uint8_t>* hash) const { 100 hash->assign(kPublicKeySHA256, 101 kPublicKeySHA256 + arraysize(kPublicKeySHA256)); 102 } 103 104 std::string CldComponentInstallerTraits::GetName() const { 105 return kCldManifestName; 106 } 107 108 void RegisterCldComponent(ComponentUpdateService* cus) { 109 // Make sure we don't start up if the CLD data source isn't compatible. 110 if (!translate::CldDataSource::ShouldRegisterForComponentUpdates()) { 111 // This is a serious build-time configuration error. 112 LOG(ERROR) << "Wrong CLD data source: " << 113 translate::CldDataSource::GetName(); 114 NOTREACHED(); 115 return; 116 } 117 118 // This log line is to help with determining which kind of provider has been 119 // configured. See also: chrome://translate-internals 120 VLOG(1) << "Registering CLD component with the component update service"; 121 122 scoped_ptr<ComponentInstallerTraits> traits( 123 new CldComponentInstallerTraits()); 124 // |cus| will take ownership of |installer| during installer->Register(cus). 125 DefaultComponentInstaller* installer = 126 new DefaultComponentInstaller(traits.Pass()); 127 installer->Register(cus); 128 } 129 130 void CldComponentInstallerTraits::SetLatestCldDataFile( 131 const base::FilePath& path) { 132 VLOG(1) << "Setting CLD data file location: " << path.value(); 133 g_latest_cld_data_file.Get() = path; 134 translate::SetCldDataFilePath(path); 135 } 136 137 base::FilePath CldComponentInstallerTraits::GetLatestCldDataFile() { 138 return g_latest_cld_data_file.Get(); 139 } 140 141 } // namespace component_updater 142