OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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/cros_component_installer.h" | |
6 #include "chrome/browser/browser_process.h" | |
7 #include "chrome/browser/component_updater/component_installer_errors.h" | |
8 #include "components/component_updater/component_updater_paths.h" | |
9 #include "content/public/browser/browser_thread.h" | |
10 | |
11 #if defined(OS_CHROMEOS) | |
Sorin Jianu
2017/03/02 22:27:25
#include "build/build_config.h"
xiaochu
2017/03/03 00:08:53
Done.
| |
12 #include "chromeos/dbus/dbus_method_call_status.h" | |
13 #include "chromeos/dbus/dbus_thread_manager.h" | |
14 #include "chromeos/dbus/image_loader_client.h" | |
15 #endif // defined(OS_CHROMEOS) | |
16 | |
17 using content::BrowserThread; | |
18 | |
19 namespace component_updater { | |
20 | |
21 #if defined(OS_CHROMEOS) | |
22 void LogRegistrationResult(chromeos::DBusMethodCallStatus call_status, | |
23 bool result) { | |
24 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
25 if (call_status != chromeos::DBUS_METHOD_CALL_SUCCESS) { | |
26 DVLOG(1) << "Call to imageloader service failed."; | |
27 return; | |
28 } | |
29 if (!result) { | |
30 DVLOG(1) << "Component registration failed"; | |
31 return; | |
32 } | |
33 } | |
34 void ImageLoaderRegistration(const std::string& version, | |
35 const base::FilePath& install_dir, | |
36 const std::string& name) { | |
37 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
38 chromeos::ImageLoaderClient* loader = | |
39 chromeos::DBusThreadManager::Get()->GetImageLoaderClient(); | |
40 | |
41 if (loader) { | |
42 loader->RegisterComponent(name, version, install_dir.value(), | |
43 base::Bind(&LogRegistrationResult)); | |
44 } else { | |
45 DVLOG(1) << "Failed to get ImageLoaderClient object."; | |
46 } | |
47 } | |
48 | |
49 CrOSComponentInstallerTraits::CrOSComponentInstallerTraits( | |
50 std::string dir_name, | |
51 std::string name, | |
52 std::string sha2HashStr) | |
53 : dir_name(dir_name), name(name) { | |
54 if (sha2HashStr.length() != 64) | |
55 return; | |
56 for (unsigned int i = 0; i < sizeof(kSha2Hash_); i++) { | |
Sorin Jianu
2017/03/02 22:27:24
use size_t for the loop variable. Also I wonder if
xiaochu
2017/03/03 00:08:53
Done.
| |
57 kSha2Hash_[i] = stoul(sha2HashStr.substr(i * 2, 2), nullptr, 16); | |
58 } | |
59 } | |
60 | |
61 bool CrOSComponentInstallerTraits::SupportsGroupPolicyEnabledComponentUpdates() | |
62 const { | |
63 return true; | |
64 } | |
65 | |
66 bool CrOSComponentInstallerTraits::RequiresNetworkEncryption() const { | |
67 return true; | |
68 } | |
69 | |
70 update_client::CrxInstaller::Result | |
71 CrOSComponentInstallerTraits::OnCustomInstall( | |
72 const base::DictionaryValue& manifest, | |
73 const base::FilePath& install_dir) { | |
74 DVLOG(1) << "[CrOSComponentInstallerTraits::OnCustomInstall]"; | |
75 std::string version; | |
76 if (!manifest.GetString("version", &version)) { | |
77 return ToInstallerResult(update_client::InstallError::GENERIC_ERROR); | |
78 } | |
79 BrowserThread::PostTask( | |
80 BrowserThread::UI, FROM_HERE, | |
81 base::Bind(&ImageLoaderRegistration, version, install_dir, name)); | |
82 return update_client::CrxInstaller::Result(update_client::InstallError::NONE); | |
83 } | |
84 | |
85 void CrOSComponentInstallerTraits::ComponentReady( | |
86 const base::Version& version, | |
87 const base::FilePath& path, | |
88 std::unique_ptr<base::DictionaryValue> manifest) {} | |
89 | |
90 bool CrOSComponentInstallerTraits::VerifyInstallation( | |
91 const base::DictionaryValue& manifest, | |
92 const base::FilePath& install_dir) const { | |
93 return true; | |
94 } | |
95 | |
96 base::FilePath CrOSComponentInstallerTraits::GetRelativeInstallDir() const { | |
97 return base::FilePath(dir_name); | |
98 } | |
99 | |
100 void CrOSComponentInstallerTraits::GetHash(std::vector<uint8_t>* hash) const { | |
101 hash->assign(kSha2Hash_, kSha2Hash_ + arraysize(kSha2Hash_)); | |
102 } | |
103 | |
104 std::string CrOSComponentInstallerTraits::GetName() const { | |
105 return name; | |
106 } | |
107 | |
108 update_client::InstallerAttributes | |
109 CrOSComponentInstallerTraits::GetInstallerAttributes() const { | |
110 return update_client::InstallerAttributes(); | |
111 } | |
112 | |
113 std::vector<std::string> CrOSComponentInstallerTraits::GetMimeTypes() const { | |
114 std::vector<std::string> mime_types; | |
115 return mime_types; | |
116 } | |
117 | |
118 void RegisterCrOSComponentInternal(ComponentUpdateService* cus, | |
119 const ComponentConfig& config) { | |
120 std::unique_ptr<ComponentInstallerTraits> traits( | |
121 new CrOSComponentInstallerTraits(config.dir, config.name, | |
122 config.sha2hashstr)); | |
123 // |cus| will take ownership of |installer| during | |
124 // installer->Register(cus). | |
125 DefaultComponentInstaller* installer = | |
126 new DefaultComponentInstaller(std::move(traits)); | |
127 installer->Register(cus, base::Closure()); | |
128 } | |
129 | |
130 bool RegisterCrOSComponentInternal(ComponentUpdateService* cus, | |
131 const std::string& name) { | |
132 if (name.length() == 0) { | |
Sorin Jianu
2017/03/02 22:27:24
use empty()
xiaochu
2017/03/03 00:08:52
Done.
| |
133 DVLOG(1) << "[RegisterCrOSComponents] name is empty."; | |
134 return false; | |
135 } | |
136 const std::map<std::string, std::map<std::string, std::string>> components = { | |
137 {"escpr", | |
138 {{"dir", "epson-inkjet-printer-escpr"}, | |
139 {"sha2hashstr", | |
140 "1913a5e0a6cad30b6f03e176177e0d7ed62c5d6700a9c66da556d7c3f5d6a47e"}}}}; | |
141 auto component = components.find(name); | |
Sorin Jianu
2017/03/02 22:27:25
std::map has this odd api, where find returns an i
xiaochu
2017/03/03 00:08:52
Done.
| |
142 if (component != components.end()) { | |
Sorin Jianu
2017/03/02 22:27:25
Some people prefer to handle the early exit from f
xiaochu
2017/03/03 00:08:53
Done.
| |
143 ComponentConfig config; | |
144 config.name = component->first; | |
145 config.dir = component->second.find("dir")->second; | |
146 config.sha2hashstr = component->second.find("sha2hashstr")->second; | |
147 RegisterCrOSComponentInternal(cus, config); | |
148 return true; | |
149 } | |
150 DVLOG(1) << "[RegisterCrOSComponents] component " << name | |
151 << " is not in configuration."; | |
152 return false; | |
153 } | |
154 | |
155 #endif // defined(OS_CHROMEOS) | |
156 | |
157 bool RegisterCrOSComponent(ComponentUpdateService* cus, | |
158 const std::string& name) { | |
159 #if defined(OS_CHROMEOS) | |
160 return RegisterCrOSComponentInternal(cus, name); | |
161 #else | |
162 return false; | |
163 #endif // defined(OS_CHROMEOS) | |
164 } | |
165 } // namespace component_updater | |
OLD | NEW |