Fix broken path in extensions/common/PRESUBMIT.py
[chromium-blink-merge.git] / components / update_client / update_engine.h
blobe902a542d9e6bc68b3fbdb0bc8ff5c8d6c3b0f0a
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.
5 #ifndef COMPONENTS_UPDATE_CLIENT_UPDATE_ENGINE_H_
6 #define COMPONENTS_UPDATE_CLIENT_UPDATE_ENGINE_H_
8 #include <list>
9 #include <queue>
10 #include <set>
11 #include <string>
12 #include <vector>
14 #include "base/callback.h"
15 #include "base/macros.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/threading/thread_checker.h"
19 #include "components/update_client/action.h"
20 #include "components/update_client/component_patcher_operation.h"
21 #include "components/update_client/component_unpacker.h"
22 #include "components/update_client/crx_downloader.h"
23 #include "components/update_client/crx_update_item.h"
24 #include "components/update_client/ping_manager.h"
25 #include "components/update_client/update_checker.h"
26 #include "components/update_client/update_client.h"
28 namespace base {
29 class SequencedTaskRunner;
30 class SingleThreadTaskRunner;
31 } // namespace base
33 namespace update_client {
35 class Configurator;
36 class CrxDownloader;
37 struct CrxUpdateItem;
38 class PingManager;
39 struct UpdateContext;
41 // Handles updates for a group of components. Updates for different groups
42 // are run concurrently but within the same group of components, updates are
43 // applied one at a time.
44 class UpdateEngine {
45 public:
46 using CompletionCallback = base::Callback<void(int error)>;
47 using NotifyObserversCallback =
48 base::Callback<void(UpdateClient::Observer::Events event,
49 const std::string& id)>;
50 using CrxDataCallback = UpdateClient::CrxDataCallback;
52 UpdateEngine(const scoped_refptr<Configurator>& config,
53 UpdateChecker::Factory update_checker_factory,
54 CrxDownloader::Factory crx_downloader_factory,
55 PingManager* ping_manager,
56 const NotifyObserversCallback& notify_observers_callback);
57 ~UpdateEngine();
59 // Returns true is the CRX identified by the given |id| is being updated.
60 bool IsUpdating(const std::string& id) const;
62 bool GetUpdateState(const std::string& id, CrxUpdateItem* update_state);
64 void Update(const std::vector<std::string>& ids,
65 const UpdateClient::CrxDataCallback& crx_data_callback,
66 const CompletionCallback& update_callback);
68 private:
69 void UpdateComplete(UpdateContext* update_context, int error);
71 base::ThreadChecker thread_checker_;
73 scoped_refptr<Configurator> config_;
75 UpdateChecker::Factory update_checker_factory_;
76 CrxDownloader::Factory crx_downloader_factory_;
78 // TODO(sorin): refactor as a ref counted class.
79 PingManager* ping_manager_; // Not owned by this class.
81 // Called when CRX state changes occur.
82 const NotifyObserversCallback notify_observers_callback_;
84 // Contains the contexts associated with each update in progress.
85 std::set<UpdateContext*> update_contexts_;
87 DISALLOW_COPY_AND_ASSIGN(UpdateEngine);
90 // TODO(sorin): consider making this a ref counted type.
91 struct UpdateContext {
92 UpdateContext(
93 const scoped_refptr<Configurator>& config,
94 const std::vector<std::string>& ids,
95 const UpdateClient::CrxDataCallback& crx_data_callback,
96 const UpdateEngine::NotifyObserversCallback& notify_observers_callback,
97 const UpdateEngine::CompletionCallback& callback,
98 UpdateChecker::Factory update_checker_factory,
99 CrxDownloader::Factory crx_downloader_factory,
100 PingManager* ping_manager);
102 ~UpdateContext();
104 scoped_refptr<Configurator> config;
106 // Contains the ids of all CRXs in this context.
107 const std::vector<std::string> ids;
109 // Called before an update check, when update metadata is needed.
110 const UpdateEngine::CrxDataCallback& crx_data_callback;
112 // Called when there is a state change for any update in this context.
113 const UpdateEngine::NotifyObserversCallback notify_observers_callback;
115 // Called when the all updates associated with this context have completed.
116 const UpdateEngine::CompletionCallback callback;
118 // Posts replies back to the main thread.
119 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner;
121 // Runs tasks in a blocking thread pool.
122 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner;
124 // Runs tasks in the same single thread.
125 scoped_refptr<base::SingleThreadTaskRunner> single_thread_task_runner;
127 // Creates instances of UpdateChecker;
128 UpdateChecker::Factory update_checker_factory;
130 // Creates instances of CrxDownloader;
131 CrxDownloader::Factory crx_downloader_factory;
133 PingManager* ping_manager; // Not owned by this class.
135 scoped_ptr<Action> current_action;
137 // TODO(sorin): use a map instead of vector.
138 std::vector<CrxUpdateItem*> update_items;
140 // Contains the ids of the items to update.
141 std::queue<std::string> queue;
143 scoped_ptr<CrxDownloader> crx_downloader;
144 scoped_refptr<ComponentUnpacker> unpacker;
147 } // namespace update_client
149 #endif // COMPONENTS_UPDATE_CLIENT_UPDATE_ENGINE_H_