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_
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/crx_downloader.h"
22 #include "components/update_client/crx_update_item.h"
23 #include "components/update_client/ping_manager.h"
24 #include "components/update_client/update_checker.h"
25 #include "components/update_client/update_client.h"
28 class SequencedTaskRunner
;
29 class SingleThreadTaskRunner
;
32 namespace update_client
{
38 // Handles updates for a group of components. Updates for different groups
39 // are run concurrently but within the same group of components, updates are
40 // applied one at a time.
43 using CompletionCallback
= base::Callback
<void(int error
)>;
44 using NotifyObserversCallback
=
45 base::Callback
<void(UpdateClient::Observer::Events event
,
46 const std::string
& id
)>;
47 using CrxDataCallback
= UpdateClient::CrxDataCallback
;
49 UpdateEngine(const scoped_refptr
<Configurator
>& config
,
50 UpdateChecker::Factory update_checker_factory
,
51 CrxDownloader::Factory crx_downloader_factory
,
52 PingManager
* ping_manager
,
53 const NotifyObserversCallback
& notify_observers_callback
);
56 // Returns true is the CRX identified by the given |id| is being updated.
57 bool IsUpdating(const std::string
& id
) const;
59 bool GetUpdateState(const std::string
& id
, CrxUpdateItem
* update_state
);
61 void Update(bool is_foreground
,
62 const std::vector
<std::string
>& ids
,
63 const UpdateClient::CrxDataCallback
& crx_data_callback
,
64 const CompletionCallback
& update_callback
);
67 void UpdateComplete(UpdateContext
* update_context
, int error
);
69 base::ThreadChecker thread_checker_
;
71 scoped_refptr
<Configurator
> config_
;
73 UpdateChecker::Factory update_checker_factory_
;
74 CrxDownloader::Factory crx_downloader_factory_
;
76 // TODO(sorin): refactor as a ref counted class.
77 PingManager
* ping_manager_
; // Not owned by this class.
79 // Called when CRX state changes occur.
80 const NotifyObserversCallback notify_observers_callback_
;
82 // Contains the contexts associated with each update in progress.
83 std::set
<UpdateContext
*> update_contexts_
;
85 DISALLOW_COPY_AND_ASSIGN(UpdateEngine
);
88 // TODO(sorin): consider making this a ref counted type.
89 struct UpdateContext
{
91 const scoped_refptr
<Configurator
>& config
,
93 const std::vector
<std::string
>& ids
,
94 const UpdateClient::CrxDataCallback
& crx_data_callback
,
95 const UpdateEngine::NotifyObserversCallback
& notify_observers_callback
,
96 const UpdateEngine::CompletionCallback
& callback
,
97 UpdateChecker::Factory update_checker_factory
,
98 CrxDownloader::Factory crx_downloader_factory
,
99 PingManager
* ping_manager
);
103 scoped_refptr
<Configurator
> config
;
105 // True if this update has been initiated by the user.
108 // Contains the ids of all CRXs in this context.
109 const std::vector
<std::string
> ids
;
111 // Called before an update check, when update metadata is needed.
112 const UpdateEngine::CrxDataCallback
& crx_data_callback
;
114 // Called when there is a state change for any update in this context.
115 const UpdateEngine::NotifyObserversCallback notify_observers_callback
;
117 // Called when the all updates associated with this context have completed.
118 const UpdateEngine::CompletionCallback callback
;
120 // Posts replies back to the main thread.
121 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
;
123 // Runs tasks in a blocking thread pool.
124 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
;
126 // Runs tasks in the same single thread.
127 scoped_refptr
<base::SingleThreadTaskRunner
> single_thread_task_runner
;
129 // Creates instances of UpdateChecker;
130 UpdateChecker::Factory update_checker_factory
;
132 // Creates instances of CrxDownloader;
133 CrxDownloader::Factory crx_downloader_factory
;
135 PingManager
* ping_manager
; // Not owned by this class.
137 scoped_ptr
<Action
> current_action
;
139 // TODO(sorin): use a map instead of vector.
140 std::vector
<CrxUpdateItem
*> update_items
;
142 // Contains the ids of the items to update.
143 std::queue
<std::string
> queue
;
146 } // namespace update_client
148 #endif // COMPONENTS_UPDATE_CLIENT_UPDATE_ENGINE_H_