JavaScriptShared.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #include "JavaScriptShared.h"
  7. #include "mozilla/dom/BindingUtils.h"
  8. #include "mozilla/dom/CPOWManagerGetter.h"
  9. #include "mozilla/dom/TabChild.h"
  10. #include "jsfriendapi.h"
  11. #include "xpcprivate.h"
  12. #include "WrapperFactory.h"
  13. #include "mozilla/Preferences.h"
  14. using namespace js;
  15. using namespace JS;
  16. using namespace mozilla;
  17. using namespace mozilla::jsipc;
  18. IdToObjectMap::IdToObjectMap()
  19. : table_(SystemAllocPolicy())
  20. {
  21. }
  22. bool
  23. IdToObjectMap::init()
  24. {
  25. if (table_.initialized())
  26. return true;
  27. return table_.init(32);
  28. }
  29. void
  30. IdToObjectMap::trace(JSTracer* trc, uint64_t minimimId)
  31. {
  32. for (Table::Range r(table_.all()); !r.empty(); r.popFront()) {
  33. if (r.front().key().serialNumber() >= minimimId)
  34. JS::TraceEdge(trc, &r.front().value(), "ipc-object");
  35. }
  36. }
  37. void
  38. IdToObjectMap::sweep()
  39. {
  40. for (Table::Enum e(table_); !e.empty(); e.popFront()) {
  41. JS::Heap<JSObject*>* objp = &e.front().value();
  42. JS_UpdateWeakPointerAfterGC(objp);
  43. if (!*objp)
  44. e.removeFront();
  45. }
  46. }
  47. JSObject*
  48. IdToObjectMap::find(ObjectId id)
  49. {
  50. Table::Ptr p = table_.lookup(id);
  51. if (!p)
  52. return nullptr;
  53. return p->value();
  54. }
  55. JSObject*
  56. IdToObjectMap::findPreserveColor(ObjectId id)
  57. {
  58. Table::Ptr p = table_.lookup(id);
  59. if (!p)
  60. return nullptr;
  61. return p->value().unbarrieredGet();
  62. }
  63. bool
  64. IdToObjectMap::add(ObjectId id, JSObject* obj)
  65. {
  66. return table_.put(id, obj);
  67. }
  68. void
  69. IdToObjectMap::remove(ObjectId id)
  70. {
  71. table_.remove(id);
  72. }
  73. void
  74. IdToObjectMap::clear()
  75. {
  76. table_.clear();
  77. }
  78. bool
  79. IdToObjectMap::empty() const
  80. {
  81. return table_.empty();
  82. }
  83. #ifdef DEBUG
  84. bool
  85. IdToObjectMap::has(const ObjectId& id, const JSObject* obj) const
  86. {
  87. auto p = table_.lookup(id);
  88. if (!p)
  89. return false;
  90. return p->value().unbarrieredGet() == obj;
  91. }
  92. #endif
  93. bool
  94. ObjectToIdMap::init()
  95. {
  96. return table_.initialized() || table_.init(32);
  97. }
  98. void
  99. ObjectToIdMap::trace(JSTracer* trc)
  100. {
  101. table_.trace(trc);
  102. }
  103. void
  104. ObjectToIdMap::sweep()
  105. {
  106. table_.sweep();
  107. }
  108. ObjectId
  109. ObjectToIdMap::find(JSObject* obj)
  110. {
  111. Table::Ptr p = table_.lookup(obj);
  112. if (!p)
  113. return ObjectId::nullId();
  114. return p->value();
  115. }
  116. bool
  117. ObjectToIdMap::add(JSContext* cx, JSObject* obj, ObjectId id)
  118. {
  119. return table_.put(obj, id);
  120. }
  121. void
  122. ObjectToIdMap::remove(JSObject* obj)
  123. {
  124. table_.remove(obj);
  125. }
  126. void
  127. ObjectToIdMap::clear()
  128. {
  129. table_.clear();
  130. }
  131. bool JavaScriptShared::sLoggingInitialized;
  132. bool JavaScriptShared::sLoggingEnabled;
  133. bool JavaScriptShared::sStackLoggingEnabled;
  134. JavaScriptShared::JavaScriptShared()
  135. : refcount_(1),
  136. nextSerialNumber_(1),
  137. nextCPOWNumber_(1)
  138. {
  139. if (!sLoggingInitialized) {
  140. sLoggingInitialized = true;
  141. if (PR_GetEnv("MOZ_CPOW_LOG")) {
  142. sLoggingEnabled = true;
  143. sStackLoggingEnabled = strstr(PR_GetEnv("MOZ_CPOW_LOG"), "stacks");
  144. } else {
  145. Preferences::AddBoolVarCache(&sLoggingEnabled,
  146. "dom.ipc.cpows.log.enabled", false);
  147. Preferences::AddBoolVarCache(&sStackLoggingEnabled,
  148. "dom.ipc.cpows.log.stack", false);
  149. }
  150. }
  151. }
  152. JavaScriptShared::~JavaScriptShared()
  153. {
  154. MOZ_RELEASE_ASSERT(cpows_.empty());
  155. }
  156. bool
  157. JavaScriptShared::init()
  158. {
  159. if (!objects_.init())
  160. return false;
  161. if (!cpows_.init())
  162. return false;
  163. if (!unwaivedObjectIds_.init())
  164. return false;
  165. if (!waivedObjectIds_.init())
  166. return false;
  167. return true;
  168. }
  169. void
  170. JavaScriptShared::decref()
  171. {
  172. refcount_--;
  173. if (!refcount_)
  174. delete this;
  175. }
  176. void
  177. JavaScriptShared::incref()
  178. {
  179. refcount_++;
  180. }
  181. bool
  182. JavaScriptShared::convertIdToGeckoString(JSContext* cx, JS::HandleId id, nsString* to)
  183. {
  184. RootedValue idval(cx);
  185. if (!JS_IdToValue(cx, id, &idval))
  186. return false;
  187. RootedString str(cx, ToString(cx, idval));
  188. if (!str)
  189. return false;
  190. return AssignJSString(cx, *to, str);
  191. }
  192. bool
  193. JavaScriptShared::convertGeckoStringToId(JSContext* cx, const nsString& from, JS::MutableHandleId to)
  194. {
  195. RootedString str(cx, JS_NewUCStringCopyN(cx, from.BeginReading(), from.Length()));
  196. if (!str)
  197. return false;
  198. return JS_StringToId(cx, str, to);
  199. }
  200. bool
  201. JavaScriptShared::toVariant(JSContext* cx, JS::HandleValue from, JSVariant* to)
  202. {
  203. switch (JS_TypeOfValue(cx, from)) {
  204. case JSTYPE_VOID:
  205. *to = UndefinedVariant();
  206. return true;
  207. case JSTYPE_OBJECT:
  208. case JSTYPE_FUNCTION:
  209. {
  210. RootedObject obj(cx, from.toObjectOrNull());
  211. if (!obj) {
  212. MOZ_ASSERT(from.isNull());
  213. *to = NullVariant();
  214. return true;
  215. }
  216. if (xpc_JSObjectIsID(cx, obj)) {
  217. JSIID iid;
  218. const nsID* id = xpc_JSObjectToID(cx, obj);
  219. ConvertID(*id, &iid);
  220. *to = iid;
  221. return true;
  222. }
  223. ObjectVariant objVar;
  224. if (!toObjectVariant(cx, obj, &objVar))
  225. return false;
  226. *to = objVar;
  227. return true;
  228. }
  229. case JSTYPE_SYMBOL:
  230. {
  231. RootedSymbol sym(cx, from.toSymbol());
  232. SymbolVariant symVar;
  233. if (!toSymbolVariant(cx, sym, &symVar))
  234. return false;
  235. *to = symVar;
  236. return true;
  237. }
  238. case JSTYPE_STRING:
  239. {
  240. nsAutoJSString autoStr;
  241. if (!autoStr.init(cx, from))
  242. return false;
  243. *to = autoStr;
  244. return true;
  245. }
  246. case JSTYPE_NUMBER:
  247. if (from.isInt32())
  248. *to = double(from.toInt32());
  249. else
  250. *to = from.toDouble();
  251. return true;
  252. case JSTYPE_BOOLEAN:
  253. *to = from.toBoolean();
  254. return true;
  255. default:
  256. MOZ_ASSERT(false);
  257. return false;
  258. }
  259. }
  260. bool
  261. JavaScriptShared::fromVariant(JSContext* cx, const JSVariant& from, MutableHandleValue to)
  262. {
  263. switch (from.type()) {
  264. case JSVariant::TUndefinedVariant:
  265. to.set(UndefinedValue());
  266. return true;
  267. case JSVariant::TNullVariant:
  268. to.set(NullValue());
  269. return true;
  270. case JSVariant::TObjectVariant:
  271. {
  272. JSObject* obj = fromObjectVariant(cx, from.get_ObjectVariant());
  273. if (!obj)
  274. return false;
  275. to.set(ObjectValue(*obj));
  276. return true;
  277. }
  278. case JSVariant::TSymbolVariant:
  279. {
  280. Symbol* sym = fromSymbolVariant(cx, from.get_SymbolVariant());
  281. if (!sym)
  282. return false;
  283. to.setSymbol(sym);
  284. return true;
  285. }
  286. case JSVariant::Tdouble:
  287. to.set(JS_NumberValue(from.get_double()));
  288. return true;
  289. case JSVariant::Tbool:
  290. to.setBoolean(from.get_bool());
  291. return true;
  292. case JSVariant::TnsString:
  293. {
  294. const nsString& old = from.get_nsString();
  295. JSString* str = JS_NewUCStringCopyN(cx, old.BeginReading(), old.Length());
  296. if (!str)
  297. return false;
  298. to.set(StringValue(str));
  299. return true;
  300. }
  301. case JSVariant::TJSIID:
  302. {
  303. nsID iid;
  304. const JSIID& id = from.get_JSIID();
  305. ConvertID(id, &iid);
  306. JSCompartment* compartment = GetContextCompartment(cx);
  307. RootedObject global(cx, JS_GetGlobalForCompartmentOrNull(cx, compartment));
  308. JSObject* obj = xpc_NewIDObject(cx, global, iid);
  309. if (!obj)
  310. return false;
  311. to.set(ObjectValue(*obj));
  312. return true;
  313. }
  314. default:
  315. MOZ_CRASH("NYI");
  316. return false;
  317. }
  318. }
  319. bool
  320. JavaScriptShared::toJSIDVariant(JSContext* cx, HandleId from, JSIDVariant* to)
  321. {
  322. if (JSID_IS_STRING(from)) {
  323. nsAutoJSString autoStr;
  324. if (!autoStr.init(cx, JSID_TO_STRING(from)))
  325. return false;
  326. *to = autoStr;
  327. return true;
  328. }
  329. if (JSID_IS_INT(from)) {
  330. *to = JSID_TO_INT(from);
  331. return true;
  332. }
  333. if (JSID_IS_SYMBOL(from)) {
  334. SymbolVariant symVar;
  335. if (!toSymbolVariant(cx, JSID_TO_SYMBOL(from), &symVar))
  336. return false;
  337. *to = symVar;
  338. return true;
  339. }
  340. MOZ_ASSERT(false);
  341. return false;
  342. }
  343. bool
  344. JavaScriptShared::fromJSIDVariant(JSContext* cx, const JSIDVariant& from, MutableHandleId to)
  345. {
  346. switch (from.type()) {
  347. case JSIDVariant::TSymbolVariant: {
  348. Symbol* sym = fromSymbolVariant(cx, from.get_SymbolVariant());
  349. if (!sym)
  350. return false;
  351. to.set(SYMBOL_TO_JSID(sym));
  352. return true;
  353. }
  354. case JSIDVariant::TnsString:
  355. return convertGeckoStringToId(cx, from.get_nsString(), to);
  356. case JSIDVariant::Tint32_t:
  357. to.set(INT_TO_JSID(from.get_int32_t()));
  358. return true;
  359. default:
  360. return false;
  361. }
  362. }
  363. bool
  364. JavaScriptShared::toSymbolVariant(JSContext* cx, JS::Symbol* symArg, SymbolVariant* symVarp)
  365. {
  366. RootedSymbol sym(cx, symArg);
  367. MOZ_ASSERT(sym);
  368. SymbolCode code = GetSymbolCode(sym);
  369. if (static_cast<uint32_t>(code) < WellKnownSymbolLimit) {
  370. *symVarp = WellKnownSymbol(static_cast<uint32_t>(code));
  371. return true;
  372. }
  373. if (code == SymbolCode::InSymbolRegistry) {
  374. nsAutoJSString autoStr;
  375. if (!autoStr.init(cx, GetSymbolDescription(sym)))
  376. return false;
  377. *symVarp = RegisteredSymbol(autoStr);
  378. return true;
  379. }
  380. JS_ReportErrorASCII(cx, "unique symbol can't be used with CPOW");
  381. return false;
  382. }
  383. JS::Symbol*
  384. JavaScriptShared::fromSymbolVariant(JSContext* cx, const SymbolVariant& symVar)
  385. {
  386. switch (symVar.type()) {
  387. case SymbolVariant::TWellKnownSymbol: {
  388. uint32_t which = symVar.get_WellKnownSymbol().which();
  389. if (which < WellKnownSymbolLimit)
  390. return GetWellKnownSymbol(cx, static_cast<SymbolCode>(which));
  391. MOZ_ASSERT(false, "bad child data");
  392. return nullptr;
  393. }
  394. case SymbolVariant::TRegisteredSymbol: {
  395. nsString key = symVar.get_RegisteredSymbol().key();
  396. RootedString str(cx, JS_NewUCStringCopyN(cx, key.get(), key.Length()));
  397. if (!str)
  398. return nullptr;
  399. return GetSymbolFor(cx, str);
  400. }
  401. default:
  402. return nullptr;
  403. }
  404. }
  405. /* static */ void
  406. JavaScriptShared::ConvertID(const nsID& from, JSIID* to)
  407. {
  408. to->m0() = from.m0;
  409. to->m1() = from.m1;
  410. to->m2() = from.m2;
  411. to->m3_0() = from.m3[0];
  412. to->m3_1() = from.m3[1];
  413. to->m3_2() = from.m3[2];
  414. to->m3_3() = from.m3[3];
  415. to->m3_4() = from.m3[4];
  416. to->m3_5() = from.m3[5];
  417. to->m3_6() = from.m3[6];
  418. to->m3_7() = from.m3[7];
  419. }
  420. /* static */ void
  421. JavaScriptShared::ConvertID(const JSIID& from, nsID* to)
  422. {
  423. to->m0 = from.m0();
  424. to->m1 = from.m1();
  425. to->m2 = from.m2();
  426. to->m3[0] = from.m3_0();
  427. to->m3[1] = from.m3_1();
  428. to->m3[2] = from.m3_2();
  429. to->m3[3] = from.m3_3();
  430. to->m3[4] = from.m3_4();
  431. to->m3[5] = from.m3_5();
  432. to->m3[6] = from.m3_6();
  433. to->m3[7] = from.m3_7();
  434. }
  435. JSObject*
  436. JavaScriptShared::findObjectById(JSContext* cx, const ObjectId& objId)
  437. {
  438. RootedObject obj(cx, objects_.find(objId));
  439. if (!obj) {
  440. JS_ReportErrorASCII(cx, "operation not possible on dead CPOW");
  441. return nullptr;
  442. }
  443. // Each process has a dedicated compartment for CPOW targets. All CPOWs
  444. // from the other process point to objects in this scope. From there, they
  445. // can access objects in other compartments using cross-compartment
  446. // wrappers.
  447. JSAutoCompartment ac(cx, scopeForTargetObjects());
  448. if (objId.hasXrayWaiver()) {
  449. {
  450. JSAutoCompartment ac2(cx, obj);
  451. obj = js::ToWindowProxyIfWindow(obj);
  452. MOZ_ASSERT(obj);
  453. }
  454. if (!xpc::WrapperFactory::WaiveXrayAndWrap(cx, &obj))
  455. return nullptr;
  456. } else {
  457. if (!JS_WrapObject(cx, &obj))
  458. return nullptr;
  459. }
  460. return obj;
  461. }
  462. static const uint64_t UnknownPropertyOp = 1;
  463. bool
  464. JavaScriptShared::fromDescriptor(JSContext* cx, Handle<PropertyDescriptor> desc,
  465. PPropertyDescriptor* out)
  466. {
  467. out->attrs() = desc.attributes();
  468. if (!toVariant(cx, desc.value(), &out->value()))
  469. return false;
  470. if (!toObjectOrNullVariant(cx, desc.object(), &out->obj()))
  471. return false;
  472. if (!desc.getter()) {
  473. out->getter() = 0;
  474. } else if (desc.hasGetterObject()) {
  475. JSObject* getter = desc.getterObject();
  476. ObjectVariant objVar;
  477. if (!toObjectVariant(cx, getter, &objVar))
  478. return false;
  479. out->getter() = objVar;
  480. } else {
  481. MOZ_ASSERT(desc.getter() != JS_PropertyStub);
  482. out->getter() = UnknownPropertyOp;
  483. }
  484. if (!desc.setter()) {
  485. out->setter() = 0;
  486. } else if (desc.hasSetterObject()) {
  487. JSObject* setter = desc.setterObject();
  488. ObjectVariant objVar;
  489. if (!toObjectVariant(cx, setter, &objVar))
  490. return false;
  491. out->setter() = objVar;
  492. } else {
  493. MOZ_ASSERT(desc.setter() != JS_StrictPropertyStub);
  494. out->setter() = UnknownPropertyOp;
  495. }
  496. return true;
  497. }
  498. bool
  499. UnknownPropertyStub(JSContext* cx, HandleObject obj, HandleId id, MutableHandleValue vp)
  500. {
  501. JS_ReportErrorASCII(cx, "getter could not be wrapped via CPOWs");
  502. return false;
  503. }
  504. bool
  505. UnknownStrictPropertyStub(JSContext* cx, HandleObject obj, HandleId id, MutableHandleValue vp,
  506. ObjectOpResult& result)
  507. {
  508. JS_ReportErrorASCII(cx, "setter could not be wrapped via CPOWs");
  509. return false;
  510. }
  511. bool
  512. JavaScriptShared::toDescriptor(JSContext* cx, const PPropertyDescriptor& in,
  513. MutableHandle<PropertyDescriptor> out)
  514. {
  515. out.setAttributes(in.attrs());
  516. if (!fromVariant(cx, in.value(), out.value()))
  517. return false;
  518. out.object().set(fromObjectOrNullVariant(cx, in.obj()));
  519. if (in.getter().type() == GetterSetter::Tuint64_t && !in.getter().get_uint64_t()) {
  520. out.setGetter(nullptr);
  521. } else if (in.attrs() & JSPROP_GETTER) {
  522. Rooted<JSObject*> getter(cx);
  523. getter = fromObjectVariant(cx, in.getter().get_ObjectVariant());
  524. if (!getter)
  525. return false;
  526. out.setGetter(JS_DATA_TO_FUNC_PTR(JSGetterOp, getter.get()));
  527. } else {
  528. out.setGetter(UnknownPropertyStub);
  529. }
  530. if (in.setter().type() == GetterSetter::Tuint64_t && !in.setter().get_uint64_t()) {
  531. out.setSetter(nullptr);
  532. } else if (in.attrs() & JSPROP_SETTER) {
  533. Rooted<JSObject*> setter(cx);
  534. setter = fromObjectVariant(cx, in.setter().get_ObjectVariant());
  535. if (!setter)
  536. return false;
  537. out.setSetter(JS_DATA_TO_FUNC_PTR(JSSetterOp, setter.get()));
  538. } else {
  539. out.setSetter(UnknownStrictPropertyStub);
  540. }
  541. return true;
  542. }
  543. bool
  544. JavaScriptShared::toObjectOrNullVariant(JSContext* cx, JSObject* obj, ObjectOrNullVariant* objVarp)
  545. {
  546. if (!obj) {
  547. *objVarp = NullVariant();
  548. return true;
  549. }
  550. ObjectVariant objVar;
  551. if (!toObjectVariant(cx, obj, &objVar))
  552. return false;
  553. *objVarp = objVar;
  554. return true;
  555. }
  556. JSObject*
  557. JavaScriptShared::fromObjectOrNullVariant(JSContext* cx, const ObjectOrNullVariant& objVar)
  558. {
  559. if (objVar.type() == ObjectOrNullVariant::TNullVariant)
  560. return nullptr;
  561. return fromObjectVariant(cx, objVar.get_ObjectVariant());
  562. }
  563. CrossProcessCpowHolder::CrossProcessCpowHolder(dom::CPOWManagerGetter* managerGetter,
  564. const InfallibleTArray<CpowEntry>& cpows)
  565. : js_(nullptr),
  566. cpows_(cpows),
  567. unwrapped_(false)
  568. {
  569. // Only instantiate the CPOW manager if we might need it later.
  570. if (cpows.Length())
  571. js_ = managerGetter->GetCPOWManager();
  572. }
  573. CrossProcessCpowHolder::~CrossProcessCpowHolder()
  574. {
  575. if (cpows_.Length() && !unwrapped_) {
  576. // This should only happen if a message manager message
  577. // containing CPOWs gets ignored for some reason. We need to
  578. // unwrap every incoming CPOW in this process to ensure that
  579. // the corresponding part of the CPOW in the other process
  580. // will eventually be collected. The scope for this object
  581. // doesn't really matter, because it immediately becomes
  582. // garbage.
  583. AutoJSAPI jsapi;
  584. if (!jsapi.Init(xpc::PrivilegedJunkScope()))
  585. return;
  586. JSContext* cx = jsapi.cx();
  587. JS::Rooted<JSObject*> cpows(cx);
  588. js_->Unwrap(cx, cpows_, &cpows);
  589. }
  590. }
  591. bool
  592. CrossProcessCpowHolder::ToObject(JSContext* cx, JS::MutableHandleObject objp)
  593. {
  594. unwrapped_ = true;
  595. if (!cpows_.Length())
  596. return true;
  597. return js_->Unwrap(cx, cpows_, objp);
  598. }
  599. bool
  600. JavaScriptShared::Unwrap(JSContext* cx, const InfallibleTArray<CpowEntry>& aCpows,
  601. JS::MutableHandleObject objp)
  602. {
  603. objp.set(nullptr);
  604. if (!aCpows.Length())
  605. return true;
  606. RootedObject obj(cx, JS_NewPlainObject(cx));
  607. if (!obj)
  608. return false;
  609. RootedValue v(cx);
  610. RootedString str(cx);
  611. for (size_t i = 0; i < aCpows.Length(); i++) {
  612. const nsString& name = aCpows[i].name();
  613. if (!fromVariant(cx, aCpows[i].value(), &v))
  614. return false;
  615. if (!JS_DefineUCProperty(cx,
  616. obj,
  617. name.BeginReading(),
  618. name.Length(),
  619. v,
  620. JSPROP_ENUMERATE))
  621. {
  622. return false;
  623. }
  624. }
  625. objp.set(obj);
  626. return true;
  627. }
  628. bool
  629. JavaScriptShared::Wrap(JSContext* cx, HandleObject aObj, InfallibleTArray<CpowEntry>* outCpows)
  630. {
  631. if (!aObj)
  632. return true;
  633. Rooted<IdVector> ids(cx, IdVector(cx));
  634. if (!JS_Enumerate(cx, aObj, &ids))
  635. return false;
  636. RootedId id(cx);
  637. RootedValue v(cx);
  638. for (size_t i = 0; i < ids.length(); i++) {
  639. id = ids[i];
  640. nsString str;
  641. if (!convertIdToGeckoString(cx, id, &str))
  642. return false;
  643. if (!JS_GetPropertyById(cx, aObj, id, &v))
  644. return false;
  645. JSVariant var;
  646. if (!toVariant(cx, v, &var))
  647. return false;
  648. outCpows->AppendElement(CpowEntry(str, var));
  649. }
  650. return true;
  651. }
  652. CPOWManager*
  653. mozilla::jsipc::CPOWManagerFor(PJavaScriptParent* aParent)
  654. {
  655. return static_cast<JavaScriptParent*>(aParent);
  656. }
  657. CPOWManager*
  658. mozilla::jsipc::CPOWManagerFor(PJavaScriptChild* aChild)
  659. {
  660. return static_cast<JavaScriptChild*>(aChild);
  661. }