summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/extensions/app_runtime_custom_bindings.cc
blob: 60fbace9b1736a8a9bae06f0ef6a552163f4a6ae (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
// Copyright (c) 2012 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 "chrome/renderer/extensions/app_runtime_custom_bindings.h"

#include "base/strings/string_number_conversions.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebCString.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebBlob.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebSerializedScriptValue.h"

using WebKit::WebBlob;
using WebKit::WebSerializedScriptValue;
using WebKit::WebString;

namespace {

v8::Handle<v8::Value> DeserializeString(const v8::Arguments &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);
  return serialized.deserialize();
}

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

  std::string v = std::string(data_webstring.utf8());
  return v8::String::New(v.c_str());
}

v8::Handle<v8::Value> CreateBlob(const v8::Arguments &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));
  WebKit::WebBlob web_blob = WebBlob::createFromFile(
      WebString::fromUTF8(blob_file_path), blob_length);
  return web_blob.toV8Value();
}

}  // namespace

namespace extensions {

AppRuntimeCustomBindings::AppRuntimeCustomBindings(
    Dispatcher* dispatcher,
    v8::Handle<v8::Context> context) : ChromeV8Extension(dispatcher, context) {
  RouteStaticFunction("DeserializeString", &DeserializeString);
  RouteStaticFunction("SerializeToString", &SerializeToString);
  RouteStaticFunction("CreateBlob", &CreateBlob);
}

}  // namespace extensions