| 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/action.h" | 5 #include "components/update_client/action.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> |
| 9 |
| 8 #include "base/bind.h" | 10 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 10 #include "base/callback.h" | 12 #include "base/callback.h" |
| 11 #include "base/location.h" | 13 #include "base/location.h" |
| 12 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 13 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "components/update_client/action_update.h" | 17 #include "components/update_client/action_update.h" |
| 16 #include "components/update_client/action_wait.h" | 18 #include "components/update_client/action_wait.h" |
| 17 #include "components/update_client/configurator.h" | 19 #include "components/update_client/configurator.h" |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 } | 118 } |
| 117 | 119 |
| 118 void ActionImpl::UpdateCrx() { | 120 void ActionImpl::UpdateCrx() { |
| 119 DCHECK(thread_checker_.CalledOnValidThread()); | 121 DCHECK(thread_checker_.CalledOnValidThread()); |
| 120 DCHECK(!update_context_->queue.empty()); | 122 DCHECK(!update_context_->queue.empty()); |
| 121 | 123 |
| 122 const std::string& id = update_context_->queue.front(); | 124 const std::string& id = update_context_->queue.front(); |
| 123 CrxUpdateItem* item = FindUpdateItemById(id); | 125 CrxUpdateItem* item = FindUpdateItemById(id); |
| 124 DCHECK(item); | 126 DCHECK(item); |
| 125 | 127 |
| 128 item->update_begin = base::TimeTicks::Now(); |
| 129 |
| 126 std::unique_ptr<Action> update_action( | 130 std::unique_ptr<Action> update_action( |
| 127 CanTryDiffUpdate(item, update_context_->config) | 131 CanTryDiffUpdate(item, update_context_->config) |
| 128 ? ActionUpdateDiff::Create() | 132 ? ActionUpdateDiff::Create() |
| 129 : ActionUpdateFull::Create()); | 133 : ActionUpdateFull::Create()); |
| 130 | 134 |
| 131 base::ThreadTaskRunnerHandle::Get()->PostTask( | 135 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 132 FROM_HERE, base::Bind(&Action::Run, base::Unretained(update_action.get()), | 136 FROM_HERE, base::Bind(&Action::Run, base::Unretained(update_action.get()), |
| 133 update_context_, callback_)); | 137 update_context_, callback_)); |
| 134 | 138 |
| 135 update_context_->current_action.reset(update_action.release()); | 139 update_context_->current_action.reset(update_action.release()); |
| 136 } | 140 } |
| 137 | 141 |
| 138 void ActionImpl::UpdateCrxComplete(CrxUpdateItem* item) { | 142 void ActionImpl::UpdateCrxComplete(CrxUpdateItem* item) { |
| 143 DCHECK(thread_checker_.CalledOnValidThread()); |
| 144 DCHECK(item); |
| 145 |
| 139 update_context_->ping_manager->SendPing(item); | 146 update_context_->ping_manager->SendPing(item); |
| 140 | 147 |
| 141 update_context_->queue.pop(); | 148 update_context_->queue.pop(); |
| 142 | 149 |
| 143 if (update_context_->queue.empty()) { | 150 if (update_context_->queue.empty()) { |
| 144 UpdateComplete(0); | 151 UpdateComplete(0); |
| 145 } else { | 152 } else { |
| 146 // TODO(sorin): the value of timing interval between CRX updates might have | 153 DCHECK(!item->update_begin.is_null()); |
| 147 // to be injected at the call site of update_client::UpdateClient::Update. | 154 |
| 148 const int wait_sec = update_context_->config->UpdateDelay(); | 155 // Assume that the cost of applying the update is proportional with how |
| 156 // long it took to apply it. Then delay the next update by the same time |
| 157 // interval or the value provided by the configurator, whichever is less. |
| 158 const base::TimeDelta max_update_delay = |
| 159 base::TimeDelta::FromSeconds(update_context_->config->UpdateDelay()); |
| 160 const base::TimeDelta update_cost(base::TimeTicks::Now() - |
| 161 item->update_begin); |
| 162 DCHECK(update_cost >= base::TimeDelta()); |
| 149 | 163 |
| 150 std::unique_ptr<ActionWait> action_wait( | 164 std::unique_ptr<ActionWait> action_wait( |
| 151 new ActionWait(base::TimeDelta::FromSeconds(wait_sec))); | 165 new ActionWait(std::min(update_cost, max_update_delay))); |
| 152 | 166 |
| 153 base::ThreadTaskRunnerHandle::Get()->PostTask( | 167 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 154 FROM_HERE, base::Bind(&Action::Run, base::Unretained(action_wait.get()), | 168 FROM_HERE, base::Bind(&Action::Run, base::Unretained(action_wait.get()), |
| 155 update_context_, callback_)); | 169 update_context_, callback_)); |
| 156 | 170 |
| 157 update_context_->current_action.reset(action_wait.release()); | 171 update_context_->current_action.reset(action_wait.release()); |
| 158 } | 172 } |
| 159 } | 173 } |
| 160 | 174 |
| 161 void ActionImpl::UpdateComplete(int error) { | 175 void ActionImpl::UpdateComplete(int error) { |
| 162 DCHECK(thread_checker_.CalledOnValidThread()); | 176 DCHECK(thread_checker_.CalledOnValidThread()); |
| 163 | 177 |
| 164 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 178 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 165 base::Bind(callback_, error)); | 179 base::Bind(callback_, error)); |
| 166 } | 180 } |
| 167 | 181 |
| 168 } // namespace update_client | 182 } // namespace update_client |
| OLD | NEW |