#include "config.h"
#include "DOMWindow.h"
#include "BarInfo.h"
#include "CSSComputedStyleDeclaration.h"
#include "CSSRuleList.h"
#include "CSSStyleSelector.h"
#include "Chrome.h"
#include "Console.h"
#include "DOMSelection.h"
#include "Document.h"
#include "Element.h"
#include "FloatRect.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameTree.h"
#include "FrameView.h"
#include "History.h"
#include "MessageEvent.h"
#include "Page.h"
#include "PlatformScreen.h"
#include "PlatformString.h"
#include "Screen.h"
#include "WebkitPoint.h"
#include <algorithm>
#include <wtf/MathExtras.h>
#if ENABLE(DATABASE)
#include "Database.h"
#endif
#if ENABLE(OFFLINE_WEB_APPLICATIONS)
#include "DOMApplicationCache.h"
#endif
using std::min;
using std::max;
namespace WebCore {
void DOMWindow::adjustWindowRect(const FloatRect& screen, FloatRect& window, const FloatRect& pendingChanges)
{
}
DOMWindow::DOMWindow(Frame* frame)
: m_frame(frame)
{
}
DOMWindow::~DOMWindow()
{
}
void DOMWindow::disconnectFrame()
{
m_frame = 0;
clear();
}
void DOMWindow::clear()
{
if (m_console)
m_console->disconnectFrame();
m_console = 0;
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 ENABLE(OFFLINE_WEB_APPLICATIONS)
if (m_applicationCache)
m_applicationCache->disconnectFrame();
m_applicationCache = 0;
#endif
}
Screen* DOMWindow::screen() const
{
if (!m_screen)
m_screen = new Screen(m_frame);
return m_screen.get();
}
History* DOMWindow::history() const
{
if (!m_history)
m_history = new History(m_frame);
return m_history.get();
}
BarInfo* DOMWindow::locationbar() const
{
if (!m_locationbar)
m_locationbar = new BarInfo(m_frame, BarInfo::Locationbar);
return m_locationbar.get();
}
BarInfo* DOMWindow::menubar() const
{
if (!m_menubar)
m_menubar = new BarInfo(m_frame, BarInfo::Menubar);
return m_menubar.get();
}
BarInfo* DOMWindow::personalbar() const
{
if (!m_personalbar)
m_personalbar = new BarInfo(m_frame, BarInfo::Personalbar);
return m_personalbar.get();
}
BarInfo* DOMWindow::scrollbars() const
{
if (!m_scrollbars)
m_scrollbars = new BarInfo(m_frame, BarInfo::Scrollbars);
return m_scrollbars.get();
}
BarInfo* DOMWindow::statusbar() const
{
if (!m_statusbar)
m_statusbar = new BarInfo(m_frame, BarInfo::Statusbar);
return m_statusbar.get();
}
BarInfo* DOMWindow::toolbar() const
{
if (!m_toolbar)
m_toolbar = new BarInfo(m_frame, BarInfo::Toolbar);
return m_toolbar.get();
}
Console* DOMWindow::console() const
{
if (!m_console)
m_console = new Console(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();
}
#endif
#if ENABLE(CROSS_DOCUMENT_MESSAGING)
void DOMWindow::postMessage(const String& message, const String& domain, const String& uri, DOMWindow* source) const
{
ExceptionCode ec;
document()->dispatchEvent(new MessageEvent(message, domain, uri, source), ec, true);
}
#endif
DOMSelection* DOMWindow::getSelection()
{
if (!m_selection)
m_selection = new DOMSelection(m_frame);
return m_selection.get();
}
Element* DOMWindow::frameElement() const
{
if (!m_frame)
return 0;
Document* doc = m_frame->document();
ASSERT(doc);
if (!doc)
return 0;
return doc->ownerElement();
}
void DOMWindow::focus()
{
if (!m_frame)
return;
m_frame->focusWindow();
}
void DOMWindow::blur()
{
if (!m_frame)
return;
m_frame->unfocusWindow();
}
void DOMWindow::close()
{
if (!m_frame)
return;
if (m_frame->loader()->openedByDOM() || m_frame->loader()->getHistoryLength() <= 1)
m_frame->scheduleClose();
}
void DOMWindow::print()
{
if (!m_frame)
return;
Page* page = m_frame->page();
if (!page)
return;
page->chrome()->print(m_frame);
}
void DOMWindow::stop()
{
if (!m_frame)
return;
m_frame->loader()->stopForUserCancel(true);
}
void DOMWindow::alert(const String& message)
{
if (!m_frame)
return;
Document* doc = m_frame->document();
ASSERT(doc);
if (doc)
doc->updateRendering();
Page* page = m_frame->page();
if (!page)
return;
page->chrome()->runJavaScriptAlert(m_frame, message);
}
bool DOMWindow::confirm(const String& message)
{
if (!m_frame)
return false;
Document* doc = m_frame->document();
ASSERT(doc);
if (doc)
doc->updateRendering();
Page* page = m_frame->page();
if (!page)
return false;
return page->chrome()->runJavaScriptConfirm(m_frame, message);
}
String DOMWindow::prompt(const String& message, const String& defaultValue)
{
if (!m_frame)
return String();
Document* doc = m_frame->document();
ASSERT(doc);
if (doc)
doc->updateRendering();
Page* page = m_frame->page();
if (!page)
return String();
String returnValue;
if (page->chrome()->runJavaScriptPrompt(m_frame, message, defaultValue, returnValue))
return returnValue;
return String();
}
bool DOMWindow::find(const String& string, bool caseSensitive, bool backwards, bool wrap, bool wholeWord, bool searchInFrames, bool showDialog) const
{
if (!m_frame)
return false;
return m_frame->findString(string, !backwards, caseSensitive, wrap, false);
}
bool DOMWindow::offscreenBuffering() const
{
return true;
}
int DOMWindow::outerHeight() const
{
if (!m_frame)
return 0;
Page* page = m_frame->page();
if (!page)
return 0;
return static_cast<int>(page->chrome()->windowRect().height());
}
int DOMWindow::outerWidth() const
{
if (!m_frame)
return 0;
Page* page = m_frame->page();
if (!page)
return 0;
return static_cast<int>(page->chrome()->windowRect().width());
}
int DOMWindow::innerHeight() const
{
if (!m_frame)
return 0;
FrameView* view = m_frame->view();
if (!view)
return 0;
return view->actualVisibleHeight();
}
int DOMWindow::innerWidth() const
{
if (!m_frame)
return 0;
FrameView* view = m_frame->view();
if (!view)
return 0;
return view->actualVisibleWidth();
}
int DOMWindow::screenX() const
{
if (!m_frame)
return 0;
Page* page = m_frame->page();
if (!page)
return 0;
return static_cast<int>(page->chrome()->windowRect().x());
}
int DOMWindow::screenY() const
{
if (!m_frame)
return 0;
Page* page = m_frame->page();
if (!page)
return 0;
return static_cast<int>(page->chrome()->windowRect().y());
}
int DOMWindow::orientation() const
{
if (!m_frame)
return 0;
return m_frame->orientation();
}
int DOMWindow::scrollX() const
{
if (!m_frame)
return 0;
FrameView* view = m_frame->view();
if (!view)
return 0;
Document* doc = m_frame->document();
ASSERT(doc);
if (doc)
doc->updateLayoutIgnorePendingStylesheets();
return view->actualContentsX();
}
int DOMWindow::scrollY() const
{
if (!m_frame)
return 0;
FrameView* view = m_frame->view();
if (!view)
return 0;
Document* doc = m_frame->document();
ASSERT(doc);
if (doc)
doc->updateLayoutIgnorePendingStylesheets();
return view->actualContentsY();
}
bool DOMWindow::closed() const
{
return !m_frame;
}
unsigned DOMWindow::length() const
{
if (!m_frame)
return 0;
return m_frame->tree()->childCount();
}
String DOMWindow::name() const
{
if (!m_frame)
return String();
return m_frame->tree()->name();
}
void DOMWindow::setName(const String& string)
{
if (!m_frame)
return;
m_frame->tree()->setName(string);
}
String DOMWindow::status() const
{
if (!m_frame)
return String();
return m_frame->jsStatusBarText();
}
void DOMWindow::setStatus(const String& string)
{
if (!m_frame)
return;
m_frame->setJSStatusBarText(string);
}
String DOMWindow::defaultStatus() const
{
if (!m_frame)
return String();
return m_frame->jsDefaultStatusBarText();
}
void DOMWindow::setDefaultStatus(const String& string)
{
if (!m_frame)
return;
m_frame->setJSDefaultStatusBarText(string);
}
DOMWindow* DOMWindow::self() const
{
if (!m_frame)
return 0;
return m_frame->domWindow();
}
DOMWindow* DOMWindow::opener() const
{
if (!m_frame)
return 0;
Frame* opener = m_frame->loader()->opener();
if (!opener)
return 0;
return opener->domWindow();
}
DOMWindow* DOMWindow::parent() const
{
if (!m_frame)
return 0;
Frame* parent = m_frame->tree()->parent();
if (parent)
return parent->domWindow();
return m_frame->domWindow();
}
DOMWindow* DOMWindow::top() const
{
if (!m_frame)
return 0;
Page* page = m_frame->page();
if (!page)
return 0;
return page->mainFrame()->domWindow();
}
Document* DOMWindow::document() const
{
if (!m_frame)
return 0;
ASSERT(m_frame->document());
return m_frame->document();
}
PassRefPtr<CSSStyleDeclaration> DOMWindow::getComputedStyle(Element* elt, const String&) const
{
if (!elt)
return 0;
return new CSSComputedStyleDeclaration(elt);
}
PassRefPtr<CSSRuleList> DOMWindow::getMatchedCSSRules(Element* elt, const String& pseudoElt, bool authorOnly) const
{
if (!m_frame)
return 0;
Document* doc = m_frame->document();
ASSERT(doc);
if (!doc)
return 0;
if (!pseudoElt.isEmpty())
return doc->styleSelector()->pseudoStyleRulesForElement(elt, pseudoElt.impl(), authorOnly);
return doc->styleSelector()->styleRulesForElement(elt, authorOnly);
}
PassRefPtr<WebKitPoint> DOMWindow::webkitConvertPointFromNodeToPage(Node* node, const WebKitPoint* p) const
{
FloatPoint pp(p->x(), p->y());
pp = node->convertToPage(pp);
return new WebKitPoint(pp.x(), pp.y());
}
PassRefPtr<WebKitPoint> DOMWindow::webkitConvertPointFromPageToNode(Node* node, const WebKitPoint* p) const
{
FloatPoint pp(p->x(), p->y());
pp = node->convertFromPage(pp);
return new WebKitPoint(pp.x(), pp.y());
}
double DOMWindow::devicePixelRatio() const
{
if (!m_frame)
return 0.0;
Page* page = m_frame->page();
if (!page)
return 0.0;
return page->chrome()->scaleFactor();
}
#if ENABLE(DATABASE)
PassRefPtr<Database> DOMWindow::openDatabase(const String& name, const String& version, const String& displayName, unsigned long estimatedSize, ExceptionCode& ec)
{
if (!m_frame)
return 0;
Document* doc = m_frame->document();
ASSERT(doc);
if (!doc)
return 0;
return Database::openDatabase(doc, name, version, displayName, estimatedSize, ec);
}
#endif
void DOMWindow::scrollBy(int x, int y) const
{
if (!m_frame)
return;
Document* doc = m_frame->document();
ASSERT(doc);
if (doc)
doc->updateLayoutIgnorePendingStylesheets();
FrameView* view = m_frame->view();
if (!view)
return;
view->scrollBy(x, y);
}
void DOMWindow::scrollTo(int x, int y) const
{
if (!m_frame)
return;
Document* doc = m_frame->document();
ASSERT(doc);
if (doc)
doc->updateLayoutIgnorePendingStylesheets();
FrameView* view = m_frame->view();
if (!view)
return;
view->setContentsPos(x, y);
}
void DOMWindow::moveBy(float x, float y) const
{
if (!m_frame)
return;
Page* page = m_frame->page();
if (!page)
return;
FloatRect fr = page->chrome()->windowRect();
FloatRect update = fr;
update.move(x, y);
adjustWindowRect(screenAvailableRect(page->mainFrame()->view()), fr, update);
page->chrome()->setWindowRect(fr);
}
void DOMWindow::moveTo(float x, float y) const
{
if (!m_frame)
return;
Page* page = m_frame->page();
if (!page)
return;
FloatRect fr = page->chrome()->windowRect();
FloatRect sr = screenAvailableRect(page->mainFrame()->view());
fr.setLocation(sr.location());
FloatRect update = fr;
update.move(x, y);
adjustWindowRect(sr, fr, update);
page->chrome()->setWindowRect(fr);
}
void DOMWindow::resizeBy(float x, float y) const
{
if (!m_frame)
return;
Page* page = m_frame->page();
if (!page)
return;
FloatRect fr = page->chrome()->windowRect();
FloatSize dest = fr.size() + FloatSize(x, y);
FloatRect update(fr.location(), dest);
adjustWindowRect(screenAvailableRect(page->mainFrame()->view()), fr, update);
page->chrome()->setWindowRect(fr);
}
void DOMWindow::resizeTo(float width, float height) const
{
if (!m_frame)
return;
Page* page = m_frame->page();
if (!page)
return;
FloatRect fr = page->chrome()->windowRect();
FloatSize dest = FloatSize(width, height);
FloatRect update(fr.location(), dest);
adjustWindowRect(screenAvailableRect(page->mainFrame()->view()), fr, update);
page->chrome()->setWindowRect(fr);
}
}