1// Copyright (c) 2013 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 "webkit/browser/blob/blob_storage_context.h"
6
7#include "base/bind.h"
8#include "base/location.h"
9#include "base/logging.h"
10#include "base/message_loop/message_loop_proxy.h"
11#include "url/gurl.h"
12#include "webkit/browser/blob/blob_data_handle.h"
13#include "webkit/common/blob/blob_data.h"
14
15namespace webkit_blob {
16
17namespace {
18
19// We can't use GURL directly for these hash fragment manipulations
20// since it doesn't have specific knowlege of the BlobURL format. GURL
21// treats BlobURLs as if they were PathURLs which don't support hash
22// fragments.
23
24bool BlobUrlHasRef(const GURL& url) {
25  return url.spec().find('#') != std::string::npos;
26}
27
28GURL ClearBlobUrlRef(const GURL& url) {
29  size_t hash_pos = url.spec().find('#');
30  if (hash_pos == std::string::npos)
31    return url;
32  return GURL(url.spec().substr(0, hash_pos));
33}
34
35// TODO(michaeln): use base::SysInfo::AmountOfPhysicalMemoryMB() in some
36// way to come up with a better limit.
37static const int64 kMaxMemoryUsage = 500 * 1024 * 1024;  // Half a gig.
38
39}  // namespace
40
41BlobStorageContext::BlobMapEntry::BlobMapEntry()
42    : refcount(0), flags(0) {
43}
44
45BlobStorageContext::BlobMapEntry::BlobMapEntry(
46    int refcount, int flags, BlobData* data)
47    : refcount(refcount), flags(flags), data(data) {
48}
49
50BlobStorageContext::BlobMapEntry::~BlobMapEntry() {
51}
52
53BlobStorageContext::BlobStorageContext()
54    : memory_usage_(0) {
55}
56
57BlobStorageContext::~BlobStorageContext() {
58}
59
60scoped_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID(
61    const std::string& uuid) {
62  scoped_ptr<BlobDataHandle> result;
63  BlobMap::iterator found = blob_map_.find(uuid);
64  if (found == blob_map_.end())
65    return result.Pass();
66  if (found->second.flags & EXCEEDED_MEMORY)
67    return result.Pass();
68  DCHECK(!(found->second.flags & BEING_BUILT));
69  result.reset(new BlobDataHandle(
70      found->second.data.get(), this, base::MessageLoopProxy::current().get()));
71  return result.Pass();
72}
73
74scoped_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL(
75    const GURL& url) {
76  BlobURLMap::iterator found = public_blob_urls_.find(
77      BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url);
78  if (found == public_blob_urls_.end())
79    return scoped_ptr<BlobDataHandle>();
80  return GetBlobDataFromUUID(found->second);
81}
82
83scoped_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob(
84    const BlobData* data) {
85  StartBuildingBlob(data->uuid());
86  for (std::vector<BlobData::Item>::const_iterator iter =
87           data->items().begin();
88       iter != data->items().end(); ++iter) {
89    AppendBlobDataItem(data->uuid(), *iter);
90  }
91  FinishBuildingBlob(data->uuid(), data->content_type());
92  scoped_ptr<BlobDataHandle> handle = GetBlobDataFromUUID(data->uuid());
93  DecrementBlobRefCount(data->uuid());
94  return handle.Pass();
95}
96
97bool BlobStorageContext::RegisterPublicBlobURL(
98    const GURL& blob_url, const std::string& uuid) {
99  DCHECK(!BlobUrlHasRef(blob_url));
100  DCHECK(IsInUse(uuid));
101  DCHECK(!IsUrlRegistered(blob_url));
102  if (!IsInUse(uuid) || IsUrlRegistered(blob_url))
103    return false;
104  IncrementBlobRefCount(uuid);
105  public_blob_urls_[blob_url] = uuid;
106  return true;
107}
108
109void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) {
110  DCHECK(!BlobUrlHasRef(blob_url));
111  if (!IsUrlRegistered(blob_url))
112    return;
113  DecrementBlobRefCount(public_blob_urls_[blob_url]);
114  public_blob_urls_.erase(blob_url);
115}
116
117void BlobStorageContext::StartBuildingBlob(const std::string& uuid) {
118  DCHECK(!IsInUse(uuid) && !uuid.empty());
119  blob_map_[uuid] = BlobMapEntry(1, BEING_BUILT, new BlobData(uuid));
120}
121
122void BlobStorageContext::AppendBlobDataItem(
123    const std::string& uuid, const BlobData::Item& item) {
124  DCHECK(IsBeingBuilt(uuid));
125  BlobMap::iterator found = blob_map_.find(uuid);
126  if (found == blob_map_.end())
127    return;
128  if (found->second.flags & EXCEEDED_MEMORY)
129    return;
130  BlobData* target_blob_data = found->second.data.get();
131  DCHECK(target_blob_data);
132
133  bool exceeded_memory = false;
134
135  // The blob data is stored in the canonical way which only contains a
136  // list of Data, File, and FileSystem items. Aggregated TYPE_BLOB items
137  // are expanded into the primitive constituent types.
138  // 1) The Data item is denoted by the raw data and length.
139  // 2) The File item is denoted by the file path, the range and the expected
140  //    modification time.
141  // 3) The FileSystem File item is denoted by the FileSystem URL, the range
142  //    and the expected modification time.
143  // 4) The Blob items are expanded.
144  // TODO(michaeln): Would be nice to avoid copying Data items when expanding.
145
146  DCHECK(item.length() > 0);
147  switch (item.type()) {
148    case BlobData::Item::TYPE_BYTES:
149      DCHECK(!item.offset());
150      exceeded_memory = !AppendBytesItem(target_blob_data,
151                                         item.bytes(),
152                                         static_cast<int64>(item.length()));
153      break;
154    case BlobData::Item::TYPE_FILE:
155      AppendFileItem(target_blob_data,
156                     item.path(),
157                     item.offset(),
158                     item.length(),
159                     item.expected_modification_time());
160      break;
161    case BlobData::Item::TYPE_FILE_FILESYSTEM:
162      AppendFileSystemFileItem(target_blob_data,
163                               item.filesystem_url(),
164                               item.offset(),
165                               item.length(),
166                               item.expected_modification_time());
167      break;
168    case BlobData::Item::TYPE_BLOB: {
169      scoped_ptr<BlobDataHandle> src = GetBlobDataFromUUID(item.blob_uuid());
170      if (src)
171        exceeded_memory = !ExpandStorageItems(target_blob_data,
172                                              src->data(),
173                                              item.offset(),
174                                              item.length());
175      break;
176    }
177    default:
178      NOTREACHED();
179      break;
180  }
181
182  // If we're using too much memory, drop this blob's data.
183  // TODO(michaeln): Blob memory storage does not yet spill over to disk,
184  // as a stop gap, we'll prevent memory usage over a max amount.
185  if (exceeded_memory) {
186    memory_usage_ -= target_blob_data->GetMemoryUsage();
187    found->second.flags |= EXCEEDED_MEMORY;
188    found->second.data = new BlobData(uuid);
189    return;
190  }
191}
192
193void BlobStorageContext::FinishBuildingBlob(
194    const std::string& uuid, const std::string& content_type) {
195  DCHECK(IsBeingBuilt(uuid));
196  BlobMap::iterator found = blob_map_.find(uuid);
197  if (found == blob_map_.end())
198    return;
199  found->second.data->set_content_type(content_type);
200  found->second.flags &= ~BEING_BUILT;
201}
202
203void BlobStorageContext::CancelBuildingBlob(const std::string& uuid) {
204  DCHECK(IsBeingBuilt(uuid));
205  DecrementBlobRefCount(uuid);
206}
207
208void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) {
209  BlobMap::iterator found = blob_map_.find(uuid);
210  if (found == blob_map_.end()) {
211    DCHECK(false);
212    return;
213  }
214  ++(found->second.refcount);
215}
216
217void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) {
218  BlobMap::iterator found = blob_map_.find(uuid);
219  if (found == blob_map_.end())
220    return;
221  DCHECK_EQ(found->second.data->uuid(), uuid);
222  if (--(found->second.refcount) == 0) {
223    memory_usage_ -= found->second.data->GetMemoryUsage();
224    blob_map_.erase(found);
225  }
226}
227
228bool BlobStorageContext::ExpandStorageItems(
229    BlobData* target_blob_data, BlobData* src_blob_data,
230    uint64 offset, uint64 length) {
231  DCHECK(target_blob_data && src_blob_data &&
232         length != static_cast<uint64>(-1));
233
234  std::vector<BlobData::Item>::const_iterator iter =
235      src_blob_data->items().begin();
236  if (offset) {
237    for (; iter != src_blob_data->items().end(); ++iter) {
238      if (offset >= iter->length())
239        offset -= iter->length();
240      else
241        break;
242    }
243  }
244
245  for (; iter != src_blob_data->items().end() && length > 0; ++iter) {
246    uint64 current_length = iter->length() - offset;
247    uint64 new_length = current_length > length ? length : current_length;
248    if (iter->type() == BlobData::Item::TYPE_BYTES) {
249      if (!AppendBytesItem(
250              target_blob_data,
251              iter->bytes() + static_cast<size_t>(iter->offset() + offset),
252              static_cast<int64>(new_length))) {
253        return false;  // exceeded memory
254      }
255    } else if (iter->type() == BlobData::Item::TYPE_FILE) {
256      AppendFileItem(target_blob_data,
257                     iter->path(),
258                     iter->offset() + offset,
259                     new_length,
260                     iter->expected_modification_time());
261    } else {
262      DCHECK(iter->type() == BlobData::Item::TYPE_FILE_FILESYSTEM);
263      AppendFileSystemFileItem(target_blob_data,
264                               iter->filesystem_url(),
265                               iter->offset() + offset,
266                               new_length,
267                               iter->expected_modification_time());
268    }
269    length -= new_length;
270    offset = 0;
271  }
272  return true;
273}
274
275bool BlobStorageContext::AppendBytesItem(
276    BlobData* target_blob_data, const char* bytes, int64 length) {
277  if (length < 0) {
278    DCHECK(false);
279    return false;
280  }
281  if (memory_usage_ + length > kMaxMemoryUsage)
282    return false;
283  target_blob_data->AppendData(bytes, static_cast<size_t>(length));
284  memory_usage_ += length;
285  return true;
286}
287
288void BlobStorageContext::AppendFileItem(
289    BlobData* target_blob_data,
290    const base::FilePath& file_path, uint64 offset, uint64 length,
291    const base::Time& expected_modification_time) {
292  target_blob_data->AppendFile(file_path, offset, length,
293                               expected_modification_time);
294
295  // It may be a temporary file that should be deleted when no longer needed.
296  scoped_refptr<ShareableFileReference> shareable_file =
297      ShareableFileReference::Get(file_path);
298  if (shareable_file.get())
299    target_blob_data->AttachShareableFileReference(shareable_file.get());
300}
301
302void BlobStorageContext::AppendFileSystemFileItem(
303    BlobData* target_blob_data,
304    const GURL& filesystem_url, uint64 offset, uint64 length,
305    const base::Time& expected_modification_time) {
306  target_blob_data->AppendFileSystemFile(filesystem_url, offset, length,
307                                         expected_modification_time);
308}
309
310bool BlobStorageContext::IsInUse(const std::string& uuid) {
311  return blob_map_.find(uuid) != blob_map_.end();
312}
313
314bool BlobStorageContext::IsBeingBuilt(const std::string& uuid) {
315  BlobMap::iterator found = blob_map_.find(uuid);
316  if (found == blob_map_.end())
317    return false;
318  return found->second.flags & BEING_BUILT;
319}
320
321bool BlobStorageContext::IsUrlRegistered(const GURL& blob_url) {
322  return public_blob_urls_.find(blob_url) != public_blob_urls_.end();
323}
324
325}  // namespace webkit_blob
326