1 // Copyright (c) 2012 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 5 #include "chrome/renderer/extensions/app_runtime_custom_bindings.h" 6 7 #include "base/bind.h" 8 #include "base/strings/string_number_conversions.h" 9 #include "third_party/WebKit/public/platform/WebCString.h" 10 #include "third_party/WebKit/public/platform/WebString.h" 11 #include "third_party/WebKit/public/web/WebBlob.h" 12 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" 13 14 using blink::WebBlob; 15 using blink::WebSerializedScriptValue; 16 using blink::WebString; 17 18 namespace { 19 DeserializeString(const v8::FunctionCallbackInfo<v8::Value> & args)20void DeserializeString(const v8::FunctionCallbackInfo<v8::Value> &args) { 21 DCHECK(args.Length() == 1); 22 DCHECK(args[0]->IsString()); 23 24 std::string data_v8(*v8::String::Utf8Value(args[0])); 25 WebString data_webstring = WebString::fromUTF8(data_v8); 26 WebSerializedScriptValue serialized = 27 WebSerializedScriptValue::fromString(data_webstring); 28 args.GetReturnValue().Set(serialized.deserialize()); 29 } 30 SerializeToString(const v8::FunctionCallbackInfo<v8::Value> & args)31void SerializeToString(const v8::FunctionCallbackInfo<v8::Value> &args) { 32 DCHECK(args.Length() == 1); 33 WebSerializedScriptValue data = 34 WebSerializedScriptValue::serialize(args[0]); 35 WebString data_webstring = data.toString(); 36 37 std::string v = std::string(data_webstring.utf8()); 38 args.GetReturnValue() 39 .Set(v8::String::NewFromUtf8(args.GetIsolate(), v.c_str())); 40 } 41 CreateBlob(const v8::FunctionCallbackInfo<v8::Value> & args)42void CreateBlob(const v8::FunctionCallbackInfo<v8::Value> &args) { 43 DCHECK(args.Length() == 2); 44 DCHECK(args[0]->IsString()); 45 DCHECK(args[1]->IsNumber()); 46 47 std::string blob_file_path(*v8::String::Utf8Value(args[0])); 48 std::string blob_length_string(*v8::String::Utf8Value(args[1])); 49 int64 blob_length = 0; 50 DCHECK(base::StringToInt64(blob_length_string, &blob_length)); 51 blink::WebBlob web_blob = WebBlob::createFromFile( 52 WebString::fromUTF8(blob_file_path), blob_length); 53 args.GetReturnValue().Set(web_blob.toV8Value()); 54 } 55 56 } // namespace 57 58 namespace extensions { 59 AppRuntimeCustomBindings(Dispatcher * dispatcher,ChromeV8Context * context)60AppRuntimeCustomBindings::AppRuntimeCustomBindings( 61 Dispatcher* dispatcher, 62 ChromeV8Context* context) : ChromeV8Extension(dispatcher, context) { 63 RouteFunction("DeserializeString", base::Bind(&DeserializeString)); 64 RouteFunction("SerializeToString", base::Bind(&SerializeToString)); 65 RouteFunction("CreateBlob", base::Bind(&CreateBlob)); 66 } 67 68 } // namespace extensions 69