OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 "components/component_updater/component_updater_paths.h" | |
7 | |
8 using content::BrowserThread; | |
9 using content::PluginService; | |
10 | |
11 namespace component_updater { | |
12 | |
13 #if defined(OS_CHROMEOS) | |
14 void LogRegistrationResult(chromeos::DBusMethodCallStatus call_status, | |
15 bool result) { | |
16 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
17 if (call_status != chromeos::DBUS_METHOD_CALL_SUCCESS) { | |
18 LOG(ERROR) << "Call to imageloader service failed."; | |
19 return; | |
20 } | |
21 if (!result) { | |
22 LOG(ERROR) << "Component registration failed"; | |
23 return; | |
24 } | |
25 } | |
26 void ImageLoaderRegistration(const std::string& version, | |
27 const base::FilePath& install_dir, | |
28 const std::string& name) { | |
29 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
30 chromeos::ImageLoaderClient* loader = | |
31 chromeos::DBusThreadManager::Get()->GetImageLoaderClient(); | |
32 | |
33 if (loader) { | |
34 loader->RegisterComponent(name, version, install_dir.value(), | |
35 base::Bind(&LogRegistrationResult)); | |
36 } else { | |
37 LOG(ERROR) << "Failed to get ImageLoaderClient object."; | |
38 } | |
39 } | |
40 | |
41 // Determine whether or not to skip registering this cros component updates. | |
42 bool SkipCupsRegistration(ComponentUpdateService* cus) { | |
43 return false; | |
44 } | |
45 #endif // defined(OS_CHROMEOS) | |
46 | |
47 CrOSComponentInstallerTraits::CrOSComponentInstallerTraits( | |
48 std::string dir_name, | |
49 std::string name, | |
50 std::string sha2HashStr) | |
51 : dir_name(dir_name), name(name) { | |
52 if (sha2HashStr.length() != 64) | |
53 return; | |
54 for (unsigned int i = 0; i < sizeof(kSha2Hash_); i++) { | |
55 kSha2Hash_[i] = stoul(sha2HashStr.substr(i * 2, 2), nullptr, 16); | |
56 } | |
57 } | |
58 | |
59 bool CrOSComponentInstallerTraits::SupportsGroupPolicyEnabledComponentUpdates() | |
60 const { | |
61 return true; | |
62 } | |
63 | |
64 bool CrOSComponentInstallerTraits::RequiresNetworkEncryption() const { | |
65 return false; | |
66 } | |
67 | |
68 update_client::CrxInstaller::Result | |
69 CrOSComponentInstallerTraits::OnCustomInstall( | |
70 const base::DictionaryValue& manifest, | |
71 const base::FilePath& install_dir) { | |
72 DVLOG(1) << "[CrOSComponentInstallerTraits::OnCustomInstall]"; | |
73 #if defined(OS_CHROMEOS) | |
74 std::string version; | |
75 if (!manifest.GetString("version", &version)) { | |
76 return ToInstallerResult(update_client::InstallError::GENERIC_ERROR); | |
77 } | |
78 BrowserThread::PostTask( | |
79 BrowserThread::UI, FROM_HERE, | |
80 base::Bind(&ImageLoaderRegistration, version, install_dir, name)); | |
81 return update_client::CrxInstaller::Result(update_client::InstallError::NONE); | |
82 #else | |
83 return ToInstallerResult(update_client::InstallError::GENERIC_ERROR); | |
84 #endif // defined(OS_CHROMEOS) | |
85 } | |
86 | |
87 void CrOSComponentInstallerTraits::ComponentReady( | |
88 const base::Version& version, | |
89 const base::FilePath& path, | |
90 std::unique_ptr<base::DictionaryValue> manifest) {} | |
91 | |
92 bool CrOSComponentInstallerTraits::VerifyInstallation( | |
93 const base::DictionaryValue& manifest, | |
94 const base::FilePath& install_dir) const { | |
95 return true; | |
96 } | |
97 | |
98 base::FilePath CrOSComponentInstallerTraits::GetRelativeInstallDir() const { | |
99 return base::FilePath(FILE_PATH_LITERAL(dir_name)); | |
100 } | |
101 | |
102 void CrOSComponentInstallerTraits::GetHash(std::vector<uint8_t>* hash) const { | |
103 hash->assign(kSha2Hash_, kSha2Hash_ + arraysize(kSha2Hash_)); | |
104 } | |
105 | |
106 std::string CrOSComponentInstallerTraits::GetName() const { | |
107 return name; | |
108 } | |
109 | |
110 update_client::InstallerAttributes | |
111 CrOSComponentInstallerTraits::GetInstallerAttributes() const { | |
112 return update_client::InstallerAttributes(); | |
113 } | |
114 | |
115 std::vector<std::string> CrOSComponentInstallerTraits::GetMimeTypes() const { | |
116 std::vector<std::string> mime_types; | |
117 return mime_types; | |
118 } | |
119 | |
120 void RegisterCrOSComponentInternal(ComponentUpdateService* cus, | |
121 ComponentConfig& config) { | |
122 std::unique_ptr<ComponentInstallerTraits> traits( | |
123 new CrOSComponentInstallerTraits(config.dir, config.name, | |
124 config.sha2hashstr)); | |
125 // |cus| will take ownership of |installer| during | |
126 // installer->Register(cus). | |
127 DefaultComponentInstaller* installer = | |
128 new DefaultComponentInstaller(std::move(traits)); | |
129 installer->Register(cus, base::Closure()); | |
waffles
2017/02/28 17:37:21
Since installer->Register must be called on the UI
xiaochu
2017/02/28 23:20:33
I changed to allow only synchronous registration (
| |
130 } | |
131 | |
132 // an example config file content: | |
133 //{ | |
134 // "components":{ | |
135 // "escpr": { | |
136 // "dir":"epson-inkjet-printer-escpr", | |
137 // "sha2hashstr":"1913a5e0a6cad30b6f03e176177e0d7ed62c5d6700a9c66da556d7c3f 5d6a47e" | |
138 // } | |
139 // } | |
140 //} | |
141 bool RegisterCrOSComponent(ComponentUpdateService* cus, std::string name) { | |
142 if (name.length() == 0) | |
143 return false; | |
144 if (!componentConfigReaderRoot.get()) { | |
145 DVLOG(1) << "[RegisterCrOSComponents] configuration is not loaded."; | |
146 return false; | |
147 } | |
148 auto root_dic = std::unique_ptr<base::DictionaryValue>( | |
149 static_cast<base::DictionaryValue*>(componentConfigReaderRoot.get())); | |
150 base::DictionaryValue* components = nullptr; | |
151 if (root_dic->GetDictionary("components", &components)) { | |
152 base::DictionaryValue* component = nullptr; | |
153 if (components->GetDictionary(name, &component)) { | |
154 std::string dir, sha2hashstr; | |
155 if (component->GetString("dir", &dir) && | |
156 component->GetString("sha2hashstr", &sha2hashstr) && | |
157 dir.length() > 0 && sha2hashstr.length() == 64) { | |
158 ComponentConfig config; | |
159 config.name = name; | |
160 config.dir = dir; | |
161 config.sha2hashstr = sha2hashstr; | |
162 DVLOG(1) << "[RegisterCrOSComponents] register component:" << name; | |
163 RegisterCrOSComponentInternal(cus, config); | |
164 return true; | |
165 } | |
166 } | |
167 } | |
168 DVLOG(1) << "[RegisterCrOSComponents] " | |
169 "component " | |
170 << name << " is not in configuration file."; | |
171 return false; | |
172 } | |
173 bool RegisterCrOSComponentIO(ComponentUpdateService* cus, std::string name) { | |
174 LoadCrOSConfig(); | |
175 return RegisterCrOSComponent(cus, name); | |
176 } | |
177 void LoadCrOSConfig() { | |
178 if (!componentConfigReaderRoot.get()) { | |
179 base::FilePath configFilePath(componentConfigReaderFilepathstr); | |
180 JSONFileValueDeserializer deserializer(configFilePath); | |
181 std::string error; | |
182 componentConfigReaderRoot = deserializer.Deserialize(NULL, &error); | |
183 } | |
184 } | |
185 } // namespace component_updater | |
OLD | NEW |