blob_native_handler.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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.
  4. #include "extensions/renderer/blob_native_handler.h"
  5. #include "base/bind.h"
  6. #include "extensions/renderer/script_context.h"
  7. #include "third_party/blink/public/platform/web_url.h"
  8. #include "third_party/blink/public/web/web_blob.h"
  9. #include "v8/include/v8-function-callback.h"
  10. #include "v8/include/v8-primitive.h"
  11. namespace {
  12. // Expects a single Blob argument. Returns the Blob's UUID.
  13. void GetBlobUuid(const v8::FunctionCallbackInfo<v8::Value>& args) {
  14. CHECK_EQ(1, args.Length());
  15. blink::WebBlob blob = blink::WebBlob::FromV8Value(args[0]);
  16. args.GetReturnValue().Set(
  17. v8::String::NewFromUtf8(args.GetIsolate(), blob.Uuid().Utf8().data())
  18. .ToLocalChecked());
  19. }
  20. } // namespace
  21. namespace extensions {
  22. BlobNativeHandler::BlobNativeHandler(ScriptContext* context)
  23. : ObjectBackedNativeHandler(context) {}
  24. void BlobNativeHandler::AddRoutes() {
  25. RouteHandlerFunction("GetBlobUuid", base::BindRepeating(&GetBlobUuid));
  26. RouteHandlerFunction(
  27. "TakeBrowserProcessBlob",
  28. base::BindRepeating(&BlobNativeHandler::TakeBrowserProcessBlob,
  29. base::Unretained(this)));
  30. }
  31. // Take ownership of a Blob created on the browser process. Expects the Blob's
  32. // UUID, type, and size as arguments. Returns the Blob we just took to
  33. // Javascript. The Blob reference in the browser process is dropped through
  34. // a separate flow to avoid leaking Blobs if the script context is destroyed.
  35. void BlobNativeHandler::TakeBrowserProcessBlob(
  36. const v8::FunctionCallbackInfo<v8::Value>& args) {
  37. CHECK_EQ(3, args.Length());
  38. CHECK(args[0]->IsString());
  39. CHECK(args[1]->IsString());
  40. CHECK(args[2]->IsInt32());
  41. v8::Isolate* isolate = args.GetIsolate();
  42. std::string uuid(*v8::String::Utf8Value(isolate, args[0]));
  43. std::string type(*v8::String::Utf8Value(isolate, args[1]));
  44. blink::WebBlob blob = blink::WebBlob::CreateFromUUID(
  45. blink::WebString::FromUTF8(uuid), blink::WebString::FromUTF8(type),
  46. args[2].As<v8::Int32>()->Value());
  47. args.GetReturnValue().Set(
  48. blob.ToV8Value(context()->v8_context()->Global(), isolate));
  49. }
  50. } // namespace extensions