webengine/osswebengine/WebCore/page/Chrome.cpp
changeset 0 dd21522fd290
child 25 0ed94ceaa377
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 // -*- mode: c++; c-basic-offset: 4 -*-
       
     2 /*
       
     3  * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
       
     4  * Copyright (C) 2007 Trolltech ASA
       
     5  *
       
     6  * This library is free software; you can redistribute it and/or
       
     7  * modify it under the terms of the GNU Library General Public
       
     8  * License as published by the Free Software Foundation; either
       
     9  * version 2 of the License, or (at your option) any later version.
       
    10  *
       
    11  * This library is distributed in the hope that it will be useful,
       
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14  * Library General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU Library General Public License
       
    17  * along with this library; see the file COPYING.LIB.  If not, write to
       
    18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    19  * Boston, MA 02110-1301, USA.
       
    20  */
       
    21 
       
    22 #include "config.h"
       
    23 #include "Chrome.h"
       
    24 
       
    25 #include "ChromeClient.h"
       
    26 #include "FloatRect.h"
       
    27 #include "Frame.h"
       
    28 #include "FrameTree.h"
       
    29 #include "HTMLFormElement.h"
       
    30 #include "HTMLInputElement.h"
       
    31 #include "HTMLNames.h"
       
    32 #include "HitTestResult.h"
       
    33 #include "InspectorController.h"
       
    34 #include "Page.h"
       
    35 #include "ResourceHandle.h"
       
    36 #include "Settings.h"
       
    37 #include "kjs_window.h"
       
    38 #include <wtf/PassRefPtr.h>
       
    39 #include <wtf/RefPtr.h>
       
    40 #include <wtf/Vector.h>
       
    41 
       
    42 namespace WebCore {
       
    43 
       
    44 using namespace HTMLNames;
       
    45 using namespace KJS;
       
    46 using namespace std;
       
    47 
       
    48 class PageGroupLoadDeferrer : Noncopyable {
       
    49 public:
       
    50     PageGroupLoadDeferrer(Page*, bool deferSelf);
       
    51     ~PageGroupLoadDeferrer();
       
    52 private:
       
    53     Vector<RefPtr<Frame>, 16> m_deferredFrames;
       
    54 #if !PLATFORM(MAC)
       
    55     Vector<pair<RefPtr<Frame>, PausedTimeouts*>, 16> m_pausedTimeouts;
       
    56 #endif
       
    57 };
       
    58 
       
    59 Chrome::Chrome(Page* page, ChromeClient* client)
       
    60     : m_page(page)
       
    61     , m_client(client)
       
    62 {
       
    63     ASSERT(m_client);
       
    64 }
       
    65 
       
    66 Chrome::~Chrome()
       
    67 {
       
    68     m_client->chromeDestroyed();
       
    69 }
       
    70 
       
    71 void Chrome::setWindowRect(const FloatRect& rect) const
       
    72 {
       
    73     m_client->setWindowRect(rect);
       
    74 }
       
    75 
       
    76 FloatRect Chrome::windowRect() const
       
    77 {
       
    78     return m_client->windowRect();
       
    79 }
       
    80 
       
    81 FloatRect Chrome::pageRect() const
       
    82 {
       
    83     return m_client->pageRect();
       
    84 }
       
    85         
       
    86 float Chrome::scaleFactor()
       
    87 {
       
    88     return m_client->scaleFactor();
       
    89 }
       
    90     
       
    91 void Chrome::focus() const
       
    92 {
       
    93     m_client->focus();
       
    94 }
       
    95 
       
    96 void Chrome::unfocus() const
       
    97 {
       
    98     m_client->unfocus();
       
    99 }
       
   100 
       
   101 bool Chrome::canTakeFocus(FocusDirection direction) const
       
   102 {
       
   103     return m_client->canTakeFocus(direction);
       
   104 }
       
   105 
       
   106 void Chrome::takeFocus(FocusDirection direction) const
       
   107 {
       
   108     m_client->takeFocus(direction);
       
   109 }
       
   110 
       
   111 Page* Chrome::createWindow(Frame* frame, const FrameLoadRequest& request) const
       
   112 {
       
   113     return m_client->createWindow(frame, request);
       
   114 }
       
   115 
       
   116 Page* Chrome::createModalDialog(Frame* frame, const FrameLoadRequest& request) const
       
   117 {
       
   118     return m_client->createModalDialog(frame, request);
       
   119 }
       
   120 
       
   121 void Chrome::show() const
       
   122 {
       
   123     m_client->show();
       
   124 }
       
   125 
       
   126 bool Chrome::canRunModal() const
       
   127 {
       
   128     return m_client->canRunModal();
       
   129 }
       
   130 
       
   131 bool Chrome::canRunModalNow() const
       
   132 {
       
   133     // If loads are blocked, we can't run modal because the contents
       
   134     // of the modal dialog will never show up!
       
   135     return canRunModal() && !ResourceHandle::loadsBlocked();
       
   136 }
       
   137 
       
   138 void Chrome::runModal() const
       
   139 {
       
   140     if (m_page->defersLoading()) {
       
   141         LOG_ERROR("Tried to run modal in a page when it was deferring loading -- should never happen.");
       
   142         return;
       
   143     }
       
   144 
       
   145     // Defer callbacks in all the other pages in this group, so we don't try to run JavaScript
       
   146     // in a way that could interact with this view.
       
   147     PageGroupLoadDeferrer deferrer(m_page, false);
       
   148 
       
   149     TimerBase::fireTimersInNestedEventLoop();
       
   150     m_client->runModal();
       
   151 }
       
   152 
       
   153 void Chrome::setToolbarsVisible(bool b) const
       
   154 {
       
   155     m_client->setToolbarsVisible(b);
       
   156 }
       
   157 
       
   158 bool Chrome::toolbarsVisible() const
       
   159 {
       
   160     return m_client->toolbarsVisible();
       
   161 }
       
   162 
       
   163 void Chrome::setStatusbarVisible(bool b) const
       
   164 {
       
   165     m_client->setStatusbarVisible(b);
       
   166 }
       
   167 
       
   168 bool Chrome::statusbarVisible() const
       
   169 {
       
   170     return m_client->statusbarVisible();
       
   171 }
       
   172 
       
   173 void Chrome::setScrollbarsVisible(bool b) const
       
   174 {
       
   175     m_client->setScrollbarsVisible(b);
       
   176 }
       
   177 
       
   178 bool Chrome::scrollbarsVisible() const
       
   179 {
       
   180     return m_client->scrollbarsVisible();
       
   181 }
       
   182 
       
   183 void Chrome::setMenubarVisible(bool b) const
       
   184 {
       
   185     m_client->setMenubarVisible(b);
       
   186 }
       
   187 
       
   188 bool Chrome::menubarVisible() const
       
   189 {
       
   190     return m_client->menubarVisible();
       
   191 }
       
   192 
       
   193 void Chrome::setResizable(bool b) const
       
   194 {
       
   195     m_client->setResizable(b);
       
   196 }
       
   197 
       
   198 void Chrome::addMessageToConsole(MessageSource source, MessageLevel level, const String& message, unsigned lineNumber, const String& sourceID)
       
   199 {
       
   200     if (source == JSMessageSource )
       
   201         m_client->addMessageToConsole(message, level, lineNumber, sourceID);
       
   202 
       
   203     if (InspectorController* inspector = m_page->inspectorController())
       
   204         inspector->addMessageToConsole(source, level, message, lineNumber, sourceID);
       
   205 }
       
   206 
       
   207 bool Chrome::canRunBeforeUnloadConfirmPanel()
       
   208 {
       
   209     return m_client->canRunBeforeUnloadConfirmPanel();
       
   210 }
       
   211 
       
   212 bool Chrome::runBeforeUnloadConfirmPanel(const String& message, Frame* frame)
       
   213 {
       
   214     // Defer loads in case the client method runs a new event loop that would 
       
   215     // otherwise cause the load to continue while we're in the middle of executing JavaScript.
       
   216     PageGroupLoadDeferrer deferrer(m_page, true);
       
   217 
       
   218     return m_client->runBeforeUnloadConfirmPanel(message, frame);
       
   219 }
       
   220 
       
   221 void Chrome::closeWindowSoon()
       
   222 {
       
   223     m_client->closeWindowSoon();
       
   224 }
       
   225 
       
   226 void Chrome::runJavaScriptAlert(Frame* frame, const String& message)
       
   227 {
       
   228     // Defer loads in case the client method runs a new event loop that would 
       
   229     // otherwise cause the load to continue while we're in the middle of executing JavaScript.
       
   230     PageGroupLoadDeferrer deferrer(m_page, true);
       
   231 
       
   232     ASSERT(frame);
       
   233     String text = message;
       
   234     text.replace('\\', frame->backslashAsCurrencySymbol());
       
   235 
       
   236     m_client->runJavaScriptAlert(frame, text);
       
   237 }
       
   238 
       
   239 bool Chrome::runJavaScriptConfirm(Frame* frame, const String& message)
       
   240 {
       
   241     // Defer loads in case the client method runs a new event loop that would 
       
   242     // otherwise cause the load to continue while we're in the middle of executing JavaScript.
       
   243     PageGroupLoadDeferrer deferrer(m_page, true);
       
   244 
       
   245     ASSERT(frame);
       
   246     String text = message;
       
   247     text.replace('\\', frame->backslashAsCurrencySymbol());
       
   248     
       
   249     return m_client->runJavaScriptConfirm(frame, text);
       
   250 }
       
   251 
       
   252 bool Chrome::runJavaScriptPrompt(Frame* frame, const String& prompt, const String& defaultValue, String& result)
       
   253 {
       
   254     // Defer loads in case the client method runs a new event loop that would 
       
   255     // otherwise cause the load to continue while we're in the middle of executing JavaScript.
       
   256     PageGroupLoadDeferrer deferrer(m_page, true);
       
   257 
       
   258     ASSERT(frame);
       
   259     String promptText = prompt;
       
   260     promptText.replace('\\', frame->backslashAsCurrencySymbol());
       
   261     String defaultValueText = defaultValue;
       
   262     defaultValueText.replace('\\', frame->backslashAsCurrencySymbol());
       
   263     
       
   264     bool ok = m_client->runJavaScriptPrompt(frame, promptText, defaultValueText, result);
       
   265     
       
   266     if (ok)
       
   267         result.replace(frame->backslashAsCurrencySymbol(), '\\');
       
   268     
       
   269     return ok;
       
   270 }
       
   271 
       
   272 void Chrome::setStatusbarText(Frame* frame, const String& status)
       
   273 {
       
   274     ASSERT(frame);
       
   275     String text = status;
       
   276     text.replace('\\', frame->backslashAsCurrencySymbol());
       
   277     
       
   278     m_client->setStatusbarText(text);
       
   279 }
       
   280 
       
   281 bool Chrome::shouldInterruptJavaScript()
       
   282 {
       
   283     // Defer loads in case the client method runs a new event loop that would 
       
   284     // otherwise cause the load to continue while we're in the middle of executing JavaScript.
       
   285     PageGroupLoadDeferrer deferrer(m_page, true);
       
   286 
       
   287     return m_client->shouldInterruptJavaScript();
       
   288 }
       
   289 
       
   290 IntRect Chrome::windowResizerRect() const
       
   291 {
       
   292     return m_client->windowResizerRect();
       
   293 }
       
   294 
       
   295 void Chrome::addToDirtyRegion(const IntRect& rect)
       
   296 {
       
   297     m_client->addToDirtyRegion(rect);
       
   298 }
       
   299 
       
   300 void Chrome::scrollBackingStore(int dx, int dy, const IntRect& scrollViewRect, const IntRect& clipRect)
       
   301 {
       
   302     m_client->scrollBackingStore(dx, dy, scrollViewRect, clipRect);
       
   303 }
       
   304 
       
   305 void Chrome::updateBackingStore()
       
   306 {
       
   307     m_client->updateBackingStore();
       
   308 }
       
   309 
       
   310 void Chrome::mouseDidMoveOverElement(const HitTestResult& result, unsigned modifierFlags)
       
   311 {
       
   312     m_client->mouseDidMoveOverElement(result, modifierFlags);
       
   313 }
       
   314 
       
   315 void Chrome::setToolTip(const HitTestResult& result)
       
   316 {
       
   317     // First priority is a potential toolTip representing a spelling or grammar error
       
   318     String toolTip = result.spellingToolTip();
       
   319 
       
   320     // Next priority is a toolTip from a URL beneath the mouse (if preference is set to show those).
       
   321     if (toolTip.isEmpty() && m_page->settings()->showsURLsInToolTips()) {
       
   322         if (Node* node = result.innerNonSharedNode()) {
       
   323             // Get tooltip representing form action, if relevant
       
   324             if (node->hasTagName(inputTag)) {
       
   325                 HTMLInputElement* input = static_cast<HTMLInputElement*>(node);
       
   326                 if (input->inputType() == HTMLInputElement::SUBMIT)
       
   327                     if (HTMLFormElement* form = input->form())
       
   328                         toolTip = form->action();
       
   329             }
       
   330         }
       
   331 
       
   332         // Get tooltip representing link's URL
       
   333         if (toolTip.isEmpty())
       
   334             // FIXME: Need to pass this URL through userVisibleString once that's in WebCore
       
   335             toolTip = result.absoluteLinkURL().url();
       
   336     }
       
   337 
       
   338     // Lastly we'll consider a tooltip for element with "title" attribute
       
   339     if (toolTip.isEmpty())
       
   340         toolTip = result.title();
       
   341 
       
   342     m_client->setToolTip(toolTip);
       
   343 }
       
   344 
       
   345 void Chrome::print(Frame* frame)
       
   346 {
       
   347     m_client->print(frame);
       
   348 }
       
   349 
       
   350 #if PLATFORM(SYMBIAN)
       
   351 void Chrome::setElementVisibilityChanged(bool visibility)
       
   352 {
       
   353     m_client->setElementVisibilityChanged(visibility);
       
   354 }
       
   355 #endif    
       
   356 PageGroupLoadDeferrer::PageGroupLoadDeferrer(Page* page, bool deferSelf)
       
   357 {
       
   358     const HashSet<Page*>* group = page->frameNamespace();
       
   359 
       
   360     if (!group)
       
   361         return;
       
   362 
       
   363     HashSet<Page*>::const_iterator end = group->end();
       
   364     for (HashSet<Page*>::const_iterator it = group->begin(); it != end; ++it) {
       
   365         Page* otherPage = *it;
       
   366         if ((deferSelf || otherPage != page)) {
       
   367             if (!otherPage->defersLoading())
       
   368                 m_deferredFrames.append(otherPage->mainFrame());
       
   369 
       
   370 #if !PLATFORM(MAC)
       
   371             for (Frame* frame = otherPage->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
       
   372                 if (Window* window = Window::retrieveWindow(frame)) {
       
   373                     PausedTimeouts* timeouts = window->pauseTimeouts();
       
   374 
       
   375                     m_pausedTimeouts.append(make_pair(frame, timeouts));
       
   376                 }
       
   377             }
       
   378 #endif
       
   379         }
       
   380     }
       
   381 
       
   382     size_t count = m_deferredFrames.size();
       
   383     for (size_t i = 0; i < count; ++i)
       
   384         if (Page* page = m_deferredFrames[i]->page())
       
   385             page->setDefersLoading(true);
       
   386 }
       
   387 
       
   388 PageGroupLoadDeferrer::~PageGroupLoadDeferrer()
       
   389 {
       
   390     size_t count = m_deferredFrames.size();
       
   391     for (size_t i = 0; i < count; ++i)
       
   392         if (Page* page = m_deferredFrames[i]->page())
       
   393             page->setDefersLoading(false);
       
   394 
       
   395 #if !PLATFORM(MAC)
       
   396     count = m_pausedTimeouts.size();
       
   397 
       
   398     for (size_t i = 0; i < count; i++) {
       
   399         Window* window = Window::retrieveWindow(m_pausedTimeouts[i].first.get());
       
   400         if (window)
       
   401             window->resumeTimeouts(m_pausedTimeouts[i].second);
       
   402         delete m_pausedTimeouts[i].second;
       
   403     }
       
   404 #endif
       
   405 }
       
   406 
       
   407 
       
   408 } // namespace WebCore