summaryrefslogtreecommitdiff
path: root/chromium/extensions/renderer/api_binding.cc
blob: e16bef1472d75bd955cad24d18260e2a29192ae8 (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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Copyright 2016 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/api_binding.h"

#include <algorithm>

#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "extensions/common/extension_api.h"
#include "extensions/renderer/api_binding_hooks.h"
#include "extensions/renderer/api_event_handler.h"
#include "extensions/renderer/api_request_handler.h"
#include "extensions/renderer/api_signature.h"
#include "extensions/renderer/api_type_reference_map.h"
#include "extensions/renderer/v8_helpers.h"
#include "gin/arguments.h"
#include "gin/per_context_data.h"
#include "third_party/WebKit/public/web/WebUserGestureIndicator.h"

namespace extensions {

namespace {

// Returns the name of the enum value for use in JavaScript; JS enum entries use
// SCREAMING_STYLE.
std::string GetJSEnumEntryName(const std::string& original) {
  std::string result;
  DCHECK(!original.empty());
  // If the original starts with a digit, prefix it with an underscore.
  if (base::IsAsciiDigit(original[0]))
    result.push_back('_');
  // Given 'myEnum-Foo':
  for (size_t i = 0; i < original.size(); ++i) {
    // Add an underscore between camelcased items:
    // 'myEnum-Foo' -> 'mY_Enum-Foo'
    if (i > 0 && base::IsAsciiLower(original[i - 1]) &&
        base::IsAsciiUpper(original[i])) {
      result.push_back('_');
      result.push_back(original[i]);
    } else if (original[i] == '-') {  // 'mY_Enum-Foo' -> 'mY_Enum_Foo'
      result.push_back('_');
    } else {  // 'mY_Enum_Foo' -> 'MY_ENUM_FOO'
      result.push_back(base::ToUpperASCII(original[i]));
    }
  }
  return result;
}

bool IsContextValid(v8::Local<v8::Context> context) {
  // If the given context has been disposed, the per-context data has been
  // deleted, and the context is no longer valid. The APIBinding (which owns
  // various necessary pieces) should outlive all contexts, so if the context
  // is valid, associated callbacks should be safe.
  return gin::PerContextData::From(context) != nullptr;
}

void CallbackHelper(const v8::FunctionCallbackInfo<v8::Value>& info) {
  gin::Arguments args(info);
  if (!IsContextValid(args.isolate()->GetCurrentContext()))
    return;

  v8::Local<v8::External> external;
  CHECK(args.GetData(&external));
  auto* callback = static_cast<APIBinding::HandlerCallback*>(external->Value());

  callback->Run(&args);
}

}  // namespace

struct APIBinding::MethodData {
  MethodData(std::string full_name, const APISignature* signature)
      : full_name(std::move(full_name)), signature(signature) {}

  // The fully-qualified name of this api (e.g. runtime.sendMessage instead of
  // sendMessage).
  std::string full_name;
  // The expected API signature.
  const APISignature* signature;
  // The callback used by the v8 function.
  APIBinding::HandlerCallback callback;
};

struct APIBinding::EventData {
  EventData(std::string exposed_name,
            std::string full_name,
            APIEventHandler* event_handler)
      : exposed_name(std::move(exposed_name)),
        full_name(std::move(full_name)),
        event_handler(event_handler) {}

  // The name of the event on the API object (e.g. onCreated).
  std::string exposed_name;
  // The fully-specified name of the event (e.g. tabs.onCreated).
  std::string full_name;
  // The associated event handler. This raw pointer is safe because the
  // EventData is only accessed from the callbacks associated with the
  // APIBinding, and both the APIBinding and APIEventHandler are owned by the
  // same object (the APIBindingsSystem).
  APIEventHandler* event_handler;
};

struct APIBinding::CustomPropertyData {
  CustomPropertyData(const std::string& type_name,
                     const std::string& property_name,
                     const CreateCustomType& create_custom_type)
      : type_name(type_name),
        property_name(property_name),
        create_custom_type(create_custom_type) {}

  // The type of the property, e.g. 'storage.StorageArea'.
  std::string type_name;
  // The name of the property on the object, e.g. 'local' for
  // chrome.storage.local.
  std::string property_name;

  CreateCustomType create_custom_type;
};

APIBinding::APIBinding(const std::string& api_name,
                       const base::ListValue* function_definitions,
                       const base::ListValue* type_definitions,
                       const base::ListValue* event_definitions,
                       const base::DictionaryValue* property_definitions,
                       const CreateCustomType& create_custom_type,
                       std::unique_ptr<APIBindingHooks> binding_hooks,
                       APITypeReferenceMap* type_refs,
                       APIRequestHandler* request_handler,
                       APIEventHandler* event_handler)
    : api_name_(api_name),
      property_definitions_(property_definitions),
      create_custom_type_(create_custom_type),
      binding_hooks_(std::move(binding_hooks)),
      type_refs_(type_refs),
      request_handler_(request_handler),
      weak_factory_(this) {
  // TODO(devlin): It might make sense to instantiate the object_template_
  // directly here, which would avoid the need to hold on to
  // |property_definitions_| and |enums_|. However, there are *some* cases where
  // we don't immediately stamp out an API from the template following
  // construction.

  if (function_definitions) {
    for (const auto& func : *function_definitions) {
      const base::DictionaryValue* func_dict = nullptr;
      CHECK(func->GetAsDictionary(&func_dict));
      std::string name;
      CHECK(func_dict->GetString("name", &name));

      const base::ListValue* params = nullptr;
      CHECK(func_dict->GetList("parameters", &params));
      auto signature = base::MakeUnique<APISignature>(*params);
      std::string full_name =
          base::StringPrintf("%s.%s", api_name_.c_str(), name.c_str());
      methods_[name] = base::MakeUnique<MethodData>(full_name, signature.get());
      type_refs->AddAPIMethodSignature(full_name, std::move(signature));
    }
  }

  if (type_definitions) {
    for (const auto& type : *type_definitions) {
      const base::DictionaryValue* type_dict = nullptr;
      CHECK(type->GetAsDictionary(&type_dict));
      std::string id;
      CHECK(type_dict->GetString("id", &id));
      auto argument_spec = base::MakeUnique<ArgumentSpec>(*type_dict);
      const std::set<std::string>& enum_values = argument_spec->enum_values();
      if (!enum_values.empty()) {
        // Type names may be prefixed by the api name. If so, remove the prefix.
        base::Optional<std::string> stripped_id;
        if (base::StartsWith(id, api_name_, base::CompareCase::SENSITIVE))
          stripped_id = id.substr(api_name_.size() + 1);  // +1 for trailing '.'
        std::vector<EnumEntry>& entries =
            enums_[stripped_id ? *stripped_id : id];
        entries.reserve(enum_values.size());
        for (const auto& enum_value : enum_values) {
          entries.push_back(
              std::make_pair(enum_value, GetJSEnumEntryName(enum_value)));
        }
      }
      type_refs->AddSpec(id, std::move(argument_spec));
      // Some types, like storage.StorageArea, have functions associated with
      // them. Cache the function signatures in the type map.
      const base::ListValue* type_functions = nullptr;
      if (type_dict->GetList("functions", &type_functions)) {
        for (const auto& func : *type_functions) {
          const base::DictionaryValue* func_dict = nullptr;
          CHECK(func->GetAsDictionary(&func_dict));
          std::string function_name;
          CHECK(func_dict->GetString("name", &function_name));

          const base::ListValue* params = nullptr;
          CHECK(func_dict->GetList("parameters", &params));
          type_refs->AddTypeMethodSignature(
              base::StringPrintf("%s.%s", id.c_str(), function_name.c_str()),
              base::MakeUnique<APISignature>(*params));
        }
      }
    }
  }

  if (event_definitions) {
    events_.reserve(event_definitions->GetSize());
    for (const auto& event : *event_definitions) {
      const base::DictionaryValue* event_dict = nullptr;
      CHECK(event->GetAsDictionary(&event_dict));
      std::string name;
      CHECK(event_dict->GetString("name", &name));
      std::string full_name =
          base::StringPrintf("%s.%s", api_name_.c_str(), name.c_str());
      events_.push_back(base::MakeUnique<EventData>(
          std::move(name), std::move(full_name), event_handler));
    }
  }
}

APIBinding::~APIBinding() {}

v8::Local<v8::Object> APIBinding::CreateInstance(
    v8::Local<v8::Context> context,
    v8::Isolate* isolate,
    const AvailabilityCallback& is_available) {
  DCHECK(IsContextValid(context));
  if (object_template_.IsEmpty())
    InitializeTemplate(isolate);
  DCHECK(!object_template_.IsEmpty());

  v8::Local<v8::Object> object =
      object_template_.Get(isolate)->NewInstance(context).ToLocalChecked();

  // The object created from the template includes all methods, but some may
  // be unavailable in this context. Iterate over them and delete any that
  // aren't available.
  // TODO(devlin): Ideally, we'd only do this check on the methods that are
  // conditionally exposed. Or, we could have multiple templates for different
  // configurations, assuming there are a small number of possibilities.
  // TODO(devlin): enums should always be exposed, but there may be events that
  // are restricted. Investigate.
  for (const auto& key_value : methods_) {
    if (!is_available.Run(key_value.second->full_name)) {
      v8::Maybe<bool> success = object->Delete(
          context, gin::StringToSymbol(isolate, key_value.first));
      CHECK(success.IsJust());
      CHECK(success.FromJust());
    }
  }

  binding_hooks_->InitializeInContext(context);

  return object;
}

void APIBinding::InitializeTemplate(v8::Isolate* isolate) {
  DCHECK(object_template_.IsEmpty());
  v8::Local<v8::ObjectTemplate> object_template =
      v8::ObjectTemplate::New(isolate);

  for (const auto& key_value : methods_) {
    MethodData& method = *key_value.second;
    DCHECK(method.callback.is_null());
    method.callback =
        base::Bind(&APIBinding::HandleCall, weak_factory_.GetWeakPtr(),
                   method.full_name, method.signature);

    object_template->Set(
        gin::StringToSymbol(isolate, key_value.first),
        v8::FunctionTemplate::New(isolate, &CallbackHelper,
                                  v8::External::New(isolate, &method.callback),
                                  v8::Local<v8::Signature>(), 0,
                                  v8::ConstructorBehavior::kThrow));
  }

  for (const auto& event : events_) {
    object_template->SetLazyDataProperty(
        gin::StringToSymbol(isolate, event->exposed_name),
        &APIBinding::GetEventObject, v8::External::New(isolate, event.get()));
  }

  for (const auto& entry : enums_) {
    v8::Local<v8::ObjectTemplate> enum_object =
        v8::ObjectTemplate::New(isolate);
    for (const auto& enum_entry : entry.second) {
      enum_object->Set(gin::StringToSymbol(isolate, enum_entry.second),
                       gin::StringToSymbol(isolate, enum_entry.first));
    }
    object_template->Set(isolate, entry.first.c_str(), enum_object);
  }

  if (property_definitions_) {
    DecorateTemplateWithProperties(isolate, object_template,
                                   *property_definitions_);
  }

  object_template_.Set(isolate, object_template);
}

void APIBinding::DecorateTemplateWithProperties(
    v8::Isolate* isolate,
    v8::Local<v8::ObjectTemplate> object_template,
    const base::DictionaryValue& properties) {
  static const char kValueKey[] = "value";
  for (base::DictionaryValue::Iterator iter(properties); !iter.IsAtEnd();
       iter.Advance()) {
    const base::DictionaryValue* dict = nullptr;
    CHECK(iter.value().GetAsDictionary(&dict));
    bool optional = false;
    if (dict->GetBoolean("optional", &optional)) {
      // TODO(devlin): What does optional even mean here? It's only used, it
      // seems, for lastError and inIncognitoContext, which are both handled
      // with custom bindings. Investigate, and remove.
      continue;
    }

    v8::Local<v8::String> v8_key = gin::StringToSymbol(isolate, iter.key());
    std::string ref;
    if (dict->GetString("$ref", &ref)) {
      auto property_data = base::MakeUnique<CustomPropertyData>(
          ref, iter.key(), create_custom_type_);
      object_template->SetLazyDataProperty(
          v8_key, &APIBinding::GetCustomPropertyObject,
          v8::External::New(isolate, property_data.get()));
      custom_properties_.push_back(std::move(property_data));
      continue;
    }

    std::string type;
    CHECK(dict->GetString("type", &type));
    if (type != "object" && !dict->HasKey(kValueKey)) {
      // TODO(devlin): What does a fundamental property not having a value mean?
      // It doesn't seem useful, and looks like it's only used by runtime.id,
      // which is set by custom bindings. Investigate, and remove.
      continue;
    }
    if (type == "integer") {
      int val = 0;
      CHECK(dict->GetInteger(kValueKey, &val));
      object_template->Set(v8_key, v8::Integer::New(isolate, val));
    } else if (type == "boolean") {
      bool val = false;
      CHECK(dict->GetBoolean(kValueKey, &val));
      object_template->Set(v8_key, v8::Boolean::New(isolate, val));
    } else if (type == "string") {
      std::string val;
      CHECK(dict->GetString(kValueKey, &val)) << iter.key();
      object_template->Set(v8_key, gin::StringToSymbol(isolate, val));
    } else if (type == "object" || !ref.empty()) {
      v8::Local<v8::ObjectTemplate> property_template =
          v8::ObjectTemplate::New(isolate);
      const base::DictionaryValue* property_dict = nullptr;
      CHECK(dict->GetDictionary("properties", &property_dict));
      DecorateTemplateWithProperties(isolate, property_template,
                                     *property_dict);
      object_template->Set(v8_key, property_template);
    }
  }
}

// static
void APIBinding::GetEventObject(
    v8::Local<v8::Name> property,
    const v8::PropertyCallbackInfo<v8::Value>& info) {
  v8::Isolate* isolate = info.GetIsolate();
  v8::HandleScope handle_scope(isolate);
  v8::Local<v8::Context> context = info.Holder()->CreationContext();
  if (!IsContextValid(context))
    return;

  CHECK(info.Data()->IsExternal());
  auto* event_data =
      static_cast<EventData*>(info.Data().As<v8::External>()->Value());
  info.GetReturnValue().Set(event_data->event_handler->CreateEventInstance(
      event_data->full_name, context));
}

void APIBinding::GetCustomPropertyObject(
    v8::Local<v8::Name> property_name,
    const v8::PropertyCallbackInfo<v8::Value>& info) {
  v8::Isolate* isolate = info.GetIsolate();
  v8::HandleScope handle_scope(isolate);
  v8::Local<v8::Context> context = info.Holder()->CreationContext();
  if (!IsContextValid(context))
    return;

  CHECK(info.Data()->IsExternal());
  auto* property_data =
      static_cast<CustomPropertyData*>(info.Data().As<v8::External>()->Value());

  v8::Local<v8::Object> property = property_data->create_custom_type.Run(
      context, property_data->type_name, property_data->property_name);
  if (property.IsEmpty())
    return;

  info.GetReturnValue().Set(property);
}

void APIBinding::HandleCall(const std::string& name,
                            const APISignature* signature,
                            gin::Arguments* arguments) {
  std::string error;
  v8::Isolate* isolate = arguments->isolate();
  v8::HandleScope handle_scope(isolate);

  // Since this is called synchronously from the JS entry point,
  // GetCurrentContext() should always be correct.
  v8::Local<v8::Context> context = isolate->GetCurrentContext();

  std::vector<v8::Local<v8::Value>> argument_list;
  if (arguments->Length() > 0) {
    // Just copying handles should never fail.
    CHECK(arguments->GetRemaining(&argument_list));
  }

  bool invalid_invocation = false;
  v8::Local<v8::Function> custom_callback;
  {
    v8::TryCatch try_catch(isolate);
    APIBindingHooks::RequestResult hooks_result = binding_hooks_->RunHooks(
        name, context, signature, &argument_list, *type_refs_);

    switch (hooks_result.code) {
      case APIBindingHooks::RequestResult::INVALID_INVOCATION:
        invalid_invocation = true;
        // Throw a type error below so that it's not caught by our try-catch.
        break;
      case APIBindingHooks::RequestResult::THROWN:
        DCHECK(try_catch.HasCaught());
        try_catch.ReThrow();
        return;
      case APIBindingHooks::RequestResult::HANDLED:
        if (!hooks_result.return_value.IsEmpty())
          arguments->Return(hooks_result.return_value);
        return;  // Our work here is done.
      case APIBindingHooks::RequestResult::NOT_HANDLED:
        break;  // Handle in the default manner.
    }
    custom_callback = hooks_result.custom_callback;
  }

  if (invalid_invocation) {
    arguments->ThrowTypeError("Invalid invocation");
    return;
  }

  std::unique_ptr<base::ListValue> converted_arguments;
  v8::Local<v8::Function> callback;
  {
    v8::TryCatch try_catch(isolate);
    invalid_invocation = !signature->ParseArgumentsToJSON(
        context, argument_list, *type_refs_,
        &converted_arguments, &callback, &error);
    if (try_catch.HasCaught()) {
      DCHECK(!converted_arguments);
      try_catch.ReThrow();
      return;
    }
  }
  if (invalid_invocation) {
    arguments->ThrowTypeError("Invalid invocation");
    return;
  }

  request_handler_->StartRequest(context, name, std::move(converted_arguments),
                                 callback, custom_callback);
}

}  // namespace extensions