[email protected] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 4d6f38f | 2013-06-18 11:09:58 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 5 | #include "extensions/renderer/blob_native_handler.h" |
[email protected] | 4d6f38f | 2013-06-18 11:09:58 | [diff] [blame] | 6 | |
| 7 | #include "base/bind.h" |
yukishiino | 42bd05a2 | 2015-06-15 08:17:08 | [diff] [blame] | 8 | #include "extensions/renderer/script_context.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 9 | #include "third_party/blink/public/platform/web_url.h" |
| 10 | #include "third_party/blink/public/web/web_blob.h" |
[email protected] | 4d6f38f | 2013-06-18 11:09:58 | [diff] [blame] | 11 | |
| 12 | namespace { |
| 13 | |
[email protected] | 4084ef9 | 2013-11-22 22:30:16 | [diff] [blame] | 14 | // Expects a single Blob argument. Returns the Blob's UUID. |
| 15 | void GetBlobUuid(const v8::FunctionCallbackInfo<v8::Value>& args) { |
rdevlin.cronin | 415b73b | 2015-11-13 01:14:47 | [diff] [blame] | 16 | CHECK_EQ(1, args.Length()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 17 | blink::WebBlob blob = blink::WebBlob::FromV8Value(args[0]); |
Yang Guo | 1d9f5a6 | 2018-09-25 19:17:37 | [diff] [blame] | 18 | args.GetReturnValue().Set(v8::String::NewFromUtf8(args.GetIsolate(), |
| 19 | blob.Uuid().Utf8().data(), |
| 20 | v8::NewStringType::kNormal) |
| 21 | .ToLocalChecked()); |
[email protected] | 4d6f38f | 2013-06-18 11:09:58 | [diff] [blame] | 22 | } |
| 23 | |
yukishiino | 42bd05a2 | 2015-06-15 08:17:08 | [diff] [blame] | 24 | } // namespace |
| 25 | |
| 26 | namespace extensions { |
| 27 | |
| 28 | BlobNativeHandler::BlobNativeHandler(ScriptContext* context) |
Devlin Cronin | d9ea834 | 2018-01-27 06:00:04 | [diff] [blame] | 29 | : ObjectBackedNativeHandler(context) {} |
| 30 | |
| 31 | void BlobNativeHandler::AddRoutes() { |
Devlin Cronin | cc02a0c | 2019-01-03 22:15:07 | [diff] [blame] | 32 | RouteHandlerFunction("GetBlobUuid", base::BindRepeating(&GetBlobUuid)); |
| 33 | RouteHandlerFunction( |
| 34 | "TakeBrowserProcessBlob", |
| 35 | base::BindRepeating(&BlobNativeHandler::TakeBrowserProcessBlob, |
| 36 | base::Unretained(this))); |
yukishiino | 42bd05a2 | 2015-06-15 08:17:08 | [diff] [blame] | 37 | } |
| 38 | |
[email protected] | c0b5eb0 | 2014-06-02 17:28:10 | [diff] [blame] | 39 | // Take ownership of a Blob created on the browser process. Expects the Blob's |
| 40 | // UUID, type, and size as arguments. Returns the Blob we just took to |
| 41 | // Javascript. The Blob reference in the browser process is dropped through |
| 42 | // a separate flow to avoid leaking Blobs if the script context is destroyed. |
yukishiino | 42bd05a2 | 2015-06-15 08:17:08 | [diff] [blame] | 43 | void BlobNativeHandler::TakeBrowserProcessBlob( |
| 44 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
rdevlin.cronin | 415b73b | 2015-11-13 01:14:47 | [diff] [blame] | 45 | CHECK_EQ(3, args.Length()); |
| 46 | CHECK(args[0]->IsString()); |
| 47 | CHECK(args[1]->IsString()); |
| 48 | CHECK(args[2]->IsInt32()); |
Adam Klein | 686c0e5 | 2018-01-17 23:42:33 | [diff] [blame] | 49 | v8::Isolate* isolate = args.GetIsolate(); |
| 50 | std::string uuid(*v8::String::Utf8Value(isolate, args[0])); |
| 51 | std::string type(*v8::String::Utf8Value(isolate, args[1])); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 52 | blink::WebBlob blob = blink::WebBlob::CreateFromUUID( |
| 53 | blink::WebString::FromUTF8(uuid), blink::WebString::FromUTF8(type), |
Dan Elphick | d010a85a | 2018-08-03 11:32:26 | [diff] [blame] | 54 | args[2].As<v8::Int32>()->Value()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 55 | args.GetReturnValue().Set( |
Adam Klein | 686c0e5 | 2018-01-17 23:42:33 | [diff] [blame] | 56 | blob.ToV8Value(context()->v8_context()->Global(), isolate)); |
[email protected] | 4d6f38f | 2013-06-18 11:09:58 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | } // namespace extensions |