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

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

Issue 1106723002: ServiceWorker: Decompose SWRegistration::SetVersionInternal for readability (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: make the argument const-ref Created 5 years, 8 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_registration.h" 5 #include "content/browser/service_worker/service_worker_registration.h"
6 6
7 #include "content/browser/service_worker/service_worker_context_core.h" 7 #include "content/browser/service_worker/service_worker_context_core.h"
8 #include "content/browser/service_worker/service_worker_info.h" 8 #include "content/browser/service_worker/service_worker_info.h"
9 #include "content/browser/service_worker/service_worker_register_job.h" 9 #include "content/browser/service_worker/service_worker_register_job.h"
10 #include "content/browser/service_worker/service_worker_utils.h" 10 #include "content/browser/service_worker/service_worker_utils.h"
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 return ServiceWorkerRegistrationInfo( 77 return ServiceWorkerRegistrationInfo(
78 pattern(), registration_id_, 78 pattern(), registration_id_,
79 is_deleted_ ? ServiceWorkerRegistrationInfo::IS_DELETED 79 is_deleted_ ? ServiceWorkerRegistrationInfo::IS_DELETED
80 : ServiceWorkerRegistrationInfo::IS_NOT_DELETED, 80 : ServiceWorkerRegistrationInfo::IS_NOT_DELETED,
81 GetVersionInfo(active_version_.get()), 81 GetVersionInfo(active_version_.get()),
82 GetVersionInfo(waiting_version_.get()), 82 GetVersionInfo(waiting_version_.get()),
83 GetVersionInfo(installing_version_.get()), resources_total_size_bytes_); 83 GetVersionInfo(installing_version_.get()), resources_total_size_bytes_);
84 } 84 }
85 85
86 void ServiceWorkerRegistration::SetActiveVersion( 86 void ServiceWorkerRegistration::SetActiveVersion(
87 ServiceWorkerVersion* version) { 87 const scoped_refptr<ServiceWorkerVersion>& version) {
88 should_activate_when_ready_ = false; 88 should_activate_when_ready_ = false;
89 SetVersionInternal(version, &active_version_, 89 if (active_version_ == version)
90 ChangedVersionAttributesMask::ACTIVE_VERSION); 90 return;
91
92 ChangedVersionAttributesMask mask;
93 if (version)
94 UnsetVersionInternal(version.get(), &mask);
95 if (active_version_)
96 active_version_->RemoveListener(this);
97 active_version_ = version;
98 if (active_version_)
99 active_version_->AddListener(this);
100 mask.add(ChangedVersionAttributesMask::ACTIVE_VERSION);
101
102 FOR_EACH_OBSERVER(Listener, listeners_,
103 OnVersionAttributesChanged(this, mask, GetInfo()));
91 } 104 }
92 105
93 void ServiceWorkerRegistration::SetWaitingVersion( 106 void ServiceWorkerRegistration::SetWaitingVersion(
94 ServiceWorkerVersion* version) { 107 const scoped_refptr<ServiceWorkerVersion>& version) {
95 should_activate_when_ready_ = false; 108 should_activate_when_ready_ = false;
96 SetVersionInternal(version, &waiting_version_, 109 if (waiting_version_ == version)
97 ChangedVersionAttributesMask::WAITING_VERSION); 110 return;
111
112 ChangedVersionAttributesMask mask;
113 if (version)
114 UnsetVersionInternal(version.get(), &mask);
115 waiting_version_ = version;
116 mask.add(ChangedVersionAttributesMask::WAITING_VERSION);
117
118 FOR_EACH_OBSERVER(Listener, listeners_,
119 OnVersionAttributesChanged(this, mask, GetInfo()));
98 } 120 }
99 121
100 void ServiceWorkerRegistration::SetInstallingVersion( 122 void ServiceWorkerRegistration::SetInstallingVersion(
101 ServiceWorkerVersion* version) { 123 const scoped_refptr<ServiceWorkerVersion>& version) {
102 SetVersionInternal(version, &installing_version_, 124 if (installing_version_ == version)
103 ChangedVersionAttributesMask::INSTALLING_VERSION); 125 return;
126
127 ChangedVersionAttributesMask mask;
128 if (version)
129 UnsetVersionInternal(version.get(), &mask);
130 installing_version_ = version;
131 mask.add(ChangedVersionAttributesMask::INSTALLING_VERSION);
132
133 FOR_EACH_OBSERVER(Listener, listeners_,
134 OnVersionAttributesChanged(this, mask, GetInfo()));
104 } 135 }
105 136
106 void ServiceWorkerRegistration::UnsetVersion(ServiceWorkerVersion* version) { 137 void ServiceWorkerRegistration::UnsetVersion(ServiceWorkerVersion* version) {
107 if (!version) 138 if (!version)
108 return; 139 return;
109 ChangedVersionAttributesMask mask; 140 ChangedVersionAttributesMask mask;
110 UnsetVersionInternal(version, &mask); 141 UnsetVersionInternal(version, &mask);
111 if (mask.changed()) { 142 if (mask.changed()) {
112 ServiceWorkerRegistrationInfo info = GetInfo(); 143 ServiceWorkerRegistrationInfo info = GetInfo();
113 FOR_EACH_OBSERVER(Listener, listeners_, 144 FOR_EACH_OBSERVER(Listener, listeners_,
114 OnVersionAttributesChanged(this, mask, info)); 145 OnVersionAttributesChanged(this, mask, info));
115 } 146 }
116 } 147 }
117 148
118 void ServiceWorkerRegistration::SetVersionInternal(
119 ServiceWorkerVersion* version,
120 scoped_refptr<ServiceWorkerVersion>* data_member,
121 int change_flag) {
122 if (version == data_member->get())
123 return;
124 scoped_refptr<ServiceWorkerVersion> protect(version);
125 ChangedVersionAttributesMask mask;
126 if (version)
127 UnsetVersionInternal(version, &mask);
128 if (*data_member && *data_member == active_version_)
129 active_version_->RemoveListener(this);
130 *data_member = version;
131 if (active_version_.get() && active_version_.get() == version)
132 active_version_->AddListener(this);
133 mask.add(change_flag);
134 ServiceWorkerRegistrationInfo info = GetInfo();
135 FOR_EACH_OBSERVER(Listener, listeners_,
136 OnVersionAttributesChanged(this, mask, info));
137 }
138
139 void ServiceWorkerRegistration::UnsetVersionInternal( 149 void ServiceWorkerRegistration::UnsetVersionInternal(
140 ServiceWorkerVersion* version, 150 ServiceWorkerVersion* version,
141 ChangedVersionAttributesMask* mask) { 151 ChangedVersionAttributesMask* mask) {
142 DCHECK(version); 152 DCHECK(version);
143 if (installing_version_.get() == version) { 153 if (installing_version_.get() == version) {
144 installing_version_ = NULL; 154 installing_version_ = NULL;
145 mask->add(ChangedVersionAttributesMask::INSTALLING_VERSION); 155 mask->add(ChangedVersionAttributesMask::INSTALLING_VERSION);
146 } else if (waiting_version_.get() == version) { 156 } else if (waiting_version_.get() == version) {
147 waiting_version_ = NULL; 157 waiting_version_ = NULL;
148 mask->add(ChangedVersionAttributesMask::WAITING_VERSION); 158 mask->add(ChangedVersionAttributesMask::WAITING_VERSION);
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 // "2. Terminate exitingWorker." 281 // "2. Terminate exitingWorker."
272 exiting_version->StopWorker( 282 exiting_version->StopWorker(
273 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 283 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
274 // "3. Run the [[UpdateState]] algorithm passing exitingWorker and 284 // "3. Run the [[UpdateState]] algorithm passing exitingWorker and
275 // "redundant" as the arguments." 285 // "redundant" as the arguments."
276 exiting_version->SetStatus(ServiceWorkerVersion::REDUNDANT); 286 exiting_version->SetStatus(ServiceWorkerVersion::REDUNDANT);
277 } 287 }
278 288
279 // "6. Set serviceWorkerRegistration.activeWorker to activatingWorker." 289 // "6. Set serviceWorkerRegistration.activeWorker to activatingWorker."
280 // "7. Set serviceWorkerRegistration.waitingWorker to null." 290 // "7. Set serviceWorkerRegistration.waitingWorker to null."
281 SetActiveVersion(activating_version.get()); 291 SetActiveVersion(activating_version);
282 292
283 // "8. Run the [[UpdateState]] algorithm passing registration.activeWorker and 293 // "8. Run the [[UpdateState]] algorithm passing registration.activeWorker and
284 // "activating" as arguments." 294 // "activating" as arguments."
285 activating_version->SetStatus(ServiceWorkerVersion::ACTIVATING); 295 activating_version->SetStatus(ServiceWorkerVersion::ACTIVATING);
286 // "9. Fire a simple event named controllerchange..." 296 // "9. Fire a simple event named controllerchange..."
287 if (activating_version->skip_waiting()) 297 if (activating_version->skip_waiting())
288 FOR_EACH_OBSERVER(Listener, listeners_, OnSkippedWaiting(this)); 298 FOR_EACH_OBSERVER(Listener, listeners_, OnSkippedWaiting(this));
289 299
290 // "10. Queue a task to fire an event named activate..." 300 // "10. Queue a task to fire an event named activate..."
291 activating_version->DispatchActivateEvent( 301 activating_version->DispatchActivateEvent(
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 if (!context_) { 401 if (!context_) {
392 callback.Run(SERVICE_WORKER_ERROR_ABORT); 402 callback.Run(SERVICE_WORKER_ERROR_ABORT);
393 return; 403 return;
394 } 404 }
395 context_->storage()->NotifyDoneInstallingRegistration( 405 context_->storage()->NotifyDoneInstallingRegistration(
396 this, version.get(), status); 406 this, version.get(), status);
397 callback.Run(status); 407 callback.Run(status);
398 } 408 }
399 409
400 } // namespace content 410 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698