summaryrefslogtreecommitdiffstats
path: root/components/html_viewer/mock_web_blob_registry_impl.cc
blob: 02cff578282b53a145f1167bece7ada8d05b85ea (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
117
118
119
120
121
// 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 "components/html_viewer/mock_web_blob_registry_impl.h"

#include "third_party/WebKit/public/platform/WebBlobData.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/platform/WebURL.h"
#include "third_party/WebKit/public/platform/WebVector.h"

using blink::WebBlobData;
using blink::WebString;
using blink::WebURL;
using blink::WebVector;

namespace html_viewer {

MockWebBlobRegistryImpl::MockWebBlobRegistryImpl() {
}

MockWebBlobRegistryImpl::~MockWebBlobRegistryImpl() {
}

void MockWebBlobRegistryImpl::registerBlobData(const WebString& uuid,
                                               const WebBlobData& data) {
  const std::string uuid_str(uuid.utf8());
  blob_ref_count_map_[uuid_str] = 1;
  scoped_ptr<ScopedVector<blink::WebBlobData::Item>> items(
      new ScopedVector<blink::WebBlobData::Item>);
  items->reserve(data.itemCount());
  for (size_t i = 0; i < data.itemCount(); ++i) {
    scoped_ptr<blink::WebBlobData::Item> item(new blink::WebBlobData::Item);
    data.itemAt(i, *item);
    items->push_back(item.release());
  }
  blob_data_items_map_.set(uuid_str, items.Pass());
}

void MockWebBlobRegistryImpl::addBlobDataRef(const WebString& uuid) {
  blob_ref_count_map_[uuid.utf8()]++;
}

void MockWebBlobRegistryImpl::removeBlobDataRef(const WebString& uuid) {
  const std::string uuid_str(uuid.utf8());
  auto it = blob_ref_count_map_.find(uuid_str);
  if (it != blob_ref_count_map_.end() && !--it->second) {
    blob_data_items_map_.erase(uuid_str);
    blob_ref_count_map_.erase(it);
  }
}

void MockWebBlobRegistryImpl::registerPublicBlobURL(const WebURL& url,
                                                    const WebString& uuid) {
  public_url_to_uuid_[url.spec()] = uuid;
  addBlobDataRef(uuid);
}

void MockWebBlobRegistryImpl::revokePublicBlobURL(const WebURL& url) {
  auto it = public_url_to_uuid_.find(url.spec());
  if (it != public_url_to_uuid_.end()) {
    removeBlobDataRef(it->second);
    public_url_to_uuid_.erase(it);
  }
}

void MockWebBlobRegistryImpl::registerStreamURL(const WebURL& url,
                                                const WebString& content_type) {
  NOTIMPLEMENTED();
}

void MockWebBlobRegistryImpl::registerStreamURL(const WebURL& url,
                                                const blink::WebURL& src_url) {
  NOTIMPLEMENTED();
}

void MockWebBlobRegistryImpl::addDataToStream(const WebURL& url,
                                              const char* data,
                                              size_t length) {
  NOTIMPLEMENTED();
}

void MockWebBlobRegistryImpl::flushStream(const WebURL& url) {
  NOTIMPLEMENTED();
}

void MockWebBlobRegistryImpl::finalizeStream(const WebURL& url) {
  NOTIMPLEMENTED();
}

void MockWebBlobRegistryImpl::abortStream(const WebURL& url) {
  NOTIMPLEMENTED();
}

void MockWebBlobRegistryImpl::unregisterStreamURL(const WebURL& url) {
  NOTIMPLEMENTED();
}

bool MockWebBlobRegistryImpl::GetUUIDForURL(const blink::WebURL& url,
                                            blink::WebString* uuid) const {
  auto it = public_url_to_uuid_.find(url.spec());
  if (it != public_url_to_uuid_.end()) {
    *uuid = it->second;
    return true;
  }

  return false;
}

bool MockWebBlobRegistryImpl::GetBlobItems(
    const WebString& uuid,
    WebVector<WebBlobData::Item*>* items) const {
  ScopedVector<WebBlobData::Item>* item_vector =
      blob_data_items_map_.get(uuid.utf8());
  if (!item_vector)
    return false;
  *items = item_vector->get();
  return true;
}

}  // namespace html_viewer