v8_value_converter.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/common/v8_value_converter.h"
  5. #include <map>
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include "base/logging.h"
  11. #include "base/values.h"
  12. #include "shell/common/gin_helper/dictionary.h"
  13. #include "shell/common/node_bindings.h"
  14. #include "shell/common/node_includes.h"
  15. namespace electron {
  16. namespace {
  17. const int kMaxRecursionDepth = 100;
  18. } // namespace
  19. // The state of a call to FromV8Value.
  20. class V8ValueConverter::FromV8ValueState {
  21. public:
  22. // Level scope which updates the current depth of some FromV8ValueState.
  23. class Level {
  24. public:
  25. explicit Level(FromV8ValueState* state) : state_(state) {
  26. state_->max_recursion_depth_--;
  27. }
  28. ~Level() { state_->max_recursion_depth_++; }
  29. private:
  30. FromV8ValueState* state_;
  31. };
  32. FromV8ValueState() : max_recursion_depth_(kMaxRecursionDepth) {}
  33. // If |handle| is not in |unique_map_|, then add it to |unique_map_| and
  34. // return true.
  35. //
  36. // Otherwise do nothing and return false. Here "A is unique" means that no
  37. // other handle B in the map points to the same object as A. Note that A can
  38. // be unique even if there already is another handle with the same identity
  39. // hash (key) in the map, because two objects can have the same hash.
  40. bool AddToUniquenessCheck(v8::Local<v8::Object> handle) {
  41. int hash;
  42. auto iter = GetIteratorInMap(handle, &hash);
  43. if (iter != unique_map_.end())
  44. return false;
  45. unique_map_.insert(std::make_pair(hash, handle));
  46. return true;
  47. }
  48. bool RemoveFromUniquenessCheck(v8::Local<v8::Object> handle) {
  49. int unused_hash;
  50. auto iter = GetIteratorInMap(handle, &unused_hash);
  51. if (iter == unique_map_.end())
  52. return false;
  53. unique_map_.erase(iter);
  54. return true;
  55. }
  56. bool HasReachedMaxRecursionDepth() { return max_recursion_depth_ < 0; }
  57. private:
  58. using HashToHandleMap = std::multimap<int, v8::Local<v8::Object>>;
  59. using Iterator = HashToHandleMap::const_iterator;
  60. Iterator GetIteratorInMap(v8::Local<v8::Object> handle, int* hash) {
  61. *hash = handle->GetIdentityHash();
  62. // We only compare using == with handles to objects with the same identity
  63. // hash. Different hash obviously means different objects, but two objects
  64. // in a couple of thousands could have the same identity hash.
  65. std::pair<Iterator, Iterator> range = unique_map_.equal_range(*hash);
  66. for (auto it = range.first; it != range.second; ++it) {
  67. // Operator == for handles actually compares the underlying objects.
  68. if (it->second == handle)
  69. return it;
  70. }
  71. // Not found.
  72. return unique_map_.end();
  73. }
  74. HashToHandleMap unique_map_;
  75. int max_recursion_depth_;
  76. };
  77. // A class to ensure that objects/arrays that are being converted by
  78. // this V8ValueConverterImpl do not have cycles.
  79. //
  80. // An example of cycle: var v = {}; v = {key: v};
  81. // Not an example of cycle: var v = {}; a = [v, v]; or w = {a: v, b: v};
  82. class V8ValueConverter::ScopedUniquenessGuard {
  83. public:
  84. ScopedUniquenessGuard(V8ValueConverter::FromV8ValueState* state,
  85. v8::Local<v8::Object> value)
  86. : state_(state),
  87. value_(value),
  88. is_valid_(state_->AddToUniquenessCheck(value_)) {}
  89. ~ScopedUniquenessGuard() {
  90. if (is_valid_) {
  91. bool removed = state_->RemoveFromUniquenessCheck(value_);
  92. DCHECK(removed);
  93. }
  94. }
  95. // disable copy
  96. ScopedUniquenessGuard(const ScopedUniquenessGuard&) = delete;
  97. ScopedUniquenessGuard& operator=(const ScopedUniquenessGuard&) = delete;
  98. bool is_valid() const { return is_valid_; }
  99. private:
  100. typedef std::multimap<int, v8::Local<v8::Object>> HashToHandleMap;
  101. V8ValueConverter::FromV8ValueState* state_;
  102. v8::Local<v8::Object> value_;
  103. bool is_valid_;
  104. };
  105. V8ValueConverter::V8ValueConverter() = default;
  106. void V8ValueConverter::SetRegExpAllowed(bool val) {
  107. reg_exp_allowed_ = val;
  108. }
  109. void V8ValueConverter::SetFunctionAllowed(bool val) {
  110. function_allowed_ = val;
  111. }
  112. void V8ValueConverter::SetStripNullFromObjects(bool val) {
  113. strip_null_from_objects_ = val;
  114. }
  115. v8::Local<v8::Value> V8ValueConverter::ToV8Value(
  116. const base::Value* value,
  117. v8::Local<v8::Context> context) const {
  118. v8::Context::Scope context_scope(context);
  119. v8::EscapableHandleScope handle_scope(context->GetIsolate());
  120. return handle_scope.Escape(ToV8ValueImpl(context->GetIsolate(), value));
  121. }
  122. std::unique_ptr<base::Value> V8ValueConverter::FromV8Value(
  123. v8::Local<v8::Value> val,
  124. v8::Local<v8::Context> context) const {
  125. v8::Context::Scope context_scope(context);
  126. v8::HandleScope handle_scope(context->GetIsolate());
  127. FromV8ValueState state;
  128. return FromV8ValueImpl(&state, val, context->GetIsolate());
  129. }
  130. v8::Local<v8::Value> V8ValueConverter::ToV8ValueImpl(
  131. v8::Isolate* isolate,
  132. const base::Value* value) const {
  133. switch (value->type()) {
  134. case base::Value::Type::NONE:
  135. return v8::Null(isolate);
  136. case base::Value::Type::BOOLEAN: {
  137. bool val = value->GetBool();
  138. return v8::Boolean::New(isolate, val);
  139. }
  140. case base::Value::Type::INTEGER: {
  141. int val = value->GetInt();
  142. return v8::Integer::New(isolate, val);
  143. }
  144. case base::Value::Type::DOUBLE: {
  145. double val = value->GetDouble();
  146. return v8::Number::New(isolate, val);
  147. }
  148. case base::Value::Type::STRING: {
  149. std::string val = value->GetString();
  150. return v8::String::NewFromUtf8(isolate, val.c_str(),
  151. v8::NewStringType::kNormal, val.length())
  152. .ToLocalChecked();
  153. }
  154. case base::Value::Type::LIST:
  155. return ToV8Array(isolate, static_cast<const base::ListValue*>(value));
  156. case base::Value::Type::DICTIONARY:
  157. return ToV8Object(isolate,
  158. static_cast<const base::DictionaryValue*>(value));
  159. case base::Value::Type::BINARY:
  160. return ToArrayBuffer(isolate, static_cast<const base::Value*>(value));
  161. default:
  162. LOG(ERROR) << "Unexpected value type: " << value->type();
  163. return v8::Null(isolate);
  164. }
  165. }
  166. v8::Local<v8::Value> V8ValueConverter::ToV8Array(
  167. v8::Isolate* isolate,
  168. const base::ListValue* val) const {
  169. v8::Local<v8::Array> result(
  170. v8::Array::New(isolate, val->GetListDeprecated().size()));
  171. auto context = isolate->GetCurrentContext();
  172. for (size_t i = 0; i < val->GetListDeprecated().size(); ++i) {
  173. const base::Value& child = val->GetListDeprecated()[i];
  174. v8::Local<v8::Value> child_v8 = ToV8ValueImpl(isolate, &child);
  175. v8::TryCatch try_catch(isolate);
  176. result->Set(context, static_cast<uint32_t>(i), child_v8).Check();
  177. if (try_catch.HasCaught())
  178. LOG(ERROR) << "Setter for index " << i << " threw an exception.";
  179. }
  180. return result;
  181. }
  182. v8::Local<v8::Value> V8ValueConverter::ToV8Object(
  183. v8::Isolate* isolate,
  184. const base::DictionaryValue* val) const {
  185. gin_helper::Dictionary result = gin::Dictionary::CreateEmpty(isolate);
  186. result.SetHidden("simple", true);
  187. for (base::DictionaryValue::Iterator iter(*val); !iter.IsAtEnd();
  188. iter.Advance()) {
  189. const std::string& key = iter.key();
  190. v8::Local<v8::Value> child_v8 = ToV8ValueImpl(isolate, &iter.value());
  191. v8::TryCatch try_catch(isolate);
  192. result.Set(key, child_v8);
  193. if (try_catch.HasCaught()) {
  194. LOG(ERROR) << "Setter for property " << key.c_str() << " threw an "
  195. << "exception.";
  196. }
  197. }
  198. return result.GetHandle();
  199. }
  200. v8::Local<v8::Value> V8ValueConverter::ToArrayBuffer(
  201. v8::Isolate* isolate,
  202. const base::Value* value) const {
  203. const auto* data = reinterpret_cast<const char*>(value->GetBlob().data());
  204. size_t length = value->GetBlob().size();
  205. if (NodeBindings::IsInitialized()) {
  206. return node::Buffer::Copy(isolate, data, length).ToLocalChecked();
  207. }
  208. if (length > node::Buffer::kMaxLength) {
  209. return v8::Local<v8::Object>();
  210. }
  211. auto context = isolate->GetCurrentContext();
  212. auto array_buffer = v8::ArrayBuffer::New(isolate, length);
  213. std::shared_ptr<v8::BackingStore> backing_store =
  214. array_buffer->GetBackingStore();
  215. memcpy(backing_store->Data(), data, length);
  216. // From this point, if something goes wrong(can't find Buffer class for
  217. // example) we'll simply return a Uint8Array based on the created ArrayBuffer.
  218. // This can happen if no preload script was specified to the renderer.
  219. gin_helper::Dictionary global(isolate, context->Global());
  220. v8::Local<v8::Value> buffer_value;
  221. // Get the Buffer class stored as a hidden value in the global object. We'll
  222. // use it return a browserified Buffer.
  223. if (!global.GetHidden("Buffer", &buffer_value) ||
  224. !buffer_value->IsFunction()) {
  225. return v8::Uint8Array::New(array_buffer, 0, length);
  226. }
  227. gin::Dictionary buffer_class(
  228. isolate,
  229. buffer_value->ToObject(isolate->GetCurrentContext()).ToLocalChecked());
  230. v8::Local<v8::Value> from_value;
  231. if (!buffer_class.Get("from", &from_value) || !from_value->IsFunction()) {
  232. return v8::Uint8Array::New(array_buffer, 0, length);
  233. }
  234. v8::Local<v8::Value> args[] = {array_buffer};
  235. auto func = from_value.As<v8::Function>();
  236. auto result = func->Call(context, v8::Null(isolate), 1, args);
  237. if (!result.IsEmpty()) {
  238. return result.ToLocalChecked();
  239. }
  240. return v8::Uint8Array::New(array_buffer, 0, length);
  241. }
  242. std::unique_ptr<base::Value> V8ValueConverter::FromV8ValueImpl(
  243. FromV8ValueState* state,
  244. v8::Local<v8::Value> val,
  245. v8::Isolate* isolate) const {
  246. FromV8ValueState::Level state_level(state);
  247. if (state->HasReachedMaxRecursionDepth())
  248. return nullptr;
  249. if (val->IsExternal())
  250. return std::make_unique<base::Value>();
  251. if (val->IsNull())
  252. return std::make_unique<base::Value>();
  253. auto context = isolate->GetCurrentContext();
  254. if (val->IsBoolean())
  255. return std::make_unique<base::Value>(val->ToBoolean(isolate)->Value());
  256. if (val->IsInt32())
  257. return std::make_unique<base::Value>(val.As<v8::Int32>()->Value());
  258. if (val->IsNumber()) {
  259. double val_as_double = val.As<v8::Number>()->Value();
  260. if (!std::isfinite(val_as_double))
  261. return nullptr;
  262. return std::make_unique<base::Value>(val_as_double);
  263. }
  264. if (val->IsString()) {
  265. v8::String::Utf8Value utf8(isolate, val);
  266. return std::make_unique<base::Value>(std::string(*utf8, utf8.length()));
  267. }
  268. if (val->IsUndefined())
  269. // JSON.stringify ignores undefined.
  270. return nullptr;
  271. if (val->IsDate()) {
  272. v8::Date* date = v8::Date::Cast(*val);
  273. v8::Local<v8::Value> toISOString =
  274. date->Get(context, v8::String::NewFromUtf8(isolate, "toISOString",
  275. v8::NewStringType::kNormal)
  276. .ToLocalChecked())
  277. .ToLocalChecked();
  278. if (toISOString->IsFunction()) {
  279. v8::MaybeLocal<v8::Value> result =
  280. toISOString.As<v8::Function>()->Call(context, val, 0, nullptr);
  281. if (!result.IsEmpty()) {
  282. v8::String::Utf8Value utf8(isolate, result.ToLocalChecked());
  283. return std::make_unique<base::Value>(std::string(*utf8, utf8.length()));
  284. }
  285. }
  286. }
  287. if (val->IsRegExp()) {
  288. if (!reg_exp_allowed_)
  289. // JSON.stringify converts to an object.
  290. return FromV8Object(val.As<v8::Object>(), state, isolate);
  291. return std::make_unique<base::Value>(*v8::String::Utf8Value(isolate, val));
  292. }
  293. // v8::Value doesn't have a ToArray() method for some reason.
  294. if (val->IsArray())
  295. return FromV8Array(val.As<v8::Array>(), state, isolate);
  296. if (val->IsFunction()) {
  297. if (!function_allowed_)
  298. // JSON.stringify refuses to convert function(){}.
  299. return nullptr;
  300. return FromV8Object(val.As<v8::Object>(), state, isolate);
  301. }
  302. if (node::Buffer::HasInstance(val)) {
  303. return FromNodeBuffer(val, state, isolate);
  304. }
  305. if (val->IsObject()) {
  306. return FromV8Object(val.As<v8::Object>(), state, isolate);
  307. }
  308. LOG(ERROR) << "Unexpected v8 value type encountered.";
  309. return nullptr;
  310. }
  311. std::unique_ptr<base::Value> V8ValueConverter::FromV8Array(
  312. v8::Local<v8::Array> val,
  313. FromV8ValueState* state,
  314. v8::Isolate* isolate) const {
  315. ScopedUniquenessGuard uniqueness_guard(state, val);
  316. if (!uniqueness_guard.is_valid())
  317. return std::make_unique<base::Value>();
  318. std::unique_ptr<v8::Context::Scope> scope;
  319. // If val was created in a different context than our current one, change to
  320. // that context, but change back after val is converted.
  321. if (!val->CreationContext().IsEmpty() &&
  322. val->CreationContext() != isolate->GetCurrentContext())
  323. scope = std::make_unique<v8::Context::Scope>(val->CreationContext());
  324. auto result = std::make_unique<base::ListValue>();
  325. // Only fields with integer keys are carried over to the ListValue.
  326. for (uint32_t i = 0; i < val->Length(); ++i) {
  327. v8::TryCatch try_catch(isolate);
  328. v8::Local<v8::Value> child_v8;
  329. v8::MaybeLocal<v8::Value> maybe_child =
  330. val->Get(isolate->GetCurrentContext(), i);
  331. if (try_catch.HasCaught() || !maybe_child.ToLocal(&child_v8)) {
  332. LOG(ERROR) << "Getter for index " << i << " threw an exception.";
  333. child_v8 = v8::Null(isolate);
  334. }
  335. if (!val->HasRealIndexedProperty(isolate->GetCurrentContext(), i)
  336. .FromMaybe(false)) {
  337. result->Append(std::make_unique<base::Value>());
  338. continue;
  339. }
  340. std::unique_ptr<base::Value> child =
  341. FromV8ValueImpl(state, child_v8, isolate);
  342. if (child)
  343. result->Append(std::move(child));
  344. else
  345. // JSON.stringify puts null in places where values don't serialize, for
  346. // example undefined and functions. Emulate that behavior.
  347. result->Append(std::make_unique<base::Value>());
  348. }
  349. return std::move(result);
  350. }
  351. std::unique_ptr<base::Value> V8ValueConverter::FromNodeBuffer(
  352. v8::Local<v8::Value> value,
  353. FromV8ValueState* state,
  354. v8::Isolate* isolate) const {
  355. std::vector<char> buffer(
  356. node::Buffer::Data(value),
  357. node::Buffer::Data(value) + node::Buffer::Length(value));
  358. return std::make_unique<base::Value>(std::move(buffer));
  359. }
  360. std::unique_ptr<base::Value> V8ValueConverter::FromV8Object(
  361. v8::Local<v8::Object> val,
  362. FromV8ValueState* state,
  363. v8::Isolate* isolate) const {
  364. ScopedUniquenessGuard uniqueness_guard(state, val);
  365. if (!uniqueness_guard.is_valid())
  366. return std::make_unique<base::Value>();
  367. std::unique_ptr<v8::Context::Scope> scope;
  368. // If val was created in a different context than our current one, change to
  369. // that context, but change back after val is converted.
  370. if (!val->CreationContext().IsEmpty() &&
  371. val->CreationContext() != isolate->GetCurrentContext())
  372. scope = std::make_unique<v8::Context::Scope>(val->CreationContext());
  373. auto result = std::make_unique<base::DictionaryValue>();
  374. v8::Local<v8::Array> property_names;
  375. if (!val->GetOwnPropertyNames(isolate->GetCurrentContext())
  376. .ToLocal(&property_names)) {
  377. return std::move(result);
  378. }
  379. for (uint32_t i = 0; i < property_names->Length(); ++i) {
  380. v8::Local<v8::Value> key =
  381. property_names->Get(isolate->GetCurrentContext(), i).ToLocalChecked();
  382. // Extend this test to cover more types as necessary and if sensible.
  383. if (!key->IsString() && !key->IsNumber()) {
  384. NOTREACHED() << "Key \"" << *v8::String::Utf8Value(isolate, key)
  385. << "\" "
  386. "is neither a string nor a number";
  387. continue;
  388. }
  389. v8::String::Utf8Value name_utf8(isolate, key);
  390. v8::TryCatch try_catch(isolate);
  391. v8::Local<v8::Value> child_v8;
  392. v8::MaybeLocal<v8::Value> maybe_child =
  393. val->Get(isolate->GetCurrentContext(), key);
  394. if (try_catch.HasCaught() || !maybe_child.ToLocal(&child_v8)) {
  395. LOG(ERROR) << "Getter for property " << *name_utf8
  396. << " threw an exception.";
  397. child_v8 = v8::Null(isolate);
  398. }
  399. std::unique_ptr<base::Value> child =
  400. FromV8ValueImpl(state, child_v8, isolate);
  401. if (!child)
  402. // JSON.stringify skips properties whose values don't serialize, for
  403. // example undefined and functions. Emulate that behavior.
  404. continue;
  405. // Strip null if asked (and since undefined is turned into null, undefined
  406. // too). The use case for supporting this is JSON-schema support,
  407. // specifically for extensions, where "optional" JSON properties may be
  408. // represented as null, yet due to buggy legacy code elsewhere isn't
  409. // treated as such (potentially causing crashes). For example, the
  410. // "tabs.create" function takes an object as its first argument with an
  411. // optional "windowId" property.
  412. //
  413. // Given just
  414. //
  415. // tabs.create({})
  416. //
  417. // this will work as expected on code that only checks for the existence of
  418. // a "windowId" property (such as that legacy code). However given
  419. //
  420. // tabs.create({windowId: null})
  421. //
  422. // there *is* a "windowId" property, but since it should be an int, code
  423. // on the browser which doesn't additionally check for null will fail.
  424. // We can avoid all bugs related to this by stripping null.
  425. if (strip_null_from_objects_ && child->is_none())
  426. continue;
  427. result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()),
  428. std::move(child));
  429. }
  430. return std::move(result);
  431. }
  432. } // namespace electron