1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
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 
7 /* WindowProxy and Window implementation, for the web browser embedding. */
8 
9 #include "js/friend/WindowProxy.h"
10 
11 #include "mozilla/Assertions.h"  // MOZ_ASSERT
12 
13 #include "jsapi.h"  // js::AssertHeapIsIdle
14 
15 #include "vm/GlobalObject.h"  // js::GlobalObject
16 #include "vm/JSContext.h"     // JSContext, CHECK_THREAD
17 #include "vm/JSObject.h"      // JSObject
18 #include "vm/Runtime.h"       // JSRuntime
19 
20 #include "vm/JSContext-inl.h"  // JSContext::check
21 #include "vm/JSObject-inl.h"   // JSObject::nonCCWGlobal
22 
23 using JS::Handle;
24 
SetWindowProxyClass(JSContext * cx,const JSClass * clasp)25 void js::SetWindowProxyClass(JSContext* cx, const JSClass* clasp) {
26   MOZ_ASSERT(!cx->runtime()->maybeWindowProxyClass());
27   cx->runtime()->setWindowProxyClass(clasp);
28 }
29 
SetWindowProxy(JSContext * cx,Handle<JSObject * > global,Handle<JSObject * > windowProxy)30 void js::SetWindowProxy(JSContext* cx, Handle<JSObject*> global,
31                         Handle<JSObject*> windowProxy) {
32   AssertHeapIsIdle();
33   CHECK_THREAD(cx);
34 
35   cx->check(global, windowProxy);
36   MOZ_ASSERT(IsWindowProxy(windowProxy));
37 
38   GlobalObject& globalObj = global->as<GlobalObject>();
39   globalObj.setWindowProxy(windowProxy);
40   globalObj.lexicalEnvironment().setWindowProxyThisObject(windowProxy);
41 }
42 
ToWindowIfWindowProxy(JSObject * obj)43 JSObject* js::ToWindowIfWindowProxy(JSObject* obj) {
44   if (IsWindowProxy(obj)) {
45     return &obj->nonCCWGlobal();
46   }
47 
48   return obj;
49 }
50 
ToWindowProxyIfWindowSlow(JSObject * obj)51 JSObject* js::detail::ToWindowProxyIfWindowSlow(JSObject* obj) {
52   if (JSObject* windowProxy = obj->as<GlobalObject>().maybeWindowProxy()) {
53     return windowProxy;
54   }
55 
56   return obj;
57 }
58 
IsWindowProxy(JSObject * obj)59 bool js::IsWindowProxy(JSObject* obj) {
60   // Note: simply checking `obj == obj->global().windowProxy()` is not
61   // sufficient: we may have transplanted the window proxy with a CCW.
62   // Check the Class to ensure we really have a window proxy.
63   return obj->getClass() ==
64          obj->runtimeFromAnyThread()->maybeWindowProxyClass();
65 }
66 
IsWindowSlow(JSObject * obj)67 bool js::detail::IsWindowSlow(JSObject* obj) {
68   return obj->as<GlobalObject>().maybeWindowProxy();
69 }
70