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/file_util.h"
12 #include "base/files/file_path.h"
13 #include "base/lazy_instance.h"
14 #include "base/logging.h"
15 #include "base/path_service.h"
16 #include "base/platform_file.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/common/chrome_constants.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "net/ssl/ssl_config_service.h"
22
23 using component_updater::ComponentUpdateService;
24 using content::BrowserThread;
25
26 namespace {
27
28 // Once we have acquired a valid file from the component installer, we need to
29 // make the path available to other parts of the system such as the
30 // translation libraries. We create a global to hold onto the path, and a
31 // lock to guard it. See GetLatestCldDataFile(...) for more info.
32 base::LazyInstance<base::Lock> cld_file_lock = LAZY_INSTANCE_INITIALIZER;
33 base::LazyInstance<base::FilePath> cld_file = LAZY_INSTANCE_INITIALIZER;
34
35 }
36
37 namespace component_updater {
38
39 // The SHA256 of the SubjectPublicKeyInfo used to sign the extension.
40 // The extension id is: dpedmmgabcgnikllifiidmijgoiihfgf
41 const uint8 kPublicKeySHA256[32] = {
42 0x3f, 0x43, 0xcc, 0x60, 0x12, 0x6d, 0x8a, 0xbb,
43 0x85, 0x88, 0x3c, 0x89, 0x6e, 0x88, 0x75, 0x65,
44 0xb9, 0x46, 0x09, 0xe8, 0xca, 0x92, 0xdd, 0x82,
45 0x4e, 0x6d, 0x0e, 0xe6, 0x79, 0x8a, 0x87, 0xf5
46 };
47
48 const char kCldManifestName[] = "CLD2 Data";
49
CldComponentInstallerTraits()50 CldComponentInstallerTraits::CldComponentInstallerTraits() {
51 }
52
CanAutoUpdate() const53 bool CldComponentInstallerTraits::CanAutoUpdate() const {
54 return true;
55 }
56
OnCustomInstall(const base::DictionaryValue & manifest,const base::FilePath & install_dir)57 bool CldComponentInstallerTraits::OnCustomInstall(
58 const base::DictionaryValue& manifest,
59 const base::FilePath& install_dir) {
60 return true; // Nothing custom here.
61 }
62
GetInstalledPath(const base::FilePath & base)63 base::FilePath CldComponentInstallerTraits::GetInstalledPath(
64 const base::FilePath& base) {
65 // Currently, all platforms have the file at the same location because there
66 // is no binary difference in the generated file on any supported platform.
67 // NB: This may change when 64-bit is officially supported.
68 return base.Append(FILE_PATH_LITERAL("_platform_specific"))
69 .Append(FILE_PATH_LITERAL("all"))
70 .Append(chrome::kCLDDataFilename);
71 }
72
ComponentReady(const base::Version & version,const base::FilePath & path,scoped_ptr<base::DictionaryValue> manifest)73 void CldComponentInstallerTraits::ComponentReady(
74 const base::Version& version,
75 const base::FilePath& path,
76 scoped_ptr<base::DictionaryValue> manifest) {
77 VLOG(1) << "Component ready, version " << version.GetString() << " in "
78 << path.value();
79 SetLatestCldDataFile(GetInstalledPath(path));
80 }
81
VerifyInstallation(const base::FilePath & install_dir) const82 bool CldComponentInstallerTraits::VerifyInstallation(
83 const base::FilePath& install_dir) const {
84 // We can't really do much to verify the CLD2 data file. In theory we could
85 // read the headers, but that won't do much other than tell us whether or
86 // not the headers are valid. So just check if the file exists.
87 const base::FilePath expected_file = GetInstalledPath(install_dir);
88 VLOG(1) << "Verifying install: " << expected_file.value();
89 const bool result = base::PathExists(expected_file);
90 VLOG(1) << "Verification result: " << (result ? "valid" : "invalid");
91 return result;
92 }
93
GetBaseDirectory() const94 base::FilePath CldComponentInstallerTraits::GetBaseDirectory() const {
95 base::FilePath result;
96 PathService::Get(chrome::DIR_COMPONENT_CLD2, &result);
97 return result;
98 }
99
GetHash(std::vector<uint8> * hash) const100 void CldComponentInstallerTraits::GetHash(std::vector<uint8>* hash) const {
101 hash->assign(kPublicKeySHA256,
102 kPublicKeySHA256 + arraysize(kPublicKeySHA256));
103 }
104
GetName() const105 std::string CldComponentInstallerTraits::GetName() const {
106 return kCldManifestName;
107 }
108
RegisterCldComponent(ComponentUpdateService * cus)109 void RegisterCldComponent(ComponentUpdateService* cus) {
110 scoped_ptr<ComponentInstallerTraits> traits(
111 new CldComponentInstallerTraits());
112 // |cus| will take ownership of |installer| during installer->Register(cus).
113 DefaultComponentInstaller* installer =
114 new DefaultComponentInstaller(traits.Pass());
115 installer->Register(cus);
116 }
117
SetLatestCldDataFile(const base::FilePath & path)118 void CldComponentInstallerTraits::SetLatestCldDataFile(
119 const base::FilePath& path) {
120 VLOG(1) << "Setting CLD data file location: " << path.value();
121 base::AutoLock lock(cld_file_lock.Get());
122 cld_file.Get() = path;
123 }
124
GetLatestCldDataFile()125 base::FilePath GetLatestCldDataFile() {
126 base::AutoLock lock(cld_file_lock.Get());
127 // cld_file is an empty path by default, meaning "file not available yet".
128 return cld_file.Get();
129 }
130
131 } // namespace component_updater
132