| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_script_cache_map.h" | 5 #include "content/browser/service_worker/service_worker_script_cache_map.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "content/browser/service_worker/service_worker_version.h" | 8 #include "content/browser/service_worker/service_worker_version.h" |
| 9 #include "content/common/service_worker/service_worker_types.h" | 9 #include "content/common/service_worker/service_worker_types.h" |
| 10 | 10 |
| 11 namespace content { | 11 namespace content { |
| 12 | 12 |
| 13 ServiceWorkerScriptCacheMap::ServiceWorkerScriptCacheMap( | 13 ServiceWorkerScriptCacheMap::ServiceWorkerScriptCacheMap( |
| 14 ServiceWorkerVersion* owner) | 14 ServiceWorkerVersion* owner) |
| 15 : owner_(owner), | 15 : owner_(owner), |
| 16 is_eval_complete_(false), | |
| 17 resources_started_(0), | |
| 18 resources_finished_(0), | |
| 19 has_error_(false) { | 16 has_error_(false) { |
| 20 } | 17 } |
| 21 | 18 |
| 22 ServiceWorkerScriptCacheMap::~ServiceWorkerScriptCacheMap() { | 19 ServiceWorkerScriptCacheMap::~ServiceWorkerScriptCacheMap() { |
| 23 } | 20 } |
| 24 | 21 |
| 25 int64 ServiceWorkerScriptCacheMap::Lookup(const GURL& url) { | 22 int64 ServiceWorkerScriptCacheMap::Lookup(const GURL& url) { |
| 26 ResourceIDMap::const_iterator found = resource_ids_.find(url); | 23 ResourceIDMap::const_iterator found = resource_ids_.find(url); |
| 27 if (found == resource_ids_.end()) | 24 if (found == resource_ids_.end()) |
| 28 return kInvalidServiceWorkerResponseId; | 25 return kInvalidServiceWorkerResponseId; |
| 29 return found->second; | 26 return found->second; |
| 30 } | 27 } |
| 31 | 28 |
| 32 void ServiceWorkerScriptCacheMap::AddObserver(Observer* observer) { | |
| 33 observers_.AddObserver(observer); | |
| 34 } | |
| 35 | |
| 36 void ServiceWorkerScriptCacheMap::RemoveObserver(Observer* observer) { | |
| 37 observers_.RemoveObserver(observer); | |
| 38 } | |
| 39 | |
| 40 void ServiceWorkerScriptCacheMap::NotifyStartedCaching( | 29 void ServiceWorkerScriptCacheMap::NotifyStartedCaching( |
| 41 const GURL& url, int64 resource_id) { | 30 const GURL& url, int64 resource_id) { |
| 42 DCHECK_EQ(kInvalidServiceWorkerResponseId, Lookup(url)); | 31 DCHECK_EQ(kInvalidServiceWorkerResponseId, Lookup(url)); |
| 43 DCHECK(owner_->status() == ServiceWorkerVersion::NEW || | 32 DCHECK(owner_->status() == ServiceWorkerVersion::NEW); |
| 44 owner_->status() == ServiceWorkerVersion::INSTALLING); | |
| 45 DCHECK(!is_eval_complete_); | |
| 46 resource_ids_[url] = resource_id; | 33 resource_ids_[url] = resource_id; |
| 47 ++resources_started_; | 34 // TODO(michaeln): Add resource id to the uncommitted list. |
| 48 } | 35 } |
| 49 | 36 |
| 50 void ServiceWorkerScriptCacheMap::NotifyFinishedCaching( | 37 void ServiceWorkerScriptCacheMap::NotifyFinishedCaching( |
| 51 const GURL& url, bool success) { | 38 const GURL& url, bool success) { |
| 52 DCHECK_NE(kInvalidServiceWorkerResponseId, Lookup(url)); | 39 DCHECK_NE(kInvalidServiceWorkerResponseId, Lookup(url)); |
| 53 DCHECK(owner_->status() == ServiceWorkerVersion::NEW || | 40 DCHECK(owner_->status() == ServiceWorkerVersion::NEW); |
| 54 owner_->status() == ServiceWorkerVersion::INSTALLING); | 41 if (!success) { |
| 55 ++resources_finished_; | |
| 56 if (!success) | |
| 57 has_error_ = true; | 42 has_error_ = true; |
| 58 if (url == owner_->script_url()) { | 43 resource_ids_.erase(url); |
| 59 FOR_EACH_OBSERVER(Observer, observers_, | 44 // TODO(michaeln): Doom the resource id. |
| 60 OnMainScriptCached(owner_, success)); | |
| 61 } | |
| 62 if (is_eval_complete_ && resources_finished_ == resources_started_) { | |
| 63 FOR_EACH_OBSERVER(Observer, observers_, | |
| 64 OnAllScriptsCached(owner_, has_error_)); | |
| 65 } | 45 } |
| 66 } | 46 } |
| 67 | 47 |
| 68 void ServiceWorkerScriptCacheMap::NotifyEvalCompletion() { | 48 void ServiceWorkerScriptCacheMap::GetResources( |
| 69 DCHECK(!is_eval_complete_); | 49 std::vector<ServiceWorkerDatabase::ResourceRecord>* resources) { |
| 70 is_eval_complete_ = true; | 50 DCHECK(resources->empty()); |
| 71 if (resources_finished_ == resources_started_) { | 51 for (ResourceIDMap::const_iterator it = resource_ids_.begin(); |
| 72 FOR_EACH_OBSERVER(Observer, observers_, | 52 it != resource_ids_.end(); ++it) { |
| 73 OnAllScriptsCached(owner_, has_error_)); | 53 ServiceWorkerDatabase::ResourceRecord record = { it->second, it->first }; |
| 54 resources->push_back(record); |
| 74 } | 55 } |
| 75 } | 56 } |
| 76 | 57 |
| 58 void ServiceWorkerScriptCacheMap::SetResources( |
| 59 const std::vector<ServiceWorkerDatabase::ResourceRecord>& resources) { |
| 60 DCHECK(resource_ids_.empty()); |
| 61 typedef std::vector<ServiceWorkerDatabase::ResourceRecord> RecordVector; |
| 62 for (RecordVector::const_iterator it = resources.begin(); |
| 63 it != resources.end(); ++it) { |
| 64 resource_ids_[it->url] = it->resource_id; |
| 65 } |
| 66 } |
| 67 |
| 77 } // namespace content | 68 } // namespace content |
| OLD | NEW |