Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_script_cache_map.cc
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
3 // found in the LICENSE file.
4
5 #include "content/browser/service_worker/service_worker_script_cache_map.h"
6
7 #include "base/logging.h"
8 #include "content/browser/service_worker/service_worker_context_core.h"
9 #include "content/browser/service_worker/service_worker_storage.h"
10 #include "content/browser/service_worker/service_worker_version.h"
11 #include "content/common/service_worker/service_worker_types.h"
12
13 namespace content {
14
15 ServiceWorkerScriptCacheMap::ServiceWorkerScriptCacheMap(
16     ServiceWorkerVersion* owner,
17     base::WeakPtr<ServiceWorkerContextCore> context)
18     : owner_(owner),
19       context_(context) {
20 }
21
22 ServiceWorkerScriptCacheMap::~ServiceWorkerScriptCacheMap() {
23 }
24
25 int64 ServiceWorkerScriptCacheMap::LookupResourceId(const GURL& url) {
26   ResourceMap::const_iterator found = resource_map_.find(url);
27   if (found == resource_map_.end())
28     return kInvalidServiceWorkerResponseId;
29   return found->second.resource_id;
30 }
31
32 int64 ServiceWorkerScriptCacheMap::LookupResourceSize(const GURL& url) {
33   ResourceMap::const_iterator found = resource_map_.find(url);
34   if (found == resource_map_.end())
35     return kInvalidServiceWorkerResponseId;
36   return found->second.size_bytes;
37 }
38
39 void ServiceWorkerScriptCacheMap::NotifyStartedCaching(
40     const GURL& url, int64 resource_id) {
41   DCHECK_EQ(kInvalidServiceWorkerResponseId, LookupResourceId(url));
42   DCHECK(owner_->status() == ServiceWorkerVersion::NEW ||
43          owner_->status() == ServiceWorkerVersion::INSTALLING);
44   resource_map_[url] =
45       ServiceWorkerDatabase::ResourceRecord(resource_id, url, -1);
46   context_->storage()->StoreUncommittedResponseId(resource_id);
47 }
48
49 void ServiceWorkerScriptCacheMap::NotifyFinishedCaching(
50     const GURL& url,
51     int64 size_bytes,
52     const net::URLRequestStatus& status) {
53   DCHECK_NE(kInvalidServiceWorkerResponseId, LookupResourceId(url));
54   DCHECK(owner_->status() == ServiceWorkerVersion::NEW ||
55          owner_->status() == ServiceWorkerVersion::INSTALLING);
56   if (!status.is_success()) {
57     context_->storage()->DoomUncommittedResponse(LookupResourceId(url));
58     resource_map_.erase(url);
59     if (owner_->script_url() == url)
60       main_script_status_ = status;
61   } else {
62     resource_map_[url].size_bytes = size_bytes;
63   }
64 }
65
66 void ServiceWorkerScriptCacheMap::GetResources(
67     std::vector<ServiceWorkerDatabase::ResourceRecord>* resources) {
68   DCHECK(resources->empty());
69   for (ResourceMap::const_iterator it = resource_map_.begin();
70        it != resource_map_.end();
71        ++it) {
72     resources->push_back(it->second);
73   }
74 }
75
76 void ServiceWorkerScriptCacheMap::SetResources(
77     const std::vector<ServiceWorkerDatabase::ResourceRecord>& resources) {
78   DCHECK(resource_map_.empty());
79   typedef std::vector<ServiceWorkerDatabase::ResourceRecord> RecordVector;
80   for (RecordVector::const_iterator it = resources.begin();
81        it != resources.end(); ++it) {
82     resource_map_[it->url] = *it;
83   }
84 }
85
86 }  // namespace content