Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "content/browser/service_worker/service_worker_context_watcher.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/containers/scoped_ptr_hash_map.h" | |
| 9 #include "content/browser/service_worker/service_worker_context_observer.h" | |
| 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" | |
| 11 #include "content/browser/service_worker/service_worker_version.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 #include "url/gurl.h" | |
| 14 | |
| 15 namespace content { | |
| 16 namespace { | |
| 17 | |
| 18 bool IsStoppedAndRedundant(const ServiceWorkerVersionInfo& version_info) { | |
| 19 return version_info.running_status == | |
| 20 content::ServiceWorkerVersion::STOPPED && | |
| 21 version_info.status == content::ServiceWorkerVersion::REDUNDANT; | |
| 22 } | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 ServiceWorkerContextWatcher::ServiceWorkerContextWatcher( | |
| 27 scoped_refptr<ServiceWorkerContextWrapper> context, | |
| 28 const WorkerRegistrationUpdatedCallback& registration_callback, | |
| 29 const WorkerVersionUpdatedCallback& version_callback) | |
| 30 : context_(context), | |
| 31 registration_callback_(registration_callback), | |
| 32 version_callback_(version_callback) { | |
| 33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 34 } | |
| 35 | |
| 36 void ServiceWorkerContextWatcher::Start() { | |
| 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 38 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 39 base::Bind(&ServiceWorkerContextWatcher:: | |
| 40 GetStoredRegistrationsOnIOThread, | |
| 41 this)); | |
| 42 } | |
| 43 | |
| 44 void ServiceWorkerContextWatcher::Stop() { | |
| 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 46 BrowserThread::PostTask( | |
| 47 BrowserThread::IO, FROM_HERE, | |
| 48 base::Bind(&ServiceWorkerContextWatcher::StopOnIOThread, this)); | |
| 49 } | |
| 50 | |
| 51 void ServiceWorkerContextWatcher::GetStoredRegistrationsOnIOThread() { | |
| 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 53 context_->context()->storage()->GetAllRegistrations(base::Bind( | |
| 54 &ServiceWorkerContextWatcher::OnStoredRegistrationsOnIOThread, | |
| 55 this)); | |
|
kinuko
2015/03/13 09:09:21
This is what we've been doing so it's ok, but will
horo
2015/03/13 10:03:49
Yes. In future, it may better to filter the regist
| |
| 56 } | |
| 57 | |
| 58 void ServiceWorkerContextWatcher::OnStoredRegistrationsOnIOThread( | |
| 59 const std::vector<ServiceWorkerRegistrationInfo>& stored_registrations) { | |
| 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 61 context_->AddObserver(this); | |
| 62 | |
| 63 base::ScopedPtrHashMap<int64, ServiceWorkerRegistrationInfo> | |
| 64 registration_info_map; | |
| 65 for (const auto& registration : stored_registrations) | |
| 66 StoreRegistrationInfo(registration, ®istration_info_map); | |
| 67 for (const auto& registration : | |
| 68 context_->context()->GetAllLiveRegistrationInfo()) { | |
| 69 StoreRegistrationInfo(registration, ®istration_info_map); | |
| 70 } | |
| 71 for (const auto& version : context_->context()->GetAllLiveVersionInfo()) | |
| 72 StoreVersionInfo(version); | |
| 73 | |
| 74 std::vector<ServiceWorkerRegistrationInfo> registrations; | |
| 75 registrations.reserve(registration_info_map.size()); | |
| 76 for (const auto& registration_id_info_pair : registration_info_map) | |
| 77 registrations.push_back(*registration_id_info_pair.second); | |
| 78 | |
| 79 std::vector<ServiceWorkerVersionInfo> versions; | |
| 80 versions.reserve(version_info_map_.size()); | |
| 81 | |
| 82 for (auto version_it = version_info_map_.begin(); | |
| 83 version_it != version_info_map_.end();) { | |
| 84 versions.push_back(*version_it->second); | |
| 85 if (IsStoppedAndRedundant(*version_it->second)) | |
| 86 version_info_map_.erase(version_it++); | |
| 87 else | |
| 88 ++version_it; | |
| 89 } | |
| 90 | |
| 91 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 92 base::Bind(registration_callback_, registrations)); | |
| 93 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 94 base::Bind(version_callback_, versions)); | |
| 95 } | |
| 96 | |
| 97 void ServiceWorkerContextWatcher::StopOnIOThread() { | |
| 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 99 context_->RemoveObserver(this); | |
| 100 } | |
| 101 | |
| 102 ServiceWorkerContextWatcher::~ServiceWorkerContextWatcher() { | |
| 103 } | |
| 104 | |
| 105 void ServiceWorkerContextWatcher::StoreRegistrationInfo( | |
| 106 const ServiceWorkerRegistrationInfo& registration_info, | |
| 107 base::ScopedPtrHashMap<int64, ServiceWorkerRegistrationInfo>* info_map) { | |
| 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 109 if (registration_info.registration_id == kInvalidServiceWorkerRegistrationId) | |
| 110 return; | |
| 111 info_map->set(registration_info.registration_id, | |
| 112 scoped_ptr<ServiceWorkerRegistrationInfo>( | |
| 113 new ServiceWorkerRegistrationInfo(registration_info))); | |
| 114 StoreVersionInfo(registration_info.active_version); | |
| 115 StoreVersionInfo(registration_info.waiting_version); | |
| 116 StoreVersionInfo(registration_info.installing_version); | |
| 117 } | |
| 118 | |
| 119 void ServiceWorkerContextWatcher::StoreVersionInfo( | |
| 120 const ServiceWorkerVersionInfo& version_info) { | |
| 121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 122 if (version_info.version_id == kInvalidServiceWorkerVersionId) | |
| 123 return; | |
| 124 version_info_map_.set(version_info.version_id, | |
| 125 scoped_ptr<ServiceWorkerVersionInfo>( | |
| 126 new ServiceWorkerVersionInfo(version_info))); | |
| 127 } | |
| 128 | |
| 129 void ServiceWorkerContextWatcher::SendRegistrationInfo( | |
| 130 int64 registration_id, | |
| 131 const GURL& pattern, | |
| 132 bool is_deleted) { | |
| 133 std::vector<ServiceWorkerRegistrationInfo> registrations; | |
| 134 registrations.push_back( | |
| 135 ServiceWorkerRegistrationInfo(pattern, registration_id, is_deleted)); | |
| 136 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 137 base::Bind(registration_callback_, registrations)); | |
| 138 } | |
| 139 | |
| 140 void ServiceWorkerContextWatcher::SendVersionInfo( | |
| 141 const ServiceWorkerVersionInfo& version_info) { | |
| 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 143 std::vector<ServiceWorkerVersionInfo> versions; | |
| 144 versions.push_back(version_info); | |
| 145 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 146 base::Bind(version_callback_, versions)); | |
| 147 } | |
| 148 | |
| 149 void ServiceWorkerContextWatcher::OnNewLiveRegistration( | |
| 150 int64 registration_id, | |
| 151 const GURL& pattern) { | |
| 152 SendRegistrationInfo(registration_id, pattern, false); | |
|
kinuko
2015/03/13 09:09:21
nit: please annotate the boolean field with commen
horo
2015/03/13 10:03:49
Done.
| |
| 153 } | |
| 154 | |
| 155 void ServiceWorkerContextWatcher::OnNewLiveVersion( | |
| 156 int64 version_id, | |
| 157 int64 registration_id, | |
| 158 const GURL& script_url) { | |
| 159 if (ServiceWorkerVersionInfo* version = version_info_map_.get(version_id)) { | |
| 160 DCHECK(version->registration_id == registration_id); | |
| 161 DCHECK(version->script_url == script_url); | |
|
kinuko
2015/03/13 09:09:21
nit: DCHECK_EQ
horo
2015/03/13 10:03:49
Done.
| |
| 162 return; | |
| 163 } | |
| 164 | |
| 165 scoped_ptr<ServiceWorkerVersionInfo> version(new ServiceWorkerVersionInfo()); | |
| 166 version->version_id = version_id; | |
| 167 version->registration_id = registration_id; | |
| 168 version->script_url = script_url; | |
| 169 SendVersionInfo(*version); | |
| 170 if (!IsStoppedAndRedundant(*version)) | |
| 171 version_info_map_.set(version_id, version.Pass()); | |
| 172 } | |
| 173 | |
| 174 void ServiceWorkerContextWatcher::OnRunningStateChanged( | |
| 175 int64 version_id, | |
| 176 content::ServiceWorkerVersion::RunningStatus running_status) { | |
| 177 ServiceWorkerVersionInfo* version = version_info_map_.get(version_id); | |
| 178 DCHECK(version); | |
| 179 if (version->running_status == running_status) | |
| 180 return; | |
| 181 version->running_status = running_status; | |
| 182 SendVersionInfo(*version); | |
| 183 if (IsStoppedAndRedundant(*version)) | |
| 184 version_info_map_.erase(version_id); | |
| 185 } | |
| 186 | |
| 187 void ServiceWorkerContextWatcher::OnVersionStateChanged( | |
| 188 int64 version_id, | |
| 189 content::ServiceWorkerVersion::Status status) { | |
| 190 ServiceWorkerVersionInfo* version = version_info_map_.get(version_id); | |
| 191 DCHECK(version); | |
| 192 if (version->status == status) | |
| 193 return; | |
| 194 version->status = status; | |
| 195 SendVersionInfo(*version); | |
| 196 if (IsStoppedAndRedundant(*version)) | |
| 197 version_info_map_.erase(version_id); | |
| 198 } | |
| 199 | |
| 200 void ServiceWorkerContextWatcher::OnRegistrationStored( | |
| 201 int64 registration_id, | |
| 202 const GURL& pattern) { | |
| 203 SendRegistrationInfo(registration_id, pattern, false); | |
| 204 } | |
| 205 | |
| 206 void ServiceWorkerContextWatcher::OnRegistrationDeleted( | |
| 207 int64 registration_id, | |
| 208 const GURL& pattern) { | |
| 209 SendRegistrationInfo(registration_id, pattern, true); | |
| 210 } | |
| 211 | |
| 212 } // namespace content | |
| OLD | NEW |