blob: 12068c3da3bd9ec6d0de511e12412ad69b3659ca [file] [log] [blame]
[email protected]701a94e2014-04-17 04:37:371// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]4d6f38f2013-06-18 11:09:582// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]701a94e2014-04-17 04:37:375#include "extensions/renderer/blob_native_handler.h"
[email protected]4d6f38f2013-06-18 11:09:586
7#include "base/bind.h"
yukishiino42bd05a22015-06-15 08:17:088#include "extensions/renderer/script_context.h"
[email protected]4d6f38f2013-06-18 11:09:589#include "third_party/WebKit/public/platform/WebCString.h"
10#include "third_party/WebKit/public/platform/WebURL.h"
11#include "third_party/WebKit/public/web/WebBlob.h"
12
13namespace {
14
[email protected]4084ef92013-11-22 22:30:1615// Expects a single Blob argument. Returns the Blob's UUID.
16void GetBlobUuid(const v8::FunctionCallbackInfo<v8::Value>& args) {
rdevlin.cronin415b73b2015-11-13 01:14:4717 CHECK_EQ(1, args.Length());
[email protected]a1221aea2013-11-07 01:31:3018 blink::WebBlob blob = blink::WebBlob::fromV8Value(args[0]);
[email protected]9c47471e2013-11-28 14:41:2119 args.GetReturnValue().Set(
20 v8::String::NewFromUtf8(args.GetIsolate(), blob.uuid().utf8().data()));
[email protected]4d6f38f2013-06-18 11:09:5821}
22
yukishiino42bd05a22015-06-15 08:17:0823} // namespace
24
25namespace extensions {
26
27BlobNativeHandler::BlobNativeHandler(ScriptContext* context)
28 : ObjectBackedNativeHandler(context) {
29 RouteFunction("GetBlobUuid", base::Bind(&GetBlobUuid));
30 RouteFunction("TakeBrowserProcessBlob",
31 base::Bind(&BlobNativeHandler::TakeBrowserProcessBlob,
32 base::Unretained(this)));
33}
34
[email protected]c0b5eb02014-06-02 17:28:1035// Take ownership of a Blob created on the browser process. Expects the Blob's
36// UUID, type, and size as arguments. Returns the Blob we just took to
37// Javascript. The Blob reference in the browser process is dropped through
38// a separate flow to avoid leaking Blobs if the script context is destroyed.
yukishiino42bd05a22015-06-15 08:17:0839void BlobNativeHandler::TakeBrowserProcessBlob(
40 const v8::FunctionCallbackInfo<v8::Value>& args) {
rdevlin.cronin415b73b2015-11-13 01:14:4741 CHECK_EQ(3, args.Length());
42 CHECK(args[0]->IsString());
43 CHECK(args[1]->IsString());
44 CHECK(args[2]->IsInt32());
dcarneya261b772014-11-20 17:55:0745 std::string uuid(*v8::String::Utf8Value(args[0]));
46 std::string type(*v8::String::Utf8Value(args[1]));
[email protected]c0b5eb02014-06-02 17:28:1047 blink::WebBlob blob =
48 blink::WebBlob::createFromUUID(blink::WebString::fromUTF8(uuid),
49 blink::WebString::fromUTF8(type),
50 args[2]->Int32Value());
yukishiino42bd05a22015-06-15 08:17:0851 args.GetReturnValue().Set(blob.toV8Value(
52 context()->v8_context()->Global(), args.GetIsolate()));
[email protected]4d6f38f2013-06-18 11:09:5853}
54
55} // namespace extensions