summaryrefslogtreecommitdiffstats
path: root/components/update_client/action.cc
blob: de795e68e2de10dbfc8748a83dbf1b1fb263c00a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/update_client/action.h"

#include <algorithm>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "components/update_client/action_update.h"
#include "components/update_client/action_wait.h"
#include "components/update_client/configurator.h"
#include "components/update_client/update_engine.h"
#include "components/update_client/utils.h"

namespace update_client {

using Events = UpdateClient::Observer::Events;

namespace {

// Returns true if a differential update is available, it has not failed yet,
// and the configuration allows this update.
bool CanTryDiffUpdate(const CrxUpdateItem* update_item,
                      const scoped_refptr<Configurator>& config) {
  return HasDiffUpdate(update_item) && !update_item->diff_update_failed &&
         config->DeltasEnabled();
}

}  // namespace

ActionImpl::ActionImpl() : update_context_(nullptr) {
}

ActionImpl::~ActionImpl() {
  DCHECK(thread_checker_.CalledOnValidThread());
}

void ActionImpl::Run(UpdateContext* update_context, Action::Callback callback) {
  DCHECK(thread_checker_.CalledOnValidThread());

  update_context_ = update_context;
  callback_ = callback;
}

CrxUpdateItem* ActionImpl::FindUpdateItemById(const std::string& id) const {
  DCHECK(thread_checker_.CalledOnValidThread());

  const auto it(std::find_if(
      update_context_->update_items.begin(),
      update_context_->update_items.end(),
      [&id](const CrxUpdateItem* item) { return item->id == id; }));

  return it != update_context_->update_items.end() ? *it : nullptr;
}

void ActionImpl::ChangeItemState(CrxUpdateItem* item, CrxUpdateItem::State to) {
  DCHECK(thread_checker_.CalledOnValidThread());

  item->state = to;

  using Events = UpdateClient::Observer::Events;

  const std::string& id(item->id);
  switch (to) {
    case CrxUpdateItem::State::kChecking:
      NotifyObservers(Events::COMPONENT_CHECKING_FOR_UPDATES, id);
      break;
    case CrxUpdateItem::State::kCanUpdate:
      NotifyObservers(Events::COMPONENT_UPDATE_FOUND, id);
      break;
    case CrxUpdateItem::State::kUpdatingDiff:
    case CrxUpdateItem::State::kUpdating:
      NotifyObservers(Events::COMPONENT_UPDATE_READY, id);
      break;
    case CrxUpdateItem::State::kUpdated:
      NotifyObservers(Events::COMPONENT_UPDATED, id);
      break;
    case CrxUpdateItem::State::kUpToDate:
    case CrxUpdateItem::State::kNoUpdate:
      NotifyObservers(Events::COMPONENT_NOT_UPDATED, id);
      break;
    case CrxUpdateItem::State::kNew:
    case CrxUpdateItem::State::kDownloading:
    case CrxUpdateItem::State::kDownloadingDiff:
    case CrxUpdateItem::State::kDownloaded:
    case CrxUpdateItem::State::kUninstalled:
    case CrxUpdateItem::State::kLastStatus:
      // No notification for these states.
      break;
  }
}

size_t ActionImpl::ChangeAllItemsState(CrxUpdateItem::State from,
                                       CrxUpdateItem::State to) {
  DCHECK(thread_checker_.CalledOnValidThread());
  size_t count = 0;
  for (auto item : update_context_->update_items) {
    if (item->state == from) {
      ChangeItemState(item, to);
      ++count;
    }
  }
  return count;
}

void ActionImpl::NotifyObservers(UpdateClient::Observer::Events event,
                                 const std::string& id) {
  DCHECK(thread_checker_.CalledOnValidThread());
  update_context_->notify_observers_callback.Run(event, id);
}

void ActionImpl::UpdateCrx() {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK(!update_context_->queue.empty());

  const std::string& id = update_context_->queue.front();
  CrxUpdateItem* item = FindUpdateItemById(id);
  DCHECK(item);

  scoped_ptr<Action> update_action(
      CanTryDiffUpdate(item, update_context_->config)
          ? ActionUpdateDiff::Create()
          : ActionUpdateFull::Create());

  base::ThreadTaskRunnerHandle::Get()->PostTask(
      FROM_HERE, base::Bind(&Action::Run, base::Unretained(update_action.get()),
                            update_context_, callback_));

  update_context_->current_action.reset(update_action.release());
}

void ActionImpl::UpdateCrxComplete(CrxUpdateItem* item) {
  update_context_->ping_manager->SendPing(item);

  update_context_->queue.pop();

  if (update_context_->queue.empty()) {
    UpdateComplete(0);
  } else {
    // TODO(sorin): the value of timing interval between CRX updates might have
    // to be injected at the call site of update_client::UpdateClient::Update.
    const int wait_sec = update_context_->config->UpdateDelay();

    scoped_ptr<ActionWait> action_wait(
        new ActionWait(base::TimeDelta::FromSeconds(wait_sec)));

    base::ThreadTaskRunnerHandle::Get()->PostTask(
        FROM_HERE, base::Bind(&Action::Run, base::Unretained(action_wait.get()),
                              update_context_, callback_));

    update_context_->current_action.reset(action_wait.release());
  }
}

void ActionImpl::UpdateComplete(int error) {
  DCHECK(thread_checker_.CalledOnValidThread());

  base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
                                                base::Bind(callback_, error));
}

}  // namespace update_client