📄 domwindow.cpp
字号:
/* * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */#include "config.h"#include "DOMWindow.h"#include "BarInfo.h"#include "CSSComputedStyleDeclaration.h"#include "CSSRuleList.h"#include "CSSStyleSelector.h"#include "CString.h"#include "Chrome.h"#include "Console.h"#include "DOMSelection.h"#include "Document.h"#include "Element.h"#include "EventListener.h"#include "EventNames.h"#include "ExceptionCode.h"#include "FloatRect.h"#include "Frame.h"#include "FrameLoader.h"#include "FrameTree.h"#include "FrameView.h"#include "HTMLFrameOwnerElement.h"#include "History.h"#include "InspectorController.h"#include "Location.h"#include "MessageEvent.h"#include "Navigator.h"#include "Page.h"#include "PageGroup.h"#include "PlatformScreen.h"#include "PlatformString.h"#include "Screen.h"#include "SecurityOrigin.h"#include "Settings.h"#include "WebKitPoint.h"#include <algorithm>#include <wtf/MathExtras.h>#if ENABLE(DATABASE)#include "Database.h"#endif#if ENABLE(DOM_STORAGE)#include "LocalStorage.h"#include "SessionStorage.h"#include "Storage.h"#include "StorageArea.h"#endif#if ENABLE(OFFLINE_WEB_APPLICATIONS)#include "DOMApplicationCache.h"#endifusing std::min;using std::max;namespace WebCore {class PostMessageTimer : public TimerBase {public: PostMessageTimer(DOMWindow* window, PassRefPtr<MessageEvent> event, SecurityOrigin* targetOrigin) : m_window(window) , m_event(event) , m_targetOrigin(targetOrigin) { } MessageEvent* event() const { return m_event.get(); } SecurityOrigin* targetOrigin() const { return m_targetOrigin.get(); }private: virtual void fired() { m_window->postMessageTimerFired(this); } RefPtr<DOMWindow> m_window; RefPtr<MessageEvent> m_event; RefPtr<SecurityOrigin> m_targetOrigin;};// This function:// 1) Validates the pending changes are not changing to NaN// 2) Constrains the window rect to no smaller than 100 in each dimension and no// bigger than the the float rect's dimensions.// 3) Constrain window rect to within the top and left boundaries of the screen rect// 4) Constraint the window rect to within the bottom and right boundaries of the// screen rect.// 5) Translate the window rect coordinates to be within the coordinate space of// the screen rect.void DOMWindow::adjustWindowRect(const FloatRect& screen, FloatRect& window, const FloatRect& pendingChanges){ // Make sure we're in a valid state before adjusting dimensions. ASSERT(isfinite(screen.x())); ASSERT(isfinite(screen.y())); ASSERT(isfinite(screen.width())); ASSERT(isfinite(screen.height())); ASSERT(isfinite(window.x())); ASSERT(isfinite(window.y())); ASSERT(isfinite(window.width())); ASSERT(isfinite(window.height())); // Update window values if new requested values are not NaN. if (!isnan(pendingChanges.x())) window.setX(pendingChanges.x()); if (!isnan(pendingChanges.y())) window.setY(pendingChanges.y()); if (!isnan(pendingChanges.width())) window.setWidth(pendingChanges.width()); if (!isnan(pendingChanges.height())) window.setHeight(pendingChanges.height()); // Resize the window to between 100 and the screen width and height. window.setWidth(min(max(100.0f, window.width()), screen.width())); window.setHeight(min(max(100.0f, window.height()), screen.height())); // Constrain the window position to the screen. window.setX(max(screen.x(), min(window.x(), screen.right() - window.width()))); window.setY(max(screen.y(), min(window.y(), screen.bottom() - window.height())));}DOMWindow::DOMWindow(Frame* frame) : m_frame(frame){}DOMWindow::~DOMWindow(){ if (m_frame) m_frame->clearFormerDOMWindow(this);}void DOMWindow::disconnectFrame(){ m_frame = 0; clear();}void DOMWindow::clear(){ if (m_screen) m_screen->disconnectFrame(); m_screen = 0; if (m_selection) m_selection->disconnectFrame(); m_selection = 0; if (m_history) m_history->disconnectFrame(); m_history = 0; if (m_locationbar) m_locationbar->disconnectFrame(); m_locationbar = 0; if (m_menubar) m_menubar->disconnectFrame(); m_menubar = 0; if (m_personalbar) m_personalbar->disconnectFrame(); m_personalbar = 0; if (m_scrollbars) m_scrollbars->disconnectFrame(); m_scrollbars = 0; if (m_statusbar) m_statusbar->disconnectFrame(); m_statusbar = 0; if (m_toolbar) m_toolbar->disconnectFrame(); m_toolbar = 0; if (m_console) m_console->disconnectFrame(); m_console = 0; if (m_navigator) m_navigator->disconnectFrame(); m_navigator = 0; if (m_location) m_location->disconnectFrame(); m_location = 0; #if ENABLE(DOM_STORAGE) if (m_sessionStorage) m_sessionStorage->disconnectFrame(); m_sessionStorage = 0; if (m_localStorage) m_localStorage->disconnectFrame(); m_localStorage = 0;#endif#if ENABLE(OFFLINE_WEB_APPLICATIONS) if (m_applicationCache) m_applicationCache->disconnectFrame(); m_applicationCache = 0;#endif}Screen* DOMWindow::screen() const{ if (!m_screen) m_screen = Screen::create(m_frame); return m_screen.get();}History* DOMWindow::history() const{ if (!m_history) m_history = History::create(m_frame); return m_history.get();}BarInfo* DOMWindow::locationbar() const{ if (!m_locationbar) m_locationbar = BarInfo::create(m_frame, BarInfo::Locationbar); return m_locationbar.get();}BarInfo* DOMWindow::menubar() const{ if (!m_menubar) m_menubar = BarInfo::create(m_frame, BarInfo::Menubar); return m_menubar.get();}BarInfo* DOMWindow::personalbar() const{ if (!m_personalbar) m_personalbar = BarInfo::create(m_frame, BarInfo::Personalbar); return m_personalbar.get();}BarInfo* DOMWindow::scrollbars() const{ if (!m_scrollbars) m_scrollbars = BarInfo::create(m_frame, BarInfo::Scrollbars); return m_scrollbars.get();}BarInfo* DOMWindow::statusbar() const{ if (!m_statusbar) m_statusbar = BarInfo::create(m_frame, BarInfo::Statusbar); return m_statusbar.get();}BarInfo* DOMWindow::toolbar() const{ if (!m_toolbar) m_toolbar = BarInfo::create(m_frame, BarInfo::Toolbar); return m_toolbar.get();}Console* DOMWindow::console() const{ if (!m_console) m_console = Console::create(m_frame); return m_console.get();}#if ENABLE(OFFLINE_WEB_APPLICATIONS)DOMApplicationCache* DOMWindow::applicationCache() const{ if (!m_applicationCache) m_applicationCache = DOMApplicationCache::create(m_frame); return m_applicationCache.get();}#endifNavigator* DOMWindow::navigator() const{ if (!m_navigator) m_navigator = Navigator::create(m_frame); return m_navigator.get();}Location* DOMWindow::location() const{ if (!m_location) m_location = Location::create(m_frame); return m_location.get();}#if ENABLE(DOM_STORAGE)Storage* DOMWindow::sessionStorage() const{ if (m_sessionStorage) return m_sessionStorage.get(); Page* page = m_frame->page(); if (!page) return 0; Document* document = m_frame->document(); RefPtr<StorageArea> storageArea = page->sessionStorage()->storageArea(document->securityOrigin()); page->inspectorController()->didUseDOMStorage(storageArea.get(), false, m_frame); m_sessionStorage = Storage::create(m_frame, storageArea.release()); return m_sessionStorage.get();}Storage* DOMWindow::localStorage() const{ Document* document = this->document(); if (!document) return 0; Page* page = document->page(); if (!page) return 0; Settings* settings = document->settings(); if (!settings || !settings->localStorageEnabled()) return 0; LocalStorage* localStorage = page->group().localStorage(); RefPtr<StorageArea> storageArea = localStorage ? localStorage->storageArea(document->securityOrigin()) : 0; if (storageArea) { page->inspectorController()->didUseDOMStorage(storageArea.get(), true, m_frame); m_localStorage = Storage::create(m_frame, storageArea.release()); } return m_localStorage.get();}#endifvoid DOMWindow::postMessage(const String& message, MessagePort* messagePort, const String& targetOrigin, DOMWindow* source, ExceptionCode& ec){ if (!m_frame) return; // Compute the target origin. We need to do this synchronously in order // to generate the SYNTAX_ERR exception correctly. RefPtr<SecurityOrigin> target; if (targetOrigin != "*") { target = SecurityOrigin::createFromString(targetOrigin); if (target->isEmpty()) { ec = SYNTAX_ERR; return; } } RefPtr<MessagePort> newMessagePort; if (messagePort) newMessagePort = messagePort->clone(ec); if (ec) return; // Capture the source of the message. We need to do this synchronously // in order to capture the source of the message correctly. Document* sourceDocument = source->document(); if (!sourceDocument) return; String sourceOrigin = sourceDocument->securityOrigin()->toString(); // Schedule the message. PostMessageTimer* timer = new PostMessageTimer(this, MessageEvent::create(message, sourceOrigin, "", source, newMessagePort), target.get()); timer->startOneShot(0);}void DOMWindow::postMessageTimerFired(PostMessageTimer* t){ OwnPtr<PostMessageTimer> timer(t); if (!document()) return; if (timer->targetOrigin()) { // Check target origin now since the target document may have changed since the simer was scheduled. if (!timer->targetOrigin()->isSameSchemeHostPort(document()->securityOrigin())) { String message = String::format("Unable to post message to %s. Recipient has origin %s.\n", timer->targetOrigin()->toString().utf8().data(), document()->securityOrigin()->toString().utf8().data()); console()->addMessage(JSMessageSource, ErrorMessageLevel, message, 0, String()); return; } } MessagePort* messagePort = timer->event()->messagePort(); ASSERT(!messagePort || !messagePort->scriptExecutionContext()); if (messagePort) messagePort->attachToContext(document()); document()->dispatchWindowEvent(timer->event());}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -