OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/files/file_enumerator.h" | 7 #include "base/files/file_enumerator.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/location.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" |
| 11 #include "base/sequenced_task_runner.h" |
| 12 #include "base/single_thread_task_runner.h" |
9 #include "base/values.h" | 13 #include "base/values.h" |
10 #include "base/version.h" | 14 #include "base/version.h" |
11 // TODO(ddorwin): Find a better place for ReadManifest. | 15 // TODO(ddorwin): Find a better place for ReadManifest. |
12 #include "chrome/browser/component_updater/component_unpacker.h" | 16 #include "chrome/browser/component_updater/component_unpacker.h" |
| 17 #include "chrome/browser/component_updater/component_updater_configurator.h" |
13 #include "chrome/browser/component_updater/default_component_installer.h" | 18 #include "chrome/browser/component_updater/default_component_installer.h" |
14 #include "content/public/browser/browser_thread.h" | |
15 | 19 |
16 namespace component_updater { | 20 namespace component_updater { |
17 | 21 |
18 namespace { | 22 namespace { |
19 // Version "0" corresponds to no installed version. By the server's conventions, | 23 // Version "0" corresponds to no installed version. By the server's conventions, |
20 // we represent it as a dotted quad. | 24 // we represent it as a dotted quad. |
21 const char kNullVersion[] = "0.0.0.0"; | 25 const char kNullVersion[] = "0.0.0.0"; |
22 } // namespace | 26 } // namespace |
23 | 27 |
24 ComponentInstallerTraits::~ComponentInstallerTraits() { | 28 ComponentInstallerTraits::~ComponentInstallerTraits() { |
25 } | 29 } |
26 | 30 |
27 DefaultComponentInstaller::DefaultComponentInstaller( | 31 DefaultComponentInstaller::DefaultComponentInstaller( |
28 scoped_ptr<ComponentInstallerTraits> installer_traits) | 32 scoped_ptr<ComponentInstallerTraits> installer_traits) |
29 : current_version_(kNullVersion) { | 33 : current_version_(kNullVersion), |
| 34 main_task_runner_(base::MessageLoopProxy::current()) { |
30 installer_traits_ = installer_traits.Pass(); | 35 installer_traits_ = installer_traits.Pass(); |
31 } | 36 } |
32 | 37 |
33 DefaultComponentInstaller::~DefaultComponentInstaller() { | 38 DefaultComponentInstaller::~DefaultComponentInstaller() { |
| 39 DCHECK(thread_checker_.CalledOnValidThread()); |
34 } | 40 } |
35 | 41 |
36 void DefaultComponentInstaller::Register(ComponentUpdateService* cus) { | 42 void DefaultComponentInstaller::Register(ComponentUpdateService* cus) { |
| 43 DCHECK(thread_checker_.CalledOnValidThread()); |
| 44 task_runner_ = cus->GetSequencedTaskRunner(); |
| 45 |
37 if (!installer_traits_) { | 46 if (!installer_traits_) { |
38 NOTREACHED() << "A DefaultComponentInstaller has been created but " | 47 NOTREACHED() << "A DefaultComponentInstaller has been created but " |
39 << "has no installer traits."; | 48 << "has no installer traits."; |
40 return; | 49 return; |
41 } | 50 } |
42 content::BrowserThread::PostBlockingPoolTask( | 51 task_runner_->PostTask( |
43 FROM_HERE, | 52 FROM_HERE, |
44 base::Bind(&DefaultComponentInstaller::StartRegistration, | 53 base::Bind(&DefaultComponentInstaller::StartRegistration, |
45 base::Unretained(this), | 54 base::Unretained(this), |
46 cus)); | 55 cus)); |
47 } | 56 } |
48 | 57 |
49 void DefaultComponentInstaller::OnUpdateError(int error) { | 58 void DefaultComponentInstaller::OnUpdateError(int error) { |
50 NOTREACHED() << "Component update error: " << error; | 59 NOTREACHED() << "Component update error: " << error; |
51 } | 60 } |
52 | 61 |
53 bool DefaultComponentInstaller::InstallHelper( | 62 bool DefaultComponentInstaller::InstallHelper( |
54 const base::DictionaryValue& manifest, | 63 const base::DictionaryValue& manifest, |
55 const base::FilePath& unpack_path, | 64 const base::FilePath& unpack_path, |
56 const base::FilePath& install_path) { | 65 const base::FilePath& install_path) { |
57 if (!base::Move(unpack_path, install_path)) | 66 if (!base::Move(unpack_path, install_path)) |
58 return false; | 67 return false; |
59 if (!installer_traits_->OnCustomInstall(manifest, install_path)) | 68 if (!installer_traits_->OnCustomInstall(manifest, install_path)) |
60 return false; | 69 return false; |
61 if (!installer_traits_->VerifyInstallation(install_path)) | 70 if (!installer_traits_->VerifyInstallation(install_path)) |
62 return false; | 71 return false; |
63 return true; | 72 return true; |
64 } | 73 } |
65 | 74 |
66 bool DefaultComponentInstaller::Install(const base::DictionaryValue& manifest, | 75 bool DefaultComponentInstaller::Install(const base::DictionaryValue& manifest, |
67 const base::FilePath& unpack_path) { | 76 const base::FilePath& unpack_path) { |
| 77 DCHECK(task_runner_); |
| 78 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
68 std::string manifest_version; | 79 std::string manifest_version; |
69 manifest.GetStringASCII("version", &manifest_version); | 80 manifest.GetStringASCII("version", &manifest_version); |
70 base::Version version(manifest_version.c_str()); | 81 base::Version version(manifest_version.c_str()); |
71 if (!version.IsValid()) | 82 if (!version.IsValid()) |
72 return false; | 83 return false; |
73 if (current_version_.CompareTo(version) > 0) | 84 if (current_version_.CompareTo(version) > 0) |
74 return false; | 85 return false; |
75 base::FilePath install_path = | 86 base::FilePath install_path = |
76 installer_traits_->GetBaseDirectory().AppendASCII(version.GetString()); | 87 installer_traits_->GetBaseDirectory().AppendASCII(version.GetString()); |
77 if (base::PathExists(install_path)) { | 88 if (base::PathExists(install_path)) { |
78 if (!base::DeleteFile(install_path, true)) | 89 if (!base::DeleteFile(install_path, true)) |
79 return false; | 90 return false; |
80 } | 91 } |
81 if (!InstallHelper(manifest, unpack_path, install_path)) { | 92 if (!InstallHelper(manifest, unpack_path, install_path)) { |
82 base::DeleteFile(install_path, true); | 93 base::DeleteFile(install_path, true); |
83 return false; | 94 return false; |
84 } | 95 } |
85 current_version_ = version; | 96 current_version_ = version; |
86 // TODO(ddorwin): Change the parameter to scoped_ptr<base::DictionaryValue> | 97 // TODO(ddorwin): Change the parameter to scoped_ptr<base::DictionaryValue> |
87 // so we can avoid this DeepCopy. | 98 // so we can avoid this DeepCopy. |
88 current_manifest_.reset(manifest.DeepCopy()); | 99 current_manifest_.reset(manifest.DeepCopy()); |
89 scoped_ptr<base::DictionaryValue> manifest_copy( | 100 scoped_ptr<base::DictionaryValue> manifest_copy( |
90 current_manifest_->DeepCopy()); | 101 current_manifest_->DeepCopy()); |
91 content::BrowserThread::PostTask( | 102 main_task_runner_->PostTask( |
92 content::BrowserThread::UI, | |
93 FROM_HERE, | 103 FROM_HERE, |
94 base::Bind(&ComponentInstallerTraits::ComponentReady, | 104 base::Bind(&ComponentInstallerTraits::ComponentReady, |
95 base::Unretained(installer_traits_.get()), | 105 base::Unretained(installer_traits_.get()), |
96 current_version_, | 106 current_version_, |
97 GetInstallDirectory(), | 107 GetInstallDirectory(), |
98 base::Passed(&manifest_copy))); | 108 base::Passed(&manifest_copy))); |
99 return true; | 109 return true; |
100 } | 110 } |
101 | 111 |
102 bool DefaultComponentInstaller::GetInstalledFile( | 112 bool DefaultComponentInstaller::GetInstalledFile( |
103 const std::string& file, | 113 const std::string& file, |
104 base::FilePath* installed_file) { | 114 base::FilePath* installed_file) { |
105 if (current_version_.Equals(base::Version(kNullVersion))) | 115 if (current_version_.Equals(base::Version(kNullVersion))) |
106 return false; // No component has been installed yet. | 116 return false; // No component has been installed yet. |
107 | 117 |
108 *installed_file = installer_traits_->GetBaseDirectory() | 118 *installed_file = installer_traits_->GetBaseDirectory() |
109 .AppendASCII(current_version_.GetString()) | 119 .AppendASCII(current_version_.GetString()) |
110 .AppendASCII(file); | 120 .AppendASCII(file); |
111 return true; | 121 return true; |
112 } | 122 } |
113 | 123 |
114 void DefaultComponentInstaller::StartRegistration(ComponentUpdateService* cus) { | 124 void DefaultComponentInstaller::StartRegistration(ComponentUpdateService* cus) { |
| 125 DCHECK(task_runner_); |
| 126 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
115 base::FilePath base_dir = installer_traits_->GetBaseDirectory(); | 127 base::FilePath base_dir = installer_traits_->GetBaseDirectory(); |
116 if (!base::PathExists(base_dir) && !base::CreateDirectory(base_dir)) { | 128 if (!base::PathExists(base_dir) && !base::CreateDirectory(base_dir)) { |
117 NOTREACHED() << "Could not create the base directory for " | 129 NOTREACHED() << "Could not create the base directory for " |
118 << installer_traits_->GetName() << " (" | 130 << installer_traits_->GetName() << " (" |
119 << base_dir.MaybeAsASCII() << ")."; | 131 << base_dir.MaybeAsASCII() << ")."; |
120 return; | 132 return; |
121 } | 133 } |
122 | 134 |
123 base::FilePath latest_dir; | 135 base::FilePath latest_dir; |
124 base::Version latest_version(kNullVersion); | 136 base::Version latest_version(kNullVersion); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 } | 181 } |
170 | 182 |
171 // Remove older versions of the component. None should be in use during | 183 // Remove older versions of the component. None should be in use during |
172 // browser startup. | 184 // browser startup. |
173 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin(); | 185 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin(); |
174 iter != older_dirs.end(); | 186 iter != older_dirs.end(); |
175 ++iter) { | 187 ++iter) { |
176 base::DeleteFile(*iter, true); | 188 base::DeleteFile(*iter, true); |
177 } | 189 } |
178 | 190 |
179 content::BrowserThread::PostTask( | 191 main_task_runner_->PostTask( |
180 content::BrowserThread::UI, | |
181 FROM_HERE, | 192 FROM_HERE, |
182 base::Bind(&DefaultComponentInstaller::FinishRegistration, | 193 base::Bind(&DefaultComponentInstaller::FinishRegistration, |
183 base::Unretained(this), | 194 base::Unretained(this), |
184 cus)); | 195 cus)); |
185 } | 196 } |
186 | 197 |
187 base::FilePath DefaultComponentInstaller::GetInstallDirectory() { | 198 base::FilePath DefaultComponentInstaller::GetInstallDirectory() { |
188 return installer_traits_->GetBaseDirectory() | 199 return installer_traits_->GetBaseDirectory() |
189 .AppendASCII(current_version_.GetString()); | 200 .AppendASCII(current_version_.GetString()); |
190 } | 201 } |
191 | 202 |
192 void DefaultComponentInstaller::FinishRegistration( | 203 void DefaultComponentInstaller::FinishRegistration( |
193 ComponentUpdateService* cus) { | 204 ComponentUpdateService* cus) { |
194 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 205 DCHECK(thread_checker_.CalledOnValidThread()); |
195 if (installer_traits_->CanAutoUpdate()) { | 206 if (installer_traits_->CanAutoUpdate()) { |
196 CrxComponent crx; | 207 CrxComponent crx; |
197 crx.name = installer_traits_->GetName(); | 208 crx.name = installer_traits_->GetName(); |
198 crx.installer = this; | 209 crx.installer = this; |
199 crx.version = current_version_; | 210 crx.version = current_version_; |
200 crx.fingerprint = current_fingerprint_; | 211 crx.fingerprint = current_fingerprint_; |
201 installer_traits_->GetHash(&crx.pk_hash); | 212 installer_traits_->GetHash(&crx.pk_hash); |
202 ComponentUpdateService::Status status = cus->RegisterComponent(crx); | 213 ComponentUpdateService::Status status = cus->RegisterComponent(crx); |
203 if (status != ComponentUpdateService::kOk && | 214 if (status != ComponentUpdateService::kOk && |
204 status != ComponentUpdateService::kReplaced) { | 215 status != ComponentUpdateService::kReplaced) { |
205 NOTREACHED() << "Component registration failed for " | 216 NOTREACHED() << "Component registration failed for " |
206 << installer_traits_->GetName(); | 217 << installer_traits_->GetName(); |
207 return; | 218 return; |
208 } | 219 } |
209 } | 220 } |
210 | 221 |
211 if (current_version_.CompareTo(base::Version(kNullVersion)) > 0) { | 222 if (current_version_.CompareTo(base::Version(kNullVersion)) > 0) { |
212 scoped_ptr<base::DictionaryValue> manifest_copy( | 223 scoped_ptr<base::DictionaryValue> manifest_copy( |
213 current_manifest_->DeepCopy()); | 224 current_manifest_->DeepCopy()); |
214 installer_traits_->ComponentReady( | 225 installer_traits_->ComponentReady( |
215 current_version_, GetInstallDirectory(), manifest_copy.Pass()); | 226 current_version_, GetInstallDirectory(), manifest_copy.Pass()); |
216 } | 227 } |
217 } | 228 } |
218 | 229 |
219 } // namespace component_updater | 230 } // namespace component_updater |
OLD | NEW |