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

Side by Side Diff: content/browser/service_worker/service_worker_register_job.cc

Issue 949293002: Implement a poor man's PostAfterStartupTask() function. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "content/browser/service_worker/service_worker_register_job.h" 5 #include "content/browser/service_worker/service_worker_register_job.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/rand_util.h"
10 #include "content/browser/service_worker/service_worker_context_core.h" 11 #include "content/browser/service_worker/service_worker_context_core.h"
11 #include "content/browser/service_worker/service_worker_job_coordinator.h" 12 #include "content/browser/service_worker/service_worker_job_coordinator.h"
12 #include "content/browser/service_worker/service_worker_registration.h" 13 #include "content/browser/service_worker/service_worker_registration.h"
13 #include "content/browser/service_worker/service_worker_storage.h" 14 #include "content/browser/service_worker/service_worker_storage.h"
14 #include "content/browser/service_worker/service_worker_utils.h" 15 #include "content/browser/service_worker/service_worker_utils.h"
15 #include "content/common/service_worker/service_worker_types.h" 16 #include "content/common/service_worker/service_worker_types.h"
17 #include "content/public/browser/browser_main_runner.h"
16 #include "net/base/net_errors.h" 18 #include "net/base/net_errors.h"
17 19
18 namespace content { 20 namespace content {
19 21
20 namespace { 22 namespace {
21 23
22 void RunSoon(const base::Closure& closure) { 24 void RunSoon(const base::Closure& closure) {
23 base::MessageLoop::current()->PostTask(FROM_HERE, closure); 25 base::MessageLoop::current()->PostTask(FROM_HERE, closure);
24 } 26 }
25 27
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 provider_host->AddScopedProcessReferenceToPattern(pattern_); 76 provider_host->AddScopedProcessReferenceToPattern(pattern_);
75 return; 77 return;
76 } 78 }
77 RunSoon(base::Bind(callback, promise_resolved_status_, 79 RunSoon(base::Bind(callback, promise_resolved_status_,
78 promise_resolved_status_message_, 80 promise_resolved_status_message_,
79 promise_resolved_registration_)); 81 promise_resolved_registration_));
80 } 82 }
81 83
82 void ServiceWorkerRegisterJob::Start() { 84 void ServiceWorkerRegisterJob::Start() {
83 SetPhase(START); 85 SetPhase(START);
86
87 if (IsBrowserStartingUp()) {
88 // Delay running update jobs until after startup.
89 delayed_start_timer_.Start(
90 FROM_HERE, base::TimeDelta::FromSeconds(base::RandInt(5, 15)),
91 base::Bind(&ServiceWorkerRegisterJob::Restart,
92 weak_factory_.GetWeakPtr()));
93 return;
94 }
95
84 ServiceWorkerStorage::FindRegistrationCallback next_step; 96 ServiceWorkerStorage::FindRegistrationCallback next_step;
85 if (job_type_ == REGISTRATION_JOB) { 97 if (job_type_ == REGISTRATION_JOB) {
86 next_step = base::Bind( 98 next_step = base::Bind(
87 &ServiceWorkerRegisterJob::ContinueWithRegistration, 99 &ServiceWorkerRegisterJob::ContinueWithRegistration,
88 weak_factory_.GetWeakPtr()); 100 weak_factory_.GetWeakPtr());
89 } else { 101 } else {
90 next_step = base::Bind( 102 next_step = base::Bind(
91 &ServiceWorkerRegisterJob::ContinueWithUpdate, 103 &ServiceWorkerRegisterJob::ContinueWithUpdate,
92 weak_factory_.GetWeakPtr()); 104 weak_factory_.GetWeakPtr());
93 } 105 }
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 case INSTALL: 183 case INSTALL:
172 DCHECK(phase_ == UPDATE) << phase_; 184 DCHECK(phase_ == UPDATE) << phase_;
173 break; 185 break;
174 case STORE: 186 case STORE:
175 DCHECK(phase_ == INSTALL) << phase_; 187 DCHECK(phase_ == INSTALL) << phase_;
176 break; 188 break;
177 case COMPLETE: 189 case COMPLETE:
178 DCHECK(phase_ != INITIAL && phase_ != COMPLETE) << phase_; 190 DCHECK(phase_ != INITIAL && phase_ != COMPLETE) << phase_;
179 break; 191 break;
180 case ABORT: 192 case ABORT:
193 delayed_start_timer_.Stop();
181 break; 194 break;
182 } 195 }
183 phase_ = phase; 196 phase_ = phase;
184 } 197 }
185 198
199 void ServiceWorkerRegisterJob::Restart() {
200 DCHECK(phase_ == START) << phase_;
cmumford 2015/02/26 23:47:45 DCHECK_EQ. Also, couldn't you get here before STA
201 phase_ = INITIAL;
202 Start();
203 }
204
186 // This function corresponds to the steps in [[Register]] following 205 // This function corresponds to the steps in [[Register]] following
187 // "Let registration be the result of running the [[GetRegistration]] algorithm. 206 // "Let registration be the result of running the [[GetRegistration]] algorithm.
188 // Throughout this file, comments in quotes are excerpts from the spec. 207 // Throughout this file, comments in quotes are excerpts from the spec.
189 void ServiceWorkerRegisterJob::ContinueWithRegistration( 208 void ServiceWorkerRegisterJob::ContinueWithRegistration(
190 ServiceWorkerStatusCode status, 209 ServiceWorkerStatusCode status,
191 const scoped_refptr<ServiceWorkerRegistration>& existing_registration) { 210 const scoped_refptr<ServiceWorkerRegistration>& existing_registration) {
192 DCHECK_EQ(REGISTRATION_JOB, job_type_); 211 DCHECK_EQ(REGISTRATION_JOB, job_type_);
193 if (status != SERVICE_WORKER_ERROR_NOT_FOUND && status != SERVICE_WORKER_OK) { 212 if (status != SERVICE_WORKER_ERROR_NOT_FOUND && status != SERVICE_WORKER_OK) {
194 Complete(status); 213 Complete(status);
195 return; 214 return;
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 ServiceWorkerProviderHost* host = it->GetProviderHost(); 575 ServiceWorkerProviderHost* host = it->GetProviderHost();
557 if (ServiceWorkerUtils::ScopeMatches(registration->pattern(), 576 if (ServiceWorkerUtils::ScopeMatches(registration->pattern(),
558 host->document_url())) { 577 host->document_url())) {
559 if (host->CanAssociateRegistration(registration)) 578 if (host->CanAssociateRegistration(registration))
560 host->AssociateRegistration(registration); 579 host->AssociateRegistration(registration);
561 } 580 }
562 } 581 }
563 } 582 }
564 583
565 } // namespace content 584 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698