sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef COMPONENTS_UPDATE_CLIENT_ACTION_H_ |
| 6 | #define COMPONENTS_UPDATE_CLIENT_ACTION_H_ |
| 7 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 10 | #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 | |
| 18 | namespace update_client { |
| 19 | |
| 20 | class Configurator; |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 21 | enum class Error; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 22 | struct CrxUpdateItem; |
| 23 | struct 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. |
| 32 | class Action { |
| 33 | public: |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 34 | 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. |
| 44 | class ActionImpl { |
| 45 | protected: |
| 46 | ActionImpl(); |
| 47 | ~ActionImpl(); |
| 48 | |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 49 | void Run(UpdateContext* update_context, Callback callback); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 50 | |
| 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. |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 75 | void UpdateComplete(Error error); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 76 | |
| 77 | base::ThreadChecker thread_checker_; |
| 78 | |
| 79 | UpdateContext* update_context_; // Not owned by this class. |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 80 | Callback callback_; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 81 | |
| 82 | private: |
| 83 | DISALLOW_COPY_AND_ASSIGN(ActionImpl); |
| 84 | }; |
| 85 | |
| 86 | } // namespace update_client |
| 87 | |
| 88 | #endif // COMPONENTS_UPDATE_CLIENT_ACTION_H_ |