Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / component_updater / cld_component_installer.cc
blobace80fe8838124735725353f23dd12d02dbf492f
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.
5 #include "chrome/browser/component_updater/cld_component_installer.h"
7 #include <string>
8 #include <vector>
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/browser/browser_cld_data_provider_factory.h"
19 #include "components/translate/content/common/cld_data_source.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "net/ssl/ssl_config_service.h"
23 using component_updater::ComponentUpdateService;
25 namespace {
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!
29 const base::FilePath::CharType kCldDataFileName[] =
30 FILE_PATH_LITERAL("cld2_data.bin");
32 // Tracks the last value seen in SetLatestCldDataFile.
33 base::LazyInstance<base::FilePath>::Leaky g_latest_cld_data_file =
34 LAZY_INSTANCE_INITIALIZER;
35 } // namespace
37 namespace component_updater {
39 // The SHA256 of the SubjectPublicKeyInfo used to sign the extension.
40 // The extension id is: dpedmmgabcgnikllifiidmijgoiihfgf
41 const uint8_t 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
48 const char kCldManifestName[] = "CLD2 Data";
50 CldComponentInstallerTraits::CldComponentInstallerTraits() {
53 bool CldComponentInstallerTraits::CanAutoUpdate() const {
54 return true;
57 bool CldComponentInstallerTraits::OnCustomInstall(
58 const base::DictionaryValue& manifest,
59 const base::FilePath& install_dir) {
60 return true; // Nothing custom here.
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(kCldDataFileName);
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));
82 bool CldComponentInstallerTraits::VerifyInstallation(
83 const base::DictionaryValue& manifest,
84 const base::FilePath& install_dir) const {
85 // We can't really do much to verify the CLD2 data file. In theory we could
86 // read the headers, but that won't do much other than tell us whether or
87 // not the headers are valid. So just check if the file exists.
88 const base::FilePath expected_file = GetInstalledPath(install_dir);
89 VLOG(1) << "Verifying install: " << expected_file.value();
90 const bool result = base::PathExists(expected_file);
91 VLOG(1) << "Verification result: " << (result ? "valid" : "invalid");
92 return result;
95 base::FilePath CldComponentInstallerTraits::GetBaseDirectory() const {
96 base::FilePath result;
97 PathService::Get(DIR_COMPONENT_CLD2, &result);
98 return result;
101 void CldComponentInstallerTraits::GetHash(std::vector<uint8_t>* hash) const {
102 hash->assign(kPublicKeySHA256,
103 kPublicKeySHA256 + arraysize(kPublicKeySHA256));
106 std::string CldComponentInstallerTraits::GetName() const {
107 return kCldManifestName;
110 // static
111 void RegisterCldComponent(ComponentUpdateService* cus) {
112 if (!translate::CldDataSource::IsUsingComponentDataSource()) {
113 // The configured CLD data source isn't the "Component" data source, so
114 // there is nothing to do.
115 return;
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";
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, base::Closure());
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::CldDataSource::Get()->SetCldDataFilePath(path);
137 base::FilePath CldComponentInstallerTraits::GetLatestCldDataFile() {
138 return g_latest_cld_data_file.Get();
141 } // namespace component_updater