summaryrefslogtreecommitdiffstats
path: root/extensions/renderer/app_runtime_custom_bindings.cc
blob: 4302590d0a2cffc770bcab352000bee5da98cb7f (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
// Copyright 2014 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 "extensions/renderer/app_runtime_custom_bindings.h"

#include "base/bind.h"
#include "base/strings/string_number_conversions.h"
#include "third_party/WebKit/public/platform/WebCString.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/web/WebBlob.h"
#include "third_party/WebKit/public/web/WebSerializedScriptValue.h"

using blink::WebBlob;
using blink::WebSerializedScriptValue;
using blink::WebString;

namespace {

void DeserializeString(const v8::FunctionCallbackInfo<v8::Value>& args) {
  DCHECK(args.Length() == 1);
  DCHECK(args[0]->IsString());

  std::string data_v8(*v8::String::Utf8Value(args[0]));
  WebString data_webstring = WebString::fromUTF8(data_v8);
  WebSerializedScriptValue serialized =
      WebSerializedScriptValue::fromString(data_webstring);
  args.GetReturnValue().Set(serialized.deserialize());
}

void SerializeToString(const v8::FunctionCallbackInfo<v8::Value>& args) {
  DCHECK(args.Length() == 1);
  WebSerializedScriptValue data = WebSerializedScriptValue::serialize(args[0]);
  WebString data_webstring = data.toString();

  std::string v = std::string(data_webstring.utf8());
  args.GetReturnValue().Set(
      v8::String::NewFromUtf8(args.GetIsolate(), v.c_str()));
}

void CreateBlob(const v8::FunctionCallbackInfo<v8::Value>& args) {
  DCHECK(args.Length() == 2);
  DCHECK(args[0]->IsString());
  DCHECK(args[1]->IsNumber());

  std::string blob_file_path(*v8::String::Utf8Value(args[0]));
  std::string blob_length_string(*v8::String::Utf8Value(args[1]));
  int64 blob_length = 0;
  DCHECK(base::StringToInt64(blob_length_string, &blob_length));
  blink::WebBlob web_blob =
      WebBlob::createFromFile(WebString::fromUTF8(blob_file_path), blob_length);
  args.GetReturnValue().Set(
      web_blob.toV8Value(args.Holder(), args.GetIsolate()));
}

}  // namespace

namespace extensions {

AppRuntimeCustomBindings::AppRuntimeCustomBindings(ScriptContext* context)
    : ObjectBackedNativeHandler(context) {
  RouteFunction("DeserializeString", base::Bind(&DeserializeString));
  RouteFunction("SerializeToString", base::Bind(&SerializeToString));
  RouteFunction("CreateBlob", base::Bind(&CreateBlob));
}

}  // namespace extensions