File: binding_generating_native_handler.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (122 lines) | stat: -rw-r--r-- 4,306 bytes parent folder | download
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// 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/binding_generating_native_handler.h"

#include "base/macros.h"
#include "base/metrics/histogram_macros.h"
#include "base/timer/elapsed_timer.h"
#include "extensions/renderer/script_context.h"
#include "extensions/renderer/v8_helpers.h"

namespace extensions {

using namespace v8_helpers;

BindingGeneratingNativeHandler::BindingGeneratingNativeHandler(
    ScriptContext* context,
    const std::string& api_name,
    const std::string& bind_to)
    : context_(context), api_name_(api_name), bind_to_(bind_to) {}

v8::Local<v8::Object> BindingGeneratingNativeHandler::NewInstance() {
  base::ElapsedTimer timer;
  // This long sequence of commands effectively runs the JavaScript code,
  // such that result[bind_to] is the compiled schema for |api_name|:
  //
  //   var result = {};
  //   result[bind_to] = require('binding').Binding.create(api_name).generate();
  //   return result;
  //
  // Unfortunately using the v8 APIs makes that quite verbose.
  // Each stage is marked with the code it executes.
  v8::Isolate* isolate = context_->isolate();
  v8::EscapableHandleScope scope(isolate);

  // Convert |api_name| and |bind_to| into their v8::Strings to pass
  // through the v8 APIs.
  v8::Local<v8::String> v8_api_name;
  v8::Local<v8::String> v8_bind_to;
  if (!ToV8String(isolate, api_name_, &v8_api_name) ||
      !ToV8String(isolate, bind_to_, &v8_bind_to)) {
    NOTREACHED();
    return v8::Local<v8::Object>();
  }

  v8::Local<v8::Context> v8_context = context_->v8_context();

  // require('binding');
  v8::Local<v8::Object> binding_module;
  if (!context_->module_system()->Require("binding").ToLocal(&binding_module)) {
    NOTREACHED();
    return v8::Local<v8::Object>();
  }

  // require('binding').Binding;
  v8::Local<v8::Value> binding_value;
  v8::Local<v8::Object> binding;
  if (!GetProperty(v8_context, binding_module, "Binding", &binding_value) ||
      !binding_value->ToObject(v8_context).ToLocal(&binding)) {
    NOTREACHED();
    return v8::Local<v8::Object>();
  }

  // require('binding').Binding.create;
  v8::Local<v8::Value> create_binding_value;
  if (!GetProperty(v8_context, binding, "create", &create_binding_value) ||
      !create_binding_value->IsFunction()) {
    NOTREACHED();
    return v8::Local<v8::Object>();
  }
  v8::Local<v8::Function> create_binding =
      create_binding_value.As<v8::Function>();

  // require('Binding').Binding.create(api_name);
  v8::Local<v8::Value> argv[] = {v8_api_name};
  v8::Local<v8::Value> binding_instance_value;
  v8::Local<v8::Object> binding_instance;
  if (!CallFunction(v8_context, create_binding, binding, arraysize(argv), argv,
                    &binding_instance_value) ||
      !binding_instance_value->ToObject(v8_context)
           .ToLocal(&binding_instance)) {
    NOTREACHED();
    return v8::Local<v8::Object>();
  }

  // require('binding').Binding.create(api_name).generate;
  v8::Local<v8::Value> generate_value;
  if (!GetProperty(v8_context, binding_instance, "generate", &generate_value) ||
      !generate_value->IsFunction()) {
    NOTREACHED();
    return v8::Local<v8::Object>();
  }
  v8::Local<v8::Function> generate = generate_value.As<v8::Function>();

  // require('binding').Binding.create(api_name).generate();
  v8::Local<v8::Object> object = v8::Object::New(isolate);
  v8::Local<v8::Value> compiled_schema;
  if (!CallFunction(v8_context, generate, binding_instance, 0, nullptr,
                    &compiled_schema)) {
    NOTREACHED();
    return v8::Local<v8::Object>();
  }

  // var result = {};
  // result[bind_to] = ...;
  if (!SetProperty(v8_context, object, v8_bind_to, compiled_schema)) {
    NOTREACHED();
    return v8::Local<v8::Object>();
  }

  // Log UMA with microsecond accuracy*; maxes at 10 seconds.
  // *Obviously, limited by our TimeTicks implementation, but as close as
  // possible.
  UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ApiBindingObjectGenerationTime",
                              timer.Elapsed().InMicroseconds(),
                              1, 10000000, 100);
  // return result;
  return scope.Escape(object);
}

}  // namespace extensions