ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_script_cache_map.cc
blob925fe6e3b4764821dd6f3132f81396e0d0334151
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.
5 #include "content/browser/service_worker/service_worker_script_cache_map.h"
7 #include "base/logging.h"
8 #include "content/browser/service_worker/service_worker_context_core.h"
9 #include "content/browser/service_worker/service_worker_disk_cache.h"
10 #include "content/browser/service_worker/service_worker_storage.h"
11 #include "content/browser/service_worker/service_worker_version.h"
12 #include "content/common/service_worker/service_worker_types.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/net_errors.h"
16 namespace content {
18 ServiceWorkerScriptCacheMap::ServiceWorkerScriptCacheMap(
19 ServiceWorkerVersion* owner,
20 base::WeakPtr<ServiceWorkerContextCore> context)
21 : owner_(owner), context_(context), weak_factory_(this) {
24 ServiceWorkerScriptCacheMap::~ServiceWorkerScriptCacheMap() {
27 int64 ServiceWorkerScriptCacheMap::LookupResourceId(const GURL& url) {
28 ResourceMap::const_iterator found = resource_map_.find(url);
29 if (found == resource_map_.end())
30 return kInvalidServiceWorkerResponseId;
31 return found->second.resource_id;
34 int64 ServiceWorkerScriptCacheMap::LookupResourceSize(const GURL& url) {
35 ResourceMap::const_iterator found = resource_map_.find(url);
36 if (found == resource_map_.end())
37 return kInvalidServiceWorkerResponseId;
38 return found->second.size_bytes;
41 void ServiceWorkerScriptCacheMap::NotifyStartedCaching(
42 const GURL& url, int64 resource_id) {
43 DCHECK_EQ(kInvalidServiceWorkerResponseId, LookupResourceId(url));
44 DCHECK(owner_->status() == ServiceWorkerVersion::NEW ||
45 owner_->status() == ServiceWorkerVersion::INSTALLING);
46 if (!context_)
47 return; // Our storage has been wiped via DeleteAndStartOver.
48 resource_map_[url] =
49 ServiceWorkerDatabase::ResourceRecord(resource_id, url, -1);
50 context_->storage()->StoreUncommittedResponseId(resource_id);
53 void ServiceWorkerScriptCacheMap::NotifyFinishedCaching(
54 const GURL& url,
55 int64 size_bytes,
56 const net::URLRequestStatus& status,
57 const std::string& status_message) {
58 DCHECK_NE(kInvalidServiceWorkerResponseId, LookupResourceId(url));
59 DCHECK(owner_->status() == ServiceWorkerVersion::NEW ||
60 owner_->status() == ServiceWorkerVersion::INSTALLING);
61 if (!context_)
62 return; // Our storage has been wiped via DeleteAndStartOver.
63 if (!status.is_success()) {
64 context_->storage()->DoomUncommittedResponse(LookupResourceId(url));
65 resource_map_.erase(url);
66 if (owner_->script_url() == url) {
67 main_script_status_ = status;
68 main_script_status_message_ = status_message;
70 } else {
71 resource_map_[url].size_bytes = size_bytes;
75 void ServiceWorkerScriptCacheMap::GetResources(
76 std::vector<ServiceWorkerDatabase::ResourceRecord>* resources) {
77 DCHECK(resources->empty());
78 for (ResourceMap::const_iterator it = resource_map_.begin();
79 it != resource_map_.end();
80 ++it) {
81 resources->push_back(it->second);
85 void ServiceWorkerScriptCacheMap::SetResources(
86 const std::vector<ServiceWorkerDatabase::ResourceRecord>& resources) {
87 DCHECK(resource_map_.empty());
88 typedef std::vector<ServiceWorkerDatabase::ResourceRecord> RecordVector;
89 for (RecordVector::const_iterator it = resources.begin();
90 it != resources.end(); ++it) {
91 resource_map_[it->url] = *it;
95 void ServiceWorkerScriptCacheMap::WriteMetadata(
96 const GURL& url,
97 const std::vector<char>& data,
98 const net::CompletionCallback& callback) {
99 ResourceMap::iterator found = resource_map_.find(url);
100 if (found == resource_map_.end() ||
101 found->second.resource_id == kInvalidServiceWorkerResponseId) {
102 callback.Run(net::ERR_FILE_NOT_FOUND);
103 return;
105 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(data.size()));
106 if (data.size())
107 memmove(buffer->data(), &data[0], data.size());
108 scoped_ptr<ServiceWorkerResponseMetadataWriter> writer;
109 writer = context_->storage()->CreateResponseMetadataWriter(
110 found->second.resource_id);
111 ServiceWorkerResponseMetadataWriter* raw_writer = writer.get();
112 raw_writer->WriteMetadata(
113 buffer.get(), data.size(),
114 base::Bind(&ServiceWorkerScriptCacheMap::OnMetadataWritten,
115 weak_factory_.GetWeakPtr(), Passed(&writer), callback));
118 void ServiceWorkerScriptCacheMap::ClearMetadata(
119 const GURL& url,
120 const net::CompletionCallback& callback) {
121 WriteMetadata(url, std::vector<char>(), callback);
124 void ServiceWorkerScriptCacheMap::OnMetadataWritten(
125 scoped_ptr<ServiceWorkerResponseMetadataWriter> writer,
126 const net::CompletionCallback& callback,
127 int result) {
128 callback.Run(result);
131 } // namespace content