bashi | 6a4854f | 2015-06-19 00:51:51 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef EXTENSIONS_RENDERER_V8_HELPERS_H_ |
| 6 | #define EXTENSIONS_RENDERER_V8_HELPERS_H_ |
| 7 | |
avi | 2d124c0 | 2015-12-23 06:36:42 | [diff] [blame] | 8 | #include <stdint.h> |
bashi | 6a4854f | 2015-06-19 00:51:51 | [diff] [blame] | 9 | #include <string.h> |
| 10 | |
rdevlin.cronin | 415b73b | 2015-11-13 01:14:47 | [diff] [blame] | 11 | #include "base/strings/string_number_conversions.h" |
bashi | 6a4854f | 2015-06-19 00:51:51 | [diff] [blame] | 12 | #include "v8/include/v8.h" |
| 13 | |
| 14 | namespace extensions { |
| 15 | namespace v8_helpers { |
| 16 | |
| 17 | // Helper functions for V8 APIs. |
| 18 | |
| 19 | // Converts |str| to a V8 string. Returns true on success. |
| 20 | inline 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 | |
bashi | 61ca3c7 | 2015-06-26 00:40:10 | [diff] [blame] | 27 | inline 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 | |
bashi | 6a4854f | 2015-06-19 00:51:51 | [diff] [blame] | 33 | // Converts |str| to a V8 string. |
| 34 | // This crashes when strlen(str) > v8::String::kMaxLength. |
| 35 | inline 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 | |
bashi | facd48b8 | 2015-06-30 05:44:20 | [diff] [blame] | 44 | inline 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 | |
bashi | 6a4854f | 2015-06-19 00:51:51 | [diff] [blame] | 51 | // Returns true if |maybe| is both a value, and that value is true. |
| 52 | inline bool IsTrue(v8::Maybe<bool> maybe) { |
| 53 | return maybe.IsJust() && maybe.FromJust(); |
| 54 | } |
| 55 | |
rdevlin.cronin | 75b803b | 2016-03-02 00:13:47 | [diff] [blame] | 56 | // Wraps v8::Object::SetPrivate(). When possible, prefer this to SetProperty(). |
| 57 | inline 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 | |
| 65 | inline 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 | |
bashi | 6a4854f | 2015-06-19 00:51:51 | [diff] [blame] | 76 | // GetProperty() family calls V8::Object::Get() and extracts a value from |
| 77 | // returned MaybeLocal. Returns true on success. |
rdevlin.cronin | 75b803b | 2016-03-02 00:13:47 | [diff] [blame] | 78 | // NOTE: Think about whether you want this or GetPrivateProperty() below. |
bashi | 7aeaf6f3 | 2015-07-10 00:10:52 | [diff] [blame] | 79 | template <typename Key> |
bashi | 6a4854f | 2015-06-19 00:51:51 | [diff] [blame] | 80 | inline bool GetProperty(v8::Local<v8::Context> context, |
| 81 | v8::Local<v8::Object> object, |
bashi | 7aeaf6f3 | 2015-07-10 00:10:52 | [diff] [blame] | 82 | Key key, |
bashi | 6a4854f | 2015-06-19 00:51:51 | [diff] [blame] | 83 | v8::Local<v8::Value>* out) { |
| 84 | return object->Get(context, key).ToLocal(out); |
| 85 | } |
| 86 | |
| 87 | inline 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.cronin | 75b803b | 2016-03-02 00:13:47 | [diff] [blame] | 97 | // Wraps v8::Object::GetPrivate(). When possible, prefer this to GetProperty(). |
| 98 | inline 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 | |
| 107 | inline 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 Cronin | 0bd2b06c | 2018-02-01 01:58:04 | [diff] [blame] | 116 | // GetPropertyUnsafe() wraps v8::Object::Get(), and crashes when an exception |
| 117 | // is thrown. |
bashi | 6a4854f | 2015-06-19 00:51:51 | [diff] [blame] | 118 | inline 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 | |
bashi | 6a4854f | 2015-06-19 00:51:51 | [diff] [blame] | 128 | } // namespace v8_helpers |
| 129 | } // namespace extensions |
| 130 | |
| 131 | #endif // EXTENSIONS_RENDERER_V8_HELPERS_H_ |