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/app_runtime_custom_bindings.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "extensions/renderer/script_context.h"
10 #include "third_party/WebKit/public/platform/WebCString.h"
11 #include "third_party/WebKit/public/platform/WebString.h"
12 #include "third_party/WebKit/public/web/WebBlob.h"
13 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h"
16 using blink::WebSerializedScriptValue
;
17 using blink::WebString
;
21 void DeserializeString(const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
22 DCHECK(args
.Length() == 1);
23 DCHECK(args
[0]->IsString());
25 std::string
data_v8(*v8::String::Utf8Value(args
[0]));
26 WebString data_webstring
= WebString::fromUTF8(data_v8
);
27 WebSerializedScriptValue serialized
=
28 WebSerializedScriptValue::fromString(data_webstring
);
29 args
.GetReturnValue().Set(serialized
.deserialize());
32 void SerializeToString(const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
33 DCHECK(args
.Length() == 1);
34 WebSerializedScriptValue data
= WebSerializedScriptValue::serialize(args
[0]);
35 WebString data_webstring
= data
.toString();
37 std::string v
= std::string(data_webstring
.utf8());
38 args
.GetReturnValue().Set(
39 v8::String::NewFromUtf8(args
.GetIsolate(), v
.c_str()));
44 namespace extensions
{
46 AppRuntimeCustomBindings::AppRuntimeCustomBindings(ScriptContext
* context
)
47 : ObjectBackedNativeHandler(context
) {
48 RouteFunction("DeserializeString", base::Bind(&DeserializeString
));
49 RouteFunction("SerializeToString", base::Bind(&SerializeToString
));
50 RouteFunction("CreateBlob", base::Bind(&AppRuntimeCustomBindings::CreateBlob
,
51 base::Unretained(this)));
54 void AppRuntimeCustomBindings::CreateBlob(
55 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
56 DCHECK(args
.Length() == 2);
57 DCHECK(args
[0]->IsString());
58 DCHECK(args
[1]->IsNumber());
60 std::string
blob_file_path(*v8::String::Utf8Value(args
[0]));
61 std::string
blob_length_string(*v8::String::Utf8Value(args
[1]));
62 int64 blob_length
= 0;
63 DCHECK(base::StringToInt64(blob_length_string
, &blob_length
));
64 blink::WebBlob web_blob
=
65 WebBlob::createFromFile(WebString::fromUTF8(blob_file_path
), blob_length
);
66 args
.GetReturnValue().Set(
67 web_blob
.toV8Value(context()->v8_context()->Global(), args
.GetIsolate()));
70 } // namespace extensions