summaryrefslogtreecommitdiffstats
path: root/storage/browser/blob/blob_storage_registry.cc
blob: 805a677d898ef84e420009205607e28aa61d6c25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "storage/browser/blob/blob_storage_registry.h"

#include "base/bind.h"
#include "base/callback.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/stl_util.h"
#include "url/gurl.h"

namespace storage {
using BlobState = BlobStorageRegistry::BlobState;

namespace {
// We can't use GURL directly for these hash fragment manipulations
// since it doesn't have specific knowlege of the BlobURL format. GURL
// treats BlobURLs as if they were PathURLs which don't support hash
// fragments.

bool BlobUrlHasRef(const GURL& url) {
  return url.spec().find('#') != std::string::npos;
}

GURL ClearBlobUrlRef(const GURL& url) {
  size_t hash_pos = url.spec().find('#');
  if (hash_pos == std::string::npos)
    return url;
  return GURL(url.spec().substr(0, hash_pos));
}

}  // namespace

BlobStorageRegistry::Entry::Entry(int refcount, BlobState state)
    : refcount(refcount), state(state), exceeded_memory(false) {}

BlobStorageRegistry::Entry::~Entry() {}

bool BlobStorageRegistry::Entry::TestAndSetState(BlobState expected,
                                                 BlobState set) {
  if (state != expected)
    return false;
  state = set;
  return true;
}

BlobStorageRegistry::BlobStorageRegistry() {}

BlobStorageRegistry::~BlobStorageRegistry() {
  // Note: We don't bother calling the construction complete callbacks, as we
  // are only being destructed at the end of the life of the browser process.
  // So it shouldn't matter.
}

BlobStorageRegistry::Entry* BlobStorageRegistry::CreateEntry(
    const std::string& uuid) {
  DCHECK(!ContainsKey(blob_map_, uuid));
  Entry* entry = new Entry(1, BlobState::RESERVED);
  blob_map_.add(uuid, make_scoped_ptr(entry));
  return entry;
}

bool BlobStorageRegistry::DeleteEntry(const std::string& uuid) {
  return blob_map_.erase(uuid) == 1;
}

BlobStorageRegistry::Entry* BlobStorageRegistry::GetEntry(
    const std::string& uuid) {
  BlobMap::iterator found = blob_map_.find(uuid);
  if (found == blob_map_.end())
    return nullptr;
  return found->second;
}

bool BlobStorageRegistry::CreateUrlMapping(const GURL& blob_url,
                                           const std::string& uuid) {
  DCHECK(!BlobUrlHasRef(blob_url));
  if (blob_map_.find(uuid) == blob_map_.end() || IsURLMapped(blob_url))
    return false;
  url_to_uuid_[blob_url] = uuid;
  return true;
}

bool BlobStorageRegistry::DeleteURLMapping(const GURL& blob_url,
                                           std::string* uuid) {
  DCHECK(!BlobUrlHasRef(blob_url));
  URLMap::iterator found = url_to_uuid_.find(blob_url);
  if (found == url_to_uuid_.end())
    return false;
  if (uuid)
    uuid->assign(found->second);
  url_to_uuid_.erase(found);
  return true;
}

bool BlobStorageRegistry::IsURLMapped(const GURL& blob_url) const {
  return ContainsKey(url_to_uuid_, blob_url);
}

BlobStorageRegistry::Entry* BlobStorageRegistry::GetEntryFromURL(
    const GURL& url,
    std::string* uuid) {
  URLMap::iterator found =
      url_to_uuid_.find(BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url);
  if (found == url_to_uuid_.end())
    return nullptr;
  Entry* entry = GetEntry(found->second);
  if (entry && uuid)
    uuid->assign(found->second);
  return entry;
}

}  // namespace storage