Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(396)

Side by Side Diff: chrome/browser/component_updater/component_updater_service.cc

Issue 15908002: Differential updates for components. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Sync to LKGR revision 207804. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "chrome/browser/component_updater/component_updater_service.h" 5 #include "chrome/browser/component_updater/component_updater_service.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/at_exit.h" 11 #include "base/at_exit.h"
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/compiler_specific.h"
13 #include "base/file_util.h" 14 #include "base/file_util.h"
14 #include "base/files/file_path.h" 15 #include "base/files/file_path.h"
15 #include "base/logging.h" 16 #include "base/logging.h"
16 #include "base/memory/scoped_ptr.h" 17 #include "base/memory/scoped_ptr.h"
17 #include "base/stl_util.h" 18 #include "base/stl_util.h"
18 #include "base/strings/string_number_conversions.h" 19 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_piece.h" 20 #include "base/strings/string_piece.h"
20 #include "base/strings/string_util.h" 21 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h" 22 #include "base/strings/stringprintf.h"
22 #include "base/timer.h" 23 #include "base/timer.h"
23 #include "chrome/browser/browser_process.h" 24 #include "chrome/browser/browser_process.h"
25 #include "chrome/browser/component_updater/component_patcher.h"
24 #include "chrome/browser/component_updater/component_unpacker.h" 26 #include "chrome/browser/component_updater/component_unpacker.h"
25 #include "chrome/common/chrome_notification_types.h" 27 #include "chrome/common/chrome_notification_types.h"
26 #include "chrome/common/chrome_utility_messages.h" 28 #include "chrome/common/chrome_utility_messages.h"
27 #include "chrome/common/chrome_version_info.h" 29 #include "chrome/common/chrome_version_info.h"
28 #include "chrome/common/extensions/extension.h" 30 #include "chrome/common/extensions/extension.h"
31 #include "chrome/common/omaha_query_params/omaha_query_params.h"
29 #include "content/public/browser/browser_thread.h" 32 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/notification_service.h" 33 #include "content/public/browser/notification_service.h"
31 #include "content/public/browser/utility_process_host.h" 34 #include "content/public/browser/utility_process_host.h"
32 #include "content/public/browser/utility_process_host_client.h" 35 #include "content/public/browser/utility_process_host_client.h"
33 #include "googleurl/src/gurl.h" 36 #include "googleurl/src/gurl.h"
34 #include "net/base/escape.h" 37 #include "net/base/escape.h"
35 #include "net/base/load_flags.h" 38 #include "net/base/load_flags.h"
36 #include "net/base/net_errors.h" 39 #include "net/base/net_errors.h"
37 #include "net/url_request/url_fetcher.h" 40 #include "net/url_request/url_fetcher.h"
38 #include "net/url_request/url_fetcher_delegate.h" 41 #include "net/url_request/url_fetcher_delegate.h"
39 #include "net/url_request/url_request_status.h" 42 #include "net/url_request/url_request_status.h"
40 43
41 using content::BrowserThread; 44 using content::BrowserThread;
42 using content::UtilityProcessHost; 45 using content::UtilityProcessHost;
43 using content::UtilityProcessHostClient; 46 using content::UtilityProcessHostClient;
44 using extensions::Extension; 47 using extensions::Extension;
45 48
46 // The component updater is designed to live until process shutdown, so 49 // The component updater is designed to live until process shutdown, so
47 // base::Bind() calls are not refcounted. 50 // base::Bind() calls are not refcounted.
48 51
49 namespace { 52 namespace {
53
50 // Manifest sources, from most important to least important. 54 // Manifest sources, from most important to least important.
51 const CrxComponent::UrlSource kManifestSources[] = { 55 const CrxComponent::UrlSource kManifestSources[] = {
52 CrxComponent::BANDAID, 56 CrxComponent::BANDAID,
53 CrxComponent::CWS_PUBLIC, 57 CrxComponent::CWS_PUBLIC,
54 CrxComponent::CWS_SANDBOX 58 CrxComponent::CWS_SANDBOX,
55 }; 59 };
56 60
57 // Extends an omaha compatible update check url |query| string. Does 61 // Extends an omaha compatible update check url |query| string. Does
58 // not mutate the string if it would be longer than |limit| chars. 62 // not mutate the string if it would be longer than |limit| chars.
59 bool AddQueryString(const std::string& id, 63 bool AddQueryString(const std::string& id,
60 const std::string& version, 64 const std::string& version,
65 const std::string& fingerprint,
61 size_t limit, 66 size_t limit,
62 std::string* query) { 67 std::string* query) {
63 std::string additional = 68 std::string additional =
64 base::StringPrintf("id=%s&v=%s&uc", id.c_str(), version.c_str()); 69 base::StringPrintf("id=%s&v=%s&fp=%s&uc",
70 id.c_str(), version.c_str(), fingerprint.c_str());
65 additional = "x=" + net::EscapeQueryParamValue(additional, true); 71 additional = "x=" + net::EscapeQueryParamValue(additional, true);
66 if ((additional.size() + query->size() + 1) > limit) 72 if ((additional.size() + query->size() + 1) > limit)
67 return false; 73 return false;
68 if (!query->empty()) 74 if (!query->empty())
69 query->append(1, '&'); 75 query->append(1, '&');
70 query->append(additional); 76 query->append(additional);
71 return true; 77 return true;
72 } 78 }
73 79
74 // Create the final omaha compatible query. The |extra| is optional and can 80 // Create the final omaha compatible query. The |extra| is optional and can
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 net::LOAD_DISABLE_CACHE); 162 net::LOAD_DISABLE_CACHE);
157 // TODO(cpu): Define our retry and backoff policy. 163 // TODO(cpu): Define our retry and backoff policy.
158 fetcher->SetAutomaticallyRetryOn5xx(false); 164 fetcher->SetAutomaticallyRetryOn5xx(false);
159 if (save_to_file) { 165 if (save_to_file) {
160 fetcher->SaveResponseToTemporaryFile( 166 fetcher->SaveResponseToTemporaryFile(
161 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); 167 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
162 } 168 }
163 fetcher->Start(); 169 fetcher->Start();
164 } 170 }
165 171
166 // Returs true if the url request of |fetcher| was succesful. 172 // Returns true if the url request of |fetcher| was succesful.
167 bool FetchSuccess(const net::URLFetcher& fetcher) { 173 bool FetchSuccess(const net::URLFetcher& fetcher) {
168 return (fetcher.GetStatus().status() == net::URLRequestStatus::SUCCESS) && 174 return (fetcher.GetStatus().status() == net::URLRequestStatus::SUCCESS) &&
169 (fetcher.GetResponseCode() == 200); 175 (fetcher.GetResponseCode() == 200);
170 } 176 }
171 177
172 // This is the one and only per-item state structure. Designed to be hosted 178 // This is the one and only per-item state structure. Designed to be hosted
173 // in a std::vector or a std::list. The two main members are |component| 179 // in a std::vector or a std::list. The two main members are |component|
174 // which is supplied by the the component updater client and |status| which 180 // which is supplied by the the component updater client and |status| which
175 // is modified as the item is processed by the update pipeline. The expected 181 // is modified as the item is processed by the update pipeline. The expected
176 // transition graph is: 182 // transition graph is:
177 // error error error 183 //
178 // +--kNoUpdate<------<-------+------<------+------<------+ 184 // kNew
179 // | | | | 185 // |
180 // V yes | | | 186 // V
181 // kNew --->kChecking-->[update?]----->kCanUpdate-->kDownloading-->kUpdating 187 // +----------------------> kChecking -<---------+-----<-------+
182 // ^ | | 188 // | | | |
183 // | |no | 189 // | error V no | |
184 // |--kUpToDate<---+ | 190 // kNoUpdate <---------------- [update?] ->---- kUpToDate kUpdated
185 // | success | 191 // ^ | ^
186 // +--kUpdated<-------------------------------------------+ 192 // | yes | |
193 // | diff=false V |
194 // | +-----------> kCanUpdate |
195 // | | | |
196 // | | V no |
197 // | | [differential update?]->----+ |
198 // | | | | |
199 // | | yes | | |
200 // | | error V | |
201 // | +---------<- kDownloadingDiff | |
202 // | | | | |
203 // | | | | |
204 // | | error V | |
205 // | +---------<- kUpdatingDiff ->--------|-----------+ success
206 // | | |
207 // | error V |
208 // +----------------------------------------- kDownloading |
209 // | | |
210 // | error V |
211 // +------------------------------------------ kUpdating ->----+ success
187 // 212 //
188 struct CrxUpdateItem { 213 struct CrxUpdateItem {
189 enum Status { 214 enum Status {
190 kNew, 215 kNew,
191 kChecking, 216 kChecking,
192 kCanUpdate, 217 kCanUpdate,
218 kDownloadingDiff,
193 kDownloading, 219 kDownloading,
220 kUpdatingDiff,
194 kUpdating, 221 kUpdating,
195 kUpdated, 222 kUpdated,
196 kUpToDate, 223 kUpToDate,
197 kNoUpdate, 224 kNoUpdate,
198 kLastStatus 225 kLastStatus
199 }; 226 };
200 227
201 Status status; 228 Status status;
229 std::string id;
230 CrxComponent component;
231
232 base::Time last_check;
233
234 // These members are initialized with their corresponding values from the
235 // update server response.
202 GURL crx_url; 236 GURL crx_url;
203 std::string id; 237 GURL diff_crx_url;
204 base::Time last_check; 238 int size;
205 CrxComponent component; 239 int diff_size;
240
241 // The from/to version and fingerprint values.
242 Version previous_version;
206 Version next_version; 243 Version next_version;
244 std::string previous_fp;
245 std::string next_fp;
207 246
208 CrxUpdateItem() : status(kNew) {} 247 // True if the differential update failed for any reason.
248 bool diff_update_failed;
249
250 CrxUpdateItem()
251 : status(kNew),
252 size(0),
253 diff_size(0),
254 diff_update_failed(false) {
255 }
209 256
210 // Function object used to find a specific component. 257 // Function object used to find a specific component.
211 class FindById { 258 class FindById {
212 public: 259 public:
213 explicit FindById(const std::string& id) : id_(id) {} 260 explicit FindById(const std::string& id) : id_(id) {}
214 261
215 bool operator() (CrxUpdateItem* item) const { 262 bool operator() (CrxUpdateItem* item) const {
216 return (item->id == id_); 263 return (item->id == id_);
217 } 264 }
218 private: 265 private:
219 const std::string& id_; 266 const std::string& id_;
220 }; 267 };
221 }; 268 };
222 269
270 // Returns true if a differential update is available for the update item.
271 bool IsDiffUpdateAvailable(const CrxUpdateItem* update_item) {
272 return update_item->diff_crx_url.is_valid();
273 }
274
275 // Returns true if a differential update is available, it has not failed yet,
276 // and the configuration allows it.
277 bool CanTryDiffUpdate(const CrxUpdateItem* update_item,
278 const ComponentUpdateService::Configurator& config) {
279 return IsDiffUpdateAvailable(update_item) &&
280 !update_item->diff_update_failed &&
281 config.DeltasEnabled();
282 }
283
223 } // namespace. 284 } // namespace.
224 285
225 typedef ComponentUpdateService::Configurator Config;
226
227 CrxComponent::CrxComponent() 286 CrxComponent::CrxComponent()
228 : installer(NULL), 287 : installer(NULL),
229 source(BANDAID) { 288 source(BANDAID) {
230 } 289 }
231 290
232 CrxComponent::~CrxComponent() { 291 CrxComponent::~CrxComponent() {
233 } 292 }
234 293
235 ////////////////////////////////////////////////////////////////////////////// 294 //////////////////////////////////////////////////////////////////////////////
236 // The one and only implementation of the ComponentUpdateService interface. In 295 // The one and only implementation of the ComponentUpdateService interface. In
237 // charge of running the show. The main method is ProcessPendingItems() which 296 // charge of running the show. The main method is ProcessPendingItems() which
238 // is called periodically to do the upgrades/installs or the update checks. 297 // is called periodically to do the upgrades/installs or the update checks.
239 // An important consideration here is to be as "low impact" as we can to the 298 // An important consideration here is to be as "low impact" as we can to the
240 // rest of the browser, so even if we have many components registered and 299 // rest of the browser, so even if we have many components registered and
241 // eligible for update, we only do one thing at a time with pauses in between 300 // eligible for update, we only do one thing at a time with pauses in between
242 // the tasks. Also when we do network requests there is only one |url_fetcher_| 301 // the tasks. Also when we do network requests there is only one |url_fetcher_|
243 // in flight at at a time. 302 // in flight at a time.
244 // There are no locks in this code, the main structure |work_items_| is mutated 303 // There are no locks in this code, the main structure |work_items_| is mutated
245 // only from the UI thread. The unpack and installation is done in the file 304 // only from the UI thread. The unpack and installation is done in the file
246 // thread and the network requests are done in the IO thread and in the file 305 // thread and the network requests are done in the IO thread and in the file
247 // thread. 306 // thread.
248 class CrxUpdateService : public ComponentUpdateService { 307 class CrxUpdateService : public ComponentUpdateService {
249 public: 308 public:
250 explicit CrxUpdateService(ComponentUpdateService::Configurator* config); 309 explicit CrxUpdateService(ComponentUpdateService::Configurator* config);
251 310
252 virtual ~CrxUpdateService(); 311 virtual ~CrxUpdateService();
253 312
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 struct UpdateContext { 356 struct UpdateContext {
298 base::Time start; 357 base::Time start;
299 UpdateContext() : start(base::Time::Now()) {} 358 UpdateContext() : start(base::Time::Now()) {}
300 }; 359 };
301 360
302 // Context for a crx download url request. See DelegateWithContext above. 361 // Context for a crx download url request. See DelegateWithContext above.
303 struct CRXContext { 362 struct CRXContext {
304 ComponentInstaller* installer; 363 ComponentInstaller* installer;
305 std::vector<uint8> pk_hash; 364 std::vector<uint8> pk_hash;
306 std::string id; 365 std::string id;
366 std::string fingerprint;
307 CRXContext() : installer(NULL) {} 367 CRXContext() : installer(NULL) {}
308 }; 368 };
309 369
310 void OnURLFetchComplete(const net::URLFetcher* source, 370 void OnURLFetchComplete(const net::URLFetcher* source,
311 UpdateContext* context); 371 UpdateContext* context);
312 372
313 void OnURLFetchComplete(const net::URLFetcher* source, 373 void OnURLFetchComplete(const net::URLFetcher* source,
314 CRXContext* context); 374 CRXContext* context);
315 375
316 private: 376 private:
317 // See ManifestParserBridge. 377 // See ManifestParserBridge.
318 void OnParseUpdateManifestSucceeded( 378 void OnParseUpdateManifestSucceeded(
319 const UpdateManifest::Results& results); 379 const UpdateManifest::Results& results);
320 380
321 // See ManifestParserBridge. 381 // See ManifestParserBridge.
322 void OnParseUpdateManifestFailed( 382 void OnParseUpdateManifestFailed(const std::string& error_message);
323 const std::string& error_message);
324 383
325 bool AddItemToUpdateCheck(CrxUpdateItem* item, std::string* query); 384 bool AddItemToUpdateCheck(CrxUpdateItem* item, std::string* query);
326 385
327 void ProcessPendingItems(); 386 void ProcessPendingItems();
328 387
329 void ScheduleNextRun(bool step_delay); 388 void ScheduleNextRun(bool step_delay);
330 389
331 void ParseManifest(const std::string& xml); 390 void ParseManifest(const std::string& xml);
332 391
333 void Install(const CRXContext* context, const base::FilePath& crx_path); 392 void Install(const CRXContext* context, const base::FilePath& crx_path);
334 393
335 void DoneInstalling(const std::string& component_id, 394 void DoneInstalling(const std::string& component_id,
336 ComponentUnpacker::Error error); 395 ComponentUnpacker::Error error,
396 int extended_error);
337 397
338 size_t ChangeItemStatus(CrxUpdateItem::Status from, 398 size_t ChangeItemStatus(CrxUpdateItem::Status from,
339 CrxUpdateItem::Status to); 399 CrxUpdateItem::Status to);
340 400
341 CrxUpdateItem* FindUpdateItemById(const std::string& id); 401 CrxUpdateItem* FindUpdateItemById(const std::string& id);
342 402
343 scoped_ptr<Config> config_; 403 scoped_ptr<ComponentUpdateService::Configurator> config_;
404
405 scoped_ptr<ComponentPatcher> component_patcher_;
344 406
345 scoped_ptr<net::URLFetcher> url_fetcher_; 407 scoped_ptr<net::URLFetcher> url_fetcher_;
346 408
409 // A collection of every work item.
347 typedef std::vector<CrxUpdateItem*> UpdateItems; 410 typedef std::vector<CrxUpdateItem*> UpdateItems;
348 // A collection of every work item.
349 UpdateItems work_items_; 411 UpdateItems work_items_;
350 412
351 // A particular set of items from work_items_, which should be checked ASAP. 413 // A particular set of items from work_items_, which should be checked ASAP.
352 std::set<CrxUpdateItem*> requested_work_items_; 414 std::set<CrxUpdateItem*> requested_work_items_;
353 415
354 base::OneShotTimer<CrxUpdateService> timer_; 416 base::OneShotTimer<CrxUpdateService> timer_;
355 417
356 Version chrome_version_; 418 const Version chrome_version_;
419 const std::string prod_id_;
357 420
358 bool running_; 421 bool running_;
359 422
360 DISALLOW_COPY_AND_ASSIGN(CrxUpdateService); 423 DISALLOW_COPY_AND_ASSIGN(CrxUpdateService);
361 }; 424 };
362 425
363 ////////////////////////////////////////////////////////////////////////////// 426 //////////////////////////////////////////////////////////////////////////////
364 427
365 CrxUpdateService::CrxUpdateService( 428 CrxUpdateService::CrxUpdateService(ComponentUpdateService::Configurator* config)
366 ComponentUpdateService::Configurator* config)
367 : config_(config), 429 : config_(config),
430 component_patcher_(config->CreateComponentPatcher()),
368 chrome_version_(chrome::VersionInfo().Version()), 431 chrome_version_(chrome::VersionInfo().Version()),
432 prod_id_(chrome::OmahaQueryParams::GetProdIdString(
433 chrome::OmahaQueryParams::CHROME)),
369 running_(false) { 434 running_(false) {
370 } 435 }
371 436
372 CrxUpdateService::~CrxUpdateService() { 437 CrxUpdateService::~CrxUpdateService() {
373 // Because we are a singleton, at this point only the UI thread should be 438 // Because we are a singleton, at this point only the UI thread should be
374 // alive, this simplifies the management of the work that could be in 439 // alive, this simplifies the management of the work that could be in
375 // flight in other threads. 440 // flight in other threads.
376 Stop(); 441 Stop();
377 STLDeleteElements(&work_items_); 442 STLDeleteElements(&work_items_);
378 } 443 }
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 CrxUpdateItem::FindById finder(id); 508 CrxUpdateItem::FindById finder(id);
444 UpdateItems::iterator it = std::find_if(work_items_.begin(), 509 UpdateItems::iterator it = std::find_if(work_items_.begin(),
445 work_items_.end(), 510 work_items_.end(),
446 finder); 511 finder);
447 if (it == work_items_.end()) 512 if (it == work_items_.end())
448 return NULL; 513 return NULL;
449 return (*it); 514 return (*it);
450 } 515 }
451 516
452 // Changes all the components in |work_items_| that have |from| status to 517 // Changes all the components in |work_items_| that have |from| status to
453 // |to| statatus and returns how many have been changed. 518 // |to| status and returns how many have been changed.
454 size_t CrxUpdateService::ChangeItemStatus(CrxUpdateItem::Status from, 519 size_t CrxUpdateService::ChangeItemStatus(CrxUpdateItem::Status from,
455 CrxUpdateItem::Status to) { 520 CrxUpdateItem::Status to) {
456 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 521 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
457 size_t count = 0; 522 size_t count = 0;
458 for (UpdateItems::iterator it = work_items_.begin(); 523 for (UpdateItems::iterator it = work_items_.begin();
459 it != work_items_.end(); ++it) { 524 it != work_items_.end(); ++it) {
460 CrxUpdateItem* item = *it; 525 CrxUpdateItem* item = *it;
461 if (item->status != from) 526 if (item->status != from)
462 continue; 527 continue;
463 item->status = to; 528 item->status = to;
(...skipping 20 matching lines...) Expand all
484 CrxUpdateItem* uit; 549 CrxUpdateItem* uit;
485 uit = FindUpdateItemById(id); 550 uit = FindUpdateItemById(id);
486 if (uit) { 551 if (uit) {
487 uit->component = component; 552 uit->component = component;
488 return kReplaced; 553 return kReplaced;
489 } 554 }
490 555
491 uit = new CrxUpdateItem; 556 uit = new CrxUpdateItem;
492 uit->id.swap(id); 557 uit->id.swap(id);
493 uit->component = component; 558 uit->component = component;
559
494 work_items_.push_back(uit); 560 work_items_.push_back(uit);
495 // If this is the first component registered we call Start to 561 // If this is the first component registered we call Start to
496 // schedule the first timer. 562 // schedule the first timer.
497 if (running_ && (work_items_.size() == 1)) 563 if (running_ && (work_items_.size() == 1))
498 Start(); 564 Start();
499 565
500 return kOk; 566 return kOk;
501 } 567 }
502 568
503 // Sets a component to be checked for updates. 569 // Sets a component to be checked for updates.
504 // The componet to add is |crxit| and the |query| string is modified with the 570 // The component to add is |crxit| and the |query| string is modified with the
505 // required omaha compatible query. Returns false when the query strings 571 // required omaha compatible query. Returns false when the query strings
506 // is longer than specified by UrlSizeLimit(). 572 // is longer than specified by UrlSizeLimit().
507 bool CrxUpdateService::AddItemToUpdateCheck(CrxUpdateItem* item, 573 bool CrxUpdateService::AddItemToUpdateCheck(CrxUpdateItem* item,
508 std::string* query) { 574 std::string* query) {
509 if (!AddQueryString(item->id, 575 if (!AddQueryString(item->id,
510 item->component.version.GetString(), 576 item->component.version.GetString(),
577 item->component.fingerprint,
511 config_->UrlSizeLimit(), query)) 578 config_->UrlSizeLimit(), query))
512 return false; 579 return false;
580
513 item->status = CrxUpdateItem::kChecking; 581 item->status = CrxUpdateItem::kChecking;
514 item->last_check = base::Time::Now(); 582 item->last_check = base::Time::Now();
583 item->previous_version = item->component.version;
584 item->next_version = Version();
585 item->previous_fp = item->component.fingerprint;
586 item->next_fp.clear();
587 item->diff_update_failed = false;
515 return true; 588 return true;
516 } 589 }
517 590
518 // Start the process of checking for an update, for a particular component 591 // Start the process of checking for an update, for a particular component
519 // that was previously registered. 592 // that was previously registered.
520 ComponentUpdateService::Status CrxUpdateService::CheckForUpdateSoon( 593 ComponentUpdateService::Status CrxUpdateService::CheckForUpdateSoon(
521 const CrxComponent& component) { 594 const CrxComponent& component) {
522 if (component.pk_hash.empty() || 595 if (component.pk_hash.empty() ||
523 !component.version.IsValid() || 596 !component.version.IsValid() ||
524 !component.installer) 597 !component.installer)
525 return kError; 598 return kError;
526 599
527 std::string id = 600 std::string id =
528 HexStringToID(StringToLowerASCII(base::HexEncode(&component.pk_hash[0], 601 HexStringToID(StringToLowerASCII(base::HexEncode(&component.pk_hash[0],
529 component.pk_hash.size()/2))); 602 component.pk_hash.size()/2)));
530 603
531 CrxUpdateItem* uit; 604 CrxUpdateItem* uit;
532 uit = FindUpdateItemById(id); 605 uit = FindUpdateItemById(id);
533 if (!uit) 606 if (!uit)
534 return kError; 607 return kError;
535 608
536 // Check if the request is too soon. 609 // Check if the request is too soon.
537 base::TimeDelta delta = base::Time::Now() - uit->last_check; 610 base::TimeDelta delta = base::Time::Now() - uit->last_check;
538 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay())) { 611 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay()))
539 return kError; 612 return kError;
540 }
541 613
542 switch (uit->status) { 614 switch (uit->status) {
543 // If the item is already in the process of being updated, there is 615 // If the item is already in the process of being updated, there is
544 // no point in this call, so return kInProgress. 616 // no point in this call, so return kInProgress.
545 case CrxUpdateItem::kChecking: 617 case CrxUpdateItem::kChecking:
546 case CrxUpdateItem::kCanUpdate: 618 case CrxUpdateItem::kCanUpdate:
619 case CrxUpdateItem::kDownloadingDiff:
547 case CrxUpdateItem::kDownloading: 620 case CrxUpdateItem::kDownloading:
621 case CrxUpdateItem::kUpdatingDiff:
548 case CrxUpdateItem::kUpdating: 622 case CrxUpdateItem::kUpdating:
549 return kInProgress; 623 return kInProgress;
550 // Otherwise the item was already checked a while back (or it is new), 624 // Otherwise the item was already checked a while back (or it is new),
551 // set its status to kNew to give it a slightly higher priority. 625 // set its status to kNew to give it a slightly higher priority.
552 case CrxUpdateItem::kNew: 626 case CrxUpdateItem::kNew:
553 case CrxUpdateItem::kUpdated: 627 case CrxUpdateItem::kUpdated:
554 case CrxUpdateItem::kUpToDate: 628 case CrxUpdateItem::kUpToDate:
555 case CrxUpdateItem::kNoUpdate: 629 case CrxUpdateItem::kNoUpdate:
556 uit->status = CrxUpdateItem::kNew; 630 uit->status = CrxUpdateItem::kNew;
557 requested_work_items_.insert(uit); 631 requested_work_items_.insert(uit);
(...skipping 18 matching lines...) Expand all
576 void CrxUpdateService::ProcessPendingItems() { 650 void CrxUpdateService::ProcessPendingItems() {
577 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 651 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
578 // First check for ready upgrades and do one. The first 652 // First check for ready upgrades and do one. The first
579 // step is to fetch the crx package. 653 // step is to fetch the crx package.
580 for (UpdateItems::const_iterator it = work_items_.begin(); 654 for (UpdateItems::const_iterator it = work_items_.begin();
581 it != work_items_.end(); ++it) { 655 it != work_items_.end(); ++it) {
582 CrxUpdateItem* item = *it; 656 CrxUpdateItem* item = *it;
583 if (item->status != CrxUpdateItem::kCanUpdate) 657 if (item->status != CrxUpdateItem::kCanUpdate)
584 continue; 658 continue;
585 // Found component to update, start the process. 659 // Found component to update, start the process.
586 item->status = CrxUpdateItem::kDownloading;
587 CRXContext* context = new CRXContext; 660 CRXContext* context = new CRXContext;
588 context->pk_hash = item->component.pk_hash; 661 context->pk_hash = item->component.pk_hash;
589 context->id = item->id; 662 context->id = item->id;
590 context->installer = item->component.installer; 663 context->installer = item->component.installer;
664 context->fingerprint = item->next_fp;
665 GURL package_url;
666 if (CanTryDiffUpdate(item, *config_)) {
667 package_url = item->diff_crx_url;
668 item->status = CrxUpdateItem::kDownloadingDiff;
669 } else {
670 package_url = item->crx_url;
671 item->status = CrxUpdateItem::kDownloading;
672 }
591 url_fetcher_.reset(net::URLFetcher::Create( 673 url_fetcher_.reset(net::URLFetcher::Create(
592 0, item->crx_url, net::URLFetcher::GET, 674 0, package_url, net::URLFetcher::GET,
593 MakeContextDelegate(this, context))); 675 MakeContextDelegate(this, context)));
594 StartFetch(url_fetcher_.get(), config_->RequestContext(), true); 676 StartFetch(url_fetcher_.get(), config_->RequestContext(), true);
595 return; 677 return;
596 } 678 }
597 679
598 for (size_t ix = 0; ix != arraysize(kManifestSources); ++ix) { 680 for (size_t ix = 0; ix != arraysize(kManifestSources); ++ix) {
599 const CrxComponent::UrlSource manifest_source = kManifestSources[ix]; 681 const CrxComponent::UrlSource manifest_source = kManifestSources[ix];
600 682
601 std::string query; 683 std::string query;
602 // If no pending upgrades, we check if there are new components we have not 684 // If no pending upgrades, we check if there are new components we have not
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
693 delete context; 775 delete context;
694 } 776 }
695 777
696 // Parsing the manifest is either done right now for tests or in a sandboxed 778 // Parsing the manifest is either done right now for tests or in a sandboxed
697 // process for the production environment. This mitigates the case where an 779 // process for the production environment. This mitigates the case where an
698 // attacker was able to feed us a malicious xml string. 780 // attacker was able to feed us a malicious xml string.
699 void CrxUpdateService::ParseManifest(const std::string& xml) { 781 void CrxUpdateService::ParseManifest(const std::string& xml) {
700 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 782 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
701 if (config_->InProcess()) { 783 if (config_->InProcess()) {
702 UpdateManifest manifest; 784 UpdateManifest manifest;
703 if (!manifest.Parse(xml)) { 785 if (!manifest.Parse(xml))
704 CrxUpdateService::OnParseUpdateManifestFailed(manifest.errors()); 786 CrxUpdateService::OnParseUpdateManifestFailed(manifest.errors());
705 } else { 787 else
706 CrxUpdateService::OnParseUpdateManifestSucceeded(manifest.results()); 788 CrxUpdateService::OnParseUpdateManifestSucceeded(manifest.results());
707 }
708 } else { 789 } else {
709 UtilityProcessHost* host = 790 UtilityProcessHost* host =
710 UtilityProcessHost::Create(new ManifestParserBridge(this), 791 UtilityProcessHost::Create(new ManifestParserBridge(this),
711 base::MessageLoopProxy::current().get()); 792 base::MessageLoopProxy::current().get());
712 host->EnableZygote(); 793 host->EnableZygote();
713 host->Send(new ChromeUtilityMsg_ParseUpdateManifest(xml)); 794 host->Send(new ChromeUtilityMsg_ParseUpdateManifest(xml));
714 } 795 }
715 } 796 }
716 797
717 // A valid Omaha update check has arrived, from only the list of components that 798 // A valid Omaha update check has arrived, from only the list of components that
(...skipping 28 matching lines...) Expand all
746 if (!it->browser_min_version.empty()) { 827 if (!it->browser_min_version.empty()) {
747 if (IsVersionNewer(chrome_version_, it->browser_min_version)) { 828 if (IsVersionNewer(chrome_version_, it->browser_min_version)) {
748 // Does not apply for this chrome version. 829 // Does not apply for this chrome version.
749 crx->status = CrxUpdateItem::kNoUpdate; 830 crx->status = CrxUpdateItem::kNoUpdate;
750 continue; 831 continue;
751 } 832 }
752 } 833 }
753 // All test passed. Queue an upgrade for this component and fire the 834 // All test passed. Queue an upgrade for this component and fire the
754 // notifications. 835 // notifications.
755 crx->crx_url = it->crx_url; 836 crx->crx_url = it->crx_url;
837 crx->size = it->size;
838 crx->diff_crx_url = it->diff_crx_url;
839 crx->diff_size = it->diff_size;
756 crx->status = CrxUpdateItem::kCanUpdate; 840 crx->status = CrxUpdateItem::kCanUpdate;
757 crx->next_version = Version(it->version); 841 crx->next_version = Version(it->version);
842 crx->next_fp = it->package_fingerprint;
758 ++update_pending; 843 ++update_pending;
759 844
760 content::NotificationService::current()->Notify( 845 content::NotificationService::current()->Notify(
761 chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND, 846 chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND,
762 content::Source<std::string>(&crx->id), 847 content::Source<std::string>(&crx->id),
763 content::NotificationService::NoDetails()); 848 content::NotificationService::NoDetails());
764 } 849 }
765 850
766 // All the components that are not mentioned in the manifest we 851 // All the components that are not mentioned in the manifest we
767 // consider them up to date. 852 // consider them up to date.
(...skipping 14 matching lines...) Expand all
782 } 867 }
783 868
784 // Called when the CRX package has been downloaded to a temporary location. 869 // Called when the CRX package has been downloaded to a temporary location.
785 // Here we fire the notifications and schedule the component-specific installer 870 // Here we fire the notifications and schedule the component-specific installer
786 // to be called in the file thread. 871 // to be called in the file thread.
787 void CrxUpdateService::OnURLFetchComplete(const net::URLFetcher* source, 872 void CrxUpdateService::OnURLFetchComplete(const net::URLFetcher* source,
788 CRXContext* context) { 873 CRXContext* context) {
789 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 874 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
790 int error_code = net::OK; 875 int error_code = net::OK;
791 876
877 CrxUpdateItem* crx = FindUpdateItemById(context->id);
878 DCHECK(crx->status == CrxUpdateItem::kDownloadingDiff ||
879 crx->status == CrxUpdateItem::kDownloading);
880
792 if (source->FileErrorOccurred(&error_code) || !FetchSuccess(*source)) { 881 if (source->FileErrorOccurred(&error_code) || !FetchSuccess(*source)) {
882 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
883 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
884 CrxUpdateItem::kCanUpdate);
885 DCHECK_EQ(count, 1ul);
886 ScheduleNextRun(true);
887 return;
888 }
793 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloading, 889 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloading,
794 CrxUpdateItem::kNoUpdate); 890 CrxUpdateItem::kNoUpdate);
795 DCHECK_EQ(count, 1ul); 891 DCHECK_EQ(count, 1ul);
796 config_->OnEvent(Configurator::kNetworkError, CrxIdtoUMAId(context->id)); 892 config_->OnEvent(Configurator::kNetworkError, CrxIdtoUMAId(context->id));
797 url_fetcher_.reset(); 893 url_fetcher_.reset();
894
798 ScheduleNextRun(false); 895 ScheduleNextRun(false);
799 } else { 896 } else {
800 base::FilePath temp_crx_path; 897 base::FilePath temp_crx_path;
801 CHECK(source->GetResponseAsFilePath(true, &temp_crx_path)); 898 CHECK(source->GetResponseAsFilePath(true, &temp_crx_path));
802 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloading, 899
803 CrxUpdateItem::kUpdating); 900 size_t count = 0;
901 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
902 count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
903 CrxUpdateItem::kUpdatingDiff);
904 } else {
905 count = ChangeItemStatus(CrxUpdateItem::kDownloading,
906 CrxUpdateItem::kUpdating);
907 }
804 DCHECK_EQ(count, 1ul); 908 DCHECK_EQ(count, 1ul);
909
805 url_fetcher_.reset(); 910 url_fetcher_.reset();
806 911
807 content::NotificationService::current()->Notify( 912 content::NotificationService::current()->Notify(
808 chrome::NOTIFICATION_COMPONENT_UPDATE_READY, 913 chrome::NOTIFICATION_COMPONENT_UPDATE_READY,
809 content::Source<std::string>(&context->id), 914 content::Source<std::string>(&context->id),
810 content::NotificationService::NoDetails()); 915 content::NotificationService::NoDetails());
811 916
812 // Why unretained? See comment at top of file. 917 // Why unretained? See comment at top of file.
813 BrowserThread::PostDelayedTask( 918 BrowserThread::PostDelayedTask(
814 BrowserThread::FILE, 919 BrowserThread::FILE,
815 FROM_HERE, 920 FROM_HERE,
816 base::Bind(&CrxUpdateService::Install, 921 base::Bind(&CrxUpdateService::Install,
817 base::Unretained(this), 922 base::Unretained(this),
818 context, 923 context,
819 temp_crx_path), 924 temp_crx_path),
820 base::TimeDelta::FromMilliseconds(config_->StepDelay())); 925 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
821 } 926 }
822 } 927 }
823 928
824 // Install consists of digital signature verification, unpacking and then 929 // Install consists of digital signature verification, unpacking and then
825 // calling the component specific installer. All that is handled by the 930 // calling the component specific installer. All that is handled by the
826 // |unpacker|. If there is an error this function is in charge of deleting 931 // |unpacker|. If there is an error this function is in charge of deleting
827 // the files created. 932 // the files created.
828 void CrxUpdateService::Install(const CRXContext* context, 933 void CrxUpdateService::Install(const CRXContext* context,
829 const base::FilePath& crx_path) { 934 const base::FilePath& crx_path) {
830 // This function owns the |crx_path| and the |context| object. 935 // This function owns the |crx_path| and the |context| object.
831 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 936 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
832 ComponentUnpacker 937 ComponentUnpacker unpacker(context->pk_hash,
833 unpacker(context->pk_hash, crx_path, context->installer); 938 crx_path,
834 if (!file_util::Delete(crx_path, false)) { 939 context->fingerprint,
940 component_patcher_.get(),
941 context->installer);
942 if (!file_util::Delete(crx_path, false))
835 NOTREACHED() << crx_path.value(); 943 NOTREACHED() << crx_path.value();
836 }
837 // Why unretained? See comment at top of file. 944 // Why unretained? See comment at top of file.
838 BrowserThread::PostDelayedTask( 945 BrowserThread::PostDelayedTask(
839 BrowserThread::UI, 946 BrowserThread::UI,
840 FROM_HERE, 947 FROM_HERE,
841 base::Bind(&CrxUpdateService::DoneInstalling, base::Unretained(this), 948 base::Bind(&CrxUpdateService::DoneInstalling, base::Unretained(this),
842 context->id, unpacker.error()), 949 context->id, unpacker.error(), unpacker.extended_error()),
843 base::TimeDelta::FromMilliseconds(config_->StepDelay())); 950 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
844 delete context; 951 delete context;
845 } 952 }
846 953
847 // Installation has been completed. Adjust the component status and 954 // Installation has been completed. Adjust the component status and
848 // schedule the next check. 955 // schedule the next check.
849 void CrxUpdateService::DoneInstalling(const std::string& component_id, 956 void CrxUpdateService::DoneInstalling(const std::string& component_id,
850 ComponentUnpacker::Error error) { 957 ComponentUnpacker::Error error,
958 int extra_code) {
851 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 959 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
852 960
853 CrxUpdateItem* item = FindUpdateItemById(component_id); 961 CrxUpdateItem* item = FindUpdateItemById(component_id);
962 if (item->status == CrxUpdateItem::kUpdatingDiff) {
963 if (error != ComponentUnpacker::kNone) {
964 item->diff_update_failed = true;
965 size_t count = ChangeItemStatus(CrxUpdateItem::kUpdatingDiff,
966 CrxUpdateItem::kCanUpdate);
967 DCHECK_EQ(count, 1ul);
968 ScheduleNextRun(true);
969 return;
970 }
971 }
972
854 item->status = (error == ComponentUnpacker::kNone) ? CrxUpdateItem::kUpdated : 973 item->status = (error == ComponentUnpacker::kNone) ? CrxUpdateItem::kUpdated :
855 CrxUpdateItem::kNoUpdate; 974 CrxUpdateItem::kNoUpdate;
856 if (item->status == CrxUpdateItem::kUpdated) 975 if (item->status == CrxUpdateItem::kUpdated) {
857 item->component.version = item->next_version; 976 item->component.version = item->next_version;
977 item->component.fingerprint = item->next_fp;
978 }
858 979
859 Configurator::Events event; 980 Configurator::Events event;
860 switch (error) { 981 switch (error) {
861 case ComponentUnpacker::kNone: 982 case ComponentUnpacker::kNone:
862 event = Configurator::kComponentUpdated; 983 event = Configurator::kComponentUpdated;
863 break; 984 break;
864 case ComponentUnpacker::kInstallerError: 985 case ComponentUnpacker::kInstallerError:
865 event = Configurator::kInstallerError; 986 event = Configurator::kInstallerError;
866 break; 987 break;
867 default: 988 default:
868 event = Configurator::kUnpackError; 989 event = Configurator::kUnpackError;
869 break; 990 break;
870 } 991 }
871 992
872 config_->OnEvent(event, CrxIdtoUMAId(component_id)); 993 config_->OnEvent(event, CrxIdtoUMAId(component_id));
873 ScheduleNextRun(false); 994 ScheduleNextRun(false);
874 } 995 }
875 996
876 // The component update factory. Using the component updater as a singleton 997 // The component update factory. Using the component updater as a singleton
877 // is the job of the browser process. 998 // is the job of the browser process.
878 ComponentUpdateService* ComponentUpdateServiceFactory( 999 ComponentUpdateService* ComponentUpdateServiceFactory(
879 ComponentUpdateService::Configurator* config) { 1000 ComponentUpdateService::Configurator* config) {
880 DCHECK(config); 1001 DCHECK(config);
881 return new CrxUpdateService(config); 1002 return new CrxUpdateService(config);
882 } 1003 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698