/platform/external/webkit/WebCore/bindings/js/JSDOMWindowBase.cpp

https://github.com/aharish/totoro-gb-opensource-update2 · C++ · 210 lines · 146 code · 32 blank · 32 comment · 16 complexity · 3b12e2356f3d97739a5eda25af00cf3c MD5 · raw file

  1. /*
  2. * Copyright (C) 2000 Harri Porten (porten@kde.org)
  3. * Copyright (C) 2006 Jon Shier (jshier@iastate.edu)
  4. * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reseved.
  5. * Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org)
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
  20. * USA
  21. */
  22. #include "config.h"
  23. #include "JSDOMWindowBase.h"
  24. #include "CString.h"
  25. #include "Chrome.h"
  26. #include "Console.h"
  27. #include "DOMWindow.h"
  28. #include "Frame.h"
  29. #include "InspectorController.h"
  30. #include "JSDOMWindowCustom.h"
  31. #include "JSNode.h"
  32. #include "Logging.h"
  33. #include "Page.h"
  34. #include "ScriptController.h"
  35. #include "SecurityOrigin.h"
  36. #include "Settings.h"
  37. using namespace JSC;
  38. namespace WebCore {
  39. const ClassInfo JSDOMWindowBase::s_info = { "Window", &JSDOMGlobalObject::s_info, 0, 0 };
  40. JSDOMWindowBase::JSDOMWindowBaseData::JSDOMWindowBaseData(PassRefPtr<DOMWindow> window, JSDOMWindowShell* shell)
  41. : JSDOMGlobalObjectData(shell->world(), destroyJSDOMWindowBaseData)
  42. , impl(window)
  43. , shell(shell)
  44. {
  45. }
  46. JSDOMWindowBase::JSDOMWindowBase(NonNullPassRefPtr<Structure> structure, PassRefPtr<DOMWindow> window, JSDOMWindowShell* shell)
  47. : JSDOMGlobalObject(structure, new JSDOMWindowBaseData(window, shell), shell)
  48. {
  49. GlobalPropertyInfo staticGlobals[] = {
  50. GlobalPropertyInfo(Identifier(globalExec(), "document"), jsNull(), DontDelete | ReadOnly),
  51. GlobalPropertyInfo(Identifier(globalExec(), "window"), d()->shell, DontDelete | ReadOnly)
  52. };
  53. addStaticGlobals(staticGlobals, sizeof(staticGlobals) / sizeof(GlobalPropertyInfo));
  54. }
  55. void JSDOMWindowBase::updateDocument()
  56. {
  57. ASSERT(d()->impl->document());
  58. ExecState* exec = globalExec();
  59. symbolTablePutWithAttributes(Identifier(exec, "document"), toJS(exec, this, d()->impl->document()), DontDelete | ReadOnly);
  60. }
  61. ScriptExecutionContext* JSDOMWindowBase::scriptExecutionContext() const
  62. {
  63. return d()->impl->document();
  64. }
  65. String JSDOMWindowBase::crossDomainAccessErrorMessage(const JSGlobalObject* other) const
  66. {
  67. KURL originURL = asJSDOMWindow(other)->impl()->url();
  68. KURL targetURL = d()->shell->window()->impl()->url();
  69. if (originURL.isNull() || targetURL.isNull())
  70. return String();
  71. // FIXME: this error message should contain more specifics of why the same origin check has failed.
  72. return String::format("Unsafe JavaScript attempt to access frame with URL %s from frame with URL %s. Domains, protocols and ports must match.\n",
  73. targetURL.string().utf8().data(), originURL.string().utf8().data());
  74. }
  75. void JSDOMWindowBase::printErrorMessage(const String& message) const
  76. {
  77. printErrorMessageForFrame(impl()->frame(), message);
  78. }
  79. ExecState* JSDOMWindowBase::globalExec()
  80. {
  81. // We need to make sure that any script execution happening in this
  82. // frame does not destroy it
  83. if (Frame *frame = impl()->frame())
  84. frame->keepAlive();
  85. return Base::globalExec();
  86. }
  87. bool JSDOMWindowBase::supportsProfiling() const
  88. {
  89. #if !ENABLE(JAVASCRIPT_DEBUGGER) || !ENABLE(INSPECTOR)
  90. return false;
  91. #else
  92. Frame* frame = impl()->frame();
  93. if (!frame)
  94. return false;
  95. Page* page = frame->page();
  96. if (!page)
  97. return false;
  98. return page->inspectorController()->profilerEnabled();
  99. #endif
  100. }
  101. bool JSDOMWindowBase::shouldInterruptScript() const
  102. {
  103. ASSERT(impl()->frame());
  104. Page* page = impl()->frame()->page();
  105. // See <rdar://problem/5479443>. We don't think that page can ever be NULL
  106. // in this case, but if it is, we've gotten into a state where we may have
  107. // hung the UI, with no way to ask the client whether to cancel execution.
  108. // For now, our solution is just to cancel execution no matter what,
  109. // ensuring that we never hang. We might want to consider other solutions
  110. // if we discover problems with this one.
  111. ASSERT(page);
  112. if (!page)
  113. return true;
  114. return page->chrome()->shouldInterruptJavaScript();
  115. }
  116. void JSDOMWindowBase::willRemoveFromWindowShell()
  117. {
  118. setCurrentEvent(0);
  119. }
  120. JSObject* JSDOMWindowBase::toThisObject(ExecState*) const
  121. {
  122. return shell();
  123. }
  124. JSDOMWindowShell* JSDOMWindowBase::shell() const
  125. {
  126. return d()->shell;
  127. }
  128. JSGlobalData* JSDOMWindowBase::commonJSGlobalData()
  129. {
  130. ASSERT(isMainThread());
  131. static JSGlobalData* globalData = 0;
  132. if (!globalData) {
  133. globalData = JSGlobalData::createLeaked().releaseRef();
  134. globalData->timeoutChecker.setTimeoutInterval(10000); // 10 seconds
  135. #ifndef NDEBUG
  136. globalData->mainThreadOnly = true;
  137. #endif
  138. globalData->clientData = new WebCoreJSClientData(globalData);
  139. }
  140. return globalData;
  141. }
  142. void JSDOMWindowBase::destroyJSDOMWindowBaseData(void* jsDOMWindowBaseData)
  143. {
  144. delete static_cast<JSDOMWindowBaseData*>(jsDOMWindowBaseData);
  145. }
  146. // JSDOMGlobalObject* is ignored, accessing a window in any context will
  147. // use that DOMWindow's prototype chain.
  148. JSValue toJS(ExecState* exec, JSDOMGlobalObject*, DOMWindow* domWindow)
  149. {
  150. return toJS(exec, domWindow);
  151. }
  152. JSValue toJS(ExecState* exec, DOMWindow* domWindow)
  153. {
  154. if (!domWindow)
  155. return jsNull();
  156. Frame* frame = domWindow->frame();
  157. if (!frame)
  158. return jsNull();
  159. return frame->script()->windowShell(currentWorld(exec));
  160. }
  161. JSDOMWindow* toJSDOMWindow(Frame* frame, DOMWrapperWorld* world)
  162. {
  163. if (!frame)
  164. return 0;
  165. return frame->script()->windowShell(world)->window();
  166. }
  167. JSDOMWindow* toJSDOMWindow(JSValue value)
  168. {
  169. if (!value.isObject())
  170. return 0;
  171. const ClassInfo* classInfo = asObject(value)->classInfo();
  172. if (classInfo == &JSDOMWindow::s_info)
  173. return static_cast<JSDOMWindow*>(asObject(value));
  174. if (classInfo == &JSDOMWindowShell::s_info)
  175. return static_cast<JSDOMWindowShell*>(asObject(value))->window();
  176. return 0;
  177. }
  178. } // namespace WebCore