| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/update_client/update_engine.h" | 5 #include "components/update_client/update_engine.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
| 12 #include "base/time/time.h" |
| 12 #include "components/update_client/action_update_check.h" | 13 #include "components/update_client/action_update_check.h" |
| 13 #include "components/update_client/configurator.h" | 14 #include "components/update_client/configurator.h" |
| 14 #include "components/update_client/crx_update_item.h" | 15 #include "components/update_client/crx_update_item.h" |
| 15 #include "components/update_client/update_checker.h" | 16 #include "components/update_client/update_checker.h" |
| 16 | 17 |
| 17 namespace update_client { | 18 namespace update_client { |
| 18 | 19 |
| 19 UpdateContext::UpdateContext( | 20 UpdateContext::UpdateContext( |
| 20 const scoped_refptr<Configurator>& config, | 21 const scoped_refptr<Configurator>& config, |
| 21 bool is_foreground, | 22 bool is_foreground, |
| 22 const std::vector<std::string>& ids, | 23 const std::vector<std::string>& ids, |
| 23 const UpdateClient::CrxDataCallback& crx_data_callback, | 24 const UpdateClient::CrxDataCallback& crx_data_callback, |
| 24 const UpdateEngine::NotifyObserversCallback& notify_observers_callback, | 25 const UpdateEngine::NotifyObserversCallback& notify_observers_callback, |
| 25 const UpdateEngine::CompletionCallback& callback, | 26 const UpdateEngine::CompletionCallback& callback, |
| 26 UpdateChecker::Factory update_checker_factory, | 27 UpdateChecker::Factory update_checker_factory, |
| 27 CrxDownloader::Factory crx_downloader_factory, | 28 CrxDownloader::Factory crx_downloader_factory, |
| 28 PingManager* ping_manager) | 29 PingManager* ping_manager) |
| 29 : config(config), | 30 : config(config), |
| 30 is_foreground(is_foreground), | 31 is_foreground(is_foreground), |
| 31 ids(ids), | 32 ids(ids), |
| 32 crx_data_callback(crx_data_callback), | 33 crx_data_callback(crx_data_callback), |
| 33 notify_observers_callback(notify_observers_callback), | 34 notify_observers_callback(notify_observers_callback), |
| 34 callback(callback), | 35 callback(callback), |
| 35 main_task_runner(base::ThreadTaskRunnerHandle::Get()), | 36 main_task_runner(base::ThreadTaskRunnerHandle::Get()), |
| 36 blocking_task_runner(config->GetSequencedTaskRunner()), | 37 blocking_task_runner(config->GetSequencedTaskRunner()), |
| 37 update_checker_factory(update_checker_factory), | 38 update_checker_factory(update_checker_factory), |
| 38 crx_downloader_factory(crx_downloader_factory), | 39 crx_downloader_factory(crx_downloader_factory), |
| 39 ping_manager(ping_manager) { | 40 ping_manager(ping_manager), |
| 40 } | 41 retry_after_sec_(0) {} |
| 41 | 42 |
| 42 UpdateContext::~UpdateContext() { | 43 UpdateContext::~UpdateContext() { |
| 43 STLDeleteElements(&update_items); | 44 STLDeleteElements(&update_items); |
| 44 } | 45 } |
| 45 | 46 |
| 46 UpdateEngine::UpdateEngine( | 47 UpdateEngine::UpdateEngine( |
| 47 const scoped_refptr<Configurator>& config, | 48 const scoped_refptr<Configurator>& config, |
| 48 UpdateChecker::Factory update_checker_factory, | 49 UpdateChecker::Factory update_checker_factory, |
| 49 CrxDownloader::Factory crx_downloader_factory, | 50 CrxDownloader::Factory crx_downloader_factory, |
| 50 PingManager* ping_manager, | 51 PingManager* ping_manager, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 77 return false; | 78 return false; |
| 78 } | 79 } |
| 79 | 80 |
| 80 void UpdateEngine::Update( | 81 void UpdateEngine::Update( |
| 81 bool is_foreground, | 82 bool is_foreground, |
| 82 const std::vector<std::string>& ids, | 83 const std::vector<std::string>& ids, |
| 83 const UpdateClient::CrxDataCallback& crx_data_callback, | 84 const UpdateClient::CrxDataCallback& crx_data_callback, |
| 84 const CompletionCallback& callback) { | 85 const CompletionCallback& callback) { |
| 85 DCHECK(thread_checker_.CalledOnValidThread()); | 86 DCHECK(thread_checker_.CalledOnValidThread()); |
| 86 | 87 |
| 88 if (IsThrottled(is_foreground)) { |
| 89 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 90 FROM_HERE, base::Bind(callback, Error::ERROR_UPDATE_RETRY_LATER)); |
| 91 return; |
| 92 } |
| 93 |
| 87 scoped_ptr<UpdateContext> update_context(new UpdateContext( | 94 scoped_ptr<UpdateContext> update_context(new UpdateContext( |
| 88 config_, is_foreground, ids, crx_data_callback, | 95 config_, is_foreground, ids, crx_data_callback, |
| 89 notify_observers_callback_, callback, update_checker_factory_, | 96 notify_observers_callback_, callback, update_checker_factory_, |
| 90 crx_downloader_factory_, ping_manager_)); | 97 crx_downloader_factory_, ping_manager_)); |
| 91 | 98 |
| 92 CrxUpdateItem update_item; | 99 CrxUpdateItem update_item; |
| 93 scoped_ptr<ActionUpdateCheck> update_check_action(new ActionUpdateCheck( | 100 scoped_ptr<ActionUpdateCheck> update_check_action(new ActionUpdateCheck( |
| 94 (*update_context->update_checker_factory)(config_), | 101 (*update_context->update_checker_factory)(config_), |
| 95 config_->GetBrowserVersion(), config_->ExtraRequestParams())); | 102 config_->GetBrowserVersion(), config_->ExtraRequestParams())); |
| 96 | 103 |
| 97 update_context->current_action.reset(update_check_action.release()); | 104 update_context->current_action.reset(update_check_action.release()); |
| 98 update_contexts_.insert(update_context.get()); | 105 update_contexts_.insert(update_context.get()); |
| 99 | 106 |
| 100 update_context->current_action->Run( | 107 update_context->current_action->Run( |
| 101 update_context.get(), | 108 update_context.get(), |
| 102 base::Bind(&UpdateEngine::UpdateComplete, base::Unretained(this), | 109 base::Bind(&UpdateEngine::UpdateComplete, base::Unretained(this), |
| 103 update_context.get())); | 110 update_context.get())); |
| 104 | 111 |
| 105 ignore_result(update_context.release()); | 112 ignore_result(update_context.release()); |
| 106 } | 113 } |
| 107 | 114 |
| 108 void UpdateEngine::UpdateComplete(UpdateContext* update_context, int error) { | 115 void UpdateEngine::UpdateComplete(UpdateContext* update_context, int error) { |
| 109 DCHECK(thread_checker_.CalledOnValidThread()); | 116 DCHECK(thread_checker_.CalledOnValidThread()); |
| 110 DCHECK(update_contexts_.find(update_context) != update_contexts_.end()); | 117 DCHECK(update_contexts_.find(update_context) != update_contexts_.end()); |
| 111 | 118 |
| 119 const int throttle_sec(update_context->retry_after_sec_); |
| 120 DCHECK_LE(throttle_sec, 24 * 60 * 60); |
| 121 |
| 122 // Only positive values for throttle_sec are effective. 0 means that no |
| 123 // throttling occurs and has the effect of resetting the member. |
| 124 // Negative values are not trusted and are ignored. |
| 125 if (throttle_sec >= 0) |
| 126 throttle_updates_until_ = |
| 127 throttle_sec |
| 128 ? base::Time::Now() + base::TimeDelta::FromSeconds(throttle_sec) |
| 129 : base::Time(); |
| 130 |
| 112 auto callback = update_context->callback; | 131 auto callback = update_context->callback; |
| 113 | 132 |
| 114 update_contexts_.erase(update_context); | 133 update_contexts_.erase(update_context); |
| 115 delete update_context; | 134 delete update_context; |
| 116 | 135 |
| 117 callback.Run(error); | 136 callback.Run(error); |
| 118 } | 137 } |
| 119 | 138 |
| 139 bool UpdateEngine::IsThrottled(bool is_foreground) const { |
| 140 if (is_foreground || throttle_updates_until_.is_null()) |
| 141 return false; |
| 142 |
| 143 const auto now(base::Time::Now()); |
| 144 |
| 145 // Throttle the calls in the interval (t - 1 day, t) to limit the effect of |
| 146 // unset clocks or clock drift. |
| 147 return throttle_updates_until_ - base::TimeDelta::FromDays(1) < now && |
| 148 now < throttle_updates_until_; |
| 149 } |
| 150 |
| 120 } // namespace update_client | 151 } // namespace update_client |
| OLD | NEW |