webengine/osswebengine/WebCore/page/DOMWindow.cpp
changeset 0 dd21522fd290
child 68 92a765b5b3e7
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions
       
     6  * are met:
       
     7  * 1. Redistributions of source code must retain the above copyright
       
     8  *    notice, this list of conditions and the following disclaimer.
       
     9  * 2. Redistributions in binary form must reproduce the above copyright
       
    10  *    notice, this list of conditions and the following disclaimer in the
       
    11  *    documentation and/or other materials provided with the distribution.
       
    12  *
       
    13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
       
    14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
       
    17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
       
    21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
       
    24  */
       
    25 
       
    26 #include "config.h"
       
    27 #include "DOMWindow.h"
       
    28 
       
    29 #include "BarInfo.h"
       
    30 #include "CSSComputedStyleDeclaration.h"
       
    31 #include "CSSRuleList.h"
       
    32 #include "CSSStyleSelector.h"
       
    33 #include "Chrome.h"
       
    34 #include "DOMSelection.h"
       
    35 #include "Document.h"
       
    36 #include "Element.h"
       
    37 #include "Frame.h"
       
    38 #include "FrameLoader.h"
       
    39 #include "FrameTree.h"
       
    40 #include "FrameView.h"
       
    41 #include "History.h"
       
    42 #include "Page.h"
       
    43 #include "PlatformScreen.h"
       
    44 #include "PlatformString.h"
       
    45 #include "Screen.h"
       
    46 
       
    47 namespace WebCore {
       
    48 
       
    49 DOMWindow::DOMWindow(Frame* frame)
       
    50     : m_frame(frame)
       
    51 {
       
    52 }
       
    53 
       
    54 DOMWindow::~DOMWindow()
       
    55 {
       
    56 }
       
    57 
       
    58 void DOMWindow::disconnectFrame()
       
    59 {
       
    60     m_frame = 0;
       
    61     clear();
       
    62 }
       
    63 
       
    64 void DOMWindow::clear()
       
    65 {
       
    66     if (m_screen)
       
    67         m_screen->disconnectFrame();
       
    68     m_screen = 0;
       
    69 
       
    70     if (m_selection)
       
    71         m_selection->disconnectFrame();
       
    72     m_selection = 0;
       
    73 
       
    74     if (m_history)
       
    75         m_history->disconnectFrame();
       
    76     m_history = 0;
       
    77 
       
    78     if (m_locationbar)
       
    79         m_locationbar->disconnectFrame();
       
    80     m_locationbar = 0;
       
    81 
       
    82     if (m_menubar)
       
    83         m_menubar->disconnectFrame();
       
    84     m_menubar = 0;
       
    85 
       
    86     if (m_personalbar)
       
    87         m_personalbar->disconnectFrame();
       
    88     m_personalbar = 0;
       
    89 
       
    90     if (m_scrollbars)
       
    91         m_scrollbars->disconnectFrame();
       
    92     m_scrollbars = 0;
       
    93 
       
    94     if (m_statusbar)
       
    95         m_statusbar->disconnectFrame();
       
    96     m_statusbar = 0;
       
    97 
       
    98     if (m_toolbar)
       
    99         m_toolbar->disconnectFrame();
       
   100     m_toolbar = 0;
       
   101 }
       
   102 
       
   103 Screen* DOMWindow::screen() const
       
   104 {
       
   105     if (!m_screen)
       
   106         m_screen = new Screen(m_frame);
       
   107     return m_screen.get();
       
   108 }
       
   109 
       
   110 History* DOMWindow::history() const
       
   111 {
       
   112     if (!m_history)
       
   113         m_history = new History(m_frame);
       
   114     return m_history.get();
       
   115 }
       
   116 
       
   117 BarInfo* DOMWindow::locationbar() const
       
   118 {
       
   119     if (!m_locationbar)
       
   120         m_locationbar = new BarInfo(m_frame, BarInfo::Locationbar);
       
   121     return m_locationbar.get();
       
   122 }
       
   123 
       
   124 BarInfo* DOMWindow::menubar() const
       
   125 {
       
   126     if (!m_menubar)
       
   127         m_menubar = new BarInfo(m_frame, BarInfo::Menubar);
       
   128     return m_menubar.get();
       
   129 }
       
   130 
       
   131 BarInfo* DOMWindow::personalbar() const
       
   132 {
       
   133     if (!m_personalbar)
       
   134         m_personalbar = new BarInfo(m_frame, BarInfo::Personalbar);
       
   135     return m_personalbar.get();
       
   136 }
       
   137 
       
   138 BarInfo* DOMWindow::scrollbars() const
       
   139 {
       
   140     if (!m_scrollbars)
       
   141         m_scrollbars = new BarInfo(m_frame, BarInfo::Scrollbars);
       
   142     return m_scrollbars.get();
       
   143 }
       
   144 
       
   145 BarInfo* DOMWindow::statusbar() const
       
   146 {
       
   147     if (!m_statusbar)
       
   148         m_statusbar = new BarInfo(m_frame, BarInfo::Statusbar);
       
   149     return m_statusbar.get();
       
   150 }
       
   151 
       
   152 BarInfo* DOMWindow::toolbar() const
       
   153 {
       
   154     if (!m_toolbar)
       
   155         m_toolbar = new BarInfo(m_frame, BarInfo::Toolbar);
       
   156     return m_toolbar.get();
       
   157 }
       
   158 
       
   159 DOMSelection* DOMWindow::getSelection()
       
   160 {
       
   161     if (!m_selection)
       
   162         m_selection = new DOMSelection(m_frame);
       
   163     return m_selection.get();
       
   164 }
       
   165 
       
   166 Element* DOMWindow::frameElement() const
       
   167 {
       
   168     if (!m_frame)
       
   169         return 0;
       
   170 
       
   171     Document* doc = m_frame->document();
       
   172     ASSERT(doc);
       
   173     if (!doc)
       
   174         return 0;
       
   175 
       
   176     // FIXME: could this use m_frame->ownerElement() instead of going through the Document.
       
   177     return doc->ownerElement();
       
   178 }
       
   179 
       
   180 void DOMWindow::focus()
       
   181 {
       
   182     if (!m_frame)
       
   183         return;
       
   184 
       
   185     m_frame->focusWindow();
       
   186 }
       
   187 
       
   188 void DOMWindow::blur()
       
   189 {
       
   190     if (!m_frame)
       
   191         return;
       
   192 
       
   193     m_frame->unfocusWindow();
       
   194 }
       
   195 
       
   196 void DOMWindow::close()
       
   197 {
       
   198     if (!m_frame)
       
   199         return;
       
   200 
       
   201     if (m_frame->loader()->openedByDOM() || m_frame->loader()->getHistoryLength() <= 1)
       
   202         m_frame->scheduleClose();
       
   203 }
       
   204 
       
   205 void DOMWindow::print()
       
   206 {
       
   207     if (!m_frame)
       
   208         return;
       
   209 
       
   210     Page* page = m_frame->page();
       
   211     if (!page)
       
   212         return;
       
   213 
       
   214     page->chrome()->print(m_frame);
       
   215 }
       
   216 
       
   217 void DOMWindow::stop()
       
   218 {
       
   219     if (!m_frame)
       
   220         return;
       
   221 
       
   222     // We must check whether the load is complete asynchronously, because we might still be parsing
       
   223     // the document until the callstack unwinds.
       
   224     m_frame->loader()->stopForUserCancel(true);
       
   225 }
       
   226 
       
   227 void DOMWindow::alert(const String& message)
       
   228 {
       
   229     if (!m_frame)
       
   230         return;
       
   231 
       
   232     Document* doc = m_frame->document();
       
   233     ASSERT(doc);
       
   234     if (doc)
       
   235         doc->updateRendering();
       
   236 
       
   237     Page* page = m_frame->page();
       
   238     if (!page)
       
   239         return;
       
   240 
       
   241     page->chrome()->runJavaScriptAlert(m_frame, message);
       
   242 }
       
   243 
       
   244 bool DOMWindow::confirm(const String& message)
       
   245 {
       
   246     if (!m_frame)
       
   247         return false;
       
   248 
       
   249     Document* doc = m_frame->document();
       
   250     ASSERT(doc);
       
   251     if (doc)
       
   252         doc->updateRendering();
       
   253 
       
   254     Page* page = m_frame->page();
       
   255     if (!page)
       
   256         return false;
       
   257 
       
   258     return page->chrome()->runJavaScriptConfirm(m_frame, message);
       
   259 }
       
   260 
       
   261 String DOMWindow::prompt(const String& message, const String& defaultValue)
       
   262 {
       
   263     if (!m_frame)
       
   264         return String();
       
   265 
       
   266     Document* doc = m_frame->document();
       
   267     ASSERT(doc);
       
   268     if (doc)
       
   269         doc->updateRendering();
       
   270 
       
   271     Page* page = m_frame->page();
       
   272     if (!page)
       
   273         return String();
       
   274 
       
   275     String returnValue;
       
   276     if (page->chrome()->runJavaScriptPrompt(m_frame, message, defaultValue, returnValue))
       
   277         return returnValue;
       
   278 
       
   279     return String();
       
   280 }
       
   281 
       
   282 bool DOMWindow::find(const String& string, bool caseSensitive, bool backwards, bool wrap, bool wholeWord, bool searchInFrames, bool showDialog) const
       
   283 {
       
   284     if (!m_frame)
       
   285         return false;
       
   286 
       
   287     // FIXME (13016): Support wholeWord, searchInFrames and showDialog
       
   288     return m_frame->findString(string, !backwards, caseSensitive, wrap, false);
       
   289 }
       
   290 
       
   291 bool DOMWindow::offscreenBuffering() const
       
   292 {
       
   293     return true;
       
   294 }
       
   295 
       
   296 int DOMWindow::outerHeight() const
       
   297 {
       
   298     if (!m_frame)
       
   299         return 0;
       
   300 
       
   301     Page* page = m_frame->page();
       
   302     if (!page)
       
   303         return 0;
       
   304 
       
   305     return static_cast<int>(page->chrome()->windowRect().height());
       
   306 }
       
   307 
       
   308 int DOMWindow::outerWidth() const
       
   309 {
       
   310     if (!m_frame)
       
   311         return 0;
       
   312 
       
   313     Page* page = m_frame->page();
       
   314     if (!page)
       
   315         return 0;
       
   316 
       
   317     return static_cast<int>(page->chrome()->windowRect().width());
       
   318 }
       
   319 
       
   320 int DOMWindow::innerHeight() const
       
   321 {
       
   322     if (!m_frame)
       
   323         return 0;
       
   324 
       
   325     FrameView* view = m_frame->view();
       
   326     if (!view)
       
   327         return 0;
       
   328 
       
   329     return view->height();
       
   330 }
       
   331 
       
   332 int DOMWindow::innerWidth() const
       
   333 {
       
   334     if (!m_frame)
       
   335         return 0;
       
   336 
       
   337     FrameView* view = m_frame->view();
       
   338     if (!view)
       
   339         return 0;
       
   340 
       
   341     return view->width();
       
   342 }
       
   343 
       
   344 int DOMWindow::screenX() const
       
   345 {
       
   346     if (!m_frame)
       
   347         return 0;
       
   348 
       
   349     Page* page = m_frame->page();
       
   350     if (!page)
       
   351         return 0;
       
   352 
       
   353     return static_cast<int>(page->chrome()->windowRect().x());
       
   354 }
       
   355 
       
   356 int DOMWindow::screenY() const
       
   357 {
       
   358     if (!m_frame)
       
   359         return 0;
       
   360 
       
   361     Page* page = m_frame->page();
       
   362     if (!page)
       
   363         return 0;
       
   364 
       
   365     return static_cast<int>(page->chrome()->windowRect().y());
       
   366 }
       
   367 
       
   368 int DOMWindow::scrollX() const
       
   369 {
       
   370     if (!m_frame)
       
   371         return 0;
       
   372 
       
   373     FrameView* view = m_frame->view();
       
   374     if (!view)
       
   375         return 0;
       
   376 
       
   377     Document* doc = m_frame->document();
       
   378     ASSERT(doc);
       
   379     if (doc)
       
   380         doc->updateLayoutIgnorePendingStylesheets();
       
   381 
       
   382     return view->contentsX();
       
   383 }
       
   384 
       
   385 int DOMWindow::scrollY() const
       
   386 {
       
   387     if (!m_frame)
       
   388         return 0;
       
   389 
       
   390     FrameView* view = m_frame->view();
       
   391     if (!view)
       
   392         return 0;
       
   393 
       
   394     Document* doc = m_frame->document();
       
   395     ASSERT(doc);
       
   396     if (doc)
       
   397         doc->updateLayoutIgnorePendingStylesheets();
       
   398 
       
   399     return view->contentsY();
       
   400 }
       
   401 
       
   402 bool DOMWindow::closed() const
       
   403 {
       
   404     return !m_frame;
       
   405 }
       
   406 
       
   407 unsigned DOMWindow::length() const
       
   408 {
       
   409     if (!m_frame)
       
   410         return 0;
       
   411 
       
   412     return m_frame->tree()->childCount();
       
   413 }
       
   414 
       
   415 String DOMWindow::name() const
       
   416 {
       
   417     if (!m_frame)
       
   418         return String();
       
   419 
       
   420     return m_frame->tree()->name();
       
   421 }
       
   422 
       
   423 void DOMWindow::setName(const String& string)
       
   424 {
       
   425     if (!m_frame)
       
   426         return;
       
   427 
       
   428     m_frame->tree()->setName(string);
       
   429 }
       
   430 
       
   431 String DOMWindow::status() const
       
   432 {
       
   433     if (!m_frame)
       
   434         return String();
       
   435 
       
   436     return m_frame->jsStatusBarText();
       
   437 }
       
   438 
       
   439 void DOMWindow::setStatus(const String& string)
       
   440 {
       
   441     if (!m_frame)
       
   442         return;
       
   443 
       
   444     m_frame->setJSStatusBarText(string);
       
   445 }
       
   446 
       
   447 String DOMWindow::defaultStatus() const
       
   448 {
       
   449     if (!m_frame)
       
   450         return String();
       
   451 
       
   452     return m_frame->jsDefaultStatusBarText();
       
   453 }
       
   454 
       
   455 void DOMWindow::setDefaultStatus(const String& string)
       
   456 {
       
   457     if (!m_frame)
       
   458         return;
       
   459 
       
   460     m_frame->setJSDefaultStatusBarText(string);
       
   461 }
       
   462 
       
   463 DOMWindow* DOMWindow::self() const
       
   464 {
       
   465     if (!m_frame)
       
   466         return 0;
       
   467 
       
   468     return m_frame->domWindow();
       
   469 }
       
   470 
       
   471 DOMWindow* DOMWindow::opener() const
       
   472 {
       
   473     if (!m_frame)
       
   474         return 0;
       
   475 
       
   476     Frame* opener = m_frame->loader()->opener();
       
   477     if (!opener)
       
   478         return 0;
       
   479 
       
   480     return opener->domWindow();
       
   481 }
       
   482 
       
   483 DOMWindow* DOMWindow::parent() const
       
   484 {
       
   485     if (!m_frame)
       
   486         return 0;
       
   487 
       
   488     Frame* parent = m_frame->tree()->parent();
       
   489     if (parent)
       
   490         return parent->domWindow();
       
   491 
       
   492     return m_frame->domWindow();
       
   493 }
       
   494 
       
   495 DOMWindow* DOMWindow::top() const
       
   496 {
       
   497     if (!m_frame)
       
   498         return 0;
       
   499 
       
   500     Page* page = m_frame->page();
       
   501     if (!page)
       
   502         return 0;
       
   503 
       
   504     return page->mainFrame()->domWindow();
       
   505 }
       
   506 
       
   507 Document* DOMWindow::document() const
       
   508 {
       
   509     if (!m_frame)
       
   510         return 0;
       
   511 
       
   512     ASSERT(m_frame->document());
       
   513     return m_frame->document();
       
   514 }
       
   515 
       
   516 PassRefPtr<CSSStyleDeclaration> DOMWindow::getComputedStyle(Element* elt, const String&) const
       
   517 {
       
   518     if (!elt)
       
   519         return 0;
       
   520 
       
   521     // FIXME: This needs to work with pseudo elements.
       
   522     return new CSSComputedStyleDeclaration(elt);
       
   523 }
       
   524 
       
   525 PassRefPtr<CSSRuleList> DOMWindow::getMatchedCSSRules(Element* elt, const String& pseudoElt, bool authorOnly) const
       
   526 {
       
   527     if (!m_frame)
       
   528         return 0;
       
   529 
       
   530     Document* doc = m_frame->document();
       
   531     ASSERT(doc);
       
   532     if (!doc)
       
   533         return 0;
       
   534 
       
   535     if (!pseudoElt.isEmpty())
       
   536         return doc->styleSelector()->pseudoStyleRulesForElement(elt, pseudoElt.impl(), authorOnly);
       
   537     return doc->styleSelector()->styleRulesForElement(elt, authorOnly);
       
   538 }
       
   539 
       
   540 double DOMWindow::devicePixelRatio() const
       
   541 {
       
   542     if (!m_frame)
       
   543         return 0.0;
       
   544 
       
   545     Page* page = m_frame->page();
       
   546     if (!page)
       
   547         return 0.0;
       
   548 
       
   549     return page->chrome()->scaleFactor();
       
   550 }
       
   551 
       
   552 } // namespace WebCore