summaryrefslogtreecommitdiffstats
path: root/content/child/webblobregistry_impl.cc
blob: 1967c7b151a0e551cf6d0cddb17c1ff5ef0567aa (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2013 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 "content/child/webblobregistry_impl.h"

#include "base/files/file_path.h"
#include "base/guid.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/shared_memory.h"
#include "base/message_loop/message_loop.h"
#include "base/trace_event/trace_event.h"
#include "content/child/child_thread_impl.h"
#include "content/child/thread_safe_sender.h"
#include "content/common/fileapi/webblob_messages.h"
#include "third_party/WebKit/public/platform/WebBlobData.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/platform/WebThreadSafeData.h"
#include "third_party/WebKit/public/platform/WebURL.h"

using blink::WebBlobData;
using blink::WebString;
using blink::WebThreadSafeData;
using blink::WebURL;
using blink::WebBlobRegistry;
using storage::DataElement;

namespace content {

namespace {

const size_t kLargeThresholdBytes = 250 * 1024;
const size_t kMaxSharedMemoryBytes = 10 * 1024 * 1024;

}  // namespace

WebBlobRegistryImpl::WebBlobRegistryImpl(ThreadSafeSender* sender)
    : sender_(sender) {
  // Record a dummy trace event on startup so the 'Storage' category shows up
  // in the chrome://tracing viewer.
  TRACE_EVENT0("Blob", "Init");
}

WebBlobRegistryImpl::~WebBlobRegistryImpl() {
}

blink::WebBlobRegistry::Builder* WebBlobRegistryImpl::createBuilder(
    const blink::WebString& uuid,
    const blink::WebString& contentType) {
  return new BuilderImpl(uuid, contentType, sender_.get());
}

void WebBlobRegistryImpl::registerBlobData(const blink::WebString& uuid,
                                           const blink::WebBlobData& data) {
  TRACE_EVENT0("Blob", "Registry::RegisterBlob");
  scoped_ptr<Builder> builder(createBuilder(uuid, data.contentType()));

  // This is temporary until we move to createBuilder() as our blob creation
  // method.
  size_t i = 0;
  WebBlobData::Item data_item;
  while (data.itemAt(i++, data_item)) {
    if (data_item.length == 0) {
      continue;
    }
    switch (data_item.type) {
      case WebBlobData::Item::TypeData: {
        // WebBlobData does not allow partial data items.
        DCHECK(!data_item.offset && data_item.length == -1);
        builder->appendData(data_item.data);
        break;
      }
      case WebBlobData::Item::TypeFile:
        builder->appendFile(data_item.filePath,
                            static_cast<uint64_t>(data_item.offset),
                            static_cast<uint64_t>(data_item.length),
                            data_item.expectedModificationTime);
        break;
      case WebBlobData::Item::TypeBlob:
        builder->appendBlob(data_item.blobUUID, data_item.offset,
                            data_item.length);
        break;
      case WebBlobData::Item::TypeFileSystemURL:
        // We only support filesystem URL as of now.
        DCHECK(GURL(data_item.fileSystemURL).SchemeIsFileSystem());
        builder->appendFileSystemURL(data_item.fileSystemURL,
                                     static_cast<uint64>(data_item.offset),
                                     static_cast<uint64>(data_item.length),
                                     data_item.expectedModificationTime);
        break;
      default:
        NOTREACHED();
    }
  }
  builder->build();
}

void WebBlobRegistryImpl::addBlobDataRef(const WebString& uuid) {
  sender_->Send(new BlobHostMsg_IncrementRefCount(uuid.utf8()));
}

void WebBlobRegistryImpl::removeBlobDataRef(const WebString& uuid) {
  sender_->Send(new BlobHostMsg_DecrementRefCount(uuid.utf8()));
}

void WebBlobRegistryImpl::registerPublicBlobURL(const WebURL& url,
                                                const WebString& uuid) {
  sender_->Send(new BlobHostMsg_RegisterPublicURL(url, uuid.utf8()));
}

void WebBlobRegistryImpl::revokePublicBlobURL(const WebURL& url) {
  sender_->Send(new BlobHostMsg_RevokePublicURL(url));
}

// ------ streams stuff -----

void WebBlobRegistryImpl::registerStreamURL(const WebURL& url,
                                            const WebString& content_type) {
  DCHECK(ChildThreadImpl::current());
  sender_->Send(new StreamHostMsg_StartBuilding(url, content_type.utf8()));
}

void WebBlobRegistryImpl::registerStreamURL(const WebURL& url,
                                            const WebURL& src_url) {
  DCHECK(ChildThreadImpl::current());
  sender_->Send(new StreamHostMsg_Clone(url, src_url));
}

void WebBlobRegistryImpl::addDataToStream(const WebURL& url,
                                          const char* data,
                                          size_t length) {
  DCHECK(ChildThreadImpl::current());
  if (length == 0)
    return;
  if (length < kLargeThresholdBytes) {
    DataElement item;
    item.SetToBytes(data, length);
    sender_->Send(new StreamHostMsg_AppendBlobDataItem(url, item));
  } else {
    // We handle larger amounts of data via SharedMemory instead of
    // writing it directly to the IPC channel.
    size_t shared_memory_size = std::min(length, kMaxSharedMemoryBytes);
    scoped_ptr<base::SharedMemory> shared_memory(
        ChildThreadImpl::AllocateSharedMemory(shared_memory_size,
                                              sender_.get()));
    CHECK(shared_memory.get());
    if (!shared_memory->Map(shared_memory_size))
      CHECK(false);

    size_t remaining_bytes = length;
    const char* current_ptr = data;
    while (remaining_bytes) {
      size_t chunk_size = std::min(remaining_bytes, shared_memory_size);
      memcpy(shared_memory->memory(), current_ptr, chunk_size);
      sender_->Send(new StreamHostMsg_SyncAppendSharedMemory(
          url, shared_memory->handle(), chunk_size));
      remaining_bytes -= chunk_size;
      current_ptr += chunk_size;
    }
  }
}

void WebBlobRegistryImpl::flushStream(const WebURL& url) {
  DCHECK(ChildThreadImpl::current());
  sender_->Send(new StreamHostMsg_Flush(url));
}

void WebBlobRegistryImpl::finalizeStream(const WebURL& url) {
  DCHECK(ChildThreadImpl::current());
  sender_->Send(new StreamHostMsg_FinishBuilding(url));
}

void WebBlobRegistryImpl::abortStream(const WebURL& url) {
  DCHECK(ChildThreadImpl::current());
  sender_->Send(new StreamHostMsg_AbortBuilding(url));
}

void WebBlobRegistryImpl::unregisterStreamURL(const WebURL& url) {
  DCHECK(ChildThreadImpl::current());
  sender_->Send(new StreamHostMsg_Remove(url));
}

WebBlobRegistryImpl::BuilderImpl::BuilderImpl(
    const blink::WebString& uuid,
    const blink::WebString& content_type,
    ThreadSafeSender* sender)
    : uuid_(uuid.utf8()), content_type_(content_type.utf8()), sender_(sender) {
}

WebBlobRegistryImpl::BuilderImpl::~BuilderImpl() {
}

void WebBlobRegistryImpl::BuilderImpl::appendData(
    const WebThreadSafeData& data) {
  consolidation_.AddDataItem(data);
}

void WebBlobRegistryImpl::BuilderImpl::appendBlob(const WebString& uuid,
                                                  uint64_t offset,
                                                  uint64_t length) {
  consolidation_.AddBlobItem(uuid.utf8(), offset, length);
}

void WebBlobRegistryImpl::BuilderImpl::appendFile(
    const WebString& path,
    uint64_t offset,
    uint64_t length,
    double expected_modification_time) {
  consolidation_.AddFileItem(
      base::FilePath::FromUTF16Unsafe(base::string16(path)), offset, length,
      expected_modification_time);
}

void WebBlobRegistryImpl::BuilderImpl::appendFileSystemURL(
    const WebURL& fileSystemURL,
    uint64_t offset,
    uint64_t length,
    double expected_modification_time) {
  DCHECK(GURL(fileSystemURL).SchemeIsFileSystem());
  consolidation_.AddFileSystemItem(GURL(fileSystemURL), offset, length,
                                   expected_modification_time);
}

void WebBlobRegistryImpl::BuilderImpl::build() {
  sender_->Send(new BlobHostMsg_StartBuilding(uuid_));
  const auto& items = consolidation_.consolidated_items();

  // We still need a buffer to hold the continuous block of data so the
  // DataElement can hold it.
  size_t buffer_size = 0;
  scoped_ptr<char[]> buffer;
  for (size_t i = 0; i < items.size(); i++) {
    const BlobConsolidation::ConsolidatedItem& item = items[i];
    DataElement element;
    // NOTE: length == -1 when we want to use the whole file.  This
    // only happens when we are creating a file object in Blink, and the file
    // object is the only item in the 'blob'.  If we use that file blob to
    // create another blob, it is sent here as a 'file' item and not a blob,
    // and the correct size is populated.
    // static_cast<uint64>(-1) == kuint64max, which is what DataElement uses
    // to specificy "use the whole file".
    switch (item.type) {
      case DataElement::TYPE_BYTES:
        if (item.length > kLargeThresholdBytes) {
          SendOversizedDataForBlob(i);
          break;
        }
        if (buffer_size < item.length) {
          buffer.reset(new char[item.length]);
          buffer_size = item.length;
        }
        consolidation_.ReadMemory(i, 0, item.length, buffer.get());
        element.SetToSharedBytes(buffer.get(), item.length);
        sender_->Send(new BlobHostMsg_AppendBlobDataItem(uuid_, element));
        break;
      case DataElement::TYPE_FILE:
        element.SetToFilePathRange(
            item.path, item.offset, item.length,
            base::Time::FromDoubleT(item.expected_modification_time));
        sender_->Send(new BlobHostMsg_AppendBlobDataItem(uuid_, element));
        break;
      case DataElement::TYPE_BLOB:
        element.SetToBlobRange(item.blob_uuid, item.offset, item.length);
        sender_->Send(new BlobHostMsg_AppendBlobDataItem(uuid_, element));
        break;
      case DataElement::TYPE_FILE_FILESYSTEM:
        element.SetToFileSystemUrlRange(
            item.filesystem_url, item.offset, item.length,
            base::Time::FromDoubleT(item.expected_modification_time));
        sender_->Send(new BlobHostMsg_AppendBlobDataItem(uuid_, element));
        break;
      default:
        NOTREACHED();
    }
  }
  sender_->Send(new BlobHostMsg_FinishBuilding(uuid_, content_type_));
}

void WebBlobRegistryImpl::BuilderImpl::SendOversizedDataForBlob(
    size_t consolidated_item_index) {
  TRACE_EVENT0("Blob", "Registry::SendOversizedBlobData");
  const BlobConsolidation::ConsolidatedItem& item =
      consolidation_.consolidated_items()[consolidated_item_index];
  // We handle larger amounts of data via SharedMemory instead of
  // writing it directly to the IPC channel.

  size_t data_size = item.length;
  size_t shared_memory_size = std::min(data_size, kMaxSharedMemoryBytes);
  scoped_ptr<base::SharedMemory> shared_memory(
      ChildThreadImpl::AllocateSharedMemory(shared_memory_size, sender_.get()));
  CHECK(shared_memory.get());
  const bool mapped = shared_memory->Map(shared_memory_size);
  CHECK(mapped) << "Unable to map shared memory.";

  size_t offset = 0;
  while (data_size) {
    TRACE_EVENT0("Blob", "Registry::SendOversizedBlobItem");
    size_t chunk_size = std::min(data_size, shared_memory_size);
    consolidation_.ReadMemory(consolidated_item_index, offset, chunk_size,
                              shared_memory->memory());
    sender_->Send(new BlobHostMsg_SyncAppendSharedMemory(
        uuid_, shared_memory->handle(), chunk_size));
    data_size -= chunk_size;
    offset += chunk_size;
  }
}

}  // namespace content