WindowProxy.cpp (2266B)
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 "js/Context.h" // js::AssertHeapIsIdle 14 #include "vm/GlobalObject.h" // js::GlobalObject 15 #include "vm/JSContext.h" // JSContext, CHECK_THREAD 16 #include "vm/JSObject.h" // JSObject 17 #include "vm/Runtime.h" // JSRuntime 18 19 #include "vm/JSContext-inl.h" // JSContext::check 20 #include "vm/JSObject-inl.h" // JSObject::nonCCWGlobal 21 22 using JS::Handle; 23 24 void js::SetWindowProxyClass(JSContext* cx, const JSClass* clasp) { 25 MOZ_ASSERT(!cx->runtime()->maybeWindowProxyClass()); 26 cx->runtime()->setWindowProxyClass(clasp); 27 } 28 29 void js::SetWindowProxy(JSContext* cx, Handle<JSObject*> global, 30 Handle<JSObject*> windowProxy) { 31 AssertHeapIsIdle(); 32 CHECK_THREAD(cx); 33 34 cx->check(global, windowProxy); 35 MOZ_ASSERT(IsWindowProxy(windowProxy)); 36 37 GlobalObject& globalObj = global->as<GlobalObject>(); 38 if (globalObj.maybeWindowProxy() != windowProxy) { 39 globalObj.setWindowProxy(windowProxy); 40 globalObj.lexicalEnvironment().setWindowProxyThisObject(windowProxy); 41 } 42 } 43 44 JSObject* js::ToWindowIfWindowProxy(JSObject* obj) { 45 if (IsWindowProxy(obj)) { 46 return &obj->nonCCWGlobal(); 47 } 48 49 return obj; 50 } 51 52 JSObject* js::detail::ToWindowProxyIfWindowSlow(JSObject* obj) { 53 if (JSObject* windowProxy = obj->as<GlobalObject>().maybeWindowProxy()) { 54 return windowProxy; 55 } 56 57 return obj; 58 } 59 60 bool js::IsWindowProxy(JSObject* obj) { 61 // Note: simply checking `obj == obj->global().windowProxy()` is not 62 // sufficient: we may have transplanted the window proxy with a CCW. 63 // Check the Class to ensure we really have a window proxy. 64 return obj->getClass() == 65 obj->runtimeFromAnyThread()->maybeWindowProxyClass(); 66 } 67 68 bool js::detail::IsWindowSlow(JSObject* obj) { 69 return obj->as<GlobalObject>().maybeWindowProxy(); 70 }