| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/component_updater/component_updater_service.h" | 5 #include "components/component_updater/component_updater_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 17 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/macros.h" | 19 #include "base/macros.h" |
| 20 #include "base/memory/ptr_util.h" | 20 #include "base/memory/ptr_util.h" |
| 21 #include "base/metrics/histogram_macros.h" | 21 #include "base/metrics/histogram_macros.h" |
| 22 #include "base/sequenced_task_runner.h" | 22 #include "base/sequenced_task_runner.h" |
| 23 #include "base/single_thread_task_runner.h" | 23 #include "base/single_thread_task_runner.h" |
| 24 #include "base/strings/utf_string_conversions.h" |
| 24 #include "base/threading/sequenced_worker_pool.h" | 25 #include "base/threading/sequenced_worker_pool.h" |
| 25 #include "base/threading/thread_checker.h" | 26 #include "base/threading/thread_checker.h" |
| 26 #include "base/threading/thread_task_runner_handle.h" | 27 #include "base/threading/thread_task_runner_handle.h" |
| 27 #include "base/time/time.h" | 28 #include "base/time/time.h" |
| 28 #include "base/timer/timer.h" | 29 #include "base/timer/timer.h" |
| 29 #include "components/component_updater/component_updater_service_internal.h" | 30 #include "components/component_updater/component_updater_service_internal.h" |
| 30 #include "components/component_updater/timer.h" | 31 #include "components/component_updater/timer.h" |
| 31 #include "components/update_client/configurator.h" | 32 #include "components/update_client/configurator.h" |
| 32 #include "components/update_client/crx_update_item.h" | 33 #include "components/update_client/crx_update_item.h" |
| 33 #include "components/update_client/update_client.h" | 34 #include "components/update_client/update_client.h" |
| 34 #include "components/update_client/utils.h" | 35 #include "components/update_client/utils.h" |
| 35 #include "url/gurl.h" | 36 #include "url/gurl.h" |
| 36 | 37 |
| 37 using CrxInstaller = update_client::CrxInstaller; | 38 using CrxInstaller = update_client::CrxInstaller; |
| 38 using UpdateClient = update_client::UpdateClient; | 39 using UpdateClient = update_client::UpdateClient; |
| 39 | 40 |
| 40 namespace { | 41 namespace { |
| 41 | 42 |
| 42 enum UpdateType { | 43 enum UpdateType { |
| 43 UPDATE_TYPE_MANUAL = 0, | 44 UPDATE_TYPE_MANUAL = 0, |
| 44 UPDATE_TYPE_AUTOMATIC, | 45 UPDATE_TYPE_AUTOMATIC, |
| 45 UPDATE_TYPE_COUNT, | 46 UPDATE_TYPE_COUNT, |
| 46 }; | 47 }; |
| 47 | 48 |
| 48 } // namespace | 49 } // namespace |
| 49 | 50 |
| 50 namespace component_updater { | 51 namespace component_updater { |
| 51 | 52 |
| 53 ComponentInfo::ComponentInfo(const std::string& id, const base::string16& name) |
| 54 : id(id), name(name) {} |
| 55 ComponentInfo::~ComponentInfo() {} |
| 56 |
| 52 CrxUpdateService::CrxUpdateService( | 57 CrxUpdateService::CrxUpdateService( |
| 53 const scoped_refptr<Configurator>& config, | 58 const scoped_refptr<Configurator>& config, |
| 54 const scoped_refptr<UpdateClient>& update_client) | 59 const scoped_refptr<UpdateClient>& update_client) |
| 55 : config_(config), | 60 : config_(config), |
| 56 update_client_(update_client), | 61 update_client_(update_client), |
| 57 blocking_task_runner_(config->GetSequencedTaskRunner()) { | 62 blocking_task_runner_(config->GetSequencedTaskRunner()) { |
| 58 AddObserver(this); | 63 AddObserver(this); |
| 59 } | 64 } |
| 60 | 65 |
| 61 CrxUpdateService::~CrxUpdateService() { | 66 CrxUpdateService::~CrxUpdateService() { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 // Update the registration data if the component has been registered before. | 120 // Update the registration data if the component has been registered before. |
| 116 const std::string id(GetCrxComponentID(component)); | 121 const std::string id(GetCrxComponentID(component)); |
| 117 auto it = components_.find(id); | 122 auto it = components_.find(id); |
| 118 if (it != components_.end()) { | 123 if (it != components_.end()) { |
| 119 it->second = component; | 124 it->second = component; |
| 120 return true; | 125 return true; |
| 121 } | 126 } |
| 122 | 127 |
| 123 components_.insert(std::make_pair(id, component)); | 128 components_.insert(std::make_pair(id, component)); |
| 124 components_order_.push_back(id); | 129 components_order_.push_back(id); |
| 130 for (const auto& mime_type : component.handled_mime_types) |
| 131 component_ids_by_mime_type_[mime_type] = id; |
| 125 | 132 |
| 126 // Create an initial state for this component. The state is mutated in | 133 // Create an initial state for this component. The state is mutated in |
| 127 // response to events from the UpdateClient instance. | 134 // response to events from the UpdateClient instance. |
| 128 CrxUpdateItem item; | 135 CrxUpdateItem item; |
| 129 item.id = id; | 136 item.id = id; |
| 130 item.component = component; | 137 item.component = component; |
| 131 const auto inserted = component_states_.insert(std::make_pair(id, item)); | 138 const auto inserted = component_states_.insert(std::make_pair(id, item)); |
| 132 DCHECK(inserted.second); | 139 DCHECK(inserted.second); |
| 133 | 140 |
| 134 // Start the timer if this is the first component registered. The first timer | 141 // Start the timer if this is the first component registered. The first timer |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 } | 185 } |
| 179 | 186 |
| 180 std::vector<std::string> CrxUpdateService::GetComponentIDs() const { | 187 std::vector<std::string> CrxUpdateService::GetComponentIDs() const { |
| 181 DCHECK(thread_checker_.CalledOnValidThread()); | 188 DCHECK(thread_checker_.CalledOnValidThread()); |
| 182 std::vector<std::string> ids; | 189 std::vector<std::string> ids; |
| 183 for (const auto& it : components_) | 190 for (const auto& it : components_) |
| 184 ids.push_back(it.first); | 191 ids.push_back(it.first); |
| 185 return ids; | 192 return ids; |
| 186 } | 193 } |
| 187 | 194 |
| 195 std::unique_ptr<ComponentInfo> CrxUpdateService::GetComponentForMimeType( |
| 196 const std::string& mime_type) const { |
| 197 DCHECK(thread_checker_.CalledOnValidThread()); |
| 198 const auto it = component_ids_by_mime_type_.find(mime_type); |
| 199 if (it == component_ids_by_mime_type_.end()) |
| 200 return nullptr; |
| 201 const auto component = GetComponent(it->second); |
| 202 if (!component) |
| 203 return nullptr; |
| 204 return base::MakeUnique<ComponentInfo>(GetCrxComponentID(*component), |
| 205 base::UTF8ToUTF16(component->name)); |
| 206 } |
| 207 |
| 188 OnDemandUpdater& CrxUpdateService::GetOnDemandUpdater() { | 208 OnDemandUpdater& CrxUpdateService::GetOnDemandUpdater() { |
| 189 DCHECK(thread_checker_.CalledOnValidThread()); | 209 DCHECK(thread_checker_.CalledOnValidThread()); |
| 190 return *this; | 210 return *this; |
| 191 } | 211 } |
| 192 | 212 |
| 193 const CrxComponent* CrxUpdateService::GetComponent( | 213 const CrxComponent* CrxUpdateService::GetComponent( |
| 194 const std::string& id) const { | 214 const std::string& id) const { |
| 195 DCHECK(thread_checker_.CalledOnValidThread()); | 215 DCHECK(thread_checker_.CalledOnValidThread()); |
| 196 const auto it(components_.find(id)); | 216 const auto it(components_.find(id)); |
| 197 return it != components_.end() ? &(it->second) : NULL; | 217 return it != components_.end() ? &(it->second) : NULL; |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 // TODO(sorin): consider making this a singleton. | 411 // TODO(sorin): consider making this a singleton. |
| 392 std::unique_ptr<ComponentUpdateService> ComponentUpdateServiceFactory( | 412 std::unique_ptr<ComponentUpdateService> ComponentUpdateServiceFactory( |
| 393 const scoped_refptr<Configurator>& config) { | 413 const scoped_refptr<Configurator>& config) { |
| 394 DCHECK(config); | 414 DCHECK(config); |
| 395 auto update_client = update_client::UpdateClientFactory(config); | 415 auto update_client = update_client::UpdateClientFactory(config); |
| 396 return base::WrapUnique( | 416 return base::WrapUnique( |
| 397 new CrxUpdateService(config, std::move(update_client))); | 417 new CrxUpdateService(config, std::move(update_client))); |
| 398 } | 418 } |
| 399 | 419 |
| 400 } // namespace component_updater | 420 } // namespace component_updater |
| OLD | NEW |