Blobs: Catching browser-process created Blobs in extension code.
[chromium-blink-merge.git] / extensions / renderer / blob_native_handler.cc
blobd4087bc80ab415064770eac40616737de2ab6727
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 "extensions/renderer/blob_native_handler.h"
7 #include "base/bind.h"
8 #include "third_party/WebKit/public/platform/WebCString.h"
9 #include "third_party/WebKit/public/platform/WebURL.h"
10 #include "third_party/WebKit/public/web/WebBlob.h"
12 namespace {
14 // Expects a single Blob argument. Returns the Blob's UUID.
15 void GetBlobUuid(const v8::FunctionCallbackInfo<v8::Value>& args) {
16 DCHECK_EQ(1, args.Length());
17 blink::WebBlob blob = blink::WebBlob::fromV8Value(args[0]);
18 args.GetReturnValue().Set(
19 v8::String::NewFromUtf8(args.GetIsolate(), blob.uuid().utf8().data()));
22 // Take ownership of a Blob created on the browser process. Expects the Blob's
23 // UUID, type, and size as arguments. Returns the Blob we just took to
24 // Javascript. The Blob reference in the browser process is dropped through
25 // a separate flow to avoid leaking Blobs if the script context is destroyed.
26 void TakeBrowserProcessBlob(const v8::FunctionCallbackInfo<v8::Value>& args) {
27 DCHECK_EQ(3, args.Length());
28 DCHECK(args[0]->IsString());
29 DCHECK(args[1]->IsString());
30 DCHECK(args[2]->IsInt32());
31 std::string uuid(*v8::String::Utf8Value(args[0]->ToString()));
32 std::string type(*v8::String::Utf8Value(args[1]->ToString()));
33 blink::WebBlob blob =
34 blink::WebBlob::createFromUUID(blink::WebString::fromUTF8(uuid),
35 blink::WebString::fromUTF8(type),
36 args[2]->Int32Value());
37 args.GetReturnValue().Set(blob.toV8Value());
40 } // namespace
42 namespace extensions {
44 BlobNativeHandler::BlobNativeHandler(ScriptContext* context)
45 : ObjectBackedNativeHandler(context) {
46 RouteFunction("GetBlobUuid", base::Bind(&GetBlobUuid));
47 RouteFunction("TakeBrowserProcessBlob", base::Bind(&TakeBrowserProcessBlob));
50 } // namespace extensions