cld_component_installer.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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 "chrome/browser/profiles/profile.h"
17#include "components/component_updater/component_updater_paths.h"
18#include "components/translate/content/browser/data_file_browser_cld_data_provider.h"
19#include "content/public/browser/browser_thread.h"
20#include "net/ssl/ssl_config_service.h"
21
22using component_updater::ComponentUpdateService;
23using content::BrowserThread;
24
25namespace {
26// TODO(andrewhayden): Make the data file path into a gyp/gn define
27// If you change this, also update component_cld_data_harness.cc
28// and cld_component_installer_unittest.cc accordingly!
29const base::FilePath::CharType kCldDataFileName[] =
30    FILE_PATH_LITERAL("cld2_data.bin");
31}  // namespace
32
33namespace component_updater {
34
35// The SHA256 of the SubjectPublicKeyInfo used to sign the extension.
36// The extension id is: dpedmmgabcgnikllifiidmijgoiihfgf
37const uint8 kPublicKeySHA256[32] = {
38    0x3f, 0x43, 0xcc, 0x60, 0x12, 0x6d, 0x8a, 0xbb,
39    0x85, 0x88, 0x3c, 0x89, 0x6e, 0x88, 0x75, 0x65,
40    0xb9, 0x46, 0x09, 0xe8, 0xca, 0x92, 0xdd, 0x82,
41    0x4e, 0x6d, 0x0e, 0xe6, 0x79, 0x8a, 0x87, 0xf5
42};
43
44const char kCldManifestName[] = "CLD2 Data";
45
46CldComponentInstallerTraits::CldComponentInstallerTraits() {
47}
48
49bool CldComponentInstallerTraits::CanAutoUpdate() const {
50  return true;
51}
52
53bool CldComponentInstallerTraits::OnCustomInstall(
54    const base::DictionaryValue& manifest,
55    const base::FilePath& install_dir) {
56  return true;  // Nothing custom here.
57}
58
59base::FilePath CldComponentInstallerTraits::GetInstalledPath(
60    const base::FilePath& base) {
61  // Currently, all platforms have the file at the same location because there
62  // is no binary difference in the generated file on any supported platform.
63  // NB: This may change when 64-bit is officially supported.
64  return base.Append(FILE_PATH_LITERAL("_platform_specific"))
65      .Append(FILE_PATH_LITERAL("all"))
66      .Append(kCldDataFileName);
67}
68
69void CldComponentInstallerTraits::ComponentReady(
70    const base::Version& version,
71    const base::FilePath& path,
72    scoped_ptr<base::DictionaryValue> manifest) {
73  VLOG(1) << "Component ready, version " << version.GetString() << " in "
74          << path.value();
75  SetLatestCldDataFile(GetInstalledPath(path));
76}
77
78bool CldComponentInstallerTraits::VerifyInstallation(
79    const base::FilePath& install_dir) const {
80  // We can't really do much to verify the CLD2 data file. In theory we could
81  // read the headers, but that won't do much other than tell us whether or
82  // not the headers are valid. So just check if the file exists.
83  const base::FilePath expected_file = GetInstalledPath(install_dir);
84  VLOG(1) << "Verifying install: " << expected_file.value();
85  const bool result = base::PathExists(expected_file);
86  VLOG(1) << "Verification result: " << (result ? "valid" : "invalid");
87  return result;
88}
89
90base::FilePath CldComponentInstallerTraits::GetBaseDirectory() const {
91  base::FilePath result;
92  PathService::Get(DIR_COMPONENT_CLD2, &result);
93  return result;
94}
95
96void CldComponentInstallerTraits::GetHash(std::vector<uint8>* hash) const {
97  hash->assign(kPublicKeySHA256,
98               kPublicKeySHA256 + arraysize(kPublicKeySHA256));
99}
100
101std::string CldComponentInstallerTraits::GetName() const {
102  return kCldManifestName;
103}
104
105void RegisterCldComponent(ComponentUpdateService* cus) {
106  // This log line is to help with determining which kind of provider has been
107  // configured. See also: chrome://translate-internals
108  VLOG(1) << "Registering CLD component with the component update service";
109
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
118void CldComponentInstallerTraits::SetLatestCldDataFile(
119    const base::FilePath& path) {
120  VLOG(1) << "Setting CLD data file location: " << path.value();
121  translate::DataFileBrowserCldDataProvider::SetCldDataFilePath(path);
122}
123
124}  // namespace component_updater
125