blob: 1b46106e333d0342534d02491a5bf8e8d0bebaae [file] [log] [blame]
sorin9797aba2015-04-17 17:15:031// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef COMPONENTS_UPDATE_CLIENT_ACTION_H_
6#define COMPONENTS_UPDATE_CLIENT_ACTION_H_
7
avi5dd91f82015-12-25 22:30:468#include <stddef.h>
9
sorin9797aba2015-04-17 17:15:0310#include <string>
11
12#include "base/callback.h"
13#include "base/macros.h"
14#include "base/threading/thread_checker.h"
15#include "components/update_client/crx_update_item.h"
16#include "components/update_client/update_client.h"
17
18namespace update_client {
19
20class Configurator;
sorin7b8650522016-11-02 18:23:4121enum class Error;
sorin9797aba2015-04-17 17:15:0322struct CrxUpdateItem;
23struct UpdateContext;
24
25// Any update can be broken down as a sequence of discrete steps, such as
26// checking for updates, downloading patches, updating, and waiting between
27// successive updates. An action is the smallest unit of work executed by
28// the update engine.
29//
30// Defines an abstract interface for a unit of work, executed by the
31// update engine as part of an update.
32class Action {
33 public:
sorin9797aba2015-04-17 17:15:0334 virtual ~Action() {}
35
36 // Runs the code encapsulated by the action. When an action completes, it can
37 // chain up and transfer the execution flow to another action or it can
38 // invoke the |callback| when this function has completed and there is nothing
39 // else to do.
40 virtual void Run(UpdateContext* update_context, Callback callback) = 0;
41};
42
43// Provides a reusable implementation of common functions needed by actions.
44class ActionImpl {
45 protected:
46 ActionImpl();
47 ~ActionImpl();
48
sorin842703b2016-11-02 23:59:2349 void Run(UpdateContext* update_context, Callback callback);
sorin9797aba2015-04-17 17:15:0350
51 // Changes the current state of the |item| to the new state |to|.
52 void ChangeItemState(CrxUpdateItem* item, CrxUpdateItem::State to);
53
54 // Changes the state of all items in |update_context_|. Returns the count
55 // of items affected by the call.
56 size_t ChangeAllItemsState(CrxUpdateItem::State from,
57 CrxUpdateItem::State to);
58
59 // Returns the item associated with the component |id| or nullptr in case
60 // of errors.
61 CrxUpdateItem* FindUpdateItemById(const std::string& id) const;
62
63 void NotifyObservers(UpdateClient::Observer::Events event,
64 const std::string& id);
65
66 // Updates the CRX at the front of the CRX queue in this update context.
67 void UpdateCrx();
68
69 // Completes updating the CRX at the front of the queue, and initiates
70 // the update for the next CRX in the queue, if the queue is not empty.
71 void UpdateCrxComplete(CrxUpdateItem* item);
72
73 // Called when the updates for all CRXs have finished and the execution
74 // flow must return back to the update engine.
sorin7b8650522016-11-02 18:23:4175 void UpdateComplete(Error error);
sorin9797aba2015-04-17 17:15:0376
77 base::ThreadChecker thread_checker_;
78
79 UpdateContext* update_context_; // Not owned by this class.
sorin842703b2016-11-02 23:59:2380 Callback callback_;
sorin9797aba2015-04-17 17:15:0381
82 private:
83 DISALLOW_COPY_AND_ASSIGN(ActionImpl);
84};
85
86} // namespace update_client
87
88#endif // COMPONENTS_UPDATE_CLIENT_ACTION_H_