blob: e9ced93de4843da62d8e7f946ce12d47622f4ce6 [file] [log] [blame]
bashi6a4854f2015-06-19 00:51:511// Copyright 2015 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#ifndef EXTENSIONS_RENDERER_V8_HELPERS_H_
6#define EXTENSIONS_RENDERER_V8_HELPERS_H_
7
avi2d124c02015-12-23 06:36:428#include <stdint.h>
bashi6a4854f2015-06-19 00:51:519#include <string.h>
10
rdevlin.cronin415b73b2015-11-13 01:14:4711#include "base/strings/string_number_conversions.h"
bashi6a4854f2015-06-19 00:51:5112#include "v8/include/v8.h"
13
14namespace extensions {
15namespace v8_helpers {
16
17// Helper functions for V8 APIs.
18
19// Converts |str| to a V8 string. Returns true on success.
20inline bool ToV8String(v8::Isolate* isolate,
21 const char* str,
22 v8::Local<v8::String>* out) {
23 return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal)
24 .ToLocal(out);
25}
26
bashi61ca3c72015-06-26 00:40:1027inline bool ToV8String(v8::Isolate* isolate,
28 const std::string& str,
29 v8::Local<v8::String>* out) {
30 return ToV8String(isolate, str.c_str(), out);
31}
32
bashi6a4854f2015-06-19 00:51:5133// Converts |str| to a V8 string.
34// This crashes when strlen(str) > v8::String::kMaxLength.
35inline v8::Local<v8::String> ToV8StringUnsafe(
36 v8::Isolate* isolate,
37 const char* str,
38 v8::NewStringType string_type = v8::NewStringType::kNormal) {
39 DCHECK(strlen(str) <= v8::String::kMaxLength);
40 return v8::String::NewFromUtf8(isolate, str, string_type)
41 .ToLocalChecked();
42}
43
bashifacd48b82015-06-30 05:44:2044inline v8::Local<v8::String> ToV8StringUnsafe(
45 v8::Isolate* isolate,
46 const std::string& str,
47 v8::NewStringType string_type = v8::NewStringType::kNormal) {
48 return ToV8StringUnsafe(isolate, str.c_str(), string_type);
49}
50
bashi6a4854f2015-06-19 00:51:5151// Returns true if |maybe| is both a value, and that value is true.
52inline bool IsTrue(v8::Maybe<bool> maybe) {
53 return maybe.IsJust() && maybe.FromJust();
54}
55
rdevlin.cronin75b803b2016-03-02 00:13:4756// Wraps v8::Object::SetPrivate(). When possible, prefer this to SetProperty().
57inline bool SetPrivateProperty(v8::Local<v8::Context> context,
58 v8::Local<v8::Object> object,
59 v8::Local<v8::String> key,
60 v8::Local<v8::Value> value) {
61 return IsTrue(object->SetPrivate(
62 context, v8::Private::ForApi(context->GetIsolate(), key), value));
63}
64
65inline bool SetPrivateProperty(v8::Local<v8::Context> context,
66 v8::Local<v8::Object> object,
67 const char* key,
68 v8::Local<v8::Value> value) {
69 v8::Local<v8::String> v8_key;
70 return ToV8String(context->GetIsolate(), key, &v8_key) &&
71 IsTrue(object->SetPrivate(
72 context, v8::Private::ForApi(context->GetIsolate(), v8_key),
73 value));
74}
75
bashi6a4854f2015-06-19 00:51:5176// GetProperty() family calls V8::Object::Get() and extracts a value from
77// returned MaybeLocal. Returns true on success.
rdevlin.cronin75b803b2016-03-02 00:13:4778// NOTE: Think about whether you want this or GetPrivateProperty() below.
bashi7aeaf6f32015-07-10 00:10:5279template <typename Key>
bashi6a4854f2015-06-19 00:51:5180inline bool GetProperty(v8::Local<v8::Context> context,
81 v8::Local<v8::Object> object,
bashi7aeaf6f32015-07-10 00:10:5282 Key key,
bashi6a4854f2015-06-19 00:51:5183 v8::Local<v8::Value>* out) {
84 return object->Get(context, key).ToLocal(out);
85}
86
87inline bool GetProperty(v8::Local<v8::Context> context,
88 v8::Local<v8::Object> object,
89 const char* key,
90 v8::Local<v8::Value>* out) {
91 v8::Local<v8::String> v8_key;
92 if (!ToV8String(context->GetIsolate(), key, &v8_key))
93 return false;
94 return GetProperty(context, object, v8_key, out);
95}
96
rdevlin.cronin75b803b2016-03-02 00:13:4797// Wraps v8::Object::GetPrivate(). When possible, prefer this to GetProperty().
98inline bool GetPrivateProperty(v8::Local<v8::Context> context,
99 v8::Local<v8::Object> object,
100 v8::Local<v8::String> key,
101 v8::Local<v8::Value>* out) {
102 return object
103 ->GetPrivate(context, v8::Private::ForApi(context->GetIsolate(), key))
104 .ToLocal(out);
105}
106
107inline bool GetPrivateProperty(v8::Local<v8::Context> context,
108 v8::Local<v8::Object> object,
109 const char* key,
110 v8::Local<v8::Value>* out) {
111 v8::Local<v8::String> v8_key;
112 return ToV8String(context->GetIsolate(), key, &v8_key) &&
113 GetPrivateProperty(context, object, v8_key, out);
114}
115
Devlin Cronin0bd2b06c2018-02-01 01:58:04116// GetPropertyUnsafe() wraps v8::Object::Get(), and crashes when an exception
117// is thrown.
bashi6a4854f2015-06-19 00:51:51118inline v8::Local<v8::Value> GetPropertyUnsafe(
119 v8::Local<v8::Context> context,
120 v8::Local<v8::Object> object,
121 const char* key,
122 v8::NewStringType string_type = v8::NewStringType::kNormal) {
123 return object->Get(context,
124 ToV8StringUnsafe(context->GetIsolate(), key, string_type))
125 .ToLocalChecked();
126}
127
bashi6a4854f2015-06-19 00:51:51128} // namespace v8_helpers
129} // namespace extensions
130
131#endif // EXTENSIONS_RENDERER_V8_HELPERS_H_