Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / extensions / renderer / v8_helpers.h
blob2bfeee8a6f3d73909da77a66b5aca5389e48adc2
1 // 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.
5 #ifndef EXTENSIONS_RENDERER_V8_HELPERS_H_
6 #define EXTENSIONS_RENDERER_V8_HELPERS_H_
8 #include <string.h>
10 #include "v8/include/v8.h"
12 namespace extensions {
13 namespace v8_helpers {
15 // Helper functions for V8 APIs.
17 // Converts |str| to a V8 string. Returns true on success.
18 inline bool ToV8String(v8::Isolate* isolate,
19 const char* str,
20 v8::Local<v8::String>* out) {
21 return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal)
22 .ToLocal(out);
25 inline bool ToV8String(v8::Isolate* isolate,
26 const std::string& str,
27 v8::Local<v8::String>* out) {
28 return ToV8String(isolate, str.c_str(), out);
31 // Converts |str| to a V8 string.
32 // This crashes when strlen(str) > v8::String::kMaxLength.
33 inline v8::Local<v8::String> ToV8StringUnsafe(
34 v8::Isolate* isolate,
35 const char* str,
36 v8::NewStringType string_type = v8::NewStringType::kNormal) {
37 DCHECK(strlen(str) <= v8::String::kMaxLength);
38 return v8::String::NewFromUtf8(isolate, str, string_type)
39 .ToLocalChecked();
42 inline v8::Local<v8::String> ToV8StringUnsafe(
43 v8::Isolate* isolate,
44 const std::string& str,
45 v8::NewStringType string_type = v8::NewStringType::kNormal) {
46 return ToV8StringUnsafe(isolate, str.c_str(), string_type);
49 // Returns true if |maybe| is both a value, and that value is true.
50 inline bool IsTrue(v8::Maybe<bool> maybe) {
51 return maybe.IsJust() && maybe.FromJust();
54 // Returns true if |value| is empty or undefined.
55 inline bool IsEmptyOrUndefied(v8::Local<v8::Value> value) {
56 return value.IsEmpty() || value->IsUndefined();
59 // SetProperty() family wraps V8::Object::Set(). Returns true on success.
60 inline bool SetProperty(v8::Local<v8::Context> context,
61 v8::Local<v8::Object> object,
62 v8::Local<v8::Value> key,
63 v8::Local<v8::Value> value) {
64 return IsTrue(object->Set(context, key, value));
67 inline bool SetProperty(v8::Local<v8::Context> context,
68 v8::Local<v8::Object> object,
69 uint32_t index,
70 v8::Local<v8::Value> value) {
71 return IsTrue(object->Set(context, index, value));
74 inline bool SetProperty(v8::Local<v8::Context> context,
75 v8::Local<v8::Object> object,
76 const char* key,
77 v8::Local<v8::Value> value) {
78 v8::Local<v8::String> v8_key;
79 if (!ToV8String(context->GetIsolate(), key, &v8_key))
80 return false;
81 return SetProperty(context, object, v8_key, value);
84 // GetProperty() family calls V8::Object::Get() and extracts a value from
85 // returned MaybeLocal. Returns true on success.
86 template <typename Key>
87 inline bool GetProperty(v8::Local<v8::Context> context,
88 v8::Local<v8::Object> object,
89 Key key,
90 v8::Local<v8::Value>* out) {
91 return object->Get(context, key).ToLocal(out);
94 inline bool GetProperty(v8::Local<v8::Context> context,
95 v8::Local<v8::Object> object,
96 const char* key,
97 v8::Local<v8::Value>* out) {
98 v8::Local<v8::String> v8_key;
99 if (!ToV8String(context->GetIsolate(), key, &v8_key))
100 return false;
101 return GetProperty(context, object, v8_key, out);
104 // GetPropertyUnsafe() family wraps v8::Object::Get(). They crash when an
105 // exception is thrown.
106 inline v8::Local<v8::Value> GetPropertyUnsafe(v8::Local<v8::Context> context,
107 v8::Local<v8::Object> object,
108 v8::Local<v8::Value> key) {
109 return object->Get(context, key).ToLocalChecked();
112 inline v8::Local<v8::Value> GetPropertyUnsafe(
113 v8::Local<v8::Context> context,
114 v8::Local<v8::Object> object,
115 const char* key,
116 v8::NewStringType string_type = v8::NewStringType::kNormal) {
117 return object->Get(context,
118 ToV8StringUnsafe(context->GetIsolate(), key, string_type))
119 .ToLocalChecked();
122 // Wraps v8::Function::Call(). Returns true on success.
123 inline bool CallFunction(v8::Local<v8::Context> context,
124 v8::Local<v8::Function> function,
125 v8::Local<v8::Value> recv,
126 int argc,
127 v8::Local<v8::Value> argv[],
128 v8::Local<v8::Value>* out) {
129 return function->Call(context, recv, argc, argv).ToLocal(out);
132 } // namespace v8_helpers
133 } // namespace extensions
135 #endif // EXTENSIONS_RENDERER_V8_HELPERS_H_