v8_value_converter.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. bool is_valid() const { return is_valid_; }
  96. private:
  97. typedef std::multimap<int, v8::Local<v8::Object>> HashToHandleMap;
  98. V8ValueConverter::FromV8ValueState* state_;
  99. v8::Local<v8::Object> value_;
  100. bool is_valid_;
  101. DISALLOW_COPY_AND_ASSIGN(ScopedUniquenessGuard);
  102. };
  103. V8ValueConverter::V8ValueConverter() = default;
  104. void V8ValueConverter::SetRegExpAllowed(bool val) {
  105. reg_exp_allowed_ = val;
  106. }
  107. void V8ValueConverter::SetFunctionAllowed(bool val) {
  108. function_allowed_ = val;
  109. }
  110. void V8ValueConverter::SetStripNullFromObjects(bool val) {
  111. strip_null_from_objects_ = val;
  112. }
  113. v8::Local<v8::Value> V8ValueConverter::ToV8Value(
  114. const base::Value* value,
  115. v8::Local<v8::Context> context) const {
  116. v8::Context::Scope context_scope(context);
  117. v8::EscapableHandleScope handle_scope(context->GetIsolate());
  118. return handle_scope.Escape(ToV8ValueImpl(context->GetIsolate(), value));
  119. }
  120. std::unique_ptr<base::Value> V8ValueConverter::FromV8Value(
  121. v8::Local<v8::Value> val,
  122. v8::Local<v8::Context> context) const {
  123. v8::Context::Scope context_scope(context);
  124. v8::HandleScope handle_scope(context->GetIsolate());
  125. FromV8ValueState state;
  126. return FromV8ValueImpl(&state, val, context->GetIsolate());
  127. }
  128. v8::Local<v8::Value> V8ValueConverter::ToV8ValueImpl(
  129. v8::Isolate* isolate,
  130. const base::Value* value) const {
  131. switch (value->type()) {
  132. case base::Value::Type::NONE:
  133. return v8::Null(isolate);
  134. case base::Value::Type::BOOLEAN: {
  135. bool val = value->GetBool();
  136. return v8::Boolean::New(isolate, val);
  137. }
  138. case base::Value::Type::INTEGER: {
  139. int val = value->GetInt();
  140. return v8::Integer::New(isolate, val);
  141. }
  142. case base::Value::Type::DOUBLE: {
  143. double val = value->GetDouble();
  144. return v8::Number::New(isolate, val);
  145. }
  146. case base::Value::Type::STRING: {
  147. std::string val = value->GetString();
  148. return v8::String::NewFromUtf8(isolate, val.c_str(),
  149. v8::NewStringType::kNormal, val.length())
  150. .ToLocalChecked();
  151. }
  152. case base::Value::Type::LIST:
  153. return ToV8Array(isolate, static_cast<const base::ListValue*>(value));
  154. case base::Value::Type::DICTIONARY:
  155. return ToV8Object(isolate,
  156. static_cast<const base::DictionaryValue*>(value));
  157. case base::Value::Type::BINARY:
  158. return ToArrayBuffer(isolate, static_cast<const base::Value*>(value));
  159. default:
  160. LOG(ERROR) << "Unexpected value type: " << value->type();
  161. return v8::Null(isolate);
  162. }
  163. }
  164. v8::Local<v8::Value> V8ValueConverter::ToV8Array(
  165. v8::Isolate* isolate,
  166. const base::ListValue* val) const {
  167. v8::Local<v8::Array> result(v8::Array::New(isolate, val->GetSize()));
  168. auto context = isolate->GetCurrentContext();
  169. for (size_t i = 0; i < val->GetSize(); ++i) {
  170. const base::Value* child = nullptr;
  171. val->Get(i, &child);
  172. v8::Local<v8::Value> child_v8 = ToV8ValueImpl(isolate, child);
  173. v8::TryCatch try_catch(isolate);
  174. result->Set(context, static_cast<uint32_t>(i), child_v8).Check();
  175. if (try_catch.HasCaught())
  176. LOG(ERROR) << "Setter for index " << i << " threw an exception.";
  177. }
  178. return result;
  179. }
  180. v8::Local<v8::Value> V8ValueConverter::ToV8Object(
  181. v8::Isolate* isolate,
  182. const base::DictionaryValue* val) const {
  183. gin_helper::Dictionary result = gin::Dictionary::CreateEmpty(isolate);
  184. result.SetHidden("simple", true);
  185. for (base::DictionaryValue::Iterator iter(*val); !iter.IsAtEnd();
  186. iter.Advance()) {
  187. const std::string& key = iter.key();
  188. v8::Local<v8::Value> child_v8 = ToV8ValueImpl(isolate, &iter.value());
  189. v8::TryCatch try_catch(isolate);
  190. result.Set(key, child_v8);
  191. if (try_catch.HasCaught()) {
  192. LOG(ERROR) << "Setter for property " << key.c_str() << " threw an "
  193. << "exception.";
  194. }
  195. }
  196. return result.GetHandle();
  197. }
  198. v8::Local<v8::Value> V8ValueConverter::ToArrayBuffer(
  199. v8::Isolate* isolate,
  200. const base::Value* value) const {
  201. const auto* data = reinterpret_cast<const char*>(value->GetBlob().data());
  202. size_t length = value->GetBlob().size();
  203. if (NodeBindings::IsInitialized()) {
  204. return node::Buffer::Copy(isolate, data, length).ToLocalChecked();
  205. }
  206. if (length > node::Buffer::kMaxLength) {
  207. return v8::Local<v8::Object>();
  208. }
  209. auto context = isolate->GetCurrentContext();
  210. auto array_buffer = v8::ArrayBuffer::New(isolate, length);
  211. std::shared_ptr<v8::BackingStore> backing_store =
  212. array_buffer->GetBackingStore();
  213. memcpy(backing_store->Data(), data, length);
  214. // From this point, if something goes wrong(can't find Buffer class for
  215. // example) we'll simply return a Uint8Array based on the created ArrayBuffer.
  216. // This can happen if no preload script was specified to the renderer.
  217. gin_helper::Dictionary global(isolate, context->Global());
  218. v8::Local<v8::Value> buffer_value;
  219. // Get the Buffer class stored as a hidden value in the global object. We'll
  220. // use it return a browserified Buffer.
  221. if (!global.GetHidden("Buffer", &buffer_value) ||
  222. !buffer_value->IsFunction()) {
  223. return v8::Uint8Array::New(array_buffer, 0, length);
  224. }
  225. gin::Dictionary buffer_class(
  226. isolate,
  227. buffer_value->ToObject(isolate->GetCurrentContext()).ToLocalChecked());
  228. v8::Local<v8::Value> from_value;
  229. if (!buffer_class.Get("from", &from_value) || !from_value->IsFunction()) {
  230. return v8::Uint8Array::New(array_buffer, 0, length);
  231. }
  232. v8::Local<v8::Value> args[] = {array_buffer};
  233. auto func = from_value.As<v8::Function>();
  234. auto result = func->Call(context, v8::Null(isolate), 1, args);
  235. if (!result.IsEmpty()) {
  236. return result.ToLocalChecked();
  237. }
  238. return v8::Uint8Array::New(array_buffer, 0, length);
  239. }
  240. std::unique_ptr<base::Value> V8ValueConverter::FromV8ValueImpl(
  241. FromV8ValueState* state,
  242. v8::Local<v8::Value> val,
  243. v8::Isolate* isolate) const {
  244. FromV8ValueState::Level state_level(state);
  245. if (state->HasReachedMaxRecursionDepth())
  246. return nullptr;
  247. if (val->IsExternal())
  248. return std::make_unique<base::Value>();
  249. if (val->IsNull())
  250. return std::make_unique<base::Value>();
  251. auto context = isolate->GetCurrentContext();
  252. if (val->IsBoolean())
  253. return std::make_unique<base::Value>(val->ToBoolean(isolate)->Value());
  254. if (val->IsInt32())
  255. return std::make_unique<base::Value>(val.As<v8::Int32>()->Value());
  256. if (val->IsNumber()) {
  257. double val_as_double = val.As<v8::Number>()->Value();
  258. if (!std::isfinite(val_as_double))
  259. return nullptr;
  260. return std::make_unique<base::Value>(val_as_double);
  261. }
  262. if (val->IsString()) {
  263. v8::String::Utf8Value utf8(isolate, val);
  264. return std::make_unique<base::Value>(std::string(*utf8, utf8.length()));
  265. }
  266. if (val->IsUndefined())
  267. // JSON.stringify ignores undefined.
  268. return nullptr;
  269. if (val->IsDate()) {
  270. v8::Date* date = v8::Date::Cast(*val);
  271. v8::Local<v8::Value> toISOString =
  272. date->Get(context, v8::String::NewFromUtf8(isolate, "toISOString",
  273. v8::NewStringType::kNormal)
  274. .ToLocalChecked())
  275. .ToLocalChecked();
  276. if (toISOString->IsFunction()) {
  277. v8::MaybeLocal<v8::Value> result =
  278. toISOString.As<v8::Function>()->Call(context, val, 0, nullptr);
  279. if (!result.IsEmpty()) {
  280. v8::String::Utf8Value utf8(isolate, result.ToLocalChecked());
  281. return std::make_unique<base::Value>(std::string(*utf8, utf8.length()));
  282. }
  283. }
  284. }
  285. if (val->IsRegExp()) {
  286. if (!reg_exp_allowed_)
  287. // JSON.stringify converts to an object.
  288. return FromV8Object(val.As<v8::Object>(), state, isolate);
  289. return std::make_unique<base::Value>(*v8::String::Utf8Value(isolate, val));
  290. }
  291. // v8::Value doesn't have a ToArray() method for some reason.
  292. if (val->IsArray())
  293. return FromV8Array(val.As<v8::Array>(), state, isolate);
  294. if (val->IsFunction()) {
  295. if (!function_allowed_)
  296. // JSON.stringify refuses to convert function(){}.
  297. return nullptr;
  298. return FromV8Object(val.As<v8::Object>(), state, isolate);
  299. }
  300. if (node::Buffer::HasInstance(val)) {
  301. return FromNodeBuffer(val, state, isolate);
  302. }
  303. if (val->IsObject()) {
  304. return FromV8Object(val.As<v8::Object>(), state, isolate);
  305. }
  306. LOG(ERROR) << "Unexpected v8 value type encountered.";
  307. return nullptr;
  308. }
  309. std::unique_ptr<base::Value> V8ValueConverter::FromV8Array(
  310. v8::Local<v8::Array> val,
  311. FromV8ValueState* state,
  312. v8::Isolate* isolate) const {
  313. ScopedUniquenessGuard uniqueness_guard(state, val);
  314. if (!uniqueness_guard.is_valid())
  315. return std::make_unique<base::Value>();
  316. std::unique_ptr<v8::Context::Scope> scope;
  317. // If val was created in a different context than our current one, change to
  318. // that context, but change back after val is converted.
  319. if (!val->CreationContext().IsEmpty() &&
  320. val->CreationContext() != isolate->GetCurrentContext())
  321. scope = std::make_unique<v8::Context::Scope>(val->CreationContext());
  322. auto result = std::make_unique<base::ListValue>();
  323. // Only fields with integer keys are carried over to the ListValue.
  324. for (uint32_t i = 0; i < val->Length(); ++i) {
  325. v8::TryCatch try_catch(isolate);
  326. v8::Local<v8::Value> child_v8;
  327. v8::MaybeLocal<v8::Value> maybe_child =
  328. val->Get(isolate->GetCurrentContext(), i);
  329. if (try_catch.HasCaught() || !maybe_child.ToLocal(&child_v8)) {
  330. LOG(ERROR) << "Getter for index " << i << " threw an exception.";
  331. child_v8 = v8::Null(isolate);
  332. }
  333. if (!val->HasRealIndexedProperty(isolate->GetCurrentContext(), i)
  334. .FromMaybe(false)) {
  335. result->Append(std::make_unique<base::Value>());
  336. continue;
  337. }
  338. std::unique_ptr<base::Value> child =
  339. FromV8ValueImpl(state, child_v8, isolate);
  340. if (child)
  341. result->Append(std::move(child));
  342. else
  343. // JSON.stringify puts null in places where values don't serialize, for
  344. // example undefined and functions. Emulate that behavior.
  345. result->Append(std::make_unique<base::Value>());
  346. }
  347. return std::move(result);
  348. }
  349. std::unique_ptr<base::Value> V8ValueConverter::FromNodeBuffer(
  350. v8::Local<v8::Value> value,
  351. FromV8ValueState* state,
  352. v8::Isolate* isolate) const {
  353. std::vector<char> buffer(
  354. node::Buffer::Data(value),
  355. node::Buffer::Data(value) + node::Buffer::Length(value));
  356. return std::make_unique<base::Value>(std::move(buffer));
  357. }
  358. std::unique_ptr<base::Value> V8ValueConverter::FromV8Object(
  359. v8::Local<v8::Object> val,
  360. FromV8ValueState* state,
  361. v8::Isolate* isolate) const {
  362. ScopedUniquenessGuard uniqueness_guard(state, val);
  363. if (!uniqueness_guard.is_valid())
  364. return std::make_unique<base::Value>();
  365. std::unique_ptr<v8::Context::Scope> scope;
  366. // If val was created in a different context than our current one, change to
  367. // that context, but change back after val is converted.
  368. if (!val->CreationContext().IsEmpty() &&
  369. val->CreationContext() != isolate->GetCurrentContext())
  370. scope = std::make_unique<v8::Context::Scope>(val->CreationContext());
  371. auto result = std::make_unique<base::DictionaryValue>();
  372. v8::Local<v8::Array> property_names;
  373. if (!val->GetOwnPropertyNames(isolate->GetCurrentContext())
  374. .ToLocal(&property_names)) {
  375. return std::move(result);
  376. }
  377. for (uint32_t i = 0; i < property_names->Length(); ++i) {
  378. v8::Local<v8::Value> key =
  379. property_names->Get(isolate->GetCurrentContext(), i).ToLocalChecked();
  380. // Extend this test to cover more types as necessary and if sensible.
  381. if (!key->IsString() && !key->IsNumber()) {
  382. NOTREACHED() << "Key \"" << *v8::String::Utf8Value(isolate, key)
  383. << "\" "
  384. "is neither a string nor a number";
  385. continue;
  386. }
  387. v8::String::Utf8Value name_utf8(isolate, key);
  388. v8::TryCatch try_catch(isolate);
  389. v8::Local<v8::Value> child_v8;
  390. v8::MaybeLocal<v8::Value> maybe_child =
  391. val->Get(isolate->GetCurrentContext(), key);
  392. if (try_catch.HasCaught() || !maybe_child.ToLocal(&child_v8)) {
  393. LOG(ERROR) << "Getter for property " << *name_utf8
  394. << " threw an exception.";
  395. child_v8 = v8::Null(isolate);
  396. }
  397. std::unique_ptr<base::Value> child =
  398. FromV8ValueImpl(state, child_v8, isolate);
  399. if (!child)
  400. // JSON.stringify skips properties whose values don't serialize, for
  401. // example undefined and functions. Emulate that behavior.
  402. continue;
  403. // Strip null if asked (and since undefined is turned into null, undefined
  404. // too). The use case for supporting this is JSON-schema support,
  405. // specifically for extensions, where "optional" JSON properties may be
  406. // represented as null, yet due to buggy legacy code elsewhere isn't
  407. // treated as such (potentially causing crashes). For example, the
  408. // "tabs.create" function takes an object as its first argument with an
  409. // optional "windowId" property.
  410. //
  411. // Given just
  412. //
  413. // tabs.create({})
  414. //
  415. // this will work as expected on code that only checks for the existence of
  416. // a "windowId" property (such as that legacy code). However given
  417. //
  418. // tabs.create({windowId: null})
  419. //
  420. // there *is* a "windowId" property, but since it should be an int, code
  421. // on the browser which doesn't additionally check for null will fail.
  422. // We can avoid all bugs related to this by stripping null.
  423. if (strip_null_from_objects_ && child->is_none())
  424. continue;
  425. result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()),
  426. std::move(child));
  427. }
  428. return std::move(result);
  429. }
  430. } // namespace electron