OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_UPDATE_CLIENT_UPDATE_ENGINE_H_ | 5 #ifndef COMPONENTS_UPDATE_CLIENT_UPDATE_ENGINE_H_ |
6 #define COMPONENTS_UPDATE_CLIENT_UPDATE_ENGINE_H_ | 6 #define COMPONENTS_UPDATE_CLIENT_UPDATE_ENGINE_H_ |
7 | 7 |
| 8 #include <iterator> |
8 #include <list> | 9 #include <list> |
9 #include <map> | 10 #include <map> |
10 #include <memory> | 11 #include <memory> |
11 #include <queue> | 12 #include <queue> |
12 #include <set> | 13 #include <set> |
13 #include <string> | 14 #include <string> |
14 #include <vector> | 15 #include <vector> |
15 | 16 |
16 #include "base/callback.h" | 17 #include "base/callback.h" |
17 #include "base/macros.h" | 18 #include "base/macros.h" |
18 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
19 #include "base/threading/thread_checker.h" | 20 #include "base/threading/thread_checker.h" |
20 #include "components/update_client/action.h" | 21 #include "base/time/time.h" |
| 22 #include "components/update_client/component.h" |
21 #include "components/update_client/component_patcher_operation.h" | 23 #include "components/update_client/component_patcher_operation.h" |
22 #include "components/update_client/crx_downloader.h" | 24 #include "components/update_client/crx_downloader.h" |
23 #include "components/update_client/crx_update_item.h" | 25 #include "components/update_client/crx_update_item.h" |
24 #include "components/update_client/ping_manager.h" | 26 #include "components/update_client/ping_manager.h" |
25 #include "components/update_client/update_checker.h" | 27 #include "components/update_client/update_checker.h" |
26 #include "components/update_client/update_client.h" | 28 #include "components/update_client/update_client.h" |
27 | 29 |
28 namespace base { | 30 namespace base { |
29 class TimeTicks; | 31 class TimeTicks; |
30 class SequencedTaskRunner; | 32 class SequencedTaskRunner; |
31 class SingleThreadTaskRunner; | |
32 } // namespace base | 33 } // namespace base |
33 | 34 |
34 namespace update_client { | 35 namespace update_client { |
35 | 36 |
36 class Configurator; | 37 class Configurator; |
37 struct UpdateContext; | 38 struct UpdateContext; |
38 | 39 |
39 // Handles updates for a group of components. Updates for different groups | 40 // Handles updates for a group of components. Updates for different groups |
40 // are run concurrently but within the same group of components, updates are | 41 // are run concurrently but within the same group of components, updates are |
41 // applied one at a time. | 42 // applied one at a time. |
(...skipping 12 matching lines...) Expand all Loading... |
54 const NotifyObserversCallback& notify_observers_callback); | 55 const NotifyObserversCallback& notify_observers_callback); |
55 ~UpdateEngine(); | 56 ~UpdateEngine(); |
56 | 57 |
57 bool GetUpdateState(const std::string& id, CrxUpdateItem* update_state); | 58 bool GetUpdateState(const std::string& id, CrxUpdateItem* update_state); |
58 | 59 |
59 void Update(bool is_foreground, | 60 void Update(bool is_foreground, |
60 const std::vector<std::string>& ids, | 61 const std::vector<std::string>& ids, |
61 const UpdateClient::CrxDataCallback& crx_data_callback, | 62 const UpdateClient::CrxDataCallback& crx_data_callback, |
62 const Callback& update_callback); | 63 const Callback& update_callback); |
63 | 64 |
| 65 void SendUninstallPing(const std::string& id, |
| 66 const base::Version& version, |
| 67 int reason, |
| 68 const Callback& update_callback); |
| 69 |
64 private: | 70 private: |
65 void UpdateComplete(UpdateContext* update_context, Error error); | 71 using UpdateContexts = std::set<std::unique_ptr<UpdateContext>>; |
| 72 using UpdateContextIterator = UpdateContexts::iterator; |
| 73 |
| 74 void UpdateComplete(const UpdateContextIterator& it, Error error); |
| 75 |
| 76 void ComponentCheckingForUpdatesStart(const UpdateContextIterator& it, |
| 77 const Component& component); |
| 78 void ComponentCheckingForUpdatesComplete(const UpdateContextIterator& it, |
| 79 const Component& component); |
| 80 void UpdateCheckComplete(const UpdateContextIterator& it); |
| 81 |
| 82 void DoUpdateCheck(const UpdateContextIterator& it); |
| 83 void UpdateCheckDone(const UpdateContextIterator& it, |
| 84 int error, |
| 85 int retry_after_sec); |
| 86 |
| 87 void HandleComponent(const UpdateContextIterator& it); |
| 88 void HandleComponentComplete(const UpdateContextIterator& it); |
66 | 89 |
67 // Returns true if the update engine rejects this update call because it | 90 // Returns true if the update engine rejects this update call because it |
68 // occurs too soon. | 91 // occurs too soon. |
69 bool IsThrottled(bool is_foreground) const; | 92 bool IsThrottled(bool is_foreground) const; |
70 | 93 |
| 94 // base::TimeDelta GetNextUpdateDelay(const Component& component) const; |
| 95 |
71 base::ThreadChecker thread_checker_; | 96 base::ThreadChecker thread_checker_; |
72 | 97 |
73 scoped_refptr<Configurator> config_; | 98 scoped_refptr<Configurator> config_; |
74 | 99 |
75 UpdateChecker::Factory update_checker_factory_; | 100 UpdateChecker::Factory update_checker_factory_; |
76 CrxDownloader::Factory crx_downloader_factory_; | 101 CrxDownloader::Factory crx_downloader_factory_; |
77 | 102 |
78 // TODO(sorin): refactor as a ref counted class. | 103 // TODO(sorin): refactor as a ref counted class. |
79 PingManager* ping_manager_; // Not owned by this class. | 104 PingManager* ping_manager_; // Not owned by this class. |
80 | 105 |
81 std::unique_ptr<PersistedData> metadata_; | 106 std::unique_ptr<PersistedData> metadata_; |
82 | 107 |
83 // Called when CRX state changes occur. | 108 // Called when CRX state changes occur. |
84 const NotifyObserversCallback notify_observers_callback_; | 109 const NotifyObserversCallback notify_observers_callback_; |
85 | 110 |
86 // Contains the contexts associated with each update in progress. | 111 // Contains the contexts associated with each update in progress. |
87 std::set<UpdateContext*> update_contexts_; | 112 UpdateContexts update_contexts_; |
88 | 113 |
89 // Implements a rate limiting mechanism for background update checks. Has the | 114 // Implements a rate limiting mechanism for background update checks. Has the |
90 // effect of rejecting the update call if the update call occurs before | 115 // effect of rejecting the update call if the update call occurs before |
91 // a certain time, which is negotiated with the server as part of the | 116 // a certain time, which is negotiated with the server as part of the |
92 // update protocol. See the comments for X-Retry-After header. | 117 // update protocol. See the comments for X-Retry-After header. |
93 base::TimeTicks throttle_updates_until_; | 118 base::TimeTicks throttle_updates_until_; |
94 | 119 |
95 DISALLOW_COPY_AND_ASSIGN(UpdateEngine); | 120 DISALLOW_COPY_AND_ASSIGN(UpdateEngine); |
96 }; | 121 }; |
97 | 122 |
98 // TODO(sorin): consider making this a ref counted type. | 123 // TODO(sorin): consider making this a ref counted type. |
99 struct UpdateContext { | 124 struct UpdateContext { |
100 UpdateContext( | 125 UpdateContext( |
101 const scoped_refptr<Configurator>& config, | 126 const scoped_refptr<Configurator>& config, |
102 bool is_foreground, | 127 bool is_foreground, |
103 const std::vector<std::string>& ids, | 128 const std::vector<std::string>& ids, |
104 const UpdateClient::CrxDataCallback& crx_data_callback, | 129 const UpdateClient::CrxDataCallback& crx_data_callback, |
105 const UpdateEngine::NotifyObserversCallback& notify_observers_callback, | 130 const UpdateEngine::NotifyObserversCallback& notify_observers_callback, |
106 const UpdateEngine::Callback& callback, | 131 const UpdateEngine::Callback& callback, |
107 UpdateChecker::Factory update_checker_factory, | 132 CrxDownloader::Factory crx_downloader_factory); |
108 CrxDownloader::Factory crx_downloader_factory, | |
109 PingManager* ping_manager); | |
110 | 133 |
111 ~UpdateContext(); | 134 ~UpdateContext(); |
112 | 135 |
113 scoped_refptr<Configurator> config; | 136 scoped_refptr<Configurator> config; |
114 | 137 |
115 // True if this update has been initiated by the user. | 138 // True if this update has been initiated by the user. |
116 bool is_foreground; | 139 bool is_foreground = false; |
117 | 140 |
118 // True if the component updates are enabled in this context. | 141 // True if the component updates are enabled in this context. |
119 const bool enabled_component_updates; | 142 const bool enabled_component_updates; |
120 | 143 |
121 // Contains the ids of all CRXs in this context. | 144 // Contains the ids of all CRXs in this context. |
122 const std::vector<std::string> ids; | 145 const std::vector<std::string> ids; |
123 | 146 |
124 // Called before an update check, when update metadata is needed. | 147 // Called before an update check, when update metadata is needed. |
125 const UpdateEngine::CrxDataCallback& crx_data_callback; | 148 const UpdateEngine::CrxDataCallback& crx_data_callback; |
126 | 149 |
127 // Called when there is a state change for any update in this context. | 150 // Called when there is a state change for any update in this context. |
128 const UpdateEngine::NotifyObserversCallback notify_observers_callback; | 151 const UpdateEngine::NotifyObserversCallback notify_observers_callback; |
129 | 152 |
130 // Called when the all updates associated with this context have completed. | 153 // Called when the all updates associated with this context have completed. |
131 const UpdateEngine::Callback callback; | 154 const UpdateEngine::Callback callback; |
132 | 155 |
133 // Posts replies back to the main thread. | |
134 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner; | |
135 | |
136 // Runs tasks in a blocking thread pool. | 156 // Runs tasks in a blocking thread pool. |
137 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner; | 157 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner; |
138 | 158 |
139 // Creates instances of UpdateChecker; | |
140 UpdateChecker::Factory update_checker_factory; | |
141 | |
142 // Creates instances of CrxDownloader; | 159 // Creates instances of CrxDownloader; |
143 CrxDownloader::Factory crx_downloader_factory; | 160 CrxDownloader::Factory crx_downloader_factory; |
144 | 161 |
145 PingManager* ping_manager; // Not owned by this class. | 162 std::unique_ptr<UpdateChecker> update_checker; |
146 | |
147 std::unique_ptr<Action> current_action; | |
148 | |
149 // Contains the CrxUpdateItem instances of the items to update. | |
150 IdToCrxUpdateItemMap update_items; | |
151 | |
152 // Contains the ids of the items to update. | |
153 std::queue<std::string> queue; | |
154 | 163 |
155 // The time in seconds to wait until doing further update checks. | 164 // The time in seconds to wait until doing further update checks. |
156 int retry_after_sec; | 165 int retry_after_sec = 0; |
| 166 |
| 167 int update_check_error = 0; |
| 168 size_t num_components_ready_to_check = 0; |
| 169 size_t num_components_checked = 0; |
| 170 |
| 171 IdToComponentPtrMap components; |
| 172 |
| 173 std::queue<std::string> component_queue; |
| 174 |
| 175 // The time to wait before handling the update for a component. |
| 176 // The wait time is proportional with the cost incurred by updating |
| 177 // the component. The more time it takes to download and apply the |
| 178 // update for the current component, the longer the wait until the engine |
| 179 // is handling the next component in the queue. |
| 180 base::TimeDelta next_update_delay; |
| 181 |
| 182 private: |
| 183 DISALLOW_COPY_AND_ASSIGN(UpdateContext); |
157 }; | 184 }; |
158 | 185 |
159 } // namespace update_client | 186 } // namespace update_client |
160 | 187 |
161 #endif // COMPONENTS_UPDATE_CLIENT_UPDATE_ENGINE_H_ | 188 #endif // COMPONENTS_UPDATE_CLIENT_UPDATE_ENGINE_H_ |
OLD | NEW |