Fix TextDocumentParser to play nice with threading
[WebKit-https.git] / Source / WebKit / blackberry / Api / WebPage.cpp
1 /*
2  * Copyright (C) 2009, 2010, 2011, 2012 Research In Motion Limited. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include "config.h"
20 #include "WebPage.h"
21
22 #include "ApplicationCacheStorage.h"
23 #include "AuthenticationChallengeManager.h"
24 #include "AutofillManager.h"
25 #include "BackForwardController.h"
26 #include "BackForwardListImpl.h"
27 #include "BackingStoreClient.h"
28 #include "BackingStore_p.h"
29 #if ENABLE(BATTERY_STATUS)
30 #include "BatteryClientBlackBerry.h"
31 #endif
32 #include "CachedImage.h"
33 #include "Chrome.h"
34 #include "ChromeClientBlackBerry.h"
35 #include "ContextMenuClientBlackBerry.h"
36 #include "CookieManager.h"
37 #include "CredentialManager.h"
38 #include "CredentialStorage.h"
39 #include "CredentialTransformData.h"
40 #include "DOMSupport.h"
41 #include "Database.h"
42 #include "DatabaseManager.h"
43 #include "DatabaseSync.h"
44 #include "DefaultTapHighlight.h"
45 #include "DeviceMotionClientBlackBerry.h"
46 #include "DeviceOrientationClientBlackBerry.h"
47 #if !defined(PUBLIC_BUILD) || !PUBLIC_BUILD
48 #include "DeviceOrientationClientMock.h"
49 #endif
50 #include "DragClientBlackBerry.h"
51 // FIXME: We should be using DumpRenderTreeClient, but I'm not sure where we should
52 // create the DRT_BB object. See PR #120355.
53 #if !defined(PUBLIC_BUILD) || !PUBLIC_BUILD
54 #include "DumpRenderTreeBlackBerry.h"
55 #endif
56 #include "EditorClientBlackBerry.h"
57 #include "FocusController.h"
58 #include "Frame.h"
59 #include "FrameLoadRequest.h"
60 #include "FrameLoaderClientBlackBerry.h"
61 #if !defined(PUBLIC_BUILD) || !PUBLIC_BUILD
62 #include "GeolocationClientMock.h"
63 #endif
64 #include "GeolocationClientBlackBerry.h"
65 #include "GroupSettings.h"
66 #include "HTMLAreaElement.h"
67 #include "HTMLFrameOwnerElement.h"
68 #include "HTMLImageElement.h"
69 #include "HTMLInputElement.h"
70 #include "HTMLMediaElement.h"
71 #include "HTMLNames.h"
72 #include "HTMLParserIdioms.h"
73 #include "HTTPParsers.h"
74 #include "HistoryItem.h"
75 #include "IconDatabaseClientBlackBerry.h"
76 #include "ImageDocument.h"
77 #include "InPageSearchManager.h"
78 #include "InRegionScrollableArea.h"
79 #include "InRegionScroller_p.h"
80 #include "InputHandler.h"
81 #include "InspectorBackendDispatcher.h"
82 #include "InspectorClientBlackBerry.h"
83 #include "InspectorController.h"
84 #include "InspectorInstrumentation.h"
85 #include "InspectorOverlay.h"
86 #include "JavaScriptVariant_p.h"
87 #include "LayerWebKitThread.h"
88 #if ENABLE(NETWORK_INFO)
89 #include "NetworkInfoClientBlackBerry.h"
90 #endif
91 #include "NetworkManager.h"
92 #include "NodeRenderStyle.h"
93 #include "NodeTraversal.h"
94 #if ENABLE(NAVIGATOR_CONTENT_UTILS)
95 #include "NavigatorContentUtilsClientBlackBerry.h"
96 #endif
97 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
98 #include "NotificationClientBlackBerry.h"
99 #endif
100 #include "Page.h"
101 #include "PageCache.h"
102 #include "PageGroup.h"
103 #include "PagePopupBlackBerry.h"
104 #include "PlatformTouchEvent.h"
105 #include "PlatformWheelEvent.h"
106 #include "PluginDatabase.h"
107 #include "PluginView.h"
108 #include "RenderLayerBacking.h"
109 #include "RenderLayerCompositor.h"
110 #if ENABLE(FULLSCREEN_API)
111 #include "RenderFullScreen.h"
112 #endif
113 #include "RenderText.h"
114 #include "RenderThemeBlackBerry.h"
115 #include "RenderTreeAsText.h"
116 #include "RenderView.h"
117 #include "RenderWidget.h"
118 #include "ScriptSourceCode.h"
119 #include "ScriptValue.h"
120 #include "ScrollTypes.h"
121 #include "SecurityPolicy.h"
122 #include "SelectionHandler.h"
123 #include "SelectionOverlay.h"
124 #include "Settings.h"
125 #include "Storage.h"
126 #include "StorageNamespace.h"
127 #include "SurfacePool.h"
128 #include "Text.h"
129 #include "ThreadCheck.h"
130 #include "TouchEventHandler.h"
131 #include "TransformationMatrix.h"
132 #if ENABLE(MEDIA_STREAM)
133 #include "UserMediaClientImpl.h"
134 #endif
135 #if ENABLE(VIBRATION)
136 #include "VibrationClientBlackBerry.h"
137 #endif
138 #include "VisiblePosition.h"
139 #include "WebCookieJar.h"
140 #if ENABLE(WEBDOM)
141 #include "WebDOMDocument.h"
142 #endif
143 #include "WebKitThreadViewportAccessor.h"
144 #include "WebKitVersion.h"
145 #include "WebOverlay.h"
146 #include "WebOverlay_p.h"
147 #include "WebPageClient.h"
148 #include "WebSocket.h"
149 #include "WebViewportArguments.h"
150 #include "npapi.h"
151 #include "runtime_root.h"
152
153 #if ENABLE(VIDEO)
154 #include "MediaPlayer.h"
155 #include "MediaPlayerPrivateBlackBerry.h"
156 #endif
157
158 #if USE(SKIA)
159 #include "PlatformContextSkia.h"
160 #endif
161
162 #if USE(ACCELERATED_COMPOSITING)
163 #include "FrameLayers.h"
164 #include "WebPageCompositorClient.h"
165 #include "WebPageCompositor_p.h"
166 #endif
167
168 #include <BlackBerryPlatformDeviceInfo.h>
169 #include <BlackBerryPlatformExecutableMessage.h>
170 #include <BlackBerryPlatformKeyboardEvent.h>
171 #include <BlackBerryPlatformMessageClient.h>
172 #include <BlackBerryPlatformMouseEvent.h>
173 #include <BlackBerryPlatformScreen.h>
174 #include <BlackBerryPlatformSettings.h>
175 #include <JavaScriptCore/APICast.h>
176 #include <JavaScriptCore/JSContextRef.h>
177 #include <JavaScriptCore/JSStringRef.h>
178 #include <SharedPointer.h>
179 #include <sys/keycodes.h>
180 #include <unicode/ustring.h> // platform ICU
181
182 #include <wtf/text/CString.h>
183
184 #ifndef USER_PROCESSES
185 #include <memalloc.h>
186 #endif
187
188 #if ENABLE(ACCELERATED_2D_CANVAS)
189 #include "GrContext.h"
190 #include "SharedGraphicsContext3D.h"
191 #endif
192
193 #if ENABLE(REQUEST_ANIMATION_FRAME)
194 #include "PlatformScreen.h"
195 #endif
196
197 #define DEBUG_TOUCH_EVENTS 0
198 #define DEBUG_WEBPAGE_LOAD 0
199 #define DEBUG_AC_COMMIT 0
200
201 using namespace std;
202 using namespace WebCore;
203
204 typedef const unsigned short* CUShortPtr;
205
206 namespace BlackBerry {
207 namespace WebKit {
208
209 static Vector<WebPage*>* visibleWebPages()
210 {
211     static Vector<WebPage*>* s_visibleWebPages = 0; // Initially, no web page is visible.
212     if (!s_visibleWebPages)
213         s_visibleWebPages = new Vector<WebPage*>;
214     return s_visibleWebPages;
215 }
216
217 const unsigned blockZoomMargin = 3; // Add 3 pixel margin on each side.
218 static int blockClickRadius = 0;
219 static double maximumBlockZoomScale = 3; // This scale can be clamped by the maximumScale set for the page.
220
221 const double manualScrollInterval = 0.1; // The time interval during which we associate user action with scrolling.
222
223 const IntSize minimumLayoutSize(10, 10); // Needs to be a small size, greater than 0, that we can grow the layout from.
224
225 const double minimumExpandingRatio = 0.15;
226
227 const double minimumZoomToFitScale = 0.25;
228 const double maximumImageDocumentZoomToFitScale = 2;
229
230 // Helper function to parse a URL and fill in missing parts.
231 static KURL parseUrl(const String& url)
232 {
233     String urlString(url);
234     KURL kurl = KURL(KURL(), urlString);
235     if (kurl.protocol().isEmpty()) {
236         urlString.insert("http://", 0);
237         kurl = KURL(KURL(), urlString);
238     }
239
240     return kurl;
241 }
242
243 // Helper functions to convert to and from WebCore types.
244 static inline WebCore::PlatformEvent::Type toWebCoreMouseEventType(const BlackBerry::Platform::MouseEvent::Type type)
245 {
246     switch (type) {
247     case BlackBerry::Platform::MouseEvent::MouseButtonDown:
248         return WebCore::PlatformEvent::MousePressed;
249     case Platform::MouseEvent::MouseButtonUp:
250         return WebCore::PlatformEvent::MouseReleased;
251     case Platform::MouseEvent::MouseMove:
252     default:
253         return WebCore::PlatformEvent::MouseMoved;
254     }
255 }
256
257 static inline ResourceRequestCachePolicy toWebCoreCachePolicy(Platform::NetworkRequest::CachePolicy policy)
258 {
259     switch (policy) {
260     case Platform::NetworkRequest::UseProtocolCachePolicy:
261         return UseProtocolCachePolicy;
262     case Platform::NetworkRequest::ReloadIgnoringCacheData:
263         return ReloadIgnoringCacheData;
264     case Platform::NetworkRequest::ReturnCacheDataElseLoad:
265         return ReturnCacheDataElseLoad;
266     case Platform::NetworkRequest::ReturnCacheDataDontLoad:
267         return ReturnCacheDataDontLoad;
268     default:
269         ASSERT_NOT_REACHED();
270         return UseProtocolCachePolicy;
271     }
272 }
273
274 #if ENABLE(EVENT_MODE_METATAGS)
275 static inline Platform::CursorEventMode toPlatformCursorEventMode(CursorEventMode mode)
276 {
277     switch (mode) {
278     case ProcessedCursorEvents:
279         return Platform::ProcessedCursorEvents;
280     case NativeCursorEvents:
281         return Platform::NativeCursorEvents;
282     default:
283         ASSERT_NOT_REACHED();
284         return Platform::ProcessedCursorEvents;
285     }
286 }
287
288 static inline Platform::TouchEventMode toPlatformTouchEventMode(TouchEventMode mode)
289 {
290     switch (mode) {
291     case ProcessedTouchEvents:
292         return Platform::ProcessedTouchEvents;
293     case NativeTouchEvents:
294         return Platform::NativeTouchEvents;
295     case PureTouchEventsWithMouseConversion:
296         return Platform::PureTouchEventsWithMouseConversion;
297     default:
298         ASSERT_NOT_REACHED();
299         return Platform::ProcessedTouchEvents;
300     }
301 }
302 #endif
303
304 static inline HistoryItem* historyItemFromBackForwardId(WebPage::BackForwardId id)
305 {
306     return reinterpret_cast<HistoryItem*>(id);
307 }
308
309 static inline WebPage::BackForwardId backForwardIdFromHistoryItem(HistoryItem* item)
310 {
311     return reinterpret_cast<WebPage::BackForwardId>(item);
312 }
313
314 void WebPage::setUserViewportArguments(const WebViewportArguments& viewportArguments)
315 {
316     d->m_userViewportArguments = *(viewportArguments.d);
317 }
318
319 void WebPage::resetUserViewportArguments()
320 {
321     d->m_userViewportArguments = ViewportArguments();
322 }
323
324 template <bool WebPagePrivate::* isActive>
325 class DeferredTask: public WebPagePrivate::DeferredTaskBase {
326 public:
327     static void finishOrCancel(WebPagePrivate* webPagePrivate)
328     {
329         webPagePrivate->*isActive = false;
330     }
331 protected:
332     DeferredTask(WebPagePrivate* webPagePrivate)
333         : DeferredTaskBase(webPagePrivate, isActive)
334     {
335     }
336     typedef DeferredTask<isActive> DeferredTaskType;
337 };
338
339 void WebPage::autofillTextField(const BlackBerry::Platform::String& item)
340 {
341     if (!d->m_webSettings->isFormAutofillEnabled())
342         return;
343
344     d->m_autofillManager->autofillTextField(item);
345 }
346
347 void WebPage::enableQnxJavaScriptObject(bool enabled)
348 {
349     d->m_enableQnxJavaScriptObject = enabled;
350 }
351
352 BlackBerry::Platform::String WebPage::renderTreeAsText()
353 {
354     return externalRepresentation(d->m_mainFrame);
355 }
356
357 WebPagePrivate::WebPagePrivate(WebPage* webPage, WebPageClient* client, const IntRect& rect)
358     : m_webPage(webPage)
359     , m_client(client)
360     , m_inspectorClient(0)
361     , m_page(0) // Initialized by init.
362     , m_mainFrame(0) // Initialized by init.
363     , m_currentContextNode(0)
364     , m_webSettings(0) // Initialized by init.
365     , m_cookieJar(0)
366     , m_visible(false)
367     , m_activationState(ActivationActive)
368     , m_shouldResetTilesWhenShown(false)
369     , m_shouldZoomToInitialScaleAfterLoadFinished(false)
370     , m_userScalable(true)
371     , m_userPerformedManualZoom(false)
372     , m_userPerformedManualScroll(false)
373     , m_contentsSizeChanged(false)
374     , m_overflowExceedsContentsSize(false)
375     , m_resetVirtualViewportOnCommitted(true)
376     , m_shouldUseFixedDesktopMode(false)
377     , m_preventIdleDimmingCount(0)
378 #if ENABLE(TOUCH_EVENTS)
379     , m_preventDefaultOnTouchStart(false)
380 #endif
381     , m_nestedLayoutFinishedCount(0)
382     , m_actualVisibleWidth(rect.width())
383     , m_actualVisibleHeight(rect.height())
384     , m_defaultLayoutSize(minimumLayoutSize)
385     , m_didRestoreFromPageCache(false)
386     , m_viewMode(WebPagePrivate::Desktop) // Default to Desktop mode for PB.
387     , m_loadState(WebPagePrivate::None)
388     , m_transformationMatrix(new TransformationMatrix())
389     , m_backingStore(0) // Initialized by init.
390     , m_backingStoreClient(0) // Initialized by init.
391     , m_webkitThreadViewportAccessor(0) // Initialized by init.
392     , m_inPageSearchManager(new InPageSearchManager(this))
393     , m_inputHandler(new InputHandler(this))
394     , m_selectionHandler(new SelectionHandler(this))
395     , m_touchEventHandler(new TouchEventHandler(this))
396 #if ENABLE(EVENT_MODE_METATAGS)
397     , m_cursorEventMode(ProcessedCursorEvents)
398     , m_touchEventMode(ProcessedTouchEvents)
399 #endif
400 #if ENABLE(FULLSCREEN_API) && ENABLE(VIDEO)
401     , m_scaleBeforeFullScreen(-1.0)
402 #endif
403     , m_currentCursor(Platform::CursorNone)
404     , m_dumpRenderTree(0) // Lazy initialization.
405     , m_initialScale(-1.0)
406     , m_minimumScale(-1.0)
407     , m_maximumScale(-1.0)
408     , m_forceRespectViewportArguments(false)
409     , m_blockZoomFinalScale(1.0)
410     , m_anchorInNodeRectRatio(-1, -1)
411     , m_currentBlockZoomNode(0)
412     , m_currentBlockZoomAdjustedNode(0)
413     , m_shouldReflowBlock(false)
414     , m_shouldConstrainScrollingToContentEdge(true)
415     , m_lastUserEventTimestamp(0.0)
416     , m_pluginMouseButtonPressed(false)
417     , m_pluginMayOpenNewTab(false)
418 #if USE(ACCELERATED_COMPOSITING)
419     , m_rootLayerCommitTimer(adoptPtr(new Timer<WebPagePrivate>(this, &WebPagePrivate::rootLayerCommitTimerFired)))
420     , m_needsOneShotDrawingSynchronization(false)
421     , m_needsCommit(false)
422     , m_suspendRootLayerCommit(false)
423 #endif
424     , m_pendingOrientation(-1)
425     , m_fullscreenNode(0)
426     , m_hasInRegionScrollableAreas(false)
427     , m_updateDelegatedOverlaysDispatched(false)
428     , m_enableQnxJavaScriptObject(false)
429     , m_deferredTasksTimer(this, &WebPagePrivate::deferredTasksTimerFired)
430     , m_selectPopup(0)
431     , m_autofillManager(AutofillManager::create(this))
432     , m_documentStyleRecalcPostponed(false)
433     , m_documentChildNeedsStyleRecalc(false)
434 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
435     , m_notificationManager(this)
436 #endif
437 {
438     static bool isInitialized = false;
439     if (!isInitialized) {
440         isInitialized = true;
441         BlackBerry::Platform::DeviceInfo::instance();
442         defaultUserAgent();
443     }
444
445     AuthenticationChallengeManager::instance()->pageCreated(this);
446     clearCachedHitTestResult();
447 }
448
449 WebPage::WebPage(WebPageClient* client, const BlackBerry::Platform::String& pageGroupName, const Platform::IntRect& rect)
450 {
451     globalInitialize();
452     d = new WebPagePrivate(this, client, rect);
453     d->init(pageGroupName);
454 }
455
456 WebPagePrivate::~WebPagePrivate()
457 {
458     // Hand the backingstore back to another owner if necessary.
459     m_webPage->setVisible(false);
460     if (BackingStorePrivate::currentBackingStoreOwner() == m_webPage)
461         BackingStorePrivate::setCurrentBackingStoreOwner(0);
462
463     delete m_webSettings;
464     m_webSettings = 0;
465
466     delete m_cookieJar;
467     m_cookieJar = 0;
468
469     delete m_webkitThreadViewportAccessor;
470     m_webkitThreadViewportAccessor = 0;
471
472     delete m_backingStoreClient;
473     m_backingStoreClient = 0;
474     m_backingStore = 0;
475
476     delete m_page;
477     m_page = 0;
478
479     delete m_transformationMatrix;
480     m_transformationMatrix = 0;
481
482     delete m_inPageSearchManager;
483     m_inPageSearchManager = 0;
484
485     delete m_selectionHandler;
486     m_selectionHandler = 0;
487
488     delete m_inputHandler;
489     m_inputHandler = 0;
490
491     delete m_touchEventHandler;
492     m_touchEventHandler = 0;
493
494 #if !defined(PUBLIC_BUILD) || !PUBLIC_BUILD
495     delete m_dumpRenderTree;
496     m_dumpRenderTree = 0;
497 #endif
498
499     AuthenticationChallengeManager::instance()->pageDeleted(this);
500 }
501
502 WebPage::~WebPage()
503 {
504     deleteGuardedObject(d);
505     d = 0;
506 }
507
508 Page* WebPagePrivate::core(const WebPage* webPage)
509 {
510     return webPage->d->m_page;
511 }
512
513 void WebPagePrivate::init(const BlackBerry::Platform::String& pageGroupName)
514 {
515     ChromeClientBlackBerry* chromeClient = new ChromeClientBlackBerry(this);
516     ContextMenuClientBlackBerry* contextMenuClient = 0;
517 #if ENABLE(CONTEXT_MENUS)
518     contextMenuClient = new ContextMenuClientBlackBerry();
519 #endif
520     EditorClientBlackBerry* editorClient = new EditorClientBlackBerry(this);
521     DragClientBlackBerry* dragClient = 0;
522 #if ENABLE(DRAG_SUPPORT)
523     dragClient = new DragClientBlackBerry();
524 #endif
525 #if ENABLE(INSPECTOR)
526     m_inspectorClient = new InspectorClientBlackBerry(this);
527 #endif
528
529     FrameLoaderClientBlackBerry* frameLoaderClient = new FrameLoaderClientBlackBerry();
530
531     Page::PageClients pageClients;
532     pageClients.chromeClient = chromeClient;
533     pageClients.contextMenuClient = contextMenuClient;
534     pageClients.editorClient = editorClient;
535     pageClients.dragClient = dragClient;
536     pageClients.inspectorClient = m_inspectorClient;
537
538     m_page = new Page(pageClients);
539 #if !defined(PUBLIC_BUILD) || !PUBLIC_BUILD
540     if (isRunningDrt()) {
541         // In case running in DumpRenderTree mode set the controller to mock provider.
542         GeolocationClientMock* mock = new GeolocationClientMock();
543         WebCore::provideGeolocationTo(m_page, mock);
544         mock->setController(WebCore::GeolocationController::from(m_page));
545     } else
546 #endif
547         WebCore::provideGeolocationTo(m_page, new GeolocationClientBlackBerry(this));
548 #if !defined(PUBLIC_BUILD) || !PUBLIC_BUILD
549     if (getenv("drtRun"))
550         WebCore::provideDeviceOrientationTo(m_page, new DeviceOrientationClientMock);
551     else
552 #endif
553         WebCore::provideDeviceOrientationTo(m_page, new DeviceOrientationClientBlackBerry(this));
554
555     WebCore::provideDeviceMotionTo(m_page, new DeviceMotionClientBlackBerry(this));
556 #if ENABLE(VIBRATION)
557     WebCore::provideVibrationTo(m_page, new VibrationClientBlackBerry());
558 #endif
559
560 #if ENABLE(BATTERY_STATUS)
561     WebCore::provideBatteryTo(m_page, new WebCore::BatteryClientBlackBerry(this));
562 #endif
563
564 #if ENABLE(MEDIA_STREAM)
565     WebCore::provideUserMediaTo(m_page, new UserMediaClientImpl(m_webPage));
566 #endif
567
568 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
569     WebCore::provideNotification(m_page, new NotificationClientBlackBerry(this));
570 #endif
571
572 #if ENABLE(NAVIGATOR_CONTENT_UTILS)
573     WebCore::provideNavigatorContentUtilsTo(m_page, new NavigatorContentUtilsClientBlackBerry(this));
574 #endif
575
576 #if ENABLE(NETWORK_INFO)
577     WebCore::provideNetworkInfoTo(m_page, new WebCore::NetworkInfoClientBlackBerry(this));
578 #endif
579
580     m_webSettings = WebSettings::createFromStandardSettings();
581     m_webSettings->setUserAgentString(defaultUserAgent());
582     m_page->setDeviceScaleFactor(m_webSettings->devicePixelRatio());
583
584     m_page->addLayoutMilestones(DidFirstVisuallyNonEmptyLayout);
585
586 #if USE(ACCELERATED_COMPOSITING)
587     m_tapHighlight = DefaultTapHighlight::create(this);
588     m_selectionOverlay = SelectionOverlay::create(this);
589     m_page->settings()->setAcceleratedCompositingForFixedPositionEnabled(true);
590 #endif
591
592     // FIXME: We explicitly call setDelegate() instead of passing ourself in createFromStandardSettings()
593     // so that we only get one didChangeSettings() callback when we set the page group name. This causes us
594     // to make a copy of the WebSettings since some WebSettings method make use of the page group name.
595     // Instead, we shouldn't be storing the page group name in WebSettings.
596     m_webSettings->setPageGroupName(pageGroupName);
597     m_webSettings->setDelegate(this);
598     didChangeSettings(m_webSettings);
599
600     RefPtr<Frame> newFrame = Frame::create(m_page, /* HTMLFrameOwnerElement* */ 0, frameLoaderClient);
601
602     m_mainFrame = newFrame.get();
603     frameLoaderClient->setFrame(m_mainFrame, this);
604     m_mainFrame->init();
605
606     m_inRegionScroller = adoptPtr(new InRegionScroller(this));
607
608 #if ENABLE(WEBGL)
609     m_page->settings()->setWebGLEnabled(true);
610 #endif
611 #if ENABLE(ACCELERATED_2D_CANVAS)
612     m_page->settings()->setCanvasUsesAcceleratedDrawing(true);
613     m_page->settings()->setAccelerated2dCanvasEnabled(true);
614 #endif
615
616     m_page->settings()->setInteractiveFormValidationEnabled(true);
617     m_page->settings()->setAllowUniversalAccessFromFileURLs(false);
618     m_page->settings()->setAllowFileAccessFromFileURLs(false);
619     m_page->settings()->setFixedPositionCreatesStackingContext(true);
620
621     m_backingStoreClient = BackingStoreClient::create(m_mainFrame, /* parent frame */ 0, m_webPage);
622     // The direct access to BackingStore is left here for convenience since it
623     // is owned by BackingStoreClient and then deleted by its destructor.
624     m_backingStore = m_backingStoreClient->backingStore();
625
626     m_webkitThreadViewportAccessor = new WebKitThreadViewportAccessor(this);
627
628     blockClickRadius = int(roundf(0.35 * Platform::Graphics::Screen::primaryScreen()->pixelsPerInch(0).width())); // The clicked rectangle area should be a fixed unit of measurement.
629
630     m_page->settings()->setDelegateSelectionPaint(true);
631
632 #if ENABLE(REQUEST_ANIMATION_FRAME)
633     m_page->windowScreenDidChange((PlatformDisplayID)0);
634 #endif
635
636 #if ENABLE(WEB_TIMING)
637     m_page->settings()->setMemoryInfoEnabled(true);
638 #endif
639
640 #if USE(ACCELERATED_COMPOSITING)
641     // The compositor will be needed for overlay rendering, so create it
642     // unconditionally. It will allocate OpenGL objects lazily, so this incurs
643     // no overhead in the unlikely case where the compositor is not needed.
644     Platform::userInterfaceThreadMessageClient()->dispatchSyncMessage(
645             createMethodCallMessage(&WebPagePrivate::createCompositor, this));
646 #endif
647 }
648
649 class DeferredTaskLoadManualScript: public DeferredTask<&WebPagePrivate::m_wouldLoadManualScript> {
650 public:
651     explicit DeferredTaskLoadManualScript(WebPagePrivate* webPagePrivate, const KURL& url)
652         : DeferredTaskType(webPagePrivate)
653     {
654         webPagePrivate->m_cachedManualScript = url;
655     }
656 private:
657     virtual void performInternal(WebPagePrivate* webPagePrivate)
658     {
659         webPagePrivate->m_mainFrame->script()->executeIfJavaScriptURL(webPagePrivate->m_cachedManualScript, DoNotReplaceDocumentIfJavaScriptURL);
660     }
661 };
662
663 void WebPagePrivate::load(const BlackBerry::Platform::String& url, const BlackBerry::Platform::String& networkToken, const BlackBerry::Platform::String& method, Platform::NetworkRequest::CachePolicy cachePolicy, const char* data, size_t dataLength, const char* const* headers, size_t headersLength, bool isInitial, bool mustHandleInternally, bool forceDownload, const BlackBerry::Platform::String& overrideContentType, const BlackBerry::Platform::String& suggestedSaveName)
664 {
665     stopCurrentLoad();
666     DeferredTaskLoadManualScript::finishOrCancel(this);
667
668     String urlString(url);
669     if (urlString.startsWith("vs:", false)) {
670         urlString = urlString.substring(3);
671         m_mainFrame->setInViewSourceMode(true);
672     } else
673         m_mainFrame->setInViewSourceMode(false);
674
675     KURL kurl = parseUrl(urlString);
676     if (protocolIs(kurl, "javascript")) {
677         // Never run javascript while loading is deferred.
678         if (m_page->defersLoading())
679             m_deferredTasks.append(adoptPtr(new DeferredTaskLoadManualScript(this, kurl)));
680         else
681             m_mainFrame->script()->executeIfJavaScriptURL(kurl, DoNotReplaceDocumentIfJavaScriptURL);
682         return;
683     }
684
685     if (isInitial)
686         NetworkManager::instance()->setInitialURL(kurl);
687
688     ResourceRequest request(kurl);
689     request.setToken(networkToken);
690     if (isInitial || mustHandleInternally)
691         request.setMustHandleInternally(true);
692     request.setHTTPMethod(method);
693     request.setCachePolicy(toWebCoreCachePolicy(cachePolicy));
694     if (!overrideContentType.empty())
695         request.setOverrideContentType(overrideContentType);
696
697     if (data)
698         request.setHTTPBody(FormData::create(data, dataLength));
699
700     for (unsigned i = 0; i + 1 < headersLength; i += 2)
701         request.addHTTPHeaderField(headers[i], headers[i + 1]);
702
703     if (forceDownload)
704         request.setForceDownload(true);
705
706     request.setSuggestedSaveName(suggestedSaveName);
707
708     FrameLoadRequest frameRequest(m_mainFrame, request);
709     frameRequest.setFrameName("");
710     frameRequest.setShouldCheckNewWindowPolicy(true);
711     m_mainFrame->loader()->load(frameRequest);
712 }
713
714 void WebPage::load(const BlackBerry::Platform::String& url, const BlackBerry::Platform::String& networkToken, bool isInitial)
715 {
716     d->load(url, networkToken, "GET", Platform::NetworkRequest::UseProtocolCachePolicy, 0, 0, 0, 0, isInitial, false);
717 }
718
719 void WebPage::loadExtended(const char* url, const char* networkToken, const char* method, Platform::NetworkRequest::CachePolicy cachePolicy, const char* data, size_t dataLength, const char* const* headers, size_t headersLength, bool mustHandleInternally)
720 {
721     d->load(url, networkToken, method, cachePolicy, data, dataLength, headers, headersLength, false, mustHandleInternally, false, "");
722 }
723
724 void WebPage::loadFile(const BlackBerry::Platform::String& path, const BlackBerry::Platform::String& overrideContentType)
725 {
726     BlackBerry::Platform::String fileUrl(path);
727     if (fileUrl.startsWith("/"))
728         fileUrl = BlackBerry::Platform::String("file://", 7) + fileUrl;
729     else if (!fileUrl.startsWith("file:///"))
730         return;
731
732     d->load(fileUrl, BlackBerry::Platform::String::emptyString(), BlackBerry::Platform::String("GET", 3), Platform::NetworkRequest::UseProtocolCachePolicy, 0, 0, 0, 0, false, false, false, overrideContentType.c_str());
733 }
734
735 void WebPage::download(const Platform::NetworkRequest& request)
736 {
737     vector<const char*> headers;
738     Platform::NetworkRequest::HeaderList& list = request.getHeaderListRef();
739     for (unsigned i = 0; i < list.size(); i++) {
740         headers.push_back(list[i].first.c_str());
741         headers.push_back(list[i].second.c_str());
742     }
743     d->load(request.getUrlRef(), BlackBerry::Platform::String::emptyString(), "GET", Platform::NetworkRequest::UseProtocolCachePolicy, 0, 0, headers.empty() ? 0 : &headers[0], headers.size(), false, false, true, "", request.getSuggestedSaveName().c_str());
744 }
745
746 void WebPagePrivate::loadString(const BlackBerry::Platform::String& string, const BlackBerry::Platform::String& baseURL, const BlackBerry::Platform::String& contentType, const BlackBerry::Platform::String& failingURL)
747 {
748     KURL kurl = parseUrl(baseURL);
749     ResourceRequest request(kurl);
750     WTF::RefPtr<SharedBuffer> buffer
751         = SharedBuffer::create(string.c_str(), string.length());
752     SubstituteData substituteData(buffer,
753         extractMIMETypeFromMediaType(contentType),
754         extractCharsetFromMediaType(contentType),
755         !failingURL.empty() ? parseUrl(failingURL) : KURL());
756     m_mainFrame->loader()->load(FrameLoadRequest(m_mainFrame, request, substituteData));
757 }
758
759 void WebPage::loadString(const BlackBerry::Platform::String& string, const BlackBerry::Platform::String& baseURL, const BlackBerry::Platform::String& mimeType, const BlackBerry::Platform::String& failingURL)
760 {
761     d->loadString(string, baseURL, mimeType, failingURL);
762 }
763
764 bool WebPagePrivate::executeJavaScript(const BlackBerry::Platform::String& scriptUTF8, JavaScriptDataType& returnType, WebString& returnValue)
765 {
766     BLACKBERRY_ASSERT(scriptUTF8.isUtf8());
767     String script = scriptUTF8;
768
769     if (script.isNull()) {
770         returnType = JSException;
771         return false;
772     }
773
774     if (script.isEmpty()) {
775         returnType = JSUndefined;
776         return true;
777     }
778
779     ScriptValue result = m_mainFrame->script()->executeScript(script, false);
780     JSC::JSValue value = result.jsValue();
781     if (!value) {
782         returnType = JSException;
783         return false;
784     }
785
786     if (value.isUndefined())
787         returnType = JSUndefined;
788     else if (value.isNull())
789         returnType = JSNull;
790     else if (value.isBoolean())
791         returnType = JSBoolean;
792     else if (value.isNumber())
793         returnType = JSNumber;
794     else if (value.isString())
795         returnType = JSString;
796     else if (value.isObject())
797         returnType = JSObject;
798     else
799         returnType = JSUndefined;
800
801     if (returnType == JSBoolean || returnType == JSNumber || returnType == JSString || returnType == JSObject) {
802         JSC::ExecState* exec = m_mainFrame->script()->globalObject(mainThreadNormalWorld())->globalExec();
803         returnValue = result.toString(exec);
804     }
805
806     return true;
807 }
808
809 bool WebPage::executeJavaScript(const BlackBerry::Platform::String& script, JavaScriptDataType& returnType, BlackBerry::Platform::String& returnValue)
810 {
811     return d->executeJavaScript(script, returnType, returnValue);
812 }
813
814 bool WebPagePrivate::executeJavaScriptInIsolatedWorld(const ScriptSourceCode& sourceCode, JavaScriptDataType& returnType, BlackBerry::Platform::String& returnValue)
815 {
816     if (!m_isolatedWorld)
817         m_isolatedWorld = m_mainFrame->script()->createWorld();
818
819     // Use evaluateInWorld to avoid canExecuteScripts check.
820     ScriptValue result = m_mainFrame->script()->evaluateInWorld(sourceCode, m_isolatedWorld.get());
821     JSC::JSValue value = result.jsValue();
822     if (!value) {
823         returnType = JSException;
824         return false;
825     }
826
827     if (value.isUndefined())
828         returnType = JSUndefined;
829     else if (value.isNull())
830         returnType = JSNull;
831     else if (value.isBoolean())
832         returnType = JSBoolean;
833     else if (value.isNumber())
834         returnType = JSNumber;
835     else if (value.isString())
836         returnType = JSString;
837     else if (value.isObject())
838         returnType = JSObject;
839     else
840         returnType = JSUndefined;
841
842     if (returnType == JSBoolean || returnType == JSNumber || returnType == JSString || returnType == JSObject) {
843         JSC::ExecState* exec = m_mainFrame->script()->globalObject(mainThreadNormalWorld())->globalExec();
844         returnValue = result.toString(exec);
845     }
846
847     return true;
848 }
849
850 bool WebPage::executeJavaScriptInIsolatedWorld(const std::wstring& script, JavaScriptDataType& returnType, BlackBerry::Platform::String& returnValue)
851 {
852     // On our platform wchar_t is unsigned int and UChar is unsigned short
853     // so we have to convert using ICU conversion function
854     int lengthCopied = 0;
855     UErrorCode error = U_ZERO_ERROR;
856     const int length = script.length() + 1 /*null termination char*/;
857     UChar data[length];
858
859     // FIXME: PR 138162 is giving U_INVALID_CHAR_FOUND error.
860     u_strFromUTF32(data, length, &lengthCopied, reinterpret_cast<const UChar32*>(script.c_str()), script.length(), &error);
861     BLACKBERRY_ASSERT(error == U_ZERO_ERROR);
862     if (error != U_ZERO_ERROR) {
863         Platform::logAlways(Platform::LogLevelCritical, "WebPage::executeJavaScriptInIsolatedWorld failed to convert UTF16 to JavaScript!");
864         return false;
865     }
866     String str = String(data, lengthCopied);
867     ScriptSourceCode sourceCode(str, KURL());
868     return d->executeJavaScriptInIsolatedWorld(sourceCode, returnType, returnValue);
869 }
870
871 bool WebPage::executeJavaScriptInIsolatedWorld(const BlackBerry::Platform::String& scriptUTF8, JavaScriptDataType& returnType, BlackBerry::Platform::String& returnValue)
872 {
873     BLACKBERRY_ASSERT(scriptUTF8.isUtf8());
874     ScriptSourceCode sourceCode(scriptUTF8, KURL());
875     return d->executeJavaScriptInIsolatedWorld(sourceCode, returnType, returnValue);
876 }
877
878 void WebPage::executeJavaScriptFunction(const std::vector<BlackBerry::Platform::String> &function, const std::vector<JavaScriptVariant> &args, JavaScriptVariant& returnValue)
879 {
880     if (!d->m_mainFrame) {
881         returnValue.setType(JavaScriptVariant::Exception);
882         return;
883     }
884
885     JSC::Bindings::RootObject* root = d->m_mainFrame->script()->bindingRootObject();
886     if (!root) {
887         returnValue.setType(JavaScriptVariant::Exception);
888         return;
889     }
890
891     JSC::ExecState* exec = root->globalObject()->globalExec();
892     JSGlobalContextRef ctx = toGlobalRef(exec);
893
894     JSC::JSLockHolder lock(exec);
895     WTF::Vector<JSValueRef> argListRef(args.size());
896     for (unsigned i = 0; i < args.size(); ++i)
897         argListRef[i] = BlackBerryJavaScriptVariantToJSValueRef(ctx, args[i]);
898
899     JSValueRef windowObjectValue = windowObject();
900     JSObjectRef obj = JSValueToObject(ctx, windowObjectValue, 0);
901     JSObjectRef thisObject = obj;
902     for (unsigned i = 0; i < function.size(); ++i) {
903         JSStringRef str = JSStringCreateWithUTF8CString(function[i].c_str());
904         thisObject = obj;
905         obj = JSValueToObject(ctx, JSObjectGetProperty(ctx, obj, str, 0), 0);
906         JSStringRelease(str);
907         if (!obj)
908             break;
909     }
910
911     JSObjectRef functionObject = obj;
912     JSValueRef result = 0;
913     if (functionObject && thisObject)
914         result = JSObjectCallAsFunction(ctx, functionObject, thisObject, args.size(), argListRef.data(), 0);
915
916     if (!result) {
917         returnValue.setType(JavaScriptVariant::Exception);
918         return;
919     }
920
921     returnValue = JSValueRefToBlackBerryJavaScriptVariant(ctx, result);
922 }
923
924 void WebPagePrivate::stopCurrentLoad()
925 {
926     // This function should contain all common code triggered by WebPage::load
927     // (which stops any load in progress before starting the new load) and
928     // WebPage::stoploading (the entry point for the client to stop the load
929     // explicitly). If it should only be done while stopping the load
930     // explicitly, it goes in WebPage::stopLoading, not here.
931     m_mainFrame->loader()->stopAllLoaders();
932
933     // Cancel any deferred script that hasn't been processed yet.
934     DeferredTaskLoadManualScript::finishOrCancel(this);
935 }
936
937 void WebPage::stopLoading()
938 {
939     d->stopCurrentLoad();
940 }
941
942 static void closeURLRecursively(Frame* frame)
943 {
944     // Do not create more frame please.
945     FrameLoaderClientBlackBerry* frameLoaderClient = static_cast<FrameLoaderClientBlackBerry*>(frame->loader()->client());
946     frameLoaderClient->suppressChildFrameCreation();
947
948     frame->loader()->closeURL();
949
950     Vector<RefPtr<Frame>, 10> childFrames;
951
952     for (RefPtr<Frame> childFrame = frame->tree()->firstChild(); childFrame; childFrame = childFrame->tree()->nextSibling())
953         childFrames.append(childFrame);
954
955     unsigned size = childFrames.size();
956     for (unsigned i = 0; i < size; i++)
957         closeURLRecursively(childFrames[i].get());
958 }
959
960 void WebPagePrivate::prepareToDestroy()
961 {
962     // Before the client starts tearing itself down, dispatch the unload event
963     // so it can take effect while all the client's state (e.g. scroll position)
964     // is still present.
965     closeURLRecursively(m_mainFrame);
966 }
967
968 void WebPage::prepareToDestroy()
969 {
970     d->prepareToDestroy();
971 }
972
973 bool WebPage::dispatchBeforeUnloadEvent()
974 {
975     return d->m_page->mainFrame()->loader()->shouldClose();
976 }
977
978 static void enableCrossSiteXHRRecursively(Frame* frame)
979 {
980     frame->document()->securityOrigin()->grantUniversalAccess();
981
982     Vector<RefPtr<Frame>, 10> childFrames;
983     for (RefPtr<Frame> childFrame = frame->tree()->firstChild(); childFrame; childFrame = childFrame->tree()->nextSibling())
984         childFrames.append(childFrame);
985
986     unsigned size = childFrames.size();
987     for (unsigned i = 0; i < size; i++)
988         enableCrossSiteXHRRecursively(childFrames[i].get());
989 }
990
991 void WebPagePrivate::enableCrossSiteXHR()
992 {
993     enableCrossSiteXHRRecursively(m_mainFrame);
994 }
995
996 void WebPage::enableCrossSiteXHR()
997 {
998     d->enableCrossSiteXHR();
999 }
1000
1001 void WebPagePrivate::addOriginAccessWhitelistEntry(const BlackBerry::Platform::String& sourceOrigin, const BlackBerry::Platform::String& destinationOrigin, bool allowDestinationSubdomains)
1002 {
1003     RefPtr<SecurityOrigin> source = SecurityOrigin::createFromString(sourceOrigin);
1004     if (source->isUnique())
1005         return;
1006
1007     KURL destination(KURL(), destinationOrigin);
1008     SecurityPolicy::addOriginAccessWhitelistEntry(*source, destination.protocol(), destination.host(), allowDestinationSubdomains);
1009 }
1010
1011 void WebPage::addOriginAccessWhitelistEntry(const BlackBerry::Platform::String& sourceOrigin, const BlackBerry::Platform::String& destinationOrigin, bool allowDestinationSubdomains)
1012 {
1013     d->addOriginAccessWhitelistEntry(sourceOrigin, destinationOrigin, allowDestinationSubdomains);
1014 }
1015
1016 void WebPagePrivate::removeOriginAccessWhitelistEntry(const BlackBerry::Platform::String& sourceOrigin, const BlackBerry::Platform::String& destinationOrigin, bool allowDestinationSubdomains)
1017 {
1018     RefPtr<SecurityOrigin> source = SecurityOrigin::createFromString(sourceOrigin);
1019     if (source->isUnique())
1020         return;
1021
1022     KURL destination(KURL(), destinationOrigin);
1023     SecurityPolicy::removeOriginAccessWhitelistEntry(*source, destination.protocol(), destination.host(), allowDestinationSubdomains);
1024 }
1025
1026 void WebPage::removeOriginAccessWhitelistEntry(const BlackBerry::Platform::String& sourceOrigin, const BlackBerry::Platform::String& destinationOrigin, bool allowDestinationSubdomains)
1027 {
1028     d->removeOriginAccessWhitelistEntry(sourceOrigin, destinationOrigin, allowDestinationSubdomains);
1029 }
1030
1031 void WebPagePrivate::setLoadState(LoadState state)
1032 {
1033     if (m_loadState == state)
1034         return;
1035
1036     bool isFirstLoad = m_loadState == None;
1037
1038     // See RIM Bug #1068.
1039     if (state == Finished && m_mainFrame && m_mainFrame->document())
1040         m_mainFrame->document()->updateStyleIfNeeded();
1041
1042     // Dispatch the backingstore background color at important state changes.
1043     m_backingStore->d->setWebPageBackgroundColor(m_mainFrame && m_mainFrame->view()
1044         ? m_mainFrame->view()->documentBackgroundColor()
1045         : m_webSettings->backgroundColor());
1046
1047     m_loadState = state;
1048
1049 #if DEBUG_WEBPAGE_LOAD
1050     Platform::logAlways(Platform::LogLevelInfo, "WebPagePrivate::setLoadState %d", state);
1051 #endif
1052
1053     switch (m_loadState) {
1054     case Provisional:
1055         if (isFirstLoad) {
1056             // Paints the visible backingstore as settings()->backgroundColor()
1057             // to prevent initial checkerboard on the first blit.
1058             m_backingStore->d->renderAndBlitVisibleContentsImmediately();
1059         }
1060         break;
1061     case Committed:
1062         {
1063 #if ENABLE(ACCELERATED_2D_CANVAS)
1064             if (m_page->settings()->canvasUsesAcceleratedDrawing()) {
1065                 // Free GPU resources as we're on a new page.
1066                 // This will help us to free memory pressure.
1067                 SharedGraphicsContext3D::get()->makeContextCurrent();
1068                 GrContext* grContext = Platform::Graphics::getGrContext();
1069                 grContext->freeGpuResources();
1070             }
1071 #endif
1072
1073 #if USE(ACCELERATED_COMPOSITING)
1074             releaseLayerResources();
1075 #endif
1076
1077             // Suspend screen update to avoid ui thread blitting while resetting backingstore.
1078             // FIXME: Do we really need to suspend/resume both backingstore and screen here?
1079             m_backingStore->d->suspendBackingStoreUpdates();
1080             m_backingStore->d->suspendScreenUpdates();
1081
1082             m_previousContentsSize = IntSize();
1083             m_backingStore->d->resetRenderQueue();
1084             m_backingStore->d->resetTiles();
1085             m_backingStore->d->setScrollingOrZooming(false, false /* shouldBlit */);
1086             m_shouldZoomToInitialScaleAfterLoadFinished = false;
1087             m_userPerformedManualZoom = false;
1088             m_userPerformedManualScroll = false;
1089             m_shouldUseFixedDesktopMode = false;
1090             m_forceRespectViewportArguments = false;
1091             if (m_resetVirtualViewportOnCommitted) // For DRT.
1092                 m_virtualViewportSize = IntSize();
1093             if (m_webSettings->viewportWidth() > 0)
1094                 m_virtualViewportSize = IntSize(m_webSettings->viewportWidth(), m_defaultLayoutSize.height());
1095
1096             // Check if we have already process the meta viewport tag, this only happens on history navigation.
1097             // For back/forward history navigation, we should only keep these previous values if the document
1098             // has the meta viewport tag when the state is Committed in setLoadState.
1099             static ViewportArguments defaultViewportArguments;
1100             bool documentHasViewportArguments = false;
1101             if (m_mainFrame && m_mainFrame->document() && m_mainFrame->document()->viewportArguments() != defaultViewportArguments)
1102                 documentHasViewportArguments = true;
1103             if (!(m_didRestoreFromPageCache && documentHasViewportArguments)) {
1104                 m_viewportArguments = ViewportArguments();
1105                 m_userScalable = m_webSettings->isUserScalable();
1106                 resetScales();
1107
1108                 // At the moment we commit a new load, set the viewport arguments
1109                 // to any fallback values. If there is a meta viewport in the
1110                 // content it will overwrite the fallback arguments soon.
1111                 dispatchViewportPropertiesDidChange(m_userViewportArguments);
1112                 if (m_userViewportArguments != defaultViewportArguments)
1113                     m_forceRespectViewportArguments = true;
1114             } else {
1115                 Platform::IntSize virtualViewport = recomputeVirtualViewportFromViewportArguments();
1116                 m_webPage->setVirtualViewportSize(virtualViewport);
1117                 if (m_shouldUseFixedDesktopMode)
1118                     setViewMode(FixedDesktop);
1119                 else
1120                     setViewMode(Desktop);
1121             }
1122
1123 #if ENABLE(EVENT_MODE_METATAGS)
1124             didReceiveCursorEventMode(ProcessedCursorEvents);
1125             didReceiveTouchEventMode(ProcessedTouchEvents);
1126 #endif
1127
1128             // Reset block zoom and reflow.
1129             resetBlockZoom();
1130 #if ENABLE(VIEWPORT_REFLOW)
1131             toggleTextReflowIfEnabledForBlockZoomOnly();
1132 #endif
1133
1134             // Notify InputHandler of state change.
1135             m_inputHandler->setInputModeEnabled(false);
1136
1137             // Set the scroll to origin here and notify the client since we'll be
1138             // zooming below without any real contents yet thus the contents size
1139             // we report to the client could make our current scroll position invalid.
1140             setScrollPosition(IntPoint::zero());
1141             notifyTransformedScrollChanged();
1142
1143             // FIXME: Do we really need to suspend/resume both backingstore and screen here?
1144             m_backingStore->d->resumeBackingStoreUpdates();
1145             // Paints the visible backingstore as white. Note it is important we do
1146             // this strictly after re-setting the scroll position to origin and resetting
1147             // the scales otherwise the visible contents calculation is wrong and we
1148             // can end up blitting artifacts instead. See: RIM Bug #401.
1149             m_backingStore->d->resumeScreenUpdates(BackingStore::RenderAndBlit);
1150
1151             // Update cursor status.
1152             updateCursor();
1153
1154             break;
1155         }
1156     case Finished:
1157     case Failed:
1158         // Notify client of the initial zoom change.
1159         m_client->scaleChanged();
1160         m_backingStore->d->updateTiles(true /* updateVisible */, false /* immediate */);
1161         break;
1162     default:
1163         break;
1164     }
1165 }
1166
1167 double WebPagePrivate::clampedScale(double scale) const
1168 {
1169     if (scale < minimumScale())
1170         return minimumScale();
1171     if (scale > maximumScale())
1172         return maximumScale();
1173     return scale;
1174 }
1175
1176 bool WebPagePrivate::shouldZoomAboutPoint(double scale, const FloatPoint&, bool enforceScaleClamping, double* clampedScale)
1177 {
1178     if (!m_mainFrame->view())
1179         return false;
1180
1181     if (enforceScaleClamping)
1182         scale = this->clampedScale(scale);
1183
1184     ASSERT(clampedScale);
1185     *clampedScale = scale;
1186
1187     if (currentScale() == scale)
1188         return false;
1189
1190     return true;
1191 }
1192
1193 bool WebPagePrivate::zoomAboutPoint(double unclampedScale, const FloatPoint& anchor, bool enforceScaleClamping, bool forceRendering, bool isRestoringZoomLevel)
1194 {
1195     if (!isRestoringZoomLevel) {
1196         // Clear any existing block zoom.  (If we are restoring a saved zoom level on page load,
1197         // there is guaranteed to be no existing block zoom and we don't want to clear m_shouldReflowBlock.)
1198         resetBlockZoom();
1199     }
1200
1201     // The reflow and block zoom stuff here needs to happen regardless of
1202     // whether we shouldZoomAboutPoint.
1203 #if ENABLE(VIEWPORT_REFLOW)
1204     toggleTextReflowIfEnabledForBlockZoomOnly(m_shouldReflowBlock);
1205     if (m_page->settings()->isTextReflowEnabled() && m_mainFrame->view())
1206         setNeedsLayout();
1207 #endif
1208
1209     double scale;
1210     if (!shouldZoomAboutPoint(unclampedScale, anchor, enforceScaleClamping, &scale)) {
1211         if (m_webPage->settings()->textReflowMode() == WebSettings::TextReflowEnabled) {
1212             m_currentPinchZoomNode = 0;
1213             m_anchorInNodeRectRatio = FloatPoint(-1, -1);
1214         }
1215         return false;
1216     }
1217     TransformationMatrix zoom;
1218     zoom.scale(scale);
1219
1220 #if DEBUG_WEBPAGE_LOAD
1221     if (loadState() < Finished) {
1222         Platform::logAlways(Platform::LogLevelInfo,
1223             "WebPagePrivate::zoomAboutPoint scale %f anchor %s",
1224             scale, Platform::FloatPoint(anchor).toString().c_str());
1225     }
1226 #endif
1227
1228     // Our current scroll position in float.
1229     FloatPoint scrollPosition = this->scrollPosition();
1230
1231     // Anchor offset from scroll position in float.
1232     FloatPoint anchorOffset(anchor.x() - scrollPosition.x(), anchor.y() - scrollPosition.y());
1233
1234     // The horizontal scaling factor and vertical scaling factor should be equal
1235     // to preserve aspect ratio of content.
1236     ASSERT(m_transformationMatrix->m11() == m_transformationMatrix->m22());
1237
1238     // Need to invert the previous transform to anchor the viewport.
1239     double inverseScale = scale / m_transformationMatrix->m11();
1240
1241     // Actual zoom.
1242     *m_transformationMatrix = zoom;
1243
1244     // Suspend all screen updates to the backingstore.
1245     // FIXME: Do we really need to suspend/resume both backingstore and screen here?
1246     m_backingStore->d->suspendBackingStoreUpdates();
1247     m_backingStore->d->suspendScreenUpdates();
1248
1249     updateViewportSize();
1250
1251     IntPoint newScrollPosition(IntPoint(max(0, static_cast<int>(roundf(anchor.x() - anchorOffset.x() / inverseScale))),
1252                                         max(0, static_cast<int>(roundf(anchor.y() - anchorOffset.y() / inverseScale)))));
1253
1254     if (m_webPage->settings()->textReflowMode() == WebSettings::TextReflowEnabled) {
1255         // This is a hack for email which has reflow always turned on.
1256         m_mainFrame->view()->setNeedsLayout();
1257         requestLayoutIfNeeded();
1258         if (m_currentPinchZoomNode)
1259             newScrollPosition = calculateReflowedScrollPosition(anchorOffset, scale == minimumScale() ? 1 : inverseScale);
1260          m_currentPinchZoomNode = 0;
1261          m_anchorInNodeRectRatio = FloatPoint(-1, -1);
1262     }
1263
1264     setScrollPosition(newScrollPosition);
1265
1266     notifyTransformChanged();
1267
1268     bool isLoading = this->isLoading();
1269
1270     // We need to invalidate all tiles both visible and non-visible if we're loading.
1271     m_backingStore->d->updateTiles(isLoading /* updateVisible */, false /* immediate */);
1272
1273     bool shouldRender = !isLoading || m_userPerformedManualZoom || forceRendering;
1274
1275     m_client->scaleChanged();
1276
1277     if (m_pendingOrientation != -1)
1278         m_client->updateInteractionViews();
1279
1280     // FIXME: Do we really need to suspend/resume both backingstore and screen here?
1281     m_backingStore->d->resumeBackingStoreUpdates();
1282
1283     // Clear window to make sure there are no artifacts.
1284     if (shouldRender) {
1285         // Resume all screen updates to the backingstore and render+blit visible contents to screen.
1286         m_backingStore->d->resumeScreenUpdates(BackingStore::RenderAndBlit);
1287     } else {
1288         // Resume all screen updates to the backingstore but do not blit to the screen because we not rendering.
1289         m_backingStore->d->resumeScreenUpdates(BackingStore::None);
1290     }
1291
1292     return true;
1293 }
1294
1295 IntPoint WebPagePrivate::calculateReflowedScrollPosition(const FloatPoint& anchorOffset, double inverseScale)
1296 {
1297     // Should only be invoked when text reflow is enabled.
1298     ASSERT(m_webPage->settings()->textReflowMode() == WebSettings::TextReflowEnabled);
1299
1300     int offsetY = 0;
1301     int offsetX = 0;
1302
1303     IntRect nodeRect = rectForNode(m_currentPinchZoomNode.get());
1304
1305     if (m_currentPinchZoomNode->renderer() && m_anchorInNodeRectRatio.y() >= 0) {
1306         offsetY = nodeRect.height() * m_anchorInNodeRectRatio.y();
1307         if (m_currentPinchZoomNode->renderer()->isImage() && m_anchorInNodeRectRatio.x() > 0)
1308             offsetX = nodeRect.width() * m_anchorInNodeRectRatio.x() - anchorOffset.x() / inverseScale;
1309     }
1310
1311     IntRect reflowedRect = adjustRectOffsetForFrameOffset(nodeRect, m_currentPinchZoomNode.get());
1312
1313     return IntPoint(max(0, static_cast<int>(roundf(reflowedRect.x() + offsetX))),
1314                     max(0, static_cast<int>(roundf(reflowedRect.y() + offsetY - anchorOffset.y() / inverseScale))));
1315 }
1316
1317 void WebPagePrivate::setNeedsLayout()
1318 {
1319     FrameView* view = m_mainFrame->view();
1320     ASSERT(view);
1321     view->setNeedsLayout();
1322 }
1323
1324 void WebPagePrivate::requestLayoutIfNeeded() const
1325 {
1326     FrameView* view = m_mainFrame->view();
1327     ASSERT(view);
1328     view->updateLayoutAndStyleIfNeededRecursive();
1329     ASSERT(!view->needsLayout());
1330 }
1331
1332 IntPoint WebPagePrivate::scrollPosition() const
1333 {
1334     return m_backingStoreClient->scrollPosition();
1335 }
1336
1337 IntPoint WebPagePrivate::maximumScrollPosition() const
1338 {
1339     return m_backingStoreClient->maximumScrollPosition();
1340 }
1341
1342 void WebPagePrivate::setScrollPosition(const IntPoint& pos)
1343 {
1344     m_backingStoreClient->setScrollPosition(pos);
1345 }
1346
1347 // Setting the scroll position is in transformed coordinates.
1348 void WebPage::setDocumentScrollPosition(const Platform::IntPoint& documentScrollPosition)
1349 {
1350     WebCore::IntPoint scrollPosition = documentScrollPosition;
1351     if (scrollPosition == d->scrollPosition())
1352         return;
1353
1354     // If the user recently performed an event, this new scroll position
1355     // could possibly be a result of that. Or not, this is just a heuristic.
1356     if (currentTime() - d->m_lastUserEventTimestamp < manualScrollInterval)
1357         d->m_userPerformedManualScroll = true;
1358
1359     d->m_backingStoreClient->setIsClientGeneratedScroll(true);
1360
1361     // UI thread can call BackingStorePrivate::setScrollingOrZooming(false) before WebKit thread calls WebPage::setScrollPosition(),
1362     // in which case it will set ScrollableArea::m_constrainsScrollingToContentEdge to true earlier.
1363     // We can cache ScrollableArea::m_constrainsScrollingToContentEdge and always set it to false before we set scroll position in
1364     // WebKit thread to avoid scroll position clamping during scrolling, and restore it to what it was after that.
1365     bool constrainsScrollingToContentEdge = d->m_mainFrame->view()->constrainsScrollingToContentEdge();
1366     d->m_mainFrame->view()->setConstrainsScrollingToContentEdge(false);
1367     d->setScrollPosition(scrollPosition);
1368     d->m_mainFrame->view()->setConstrainsScrollingToContentEdge(constrainsScrollingToContentEdge);
1369
1370     d->m_backingStoreClient->setIsClientGeneratedScroll(false);
1371 }
1372
1373 bool WebPagePrivate::shouldSendResizeEvent()
1374 {
1375     if (!m_mainFrame->document())
1376         return false;
1377
1378     // PR#96865 : Provide an option to always send resize events, regardless of the loading
1379     //            status. The scenario for this are Sapphire applications which tend to
1380     //            maintain an open GET request to the server. This open GET results in
1381     //            webkit thinking that content is still arriving when at the application
1382     //            level it is considered fully loaded.
1383     //
1384     //            NOTE: Care must be exercised in the use of this option, as it bypasses
1385     //                  the sanity provided in 'isLoadingInAPISense()' below.
1386     //
1387     static const bool unrestrictedResizeEvents = Platform::Settings::instance()->unrestrictedResizeEvents();
1388     if (unrestrictedResizeEvents)
1389         return true;
1390
1391     // Don't send the resize event if the document is loading. Some pages automatically reload
1392     // when the window is resized; Safari on iPhone often resizes the window while setting up its
1393     // viewport. This obviously can cause problems.
1394     DocumentLoader* documentLoader = m_mainFrame->loader()->documentLoader();
1395     if (documentLoader && documentLoader->isLoadingInAPISense())
1396         return false;
1397
1398     return true;
1399 }
1400
1401 void WebPagePrivate::willDeferLoading()
1402 {
1403     m_deferredTasksTimer.stop();
1404     m_client->willDeferLoading();
1405 }
1406
1407 void WebPagePrivate::didResumeLoading()
1408 {
1409     if (!m_deferredTasks.isEmpty())
1410         m_deferredTasksTimer.startOneShot(0);
1411     m_client->didResumeLoading();
1412 }
1413
1414 void WebPagePrivate::deferredTasksTimerFired(WebCore::Timer<WebPagePrivate>*)
1415 {
1416     ASSERT(!m_deferredTasks.isEmpty());
1417     if (m_deferredTasks.isEmpty())
1418         return;
1419
1420     OwnPtr<DeferredTaskBase> task = m_deferredTasks[0].release();
1421     m_deferredTasks.remove(0);
1422
1423     if (!m_deferredTasks.isEmpty())
1424         m_deferredTasksTimer.startOneShot(0);
1425
1426     task->perform(this);
1427 }
1428
1429 void WebPagePrivate::notifyInRegionScrollStopped()
1430 {
1431     if (m_inRegionScroller->d->isActive())
1432         m_inRegionScroller->d->reset();
1433 }
1434
1435 void WebPage::notifyInRegionScrollStopped()
1436 {
1437     d->notifyInRegionScrollStopped();
1438 }
1439
1440 void WebPagePrivate::setHasInRegionScrollableAreas(bool b)
1441 {
1442     if (b != m_hasInRegionScrollableAreas)
1443         m_hasInRegionScrollableAreas = b;
1444 }
1445
1446 IntSize WebPagePrivate::viewportSize() const
1447 {
1448     return mapFromTransformed(transformedViewportSize());
1449 }
1450
1451 IntSize WebPagePrivate::actualVisibleSize() const
1452 {
1453     return mapFromTransformed(transformedActualVisibleSize());
1454 }
1455
1456 bool WebPagePrivate::hasVirtualViewport() const
1457 {
1458     return !m_virtualViewportSize.isEmpty();
1459 }
1460
1461 void WebPagePrivate::updateViewportSize(bool setFixedReportedSize, bool sendResizeEvent)
1462 {
1463     // This checks to make sure we're not calling updateViewportSize
1464     // during WebPagePrivate::init().
1465     if (!m_backingStore)
1466         return;
1467     ASSERT(m_mainFrame->view());
1468     IntSize visibleSize = actualVisibleSize();
1469     if (setFixedReportedSize)
1470         m_mainFrame->view()->setFixedReportedSize(visibleSize);
1471
1472     IntRect frameRect = IntRect(scrollPosition(), visibleSize);
1473     if (frameRect != m_mainFrame->view()->frameRect()) {
1474         m_mainFrame->view()->setFrameRect(frameRect);
1475         m_mainFrame->view()->adjustViewSize();
1476
1477 #if ENABLE(FULLSCREEN_API)
1478         adjustFullScreenElementDimensionsIfNeeded();
1479 #endif
1480     }
1481
1482     // We're going to need to send a resize event to JavaScript because
1483     // innerWidth and innerHeight depend on fixed reported size.
1484     // This is how we support mobile pages where JavaScript resizes
1485     // the page in order to get around the fixed layout size, e.g.
1486     // google maps when it detects a mobile user agent.
1487     if (sendResizeEvent && shouldSendResizeEvent())
1488         m_mainFrame->eventHandler()->sendResizeEvent();
1489
1490     // When the actual visible size changes, we also
1491     // need to reposition fixed elements.
1492     m_mainFrame->view()->repaintFixedElementsAfterScrolling();
1493 }
1494
1495 FloatPoint WebPagePrivate::centerOfVisibleContentsRect() const
1496 {
1497     // The visible contents rect in float.
1498     FloatRect visibleContentsRect = this->visibleContentsRect();
1499
1500     // The center of the visible contents rect in float.
1501     return FloatPoint(visibleContentsRect.x() + visibleContentsRect.width() / 2.0,
1502                       visibleContentsRect.y() + visibleContentsRect.height() / 2.0);
1503 }
1504
1505 IntRect WebPagePrivate::visibleContentsRect() const
1506 {
1507     return m_backingStoreClient->visibleContentsRect();
1508 }
1509
1510 IntSize WebPagePrivate::contentsSize() const
1511 {
1512     if (!m_mainFrame->view())
1513         return IntSize();
1514
1515     return m_backingStoreClient->contentsSize();
1516 }
1517
1518 IntSize WebPagePrivate::absoluteVisibleOverflowSize() const
1519 {
1520     if (!m_mainFrame->contentRenderer())
1521         return IntSize();
1522
1523     return IntSize(m_mainFrame->contentRenderer()->rightAbsoluteVisibleOverflow(), m_mainFrame->contentRenderer()->bottomAbsoluteVisibleOverflow());
1524 }
1525
1526 void WebPagePrivate::contentsSizeChanged(const IntSize& contentsSize)
1527 {
1528     if (m_previousContentsSize == contentsSize)
1529         return;
1530
1531     // This should only occur in the middle of layout so we set a flag here and
1532     // handle it at the end of the layout.
1533     m_contentsSizeChanged = true;
1534
1535 #if DEBUG_WEBPAGE_LOAD
1536     Platform::logAlways(Platform::LogLevelInfo, "WebPagePrivate::contentsSizeChanged %s", Platform::IntSize(contentsSize).toString().c_str());
1537 #endif
1538 }
1539
1540 void WebPagePrivate::overflowExceedsContentsSize()
1541 {
1542     m_overflowExceedsContentsSize = true;
1543     if (absoluteVisibleOverflowSize().width() < DEFAULT_MAX_LAYOUT_WIDTH && !hasVirtualViewport()) {
1544         if (setViewMode(viewMode())) {
1545             setNeedsLayout();
1546             requestLayoutIfNeeded();
1547         }
1548     }
1549 }
1550
1551 void WebPagePrivate::layoutFinished()
1552 {
1553     if (!m_contentsSizeChanged && !m_overflowExceedsContentsSize)
1554         return;
1555
1556     m_contentsSizeChanged = false; // Toggle to turn off notification again.
1557     m_overflowExceedsContentsSize = false;
1558
1559     if (contentsSize().isEmpty())
1560         return;
1561
1562     // The call to zoomToInitialScaleOnLoad can cause recursive layout when called from
1563     // the middle of a layout, but the recursion is limited by detection code in
1564     // setViewMode() and mitigation code in fixedLayoutSize().
1565     if (didLayoutExceedMaximumIterations()) {
1566         notifyTransformedContentsSizeChanged();
1567         return;
1568     }
1569
1570     // Temporarily save the m_previousContentsSize here before updating it (in
1571     // notifyTransformedContentsSizeChanged()) so we can compare if our contents
1572     // shrunk afterwards.
1573     IntSize previousContentsSize = m_previousContentsSize;
1574
1575     m_nestedLayoutFinishedCount++;
1576
1577     if (shouldZoomToInitialScaleOnLoad()) {
1578         zoomToInitialScaleOnLoad();
1579         m_shouldZoomToInitialScaleAfterLoadFinished = false;
1580     } else if (loadState() != None)
1581         notifyTransformedContentsSizeChanged();
1582
1583     m_nestedLayoutFinishedCount--;
1584
1585     if (!m_nestedLayoutFinishedCount) {
1586         // When the contents shrinks, there is a risk that we
1587         // will be left at a scroll position that lies outside of the
1588         // contents rect. Since we allow overscrolling and neglect
1589         // to clamp overscroll in order to retain input focus (RIM Bug #414)
1590         // we need to clamp somewhere, and this is where we know the
1591         // contents size has changed.
1592
1593         if (contentsSize() != previousContentsSize) {
1594
1595             IntPoint newScrollPosition = scrollPosition();
1596
1597             if (contentsSize().height() < previousContentsSize.height()) {
1598                 IntPoint scrollPositionWithHeightShrunk = IntPoint(newScrollPosition.x(), maximumScrollPosition().y());
1599                 newScrollPosition = newScrollPosition.shrunkTo(scrollPositionWithHeightShrunk);
1600             }
1601
1602             if (contentsSize().width() < previousContentsSize.width()) {
1603                 IntPoint scrollPositionWithWidthShrunk = IntPoint(maximumScrollPosition().x(), newScrollPosition.y());
1604                 newScrollPosition = newScrollPosition.shrunkTo(scrollPositionWithWidthShrunk);
1605             }
1606
1607             if (newScrollPosition != scrollPosition()) {
1608                 setScrollPosition(newScrollPosition);
1609                 notifyTransformedScrollChanged();
1610             }
1611
1612             // If the content size is too small, zoom it to fit the viewport.
1613             if ((loadState() == Finished || loadState() == Committed)
1614                 && (transformedContentsSize().width() < transformedActualVisibleSize().width() || transformedContentsSize().height() < transformedActualVisibleSize().height()))
1615                     zoomAboutPoint(initialScale(), newScrollPosition);
1616
1617         }
1618     }
1619 }
1620
1621 void WebPagePrivate::zoomToInitialScaleOnLoad()
1622 {
1623 #if DEBUG_WEBPAGE_LOAD
1624     Platform::logAlways(Platform::LogLevelInfo, "WebPagePrivate::zoomToInitialScaleOnLoad");
1625 #endif
1626
1627     bool needsLayout = false;
1628
1629     // If the contents width exceeds the viewport width set to desktop mode.
1630     if (m_shouldUseFixedDesktopMode)
1631         needsLayout = setViewMode(FixedDesktop);
1632     else
1633         needsLayout = setViewMode(Desktop);
1634
1635     if (needsLayout) {
1636         // This can cause recursive layout...
1637         setNeedsLayout();
1638     }
1639
1640     if (contentsSize().isEmpty()) {
1641 #if DEBUG_WEBPAGE_LOAD
1642         Platform::logAlways(Platform::LogLevelInfo, "WebPagePrivate::zoomToInitialScaleOnLoad content is empty!");
1643 #endif
1644         requestLayoutIfNeeded();
1645         notifyTransformedContentsSizeChanged();
1646         return;
1647     }
1648
1649     bool performedZoom = false;
1650     bool shouldZoom = !m_userPerformedManualZoom;
1651
1652     // If this is a back/forward type navigation, don't zoom to initial scale
1653     // but instead let the HistoryItem's saved viewport reign supreme.
1654     if (m_mainFrame && m_mainFrame->loader() && isBackForwardLoadType(m_mainFrame->loader()->loadType()))
1655         shouldZoom = false;
1656
1657     if (shouldZoom && shouldZoomToInitialScaleOnLoad()) {
1658         // Preserve at top and at left position, to avoid scrolling
1659         // to a non top-left position for web page with viewport meta tag
1660         // that specifies an initial-scale that is zoomed in.
1661         FloatPoint anchor = centerOfVisibleContentsRect();
1662         if (!scrollPosition().x())
1663             anchor.setX(0);
1664         if (!scrollPosition().y())
1665             anchor.setY(0);
1666         performedZoom = zoomAboutPoint(initialScale(), anchor);
1667     }
1668
1669     // zoomAboutPoint above can also toggle setNeedsLayout and cause recursive layout...
1670     requestLayoutIfNeeded();
1671
1672     if (!performedZoom) {
1673         // We only notify if we didn't perform zoom, because zoom will notify on
1674         // its own...
1675         notifyTransformedContentsSizeChanged();
1676     }
1677 }
1678
1679 double WebPagePrivate::zoomToFitScale() const
1680 {
1681     int contentWidth = contentsSize().width();
1682
1683     // For image document, zoom to fit the screen based on the actual image width
1684     // instead of the contents width within a maximum scale .
1685     Document* doc = m_page->mainFrame()->document();
1686     bool isImageDocument = doc && doc->isImageDocument();
1687     if (isImageDocument)
1688         contentWidth = static_cast<ImageDocument*>(doc)->imageSize().width();
1689
1690     double zoomToFitScale = contentWidth > 0.0 ? static_cast<double>(m_actualVisibleWidth) / contentWidth : 1.0;
1691     int contentHeight = contentsSize().height();
1692     if (contentHeight * zoomToFitScale < static_cast<double>(m_defaultLayoutSize.height()))
1693         zoomToFitScale = contentHeight > 0 ? static_cast<double>(m_defaultLayoutSize.height()) / contentHeight : 1.0;
1694     zoomToFitScale = std::max(zoomToFitScale, minimumZoomToFitScale);
1695
1696     if (!isImageDocument)
1697         return zoomToFitScale;
1698
1699     return std::min(zoomToFitScale, maximumImageDocumentZoomToFitScale);
1700 }
1701
1702 bool WebPagePrivate::respectViewport() const
1703 {
1704     return m_forceRespectViewportArguments || contentsSize().width() <= m_virtualViewportSize.width() + 1;
1705 }
1706
1707 double WebPagePrivate::initialScale() const
1708 {
1709
1710     if (m_initialScale > 0.0 && respectViewport())
1711         return m_initialScale;
1712
1713     if (m_webSettings->isZoomToFitOnLoad())
1714         return zoomToFitScale();
1715
1716     return 1.0;
1717 }
1718
1719 double WebPage::initialScale() const
1720 {
1721     return d->initialScale();
1722 }
1723
1724 void WebPage::initializeIconDataBase()
1725 {
1726     IconDatabaseClientBlackBerry::instance()->initIconDatabase(d->m_webSettings);
1727 }
1728
1729 bool WebPage::isUserScalable() const
1730 {
1731     return d->isUserScalable();
1732 }
1733
1734 void WebPage::setUserScalable(bool userScalable)
1735 {
1736     d->setUserScalable(userScalable);
1737 }
1738
1739 double WebPage::currentScale() const
1740 {
1741     return d->currentScale();
1742 }
1743
1744 void WebPage::setInitialScale(double initialScale)
1745 {
1746     d->setInitialScale(initialScale);
1747 }
1748
1749 double WebPage::minimumScale() const
1750 {
1751     return d->minimumScale();
1752 }
1753
1754 void WebPage::setMinimumScale(double minimumScale)
1755 {
1756     d->setMinimumScale(minimumScale);
1757 }
1758
1759 void WebPage::setMaximumScale(double maximumScale)
1760 {
1761     d->setMaximumScale(maximumScale);
1762 }
1763
1764 double WebPagePrivate::maximumScale() const
1765 {
1766     double zoomToFitScale = this->zoomToFitScale();
1767     if (m_maximumScale >= m_minimumScale && respectViewport())
1768         return std::max(zoomToFitScale, m_maximumScale);
1769
1770     return hasVirtualViewport() ? std::max<double>(zoomToFitScale, 4.0) : 4.0;
1771 }
1772
1773 double WebPage::maximumScale() const
1774 {
1775     return d->maximumScale();
1776 }
1777
1778 void WebPagePrivate::resetScales()
1779 {
1780     TransformationMatrix identity;
1781     *m_transformationMatrix = identity;
1782     m_initialScale = m_webSettings->initialScale() > 0 ? m_webSettings->initialScale() : -1.0;
1783     m_minimumScale = -1.0;
1784     m_maximumScale = -1.0;
1785     m_client->scaleChanged();
1786
1787     // We have to let WebCore know about updated framerect now that we've
1788     // reset our scales. See: RIM Bug #401.
1789     updateViewportSize();
1790 }
1791
1792 IntPoint WebPagePrivate::transformedScrollPosition() const
1793 {
1794     return m_backingStoreClient->transformedScrollPosition();
1795 }
1796
1797 IntPoint WebPagePrivate::transformedMaximumScrollPosition() const
1798 {
1799     return m_backingStoreClient->transformedMaximumScrollPosition();
1800 }
1801
1802 IntSize WebPagePrivate::transformedActualVisibleSize() const
1803 {
1804     return IntSize(m_actualVisibleWidth, m_actualVisibleHeight);
1805 }
1806
1807 Platform::ViewportAccessor* WebPage::webkitThreadViewportAccessor() const
1808 {
1809     return d->m_webkitThreadViewportAccessor;
1810 }
1811
1812 Platform::IntSize WebPage::viewportSize() const
1813 {
1814     return d->transformedActualVisibleSize();
1815 }
1816
1817 IntSize WebPagePrivate::transformedViewportSize() const
1818 {
1819     return BlackBerry::Platform::Settings::instance()->applicationSize();
1820 }
1821
1822 IntRect WebPagePrivate::transformedVisibleContentsRect() const
1823 {
1824     // Usually this would be mapToTransformed(visibleContentsRect()), but
1825     // that results in rounding errors because we already set the WebCore
1826     // viewport size from our original transformedViewportSize().
1827     // Instead, we only transform the scroll position and take the
1828     // viewport size as it is, which ensures that e.g. blitting operations
1829     // always cover the whole widget/screen.
1830     return IntRect(transformedScrollPosition(), transformedViewportSize());
1831 }
1832
1833 IntSize WebPagePrivate::transformedContentsSize() const
1834 {
1835     // mapToTransformed() functions use this method to crop their results,
1836     // so we can't make use of them here. While we want rounding inside page
1837     // boundaries to extend rectangles and round points, we need to crop the
1838     // contents size to the floored values so that we don't try to display
1839     // or report points that are not fully covered by the actual float-point
1840     // contents rectangle.
1841     const IntSize untransformedContentsSize = contentsSize();
1842     const FloatPoint transformedBottomRight = m_transformationMatrix->mapPoint(
1843         FloatPoint(untransformedContentsSize.width(), untransformedContentsSize.height()));
1844     return IntSize(floorf(transformedBottomRight.x()), floorf(transformedBottomRight.y()));
1845 }
1846
1847 IntPoint WebPagePrivate::mapFromContentsToViewport(const IntPoint& point) const
1848 {
1849     return m_backingStoreClient->mapFromContentsToViewport(point);
1850 }
1851
1852 IntPoint WebPagePrivate::mapFromViewportToContents(const IntPoint& point) const
1853 {
1854     return m_backingStoreClient->mapFromViewportToContents(point);
1855 }
1856
1857 IntRect WebPagePrivate::mapFromContentsToViewport(const IntRect& rect) const
1858 {
1859     return m_backingStoreClient->mapFromContentsToViewport(rect);
1860 }
1861
1862 IntRect WebPagePrivate::mapFromViewportToContents(const IntRect& rect) const
1863 {
1864     return m_backingStoreClient->mapFromViewportToContents(rect);
1865 }
1866
1867 IntPoint WebPagePrivate::mapFromTransformedContentsToTransformedViewport(const IntPoint& point) const
1868 {
1869     return m_backingStoreClient->mapFromTransformedContentsToTransformedViewport(point);
1870 }
1871
1872 IntPoint WebPagePrivate::mapFromTransformedViewportToTransformedContents(const IntPoint& point) const
1873 {
1874     return m_backingStoreClient->mapFromTransformedViewportToTransformedContents(point);
1875 }
1876
1877 IntRect WebPagePrivate::mapFromTransformedContentsToTransformedViewport(const IntRect& rect) const
1878 {
1879     return m_backingStoreClient->mapFromTransformedContentsToTransformedViewport(rect);
1880 }
1881
1882 IntRect WebPagePrivate::mapFromTransformedViewportToTransformedContents(const IntRect& rect) const
1883 {
1884     return m_backingStoreClient->mapFromTransformedViewportToTransformedContents(rect);
1885 }
1886
1887 // NOTE: PIXEL ROUNDING!
1888 // Accurate back-and-forth rounding is not possible with information loss
1889 // by integer points and sizes, so we always expand the resulting mapped
1890 // float rectangles to the nearest integer. For points, we always use
1891 // floor-rounding in mapToTransformed() so that we don't have to crop to
1892 // the (floor'd) transformed contents size.
1893 static inline IntPoint roundTransformedPoint(const FloatPoint &point)
1894 {
1895     // Maps by rounding half towards zero.
1896     return IntPoint(static_cast<int>(floorf(point.x())), static_cast<int>(floorf(point.y())));
1897 }
1898
1899 static inline IntPoint roundUntransformedPoint(const FloatPoint &point)
1900 {
1901     // Maps by rounding half away from zero.
1902     return IntPoint(static_cast<int>(ceilf(point.x())), static_cast<int>(ceilf(point.y())));
1903 }
1904
1905 IntPoint WebPagePrivate::mapToTransformed(const IntPoint& point) const
1906 {
1907     return roundTransformedPoint(m_transformationMatrix->mapPoint(FloatPoint(point)));
1908 }
1909
1910 FloatPoint WebPagePrivate::mapToTransformedFloatPoint(const FloatPoint& point) const
1911 {
1912     return m_transformationMatrix->mapPoint(point);
1913 }
1914
1915 IntPoint WebPagePrivate::mapFromTransformed(const IntPoint& point) const
1916 {
1917     return roundUntransformedPoint(m_transformationMatrix->inverse().mapPoint(FloatPoint(point)));
1918 }
1919
1920 FloatPoint WebPagePrivate::mapFromTransformedFloatPoint(const FloatPoint& point) const
1921 {
1922     return m_transformationMatrix->inverse().mapPoint(point);
1923 }
1924
1925 FloatRect WebPagePrivate::mapFromTransformedFloatRect(const FloatRect& rect) const
1926 {
1927     return m_transformationMatrix->inverse().mapRect(rect);
1928 }
1929
1930 IntSize WebPagePrivate::mapToTransformed(const IntSize& size) const
1931 {
1932     return mapToTransformed(IntRect(IntPoint::zero(), size)).size();
1933 }
1934
1935 IntSize WebPagePrivate::mapFromTransformed(const IntSize& size) const
1936 {
1937     return mapFromTransformed(IntRect(IntPoint::zero(), size)).size();
1938 }
1939
1940 IntRect WebPagePrivate::mapToTransformed(const IntRect& rect) const
1941 {
1942     return enclosingIntRect(m_transformationMatrix->mapRect(FloatRect(rect)));
1943 }
1944
1945 // Use this in conjunction with mapToTransformed(IntRect), in most cases.
1946 void WebPagePrivate::clipToTransformedContentsRect(IntRect& rect) const
1947 {
1948     rect.intersect(IntRect(IntPoint::zero(), transformedContentsSize()));
1949 }
1950
1951 IntRect WebPagePrivate::mapFromTransformed(const IntRect& rect) const
1952 {
1953     return enclosingIntRect(m_transformationMatrix->inverse().mapRect(FloatRect(rect)));
1954 }
1955
1956 bool WebPagePrivate::transformedPointEqualsUntransformedPoint(const IntPoint& transformedPoint, const IntPoint& untransformedPoint)
1957 {
1958     // Scaling down is always more accurate than scaling up.
1959     if (m_transformationMatrix->a() > 1.0)
1960         return transformedPoint == mapToTransformed(untransformedPoint);
1961
1962     return mapFromTransformed(transformedPoint) == untransformedPoint;
1963 }
1964
1965 void WebPagePrivate::notifyTransformChanged()
1966 {
1967     notifyTransformedContentsSizeChanged();
1968     notifyTransformedScrollChanged();
1969
1970     m_backingStore->d->transformChanged();
1971 }
1972
1973 void WebPagePrivate::notifyTransformedContentsSizeChanged()
1974 {
1975     // We mark here as the last reported content size we sent to the client.
1976     m_previousContentsSize = contentsSize();
1977
1978     const IntSize size = transformedContentsSize();
1979     m_backingStore->d->contentsSizeChanged(size);
1980     m_client->contentsSizeChanged();
1981 }
1982
1983 void WebPagePrivate::notifyTransformedScrollChanged()
1984 {
1985     const IntPoint pos = transformedScrollPosition();
1986     m_backingStore->d->scrollChanged(pos);
1987     m_client->scrollChanged();
1988
1989 #if ENABLE(FULLSCREEN_API)
1990     adjustFullScreenElementDimensionsIfNeeded();
1991 #endif
1992 }
1993
1994 bool WebPagePrivate::setViewMode(ViewMode mode)
1995 {
1996     if (!m_mainFrame->view())
1997         return false;
1998
1999     m_viewMode = mode;
2000
2001     // If we're in the middle of a nested layout with a recursion count above
2002     // some maximum threshold, then our algorithm for finding the minimum content
2003     // width of a given page has become dependent on the visible width.
2004     //
2005     // We need to find some method to ensure that we don't experience excessive
2006     // and even infinite recursion. This can even happen with valid html. The
2007     // former can happen when we run into inline text with few candidates for line
2008     // break. The latter can happen for instance if the page has a negative margin
2009     // set against the right border. Note: this is valid by spec and can lead to
2010     // a situation where there is no value for which the content width will ensure
2011     // no horizontal scrollbar.
2012     // Example: LayoutTests/css1/box_properties/margin.html
2013     //
2014     // In order to address such situations when we detect a recursion above some
2015     // maximum threshold we snap our fixed layout size to a defined quantum increment.
2016     // Eventually, either the content width will be satisfied to ensure no horizontal
2017     // scrollbar or this increment will run into the maximum layout size and the
2018     // recursion will necessarily end.
2019     bool snapToIncrement = didLayoutExceedMaximumIterations();
2020
2021     IntSize currentSize = m_mainFrame->view()->fixedLayoutSize();
2022     IntSize newSize = fixedLayoutSize(snapToIncrement);
2023     if (currentSize == newSize)
2024         return false;
2025
2026     // FIXME: Temp solution. We'll get back to this.
2027     if (m_nestedLayoutFinishedCount) {
2028         double widthChange = fabs(double(newSize.width() - currentSize.width()) / currentSize.width());
2029         double heightChange = fabs(double(newSize.height() - currentSize.height()) / currentSize.height());
2030         if (widthChange < 0.05 && heightChange < 0.05)
2031             return false;
2032     }
2033
2034     m_mainFrame->view()->setUseFixedLayout(useFixedLayout());
2035     m_mainFrame->view()->setFixedLayoutSize(newSize);
2036     return true; // Needs re-layout!
2037 }
2038
2039 int WebPagePrivate::playerID() const
2040 {
2041     return m_client ? m_client->getInstanceId() : 0;
2042 }
2043
2044 void WebPagePrivate::setCursor(PlatformCursor handle)
2045 {
2046     if (m_currentCursor.type() != handle.type()) {
2047         m_currentCursor = handle;
2048         m_client->cursorChanged(handle.type(), handle.url().c_str(), handle.hotspot());
2049     }
2050 }
2051
2052 Platform::NetworkStreamFactory* WebPagePrivate::networkStreamFactory()
2053 {
2054     return m_client->networkStreamFactory();
2055 }
2056
2057 Platform::Graphics::Window* WebPagePrivate::platformWindow() const
2058 {
2059     return m_client->window();
2060 }
2061
2062 void WebPagePrivate::setPreventsScreenDimming(bool keepAwake)
2063 {
2064     if (keepAwake) {
2065         if (!m_preventIdleDimmingCount)
2066             m_client->setPreventsScreenIdleDimming(true);
2067         m_preventIdleDimmingCount++;
2068     } else if (m_preventIdleDimmingCount > 0) {
2069         m_preventIdleDimmingCount--;
2070         if (!m_preventIdleDimmingCount)
2071             m_client->setPreventsScreenIdleDimming(false);
2072     } else
2073         ASSERT_NOT_REACHED(); // SetPreventsScreenIdleDimming(false) called too many times.
2074 }
2075
2076 void WebPagePrivate::showVirtualKeyboard(bool showKeyboard)
2077 {
2078     m_client->showVirtualKeyboard(showKeyboard);
2079 }
2080
2081 void WebPagePrivate::ensureContentVisible(bool centerInView)
2082 {
2083     m_inputHandler->ensureFocusElementVisible(centerInView);
2084 }
2085
2086 void WebPagePrivate::zoomToContentRect(const IntRect& rect)
2087 {
2088     // Don't scale if the user is not supposed to scale.
2089     if (!isUserScalable())
2090         return;
2091
2092     FloatPoint anchor = FloatPoint(rect.width() / 2.0 + rect.x(), rect.height() / 2.0 + rect.y());
2093     IntSize viewSize = viewportSize();
2094
2095     // Calculate the scale required to scale that dimension to fit.
2096     double scaleH = (double)viewSize.width() / (double)rect.width();
2097     double scaleV = (double)viewSize.height() / (double)rect.height();
2098
2099     // Choose the smaller scale factor so that all of the content is visible.
2100     zoomAboutPoint(min(scaleH, scaleV), anchor);
2101 }
2102
2103 void WebPagePrivate::registerPlugin(PluginView* plugin, bool shouldRegister)
2104 {
2105     if (shouldRegister)
2106         m_pluginViews.add(plugin);
2107     else
2108         m_pluginViews.remove(plugin);
2109 }
2110
2111 #define FOR_EACH_PLUGINVIEW(pluginViews) \
2112     HashSet<PluginView*>::const_iterator it = pluginViews.begin(); \
2113     HashSet<PluginView*>::const_iterator last = pluginViews.end(); \
2114     for (; it != last; ++it)
2115
2116 void WebPagePrivate::notifyPageOnLoad()
2117 {
2118     FOR_EACH_PLUGINVIEW(m_pluginViews)
2119         (*it)->handleOnLoadEvent();
2120 }
2121
2122 bool WebPagePrivate::shouldPluginEnterFullScreen(PluginView* plugin, const char* windowUniquePrefix)
2123 {
2124     return m_client->shouldPluginEnterFullScreen();
2125 }
2126
2127 void WebPagePrivate::didPluginEnterFullScreen(PluginView* plugin, const char* windowUniquePrefix)
2128 {
2129     m_fullScreenPluginView = plugin;
2130     m_client->didPluginEnterFullScreen();
2131
2132     if (!m_client->window())
2133         return;
2134
2135     Platform::Graphics::Window::setTransparencyDiscardFilter(windowUniquePrefix);
2136     m_client->window()->setSensitivityFullscreenOverride(true);
2137 }
2138
2139 void WebPagePrivate::didPluginExitFullScreen(PluginView* plugin, const char* windowUniquePrefix)
2140 {
2141     m_fullScreenPluginView = 0;
2142     m_client->didPluginExitFullScreen();
2143
2144     if (!m_client->window())
2145         return;
2146
2147     Platform::Graphics::Window::setTransparencyDiscardFilter(0);
2148     m_client->window()->setSensitivityFullscreenOverride(false);
2149 }
2150
2151 void WebPagePrivate::onPluginStartBackgroundPlay(PluginView* plugin, const char* windowUniquePrefix)
2152 {
2153     m_client->onPluginStartBackgroundPlay();
2154 }
2155
2156 void WebPagePrivate::onPluginStopBackgroundPlay(PluginView* plugin, const char* windowUniquePrefix)
2157 {
2158     m_client->onPluginStopBackgroundPlay();
2159 }
2160
2161 bool WebPagePrivate::lockOrientation(bool landscape)
2162 {
2163     return m_client->lockOrientation(landscape);
2164 }
2165
2166 void WebPagePrivate::unlockOrientation()
2167 {
2168     return m_client->unlockOrientation();
2169 }
2170
2171 int WebPagePrivate::orientation() const
2172 {
2173 #if ENABLE(ORIENTATION_EVENTS)
2174     return m_mainFrame->orientation();
2175 #else
2176 #error ORIENTATION_EVENTS must be defined.
2177 // Or a copy of the orientation value will have to be stored in these objects.
2178 #endif
2179 }
2180
2181 double WebPagePrivate::currentZoomFactor() const
2182 {
2183     return currentScale();
2184 }
2185
2186 int WebPagePrivate::showAlertDialog(WebPageClient::AlertType atype)
2187 {
2188     return m_client->showAlertDialog(atype);
2189 }
2190
2191 bool WebPagePrivate::isActive() const
2192 {
2193     return m_client->isActive();
2194 }
2195
2196 void WebPagePrivate::authenticationChallenge(const KURL& url, const ProtectionSpace& protectionSpace, const Credential& inputCredential)
2197 {
2198     AuthenticationChallengeManager* authmgr = AuthenticationChallengeManager::instance();
2199     BlackBerry::Platform::String username;
2200     BlackBerry::Platform::String password;
2201
2202 #if !defined(PUBLIC_BUILD) || !PUBLIC_BUILD
2203     if (m_dumpRenderTree) {
2204         Credential credential(inputCredential, inputCredential.persistence());
2205         if (m_dumpRenderTree->didReceiveAuthenticationChallenge(credential))
2206             authmgr->notifyChallengeResult(url, protectionSpace, AuthenticationChallengeSuccess, credential);
2207         else
2208             authmgr->notifyChallengeResult(url, protectionSpace, AuthenticationChallengeCancelled, inputCredential);
2209         return;
2210     }
2211 #endif
2212
2213 #if ENABLE(BLACKBERRY_CREDENTIAL_PERSIST)
2214     if (m_webSettings->isCredentialAutofillEnabled() && !m_webSettings->isPrivateBrowsingEnabled())
2215         credentialManager().autofillAuthenticationChallenge(protectionSpace, username, password);
2216 #endif
2217
2218     bool isConfirmed = m_client->authenticationChallenge(protectionSpace.realm().characters(), protectionSpace.realm().length(), username, password);
2219
2220 #if ENABLE(BLACKBERRY_CREDENTIAL_PERSIST)
2221     Credential credential(username, password, CredentialPersistencePermanent);
2222     if (m_webSettings->isCredentialAutofillEnabled() && !m_webSettings->isPrivateBrowsingEnabled() && isConfirmed)
2223         credentialManager().saveCredentialIfConfirmed(this, CredentialTransformData(url, protectionSpace, credential));
2224 #else
2225     Credential credential(username, password, CredentialPersistenceNone);
2226 #endif
2227
2228     if (isConfirmed)
2229         authmgr->notifyChallengeResult(url, protectionSpace, AuthenticationChallengeSuccess, credential);
2230     else
2231         authmgr->notifyChallengeResult(url, protectionSpace, AuthenticationChallengeCancelled, inputCredential);
2232 }
2233
2234 PageClientBlackBerry::SaveCredentialType WebPagePrivate::notifyShouldSaveCredential(bool isNew)
2235 {
2236     return static_cast<PageClientBlackBerry::SaveCredentialType>(m_client->notifyShouldSaveCredential(isNew));
2237 }
2238
2239 void WebPagePrivate::syncProxyCredential(const WebCore::Credential& credential)
2240 {
2241     m_client->syncProxyCredential(credential.user(), credential.password());
2242 }
2243
2244 void WebPagePrivate::notifyPopupAutofillDialog(const Vector<String>& candidates)
2245 {
2246     vector<BlackBerry::Platform::String> textItems;
2247     for (size_t i = 0; i < candidates.size(); i++)
2248         textItems.push_back(candidates[i]);
2249     m_client->notifyPopupAutofillDialog(textItems);
2250 }
2251
2252 void WebPagePrivate::notifyDismissAutofillDialog()
2253 {
2254     m_client->notifyDismissAutofillDialog();
2255 }
2256
2257 bool WebPagePrivate::useFixedLayout() const
2258 {
2259     return true;
2260 }
2261
2262 Platform::WebContext WebPagePrivate::webContext(TargetDetectionStrategy strategy)
2263 {
2264     Platform::WebContext context;
2265
2266     RefPtr<Node> node = contextNode(strategy);
2267     m_currentContextNode = node;
2268     if (!m_currentContextNode)
2269         return context;
2270
2271     // Send an onContextMenu event to the current context ndoe and get the result. Since we've already figured out
2272     // which node we want, we can send it directly to the node and not do a hit test. The onContextMenu event doesn't require
2273     // mouse positions so we just set the position at (0,0)
2274     PlatformMouseEvent mouseEvent(IntPoint(), IntPoint(), PlatformEvent::MouseMoved, 0, NoButton, false, false, false, TouchScreen);
2275     if (m_currentContextNode->dispatchMouseEvent(mouseEvent, eventNames().contextmenuEvent, 0)) {
2276         context.setFlag(Platform::WebContext::IsOnContextMenuPrevented);
2277         return context;
2278     }
2279
2280     requestLayoutIfNeeded();
2281
2282     bool nodeAllowSelectionOverride = false;
2283     if (Node* linkNode = node->enclosingLinkEventParentOrSelf()) {
2284         KURL href;
2285         if (linkNode->isLink() && linkNode->hasAttributes()) {
2286             if (Attribute* attribute = static_cast<Element*>(linkNode)->getAttributeItem(HTMLNames::hrefAttr))
2287                 href = linkNode->document()->completeURL(stripLeadingAndTrailingHTMLSpaces(attribute->value()));
2288         }
2289
2290         String pattern = findPatternStringForUrl(href);
2291         if (!pattern.isEmpty())
2292             context.setPattern(pattern);
2293
2294         if (!href.string().isEmpty()) {
2295             context.setUrl(href.string());
2296
2297             // Links are non-selectable by default, but selection should be allowed
2298             // providing the page is selectable, use the parent to determine it.
2299             if (linkNode->parentNode() && linkNode->parentNode()->canStartSelection())
2300                 nodeAllowSelectionOverride = true;
2301         }
2302     }
2303
2304     if (node->isHTMLElement()) {
2305         HTMLImageElement* imageElement = 0;
2306         HTMLMediaElement* mediaElement = 0;
2307
2308         if (node->hasTagName(HTMLNames::imgTag))
2309             imageElement = static_cast<HTMLImageElement*>(node.get());
2310         else if (node->hasTagName(HTMLNames::areaTag))
2311             imageElement = static_cast<HTMLAreaElement*>(node.get())->imageElement();
2312
2313         if (static_cast<HTMLElement*>(node.get())->isMediaElement())
2314             mediaElement = static_cast<HTMLMediaElement*>(node.get());
2315
2316         if (imageElement && imageElement->renderer()) {
2317             context.setFlag(Platform::WebContext::IsImage);
2318             // FIXME: At the mean time, we only show "Save Image" when the image data is available.
2319             if (CachedResource* cachedResource = imageElement->cachedImage()) {
2320                 if (cachedResource->isLoaded() && cachedResource->data()) {
2321                     String url = stripLeadingAndTrailingHTMLSpaces(imageElement->getAttribute(HTMLNames::srcAttr).string());
2322                     context.setSrc(node->document()->completeURL(url).string());
2323                 }
2324             }
2325             String alt = imageElement->altText();
2326             if (!alt.isNull())
2327                 context.setAlt(alt);
2328         }
2329
2330         if (mediaElement) {
2331             if (mediaElement->hasAudio())
2332                 context.setFlag(Platform::WebContext::IsAudio);
2333             if (mediaElement->hasVideo())
2334                 context.setFlag(Platform::WebContext::IsVideo);
2335
2336             String src = stripLeadingAndTrailingHTMLSpaces(mediaElement->getAttribute(HTMLNames::srcAttr).string());
2337             context.setSrc(node->document()->completeURL(src).string());
2338         }
2339     }
2340
2341     if (node->isTextNode()) {
2342         Text* curText = toText(node.get());
2343         if (!curText->wholeText().isEmpty())
2344             context.setText(curText->wholeText());
2345     }
2346
2347     bool canStartSelection = node->canStartSelection();
2348
2349     if (node->isElementNode()) {
2350         Element* element = static_cast<Element*>(node->deprecatedShadowAncestorNode());
2351
2352         if (DOMSupport::isTextBasedContentEditableElement(element)) {
2353             if (!canStartSelection) {
2354                 // Input fields host node is by spec non-editable unless the field itself has content editable enabled.
2355                 // Enable selection if the shadow tree for the input field is selectable.
2356                 Node* nodeUnderFinger = m_touchEventHandler->lastFatFingersResult().isValid() ? m_touchEventHandler->lastFatFingersResult().node(FatFingersResult::ShadowContentAllowed) : 0;
2357                 if (nodeUnderFinger)
2358                     canStartSelection = nodeUnderFinger->canStartSelection();
2359             }
2360             context.setFlag(Platform::WebContext::IsInput);
2361             if (element->hasTagName(HTMLNames::inputTag))
2362                 context.setFlag(Platform::WebContext::IsSingleLine);
2363             if (DOMSupport::isPasswordElement(element))
2364                 context.setFlag(Platform::WebContext::IsPassword);
2365
2366             String elementText(DOMSupport::inputElementText(element));
2367             if (!elementText.stripWhiteSpace().isEmpty())
2368                 context.setText(elementText);
2369             else if (!node->focused() && m_touchEventHandler->lastFatFingersResult().isValid() && strategy == RectBased) {
2370                 // If an input field is empty and not focused send a mouse click so that it gets a cursor and we can paste into it.
2371                 m_touchEventHandler->sendClickAtFatFingersPoint();
2372             }
2373         }
2374     }
2375
2376     if (!nodeAllowSelectionOverride && !canStartSelection)
2377         context.resetFlag(Platform::WebContext::IsSelectable);
2378
2379     if (node->isFocusable())
2380         context.setFlag(Platform::WebContext::IsFocusable);
2381
2382     // Walk up the node tree looking for our custom webworks context attribute.
2383     while (node) {
2384         if (node->isElementNode()) {
2385             Element* element = static_cast<Element*>(node->deprecatedShadowAncestorNode());
2386             String webWorksContext(DOMSupport::webWorksContext(element));
2387             if (!webWorksContext.stripWhiteSpace().isEmpty()) {
2388                 context.setFlag(Platform::WebContext::IsWebWorksContext);
2389                 context.setWebWorksContext(webWorksContext);
2390                 break;
2391             }
2392         }
2393         node = node->parentNode();
2394     }
2395
2396     return context;
2397 }
2398
2399 Platform::WebContext WebPage::webContext(TargetDetectionStrategy strategy) const
2400 {
2401     return d->webContext(strategy);
2402 }
2403
2404 void WebPagePrivate::updateCursor()
2405 {
2406     int buttonMask = 0;
2407     if (m_lastMouseEvent.button() == LeftButton)
2408         buttonMask = Platform::MouseEvent::ScreenLeftMouseButton;
2409     else if (m_lastMouseEvent.button() == MiddleButton)
2410         buttonMask = Platform::MouseEvent::ScreenMiddleMouseButton;
2411     else if (m_lastMouseEvent.button() == RightButton)
2412         buttonMask = Platform::MouseEvent::ScreenRightMouseButton;
2413
2414     unsigned modifiers = m_lastMouseEvent.shiftKey() ? 0 : KEYMOD_SHIFT |
2415         m_lastMouseEvent.ctrlKey() ? 0 : KEYMOD_CTRL |
2416         m_lastMouseEvent.altKey() ? 0 : KEYMOD_ALT;
2417
2418     BlackBerry::Platform::MouseEvent event(buttonMask, buttonMask, mapToTransformed(m_lastMouseEvent.position()), mapToTransformed(m_lastMouseEvent.globalPosition()), 0, modifiers,  0);
2419     m_webPage->mouseEvent(event);
2420 }
2421
2422 IntSize WebPagePrivate::fixedLayoutSize(bool snapToIncrement) const
2423 {
2424     if (hasVirtualViewport())
2425         return m_virtualViewportSize;
2426
2427     const int defaultLayoutWidth = m_defaultLayoutSize.width();
2428     const int defaultLayoutHeight = m_defaultLayoutSize.height();
2429
2430     int minWidth = defaultLayoutWidth;
2431     int maxWidth = DEFAULT_MAX_LAYOUT_WIDTH;
2432     int maxHeight = DEFAULT_MAX_LAYOUT_HEIGHT;
2433
2434     // If the load state is none then we haven't actually got anything yet, but we need to layout
2435     // the entire page so that the user sees the entire page (unrendered) instead of just part of it.
2436     if (m_loadState == None)
2437         return IntSize(defaultLayoutWidth, defaultLayoutHeight);
2438
2439     if (m_viewMode == FixedDesktop) {
2440         int width  = maxWidth;
2441         // if the defaultLayoutHeight is at minimum, it probably was set as 0
2442         // and clamped, meaning it's effectively not set.  (Even if it happened
2443         // to be set exactly to the minimum, it's too small to be useful.)  So
2444         // ignore it.
2445         int height;
2446         if (defaultLayoutHeight <= minimumLayoutSize.height())
2447             height = maxHeight;
2448         else
2449             height = ceilf(static_cast<float>(width) / static_cast<float>(defaultLayoutWidth) * static_cast<float>(defaultLayoutHeight));
2450         return IntSize(width, height);
2451     }
2452
2453     if (m_viewMode == Desktop) {
2454         // If we detect an overflow larger than the contents size then use that instead since
2455         // it'll still be clamped by the maxWidth below...
2456         int width = std::max(absoluteVisibleOverflowSize().width(), contentsSize().width());
2457         if (m_pendingOrientation != -1 && !m_nestedLayoutFinishedCount && !m_overflowExceedsContentsSize)
2458             width = 0;
2459
2460         if (snapToIncrement) {
2461             // Snap to increments of defaultLayoutWidth / 2.0.
2462             float factor = static_cast<float>(width) / (defaultLayoutWidth / 2.0);
2463             factor = ceilf(factor);
2464             width = (defaultLayoutWidth / 2.0) * factor;
2465         }
2466
2467         if (width < minWidth)
2468             width = minWidth;
2469         if (width > maxWidth)
2470             width = maxWidth;
2471         int height = ceilf(static_cast<float>(width) / static_cast<float>(defaultLayoutWidth) * static_cast<float>(defaultLayoutHeight));
2472         return IntSize(width, height);
2473     }
2474
2475     ASSERT_NOT_REACHED();
2476     return IntSize(defaultLayoutWidth, defaultLayoutHeight);
2477 }
2478
2479 BackingStoreClient* WebPagePrivate::backingStoreClient() const
2480 {
2481     return m_backingStoreClient;
2482 }
2483
2484 void WebPagePrivate::clearDocumentData(const Document* documentGoingAway)
2485 {
2486     ASSERT(documentGoingAway);
2487     if (m_currentContextNode && m_currentContextNode->document() == documentGoingAway)
2488         m_currentContextNode = 0;
2489
2490     if (m_currentPinchZoomNode && m_currentPinchZoomNode->document() == documentGoingAway)
2491         m_currentPinchZoomNode = 0;
2492
2493     if (m_currentBlockZoomAdjustedNode && m_currentBlockZoomAdjustedNode->document() == documentGoingAway)
2494         m_currentBlockZoomAdjustedNode = 0;
2495
2496     if (m_inRegionScroller->d->isActive())
2497         m_inRegionScroller->d->clearDocumentData(documentGoingAway);
2498
2499     if (documentGoingAway->frame())
2500         m_inputHandler->frameUnloaded(documentGoingAway->frame());
2501
2502     Node* nodeUnderFatFinger = m_touchEventHandler->lastFatFingersResult().node();
2503     if (nodeUnderFatFinger && nodeUnderFatFinger->document() == documentGoingAway)
2504         m_touchEventHandler->resetLastFatFingersResult();
2505
2506     // NOTE: m_fullscreenNode, m_fullScreenPluginView and m_pluginViews
2507     // are cleared in other methods already.
2508 }
2509
2510 typedef bool (*PredicateFunction)(RenderLayer*);
2511 static bool isPositionedContainer(RenderLayer* layer)
2512 {
2513     RenderObject* o = layer->renderer();
2514     return o->isRenderView() || o->isOutOfFlowPositioned() || o->isRelPositioned() || layer->hasTransform();
2515 }
2516
2517 static bool isFixedPositionedContainer(RenderLayer* layer)
2518 {
2519     RenderObject* o = layer->renderer();
2520     return o->isRenderView() || (o->isOutOfFlowPositioned() && o->style()->position() == FixedPosition);
2521 }
2522
2523 static RenderLayer* findAncestorOrSelfNotMatching(PredicateFunction predicate, RenderLayer* layer)
2524 {
2525     RenderLayer* curr = layer;
2526     while (curr && !predicate(curr))
2527         curr = curr->parent();
2528
2529     return curr;
2530 }
2531
2532 RenderLayer* WebPagePrivate::enclosingFixedPositionedAncestorOrSelfIfFixedPositioned(RenderLayer* layer)
2533 {
2534     return findAncestorOrSelfNotMatching(&isFixedPositionedContainer, layer);
2535 }
2536
2537 RenderLayer* WebPagePrivate::enclosingPositionedAncestorOrSelfIfPositioned(RenderLayer* layer)
2538 {
2539     return findAncestorOrSelfNotMatching(&isPositionedContainer, layer);
2540 }
2541
2542 static inline Frame* frameForNode(Node* node)
2543 {
2544     Node* origNode = node;
2545     for (; node; node = node->parentNode()) {
2546         if (RenderObject* renderer = node->renderer()) {
2547             if (renderer->isRenderView()) {
2548                 if (FrameView* view = toRenderView(renderer)->frameView()) {
2549                     if (Frame* frame = view->frame())
2550                         return frame;
2551                 }
2552             }
2553             if (renderer->isWidget()) {
2554                 Widget* widget = toRenderWidget(renderer)->widget();
2555                 if (widget && widget->isFrameView()) {
2556                     if (Frame* frame = static_cast<FrameView*>(widget)->frame())
2557                         return frame;
2558                 }
2559             }
2560         }
2561     }
2562
2563     for (node = origNode; node; node = node->parentNode()) {
2564         if (Document* doc = node->document()) {
2565             if (Frame* frame = doc->frame())
2566                 return frame;
2567         }
2568     }
2569
2570     return 0;
2571 }
2572
2573 static IntRect getNodeWindowRect(Node* node)
2574 {
2575     if (Frame* frame = frameForNode(node)) {
2576         if (FrameView* view = frame->view())
2577             return view->contentsToWindow(node->getRect());
2578     }
2579     ASSERT_NOT_REACHED();
2580     return IntRect();
2581 }
2582
2583 IntRect WebPagePrivate::getRecursiveVisibleWindowRect(ScrollView* view, bool noClipOfMainFrame)
2584 {
2585     ASSERT(m_mainFrame);
2586
2587     // Don't call this function asking to not clip the main frame providing only
2588     // the main frame. All that can be returned is the content rect which
2589     // isn't what this function is for.
2590     if (noClipOfMainFrame && view == m_mainFrame->view()) {
2591         ASSERT_NOT_REACHED();
2592         return IntRect(IntPoint::zero(), view->contentsSize());
2593     }
2594
2595     IntRect visibleWindowRect(view->contentsToWindow(view->visibleContentRect(false)));
2596     if (view->parent() && !(noClipOfMainFrame && view->parent() == m_mainFrame->view())) {
2597         // Intersect with parent visible rect.
2598         visibleWindowRect.intersect(getRecursiveVisibleWindowRect(view->parent(), noClipOfMainFrame));
2599     }
2600     return visibleWindowRect;
2601 }
2602
2603 void WebPagePrivate::assignFocus(Platform::FocusDirection direction)
2604 {
2605     ASSERT((int) Platform::FocusDirectionNone == (int) FocusDirectionNone);
2606     ASSERT((int) Platform::FocusDirectionForward == (int) FocusDirectionForward);
2607     ASSERT((int) Platform::FocusDirectionBackward == (int) FocusDirectionBackward);
2608
2609     // First we clear the focus, since we want to focus either initial or the last
2610     // focusable element in the webpage (according to the TABINDEX), or simply clear
2611     // the focus.
2612     clearFocusNode();
2613
2614     switch (direction) {
2615     case FocusDirectionForward:
2616     case FocusDirectionBackward:
2617         m_page->focusController()->setInitialFocus((FocusDirection) direction, 0);
2618         break;
2619     case FocusDirectionNone:
2620         break;
2621     default:
2622         ASSERT_NOT_REACHED();
2623     }
2624 }
2625
2626 void WebPage::assignFocus(Platform::FocusDirection direction)
2627 {
2628     if (d->m_page->defersLoading())
2629        return;
2630     d->assignFocus(direction);
2631 }
2632
2633 Platform::IntRect WebPagePrivate::focusNodeRect()
2634 {
2635     Frame* frame = focusedOrMainFrame();
2636     if (!frame)
2637         return Platform::IntRect();
2638
2639     Document* doc = frame->document();
2640     FrameView* view = frame->view();
2641     if (!doc || !view || view->needsLayout())
2642         return Platform::IntRect();
2643
2644     IntRect focusRect = rectForNode(doc->focusedNode());
2645     focusRect = adjustRectOffsetForFrameOffset(focusRect, doc->focusedNode());
2646     focusRect = mapToTransformed(focusRect);
2647     clipToTransformedContentsRect(focusRect);
2648     return focusRect;
2649 }
2650
2651 PassRefPtr<Node> WebPagePrivate::contextNode(TargetDetectionStrategy strategy)
2652 {
2653     EventHandler* eventHandler = focusedOrMainFrame()->eventHandler();
2654     const FatFingersResult lastFatFingersResult = m_touchEventHandler->lastFatFingersResult();
2655     bool isTouching = lastFatFingersResult.isValid() && strategy == RectBased;
2656
2657     // Check if we're using LinkToLink and the user is not touching the screen.
2658     if (m_webSettings->doesGetFocusNodeContext() && !isTouching) {
2659         RefPtr<Node> node;
2660         node = m_page->focusController()->focusedOrMainFrame()->document()->focusedNode();
2661         if (node) {
2662             IntRect visibleRect = IntRect(IntPoint(), actualVisibleSize());
2663             if (!visibleRect.intersects(getNodeWindowRect(node.get())))
2664                 return 0;
2665         }
2666         return node.release();
2667     }
2668
2669     // Check for text input.
2670     if (isTouching && lastFatFingersResult.isTextInput())
2671         return lastFatFingersResult.node(FatFingersResult::ShadowContentNotAllowed);
2672
2673     if (strategy == RectBased) {
2674         FatFingersResult result = FatFingers(this, lastFatFingersResult.adjustedPosition(), FatFingers::Text).findBestPoint();
2675         return result.node(FatFingersResult::ShadowContentNotAllowed);
2676     }
2677     if (strategy == FocusBased)
2678         return m_inputHandler->currentFocusElement();
2679
2680     IntPoint contentPos;
2681     if (isTouching)
2682         contentPos = lastFatFingersResult.adjustedPosition();
2683     else
2684         contentPos = mapFromViewportToContents(m_lastMouseEvent.position());
2685
2686     HitTestResult result = eventHandler->hitTestResultAtPoint(contentPos, false /*allowShadowContent*/);
2687     return result.innerNode();
2688 }
2689
2690 static inline int distanceBetweenPoints(IntPoint p1, IntPoint p2)
2691 {
2692     // Change int to double, because (dy * dy) can cause int overflow in reality, e.g, (-46709 * -46709).
2693     double dx = static_cast<double>(p1.x() - p2.x());
2694     double dy = static_cast<double>(p1.y() - p2.y());
2695     return sqrt((dx * dx) + (dy * dy));
2696 }
2697
2698 Node* WebPagePrivate::bestNodeForZoomUnderPoint(const IntPoint& documentPoint)
2699 {
2700     IntRect clickRect(documentPoint.x() - blockClickRadius, documentPoint.y() - blockClickRadius, 2 * blockClickRadius, 2 * blockClickRadius);
2701     Node* originalNode = nodeForZoomUnderPoint(documentPoint);
2702     if (!originalNode)
2703         return 0;
2704     Node* node = bestChildNodeForClickRect(originalNode, clickRect);
2705     return node ? adjustedBlockZoomNodeForZoomAndExpandingRatioLimits(node) : adjustedBlockZoomNodeForZoomAndExpandingRatioLimits(originalNode);
2706 }
2707
2708 Node* WebPagePrivate::bestChildNodeForClickRect(Node* parentNode, const IntRect& clickRect)
2709 {
2710     if (!parentNode)
2711         return 0;
2712
2713     int bestDistance = std::numeric_limits<int>::max();
2714
2715     Node* node = parentNode->firstChild();
2716     Node* bestNode = 0;
2717     for (; node; node = node->nextSibling()) {
2718         IntRect rect = rectForNode(node);
2719         if (!clickRect.intersects(rect))
2720             continue;
2721
2722         int distance = distanceBetweenPoints(rect.center(), clickRect.center());
2723         Node* bestChildNode = bestChildNodeForClickRect(node, clickRect);
2724         if (bestChildNode) {
2725             IntRect bestChildRect = rectForNode(bestChildNode);
2726             int bestChildDistance = distanceBetweenPoints(bestChildRect.center(), clickRect.center());
2727             if (bestChildDistance < distance && bestChildDistance < bestDistance) {
2728                 bestNode = bestChildNode;
2729                 bestDistance = bestChildDistance;
2730             } else {
2731                 if (distance < bestDistance) {
2732                     bestNode = node;
2733                     bestDistance = distance;
2734                 }
2735             }
2736         } else {
2737             if (distance < bestDistance) {
2738                 bestNode = node;
2739                 bestDistance = distance;
2740             }
2741         }
2742     }
2743
2744     return bestNode;
2745 }
2746
2747 double WebPagePrivate::maxBlockZoomScale() const
2748 {
2749     return std::min(maximumBlockZoomScale, maximumScale());
2750 }
2751
2752 Node* WebPagePrivate::nodeForZoomUnderPoint(const IntPoint& documentPoint)
2753 {
2754     if (!m_mainFrame)
2755         return 0;
2756
2757     HitTestResult result = m_mainFrame->eventHandler()->hitTestResultAtPoint(documentPoint, false);
2758
2759     Node* node = result.innerNonSharedNode();
2760
2761     if (!node)
2762         return 0;
2763
2764     RenderObject* renderer = node->renderer();
2765     while (!renderer) {
2766         node = node->parentNode();
2767         renderer = node->renderer();
2768     }
2769
2770     return node;
2771 }
2772
2773 Node* WebPagePrivate::adjustedBlockZoomNodeForZoomAndExpandingRatioLimits(Node* node)
2774 {
2775     Node* initialNode = node;
2776     RenderObject* renderer = node->renderer();
2777     bool acceptableNodeSize = newScaleForBlockZoomRect(rectForNode(node), 1.0, 0) < maxBlockZoomScale();
2778     IntSize actualVisibleSize = this->actualVisibleSize();
2779
2780     while (!renderer || !acceptableNodeSize) {
2781         node = node->parentNode();
2782         IntRect nodeRect = rectForNode(node);
2783
2784         // Don't choose a node if the width of the node size is very close to the width of the actual visible size,
2785         // as block zoom can do nothing on such kind of node.
2786         if (!node || static_cast<double>(actualVisibleSize.width() - nodeRect.width()) / actualVisibleSize.width() < minimumExpandingRatio)
2787             return initialNode;
2788
2789         renderer = node->renderer();
2790         acceptableNodeSize = newScaleForBlockZoomRect(rectForNode(node), 1.0, 0) < maxBlockZoomScale();
2791     }
2792
2793     return node;
2794 }
2795
2796 bool WebPagePrivate::compareNodesForBlockZoom(Node* n1, Node* n2)
2797 {
2798     if (!n1 || !n2)
2799         return false;
2800
2801     return (n2 == n1) || n2->isDescendantOf(n1);
2802 }
2803
2804 double WebPagePrivate::newScaleForBlockZoomRect(const IntRect& rect, double oldScale, double margin)
2805 {
2806     if (rect.isEmpty())
2807         return std::numeric_limits<double>::max();
2808
2809     ASSERT(rect.width() + margin);
2810
2811     double newScale = oldScale * static_cast<double>(transformedActualVisibleSize().width()) / (rect.width() + margin);
2812
2813     return newScale;
2814 }
2815
2816 IntRect WebPagePrivate::rectForNode(Node* node)
2817 {
2818     if (!node)
2819         return IntRect();
2820
2821     RenderObject* renderer = node->renderer();
2822
2823     if (!renderer)
2824         return IntRect();
2825
2826     // Return rect in un-transformed content coordinates.
2827     IntRect blockRect;
2828
2829     // FIXME: Ensure this works with iframes.
2830     if (m_webPage->settings()->textReflowMode() == WebSettings::TextReflowEnabled && renderer->isText()) {
2831         RenderBlock* renderBlock = renderer->containingBlock();
2832         int xOffset = 0;
2833         int yOffset = 0;
2834         while (!renderBlock->isRoot()) {
2835             xOffset += renderBlock->x();
2836             yOffset += renderBlock->y();
2837             renderBlock = renderBlock->containingBlock();
2838         }
2839         const RenderText* renderText = toRenderText(renderer);
2840         IntRect linesBox = renderText->linesBoundingBox();
2841         blockRect = IntRect(xOffset + linesBox.x(), yOffset + linesBox.y(), linesBox.width(), linesBox.height());
2842     } else
2843         blockRect = renderer->absoluteClippedOverflowRect();
2844
2845     if (renderer->isText()) {
2846         RenderBlock* rb = renderer->containingBlock();
2847
2848         // Inefficient? Way to find width when floats intersect a block.
2849         int blockWidth = 0;
2850         int lineCount = rb->lineCount();
2851         for (int i = 0; i < lineCount; i++)
2852             blockWidth = max(blockWidth, rb->availableLogicalWidthForLine(i, false));
2853
2854         blockRect.setWidth(blockWidth);
2855         blockRect.setX(blockRect.x() + rb->logicalLeftOffsetForLine(1, false));
2856     }
2857
2858     // Strip off padding.
2859     if (renderer->style()->hasPadding()) {
2860         blockRect.setX(blockRect.x() + renderer->style()->paddingLeft().value());
2861         blockRect.setY(blockRect.y() + renderer->style()->paddingTop().value());
2862         blockRect.setWidth(blockRect.width() - renderer->style()->paddingRight().value());
2863         blockRect.setHeight(blockRect.height() - renderer->style()->paddingBottom().value());
2864     }
2865
2866     return blockRect;
2867 }
2868
2869 IntPoint WebPagePrivate::frameOffset(const Frame* frame) const
2870 {
2871     ASSERT(frame);
2872
2873     // FIXME: This function can be called when page is being destroyed and JS triggers selection change.
2874     // We could break the call chain at upper levels, but I think it is better to check the frame pointer
2875     // here because the pointer is explicitly cleared in WebPage::destroy().
2876     if (!mainFrame())
2877         return IntPoint();
2878
2879     // Convert 0,0 in the frame's coordinate system to window coordinates to
2880     // get the frame's global position, and return this position in the main
2881     // frame's coordinates.  (So the main frame's coordinates will be 0,0.)
2882     return mainFrame()->view()->windowToContents(frame->view()->contentsToWindow(IntPoint::zero()));
2883 }
2884
2885 IntRect WebPagePrivate::adjustRectOffsetForFrameOffset(const IntRect& rect, const Node* node)
2886 {
2887     if (!node)
2888         return rect;
2889
2890     // Adjust the offset of the rect if it is in an iFrame/frame or set of iFrames/frames.
2891     // FIXME: can we just use frameOffset instead of this big routine?
2892     const Node* tnode = node;
2893     IntRect adjustedRect = rect;
2894     do {
2895         Frame* frame = tnode->document()->frame();
2896         if (!frame)
2897             continue;
2898
2899         Node* ownerNode = static_cast<Node*>(frame->ownerElement());
2900         tnode = ownerNode;
2901         if (ownerNode && (ownerNode->hasTagName(HTMLNames::iframeTag) || ownerNode->hasTagName(HTMLNames::frameTag))) {
2902             IntRect iFrameRect;
2903             do {
2904                 iFrameRect = rectForNode(ownerNode);
2905                 adjustedRect.move(iFrameRect.x(), iFrameRect.y());
2906                 adjustedRect.intersect(iFrameRect);
2907                 ownerNode = ownerNode->parentNode();
2908             } while (iFrameRect.isEmpty() && ownerNode);
2909         } else
2910             break;
2911     } while (tnode = tnode->parentNode());
2912
2913     return adjustedRect;
2914 }
2915
2916 IntRect WebPagePrivate::blockZoomRectForNode(Node* node)
2917 {
2918     if (!node || contentsSize().isEmpty())
2919         return IntRect();
2920
2921     Node* tnode = node;
2922     m_currentBlockZoomAdjustedNode = tnode;
2923
2924     IntRect blockRect = rectForNode(tnode);
2925     IntRect originalRect = blockRect;
2926
2927     int originalArea = originalRect.width() * originalRect.height();
2928     int pageArea = contentsSize().width() * contentsSize().height();
2929     double blockToPageRatio = static_cast<double>(pageArea - originalArea) / pageArea;
2930     double blockExpansionRatio = 5.0 * blockToPageRatio * blockToPageRatio;
2931
2932     if (!tnode->hasTagName(HTMLNames::imgTag) && !tnode->hasTagName(HTMLNames::inputTag) && !tnode->hasTagName(HTMLNames::textareaTag)) {
2933         while (tnode = tnode->parentNode()) {
2934             ASSERT(tnode);
2935             IntRect tRect = rectForNode(tnode);
2936             int tempBlockArea = tRect.width() * tRect.height();
2937             // Don't expand the block if it will be too large relative to the content.
2938             if (static_cast<double>(pageArea - tempBlockArea) / pageArea < minimumExpandingRatio)
2939                 break;
2940             if (tRect.isEmpty())
2941                 continue; // No renderer.
2942             if (tempBlockArea < 1.1 * originalArea)
2943                 continue; // The size of this parent is very close to the child, no need to go to this parent.
2944             // Don't expand the block if the parent node size is already almost the size of actual visible size.
2945             IntSize actualSize = actualVisibleSize();
2946             if (static_cast<double>(actualSize.width() - tRect.width()) / actualSize.width() < minimumExpandingRatio)
2947                 break;
2948             if (tempBlockArea < blockExpansionRatio * originalArea) {
2949                 blockRect = tRect;
2950                 m_currentBlockZoomAdjustedNode = tnode;
2951             } else
2952                 break;
2953         }
2954     }
2955
2956     blockRect = adjustRectOffsetForFrameOffset(blockRect, node);
2957     blockRect = mapToTransformed(blockRect);
2958     clipToTransformedContentsRect(blockRect);
2959
2960     return blockRect;
2961 }
2962
2963 // This function should not be called directly.
2964 // It is called after the animation ends (see above).
2965 void WebPagePrivate::zoomBlock()
2966 {
2967     if (!m_mainFrame)
2968         return;
2969
2970     IntPoint anchor(roundUntransformedPoint(m_finalBlockPoint));
2971     bool willUseTextReflow = false;
2972
2973 #if ENABLE(VIEWPORT_REFLOW)
2974     willUseTextReflow = m_webPage->settings()->textReflowMode() != WebSettings::TextReflowDisabled;
2975     toggleTextReflowIfEnabledForBlockZoomOnly(m_shouldReflowBlock);
2976     setNeedsLayout();
2977 #endif
2978
2979     TransformationMatrix zoom;
2980     zoom.scale(m_blockZoomFinalScale);
2981     *m_transformationMatrix = zoom;
2982     // FIXME: Do we really need to suspend/resume both backingstore and screen here?
2983     m_backingStore->d->suspendBackingStoreUpdates();
2984     m_backingStore->d->suspendScreenUpdates();
2985     updateViewportSize();
2986
2987     FrameView* mainFrameView = m_mainFrame->view();
2988     bool constrainsScrollingToContentEdge = true;
2989     if (mainFrameView) {
2990         constrainsScrollingToContentEdge = mainFrameView->constrainsScrollingToContentEdge();
2991         mainFrameView->setConstrainsScrollingToContentEdge(m_shouldConstrainScrollingToContentEdge);
2992     }
2993
2994 #if ENABLE(VIEWPORT_REFLOW)
2995     requestLayoutIfNeeded();
2996     if (willUseTextReflow && m_shouldReflowBlock) {
2997         IntRect reflowedRect = rectForNode(m_currentBlockZoomAdjustedNode.get());
2998         reflowedRect = adjustRectOffsetForFrameOffset(reflowedRect, m_currentBlockZoomAdjustedNode.get());
2999         reflowedRect.move(roundTransformedPoint(m_finalBlockPointReflowOffset).x(), roundTransformedPoint(m_finalBlockPointReflowOffset).y());
3000         RenderObject* renderer = m_currentBlockZoomAdjustedNode->renderer();
3001         IntPoint topLeftPoint(reflowedRect.location());
3002         if (renderer && renderer->isText()) {
3003             ETextAlign textAlign = renderer->style()->textAlign();
3004             IntPoint textAnchor;
3005             switch (textAlign) {
3006             case CENTER:
3007             case WEBKIT_CENTER:
3008                 textAnchor = IntPoint(reflowedRect.x() + (reflowedRect.width() - actualVisibleSize().width()) / 2, topLeftPoint.y());
3009                 break;
3010             case LEFT:
3011             case WEBKIT_LEFT:
3012                 textAnchor = topLeftPoint;
3013                 break;
3014             case RIGHT:
3015             case WEBKIT_RIGHT:
3016                 textAnchor = IntPoint(reflowedRect.x() + reflowedRect.width() - actualVisibleSize().width(), topLeftPoint.y());
3017                 break;
3018             case TAAUTO:
3019             case JUSTIFY:
3020             default:
3021                 if (renderer->style()->isLeftToRightDirection())
3022                     textAnchor = topLeftPoint;
3023                 else
3024                     textAnchor = IntPoint(reflowedRect.x() + reflowedRect.width() - actualVisibleSize().width(), topLeftPoint.y());
3025                 break;
3026             }
3027             setScrollPosition(textAnchor);
3028         } else {
3029             renderer->style()->isLeftToRightDirection()
3030                 ? setScrollPosition(topLeftPoint)
3031                 : setScrollPosition(IntPoint(reflowedRect.x() + reflowedRect.width() - actualVisibleSize().width(), topLeftPoint.y()));
3032         }
3033     } else if (willUseTextReflow) {
3034         IntRect finalRect = rectForNode(m_currentBlockZoomAdjustedNode.get());
3035         finalRect = adjustRectOffsetForFrameOffset(finalRect, m_currentBlockZoomAdjustedNode.get());
3036         setScrollPosition(IntPoint(0, finalRect.y() + m_finalBlockPointReflowOffset.y()));
3037         resetBlockZoom();
3038     }
3039 #endif
3040     if (!willUseTextReflow) {
3041         setScrollPosition(anchor);
3042         if (!m_shouldReflowBlock)
3043             resetBlockZoom();
3044     }
3045
3046     notifyTransformChanged();
3047     m_client->scaleChanged();
3048
3049     if (mainFrameView)
3050         mainFrameView->setConstrainsScrollingToContentEdge(constrainsScrollingToContentEdge);
3051
3052     // FIXME: Do we really need to suspend/resume both backingstore and screen here?
3053     m_backingStore->d->resumeBackingStoreUpdates();
3054     m_backingStore->d->resumeScreenUpdates(BackingStore::RenderAndBlit);
3055 }
3056
3057 void WebPage::blockZoomAnimationFinished()
3058 {
3059     d->zoomBlock();
3060 }
3061
3062 void WebPage::resetBlockZoom()
3063 {
3064     d->resetBlockZoom();
3065 }
3066
3067 void WebPagePrivate::resetBlockZoom()
3068 {
3069     m_currentBlockZoomNode = 0;
3070     m_currentBlockZoomAdjustedNode = 0;
3071     m_shouldReflowBlock = false;
3072     m_shouldConstrainScrollingToContentEdge = true;
3073 }
3074
3075 void WebPage::destroyWebPageCompositor()
3076 {
3077 #if USE(ACCELERATED_COMPOSITING)
3078     // Destroy the layer renderer in a sync command before we destroy the backing store,
3079     // to flush any pending compositing messages on the compositing thread.
3080     // The backing store is indirectly deleted by the 'detachFromParent' call below.
3081     d->syncDestroyCompositorOnCompositingThread();
3082 #endif
3083 }
3084
3085 void WebPage::destroy()
3086 {
3087     // TODO: need to verify if this call needs to be made before calling
3088     // Close the Inspector to resume the JS engine if it's paused.
3089     disableWebInspector();
3090
3091     // WebPage::destroyWebPageCompositor()
3092     // FIXME: Do we really need to suspend/resume both backingstore and screen here?
3093     d->m_backingStore->d->suspendBackingStoreUpdates();
3094     d->m_backingStore->d->suspendScreenUpdates();
3095
3096     // Close the backforward list and release the cached pages.
3097     d->m_page->backForward()->close();
3098     pageCache()->releaseAutoreleasedPagesNow();
3099
3100     FrameLoader* loader = d->m_mainFrame->loader();
3101
3102     // Set m_mainFrame to 0 to avoid calls back in to the backingstore during webpage deletion.
3103     d->m_mainFrame = 0;
3104     if (loader)
3105         loader->detachFromParent();
3106
3107     deleteGuardedObject(this);
3108 }
3109
3110 WebPageClient* WebPage::client() const
3111 {
3112     return d->m_client;
3113 }
3114
3115 int WebPage::backForwardListLength() const
3116 {
3117     return d->m_page->getHistoryLength();
3118 }
3119
3120 bool WebPage::canGoBackOrForward(int delta) const
3121 {
3122     return d->m_page->canGoBackOrForward(delta);
3123 }
3124
3125 bool WebPage::goBackOrForward(int delta)
3126 {
3127     if (d->m_page->canGoBackOrForward(delta)) {
3128         d->m_backingStore->d->suspendScreenUpdates();
3129         d->m_page->goBackOrForward(delta);
3130         d->m_backingStore->d->resumeScreenUpdates(BackingStore::None);
3131         return true;
3132     }
3133     return false;
3134 }
3135
3136 void WebPage::goToBackForwardEntry(BackForwardId id)
3137 {
3138     HistoryItem* item = historyItemFromBackForwardId(id);
3139     ASSERT(item);
3140     d->m_page->goToItem(item, FrameLoadTypeIndexedBackForward);
3141 }
3142
3143 void WebPage::reload()
3144 {
3145     d->m_mainFrame->loader()->reload(/* bypassCache */ true);
3146 }
3147
3148 void WebPage::reloadFromCache()
3149 {
3150     d->m_mainFrame->loader()->reload(/* bypassCache */ false);
3151 }
3152
3153 WebSettings* WebPage::settings() const
3154 {
3155     return d->m_webSettings;
3156 }
3157
3158 WebCookieJar* WebPage::cookieJar() const
3159 {
3160     if (!d->m_cookieJar)
3161         d->m_cookieJar = new WebCookieJar();
3162
3163     return d->m_cookieJar;
3164 }
3165
3166 bool WebPage::isLoading() const
3167 {
3168     return d->isLoading();
3169 }
3170
3171 bool WebPage::isVisible() const
3172 {
3173     return d->m_visible;
3174 }
3175
3176 #if ENABLE(PAGE_VISIBILITY_API)
3177 class DeferredTaskSetPageVisibilityState: public DeferredTask<&WebPagePrivate::m_wouldSetPageVisibilityState> {
3178 public:
3179     explicit DeferredTaskSetPageVisibilityState(WebPagePrivate* webPagePrivate)
3180         : DeferredTaskType(webPagePrivate)
3181     {
3182     }
3183 private:
3184     virtual void performInternal(WebPagePrivate* webPagePrivate)
3185     {
3186         webPagePrivate->setPageVisibilityState();
3187     }
3188 };
3189
3190 void WebPagePrivate::setPageVisibilityState()
3191 {
3192     if (m_page->defersLoading())
3193         m_deferredTasks.append(adoptPtr(new DeferredTaskSetPageVisibilityState(this)));
3194     else {
3195         DeferredTaskSetPageVisibilityState::finishOrCancel(this);
3196
3197         static bool s_initialVisibilityState = true;
3198
3199         m_page->setVisibilityState(m_visible && m_activationState == ActivationActive ? PageVisibilityStateVisible : PageVisibilityStateHidden, s_initialVisibilityState);
3200         s_initialVisibilityState = false;
3201     }
3202 }
3203 #endif
3204
3205 void WebPagePrivate::setVisible(bool visible)
3206 {
3207     if (visible != m_visible) {
3208         if (visible) {
3209             if (m_mainFrame)
3210                 m_mainFrame->animation()->resumeAnimations();
3211             if (m_page->scriptedAnimationsSuspended())
3212                 m_page->resumeScriptedAnimations();
3213         } else {
3214             if (m_mainFrame)
3215                 m_mainFrame->animation()->suspendAnimations();
3216             if (!m_page->scriptedAnimationsSuspended())
3217                 m_page->suspendScriptedAnimations();
3218
3219             if (m_webPage->hasOpenedPopup())
3220                 m_page->chrome()->client()->closePagePopup(0);
3221         }
3222
3223         m_visible = visible;
3224         m_backingStore->d->updateSuspendScreenUpdateState();
3225     }
3226
3227 #if ENABLE(PAGE_VISIBILITY_API)
3228     setPageVisibilityState();
3229 #endif
3230 }
3231
3232 void WebPage::setVisible(bool visible)
3233 {
3234     if (d->m_visible == visible)
3235         return;
3236
3237     d->setVisible(visible);
3238     AuthenticationChallengeManager::instance()->pageVisibilityChanged(d, visible);
3239
3240     if (!visible) {
3241         d->suspendBackingStore();
3242
3243         // Remove this WebPage from the visible pages list.
3244         size_t foundIndex = visibleWebPages()->find(this);
3245         if (foundIndex != WTF::notFound)
3246             visibleWebPages()->remove(foundIndex);
3247
3248         // Return the backing store to the last visible WebPage.
3249         if (BackingStorePrivate::currentBackingStoreOwner() == this && !visibleWebPages()->isEmpty())
3250             visibleWebPages()->last()->d->resumeBackingStore();
3251
3252 #if USE(ACCELERATED_COMPOSITING)
3253         // Root layer commit is not necessary for invisible tabs.
3254         // And release layer resources can reduce memory consumption.
3255         d->suspendRootLayerCommit();
3256 #endif
3257
3258         return;
3259     }
3260
3261 #if USE(ACCELERATED_COMPOSITING)
3262     d->resumeRootLayerCommit();
3263 #endif
3264
3265     // We want to become visible but not get backing store ownership.
3266     if (d->m_backingStore->d->isOpenGLCompositing() && !d->m_webSettings->isBackingStoreEnabled()) {
3267         d->setCompositorDrawsRootLayer(true);
3268 #if USE(ACCELERATED_COMPOSITING)
3269         d->setNeedsOneShotDrawingSynchronization();
3270 #endif
3271         d->setShouldResetTilesWhenShown(true);
3272         return;
3273     }
3274
3275     // Push this WebPage to the top of the visible pages list.
3276     if (!visibleWebPages()->isEmpty() && visibleWebPages()->last() != this) {
3277         size_t foundIndex = visibleWebPages()->find(this);
3278         if (foundIndex != WTF::notFound)
3279             visibleWebPages()->remove(foundIndex);
3280     }
3281     visibleWebPages()->append(this);
3282
3283     if (BackingStorePrivate::currentBackingStoreOwner()
3284         && BackingStorePrivate::currentBackingStoreOwner() != this)
3285         BackingStorePrivate::currentBackingStoreOwner()->d->suspendBackingStore();
3286
3287     // resumeBackingStore will set the current owner to this webpage.
3288     // If we set the owner prematurely, then the tiles will not be reset.
3289     d->resumeBackingStore();
3290 }
3291
3292 void WebPagePrivate::selectionChanged(Frame* frame)
3293 {
3294     m_inputHandler->selectionChanged();
3295
3296     // FIXME: This is a hack!
3297     // To ensure the selection being changed has its frame 'focused', lets
3298     // set it as focused ourselves (PR #104724).
3299     m_page->focusController()->setFocusedFrame(frame);
3300 }
3301
3302 void WebPagePrivate::updateDelegatedOverlays(bool dispatched)
3303 {
3304     // Track a dispatched message, we don't want to flood the webkit thread.
3305     // There can be as many as one more message enqued as needed but never less.
3306     if (dispatched)
3307         m_updateDelegatedOverlaysDispatched = false;
3308     else if (m_updateDelegatedOverlaysDispatched) {
3309         // Early return if there is message already pending on the webkit thread.
3310         return;
3311     }
3312
3313     if (Platform::webKitThreadMessageClient()->isCurrentThread()) {
3314         // Must be called on the WebKit thread.
3315         if (m_selectionHandler->isSelectionActive())
3316             m_selectionHandler->selectionPositionChanged();
3317         if (m_inspectorOverlay)
3318             m_inspectorOverlay->update();
3319
3320     } else if (m_selectionHandler->isSelectionActive()) {
3321         // Don't bother dispatching to webkit thread if selection and tap highlight are not active.
3322         m_updateDelegatedOverlaysDispatched = true;
3323         Platform::webKitThreadMessageClient()->dispatchMessage(Platform::createMethodCallMessage(&WebPagePrivate::updateDelegatedOverlays, this, true /*dispatched*/));
3324     }
3325 }
3326
3327 void WebPage::setCaretHighlightStyle(Platform::CaretHighlightStyle style)
3328 {
3329 }
3330
3331 bool WebPage::setBatchEditingActive(bool active)
3332 {
3333     return d->m_inputHandler->setBatchEditingActive(active);
3334 }
3335
3336 bool WebPage::setInputSelection(unsigned start, unsigned end)
3337 {
3338     if (d->m_page->defersLoading())
3339         return false;
3340     return d->m_inputHandler->setSelection(start, end);
3341 }
3342
3343 int WebPage::inputCaretPosition() const
3344 {
3345     return d->m_inputHandler->caretPosition();
3346 }
3347
3348 class DeferredTaskPopupListSelectMultiple: public DeferredTask<&WebPagePrivate::m_wouldPopupListSelectMultiple> {
3349 public:
3350     DeferredTaskPopupListSelectMultiple(WebPagePrivate* webPagePrivate, int size, const bool* selecteds)
3351         : DeferredTaskType(webPagePrivate)
3352     {
3353         webPagePrivate->m_cachedPopupListSelecteds.append(selecteds, size);
3354     }
3355 private:
3356     virtual void performInternal(WebPagePrivate* webPagePrivate)
3357     {
3358         webPagePrivate->m_webPage->popupListClosed(webPagePrivate->m_cachedPopupListSelecteds.size(), webPagePrivate->m_cachedPopupListSelecteds.data());
3359     }
3360 };
3361
3362 class DeferredTaskPopupListSelectSingle: public DeferredTask<&WebPagePrivate::m_wouldPopupListSelectSingle> {
3363 public:
3364     explicit DeferredTaskPopupListSelectSingle(WebPagePrivate* webPagePrivate, int index)
3365         : DeferredTaskType(webPagePrivate)
3366     {
3367         webPagePrivate->m_cachedPopupListSelectedIndex = index;
3368     }
3369 private:
3370     virtual void performInternal(WebPagePrivate* webPagePrivate)
3371     {
3372         webPagePrivate->m_webPage->popupListClosed(webPagePrivate->m_cachedPopupListSelectedIndex);
3373     }
3374 };
3375
3376 void WebPage::popupListClosed(int size, const bool* selecteds)
3377 {
3378     DeferredTaskPopupListSelectSingle::finishOrCancel(d);
3379     if (d->m_page->defersLoading()) {
3380         d->m_deferredTasks.append(adoptPtr(new DeferredTaskPopupListSelectMultiple(d, size, selecteds)));
3381         return;
3382     }
3383     DeferredTaskPopupListSelectMultiple::finishOrCancel(d);
3384     d->m_inputHandler->setPopupListIndexes(size, selecteds);
3385 }
3386
3387 void WebPage::popupListClosed(int index)
3388 {
3389     DeferredTaskPopupListSelectMultiple::finishOrCancel(d);
3390     if (d->m_page->defersLoading()) {
3391         d->m_deferredTasks.append(adoptPtr(new DeferredTaskPopupListSelectSingle(d, index)));
3392         return;
3393     }
3394     DeferredTaskPopupListSelectSingle::finishOrCancel(d);
3395     d->m_inputHandler->setPopupListIndex(index);
3396 }
3397
3398 class DeferredTaskSetDateTimeInput: public DeferredTask<&WebPagePrivate::m_wouldSetDateTimeInput> {
3399 public:
3400     explicit DeferredTaskSetDateTimeInput(WebPagePrivate* webPagePrivate, BlackBerry::Platform::String value)
3401         : DeferredTaskType(webPagePrivate)
3402     {
3403         webPagePrivate->m_cachedDateTimeInput = value;
3404     }
3405 private:
3406     virtual void performInternal(WebPagePrivate* webPagePrivate)
3407     {
3408         webPagePrivate->m_webPage->setDateTimeInput(webPagePrivate->m_cachedDateTimeInput);
3409     }
3410 };
3411
3412 void WebPage::setDateTimeInput(const BlackBerry::Platform::String& value)
3413 {
3414     if (d->m_page->defersLoading()) {
3415         d->m_deferredTasks.append(adoptPtr(new DeferredTaskSetDateTimeInput(d, value)));
3416         return;
3417     }
3418     DeferredTaskSetDateTimeInput::finishOrCancel(d);
3419     d->m_inputHandler->setInputValue(value);
3420 }
3421
3422 class DeferredTaskSetColorInput: public DeferredTask<&WebPagePrivate::m_wouldSetColorInput> {
3423 public:
3424     explicit DeferredTaskSetColorInput(WebPagePrivate* webPagePrivate, BlackBerry::Platform::String value)
3425         : DeferredTaskType(webPagePrivate)
3426     {
3427         webPagePrivate->m_cachedColorInput = value;
3428     }
3429 private:
3430     virtual void performInternal(WebPagePrivate* webPagePrivate)
3431     {
3432         webPagePrivate->m_webPage->setColorInput(webPagePrivate->m_cachedColorInput);
3433     }
3434 };
3435
3436 void WebPage::setColorInput(const BlackBerry::Platform::String& value)
3437 {
3438     if (d->m_page->defersLoading()) {
3439         d->m_deferredTasks.append(adoptPtr(new DeferredTaskSetColorInput(d, value)));
3440         return;
3441     }
3442     DeferredTaskSetColorInput::finishOrCancel(d);
3443     d->m_inputHandler->setInputValue(value);
3444 }
3445
3446 void WebPage::setVirtualViewportSize(const Platform::IntSize& size)
3447 {
3448     d->m_virtualViewportSize = WebCore::IntSize(size);
3449 }
3450
3451 void WebPage::resetVirtualViewportOnCommitted(bool reset)
3452 {
3453     d->m_resetVirtualViewportOnCommitted = reset;
3454 }
3455
3456 Platform::IntSize WebPagePrivate::recomputeVirtualViewportFromViewportArguments()
3457 {
3458     static const ViewportArguments defaultViewportArguments;
3459     if (m_viewportArguments == defaultViewportArguments)
3460         return IntSize();
3461
3462     int desktopWidth = DEFAULT_MAX_LAYOUT_WIDTH;
3463     int deviceWidth = Platform::Graphics::Screen::primaryScreen()->width();
3464     int deviceHeight = Platform::Graphics::Screen::primaryScreen()->height();
3465     float devicePixelRatio = m_webSettings->devicePixelRatio();
3466     ViewportAttributes result = computeViewportAttributes(m_viewportArguments, desktopWidth, deviceWidth, deviceHeight, devicePixelRatio, m_defaultLayoutSize);
3467     m_page->setDeviceScaleFactor(devicePixelRatio);
3468
3469     setUserScalable(m_webSettings->isUserScalable() && result.userScalable);
3470     if (result.initialScale > 0)
3471         setInitialScale(result.initialScale * devicePixelRatio);
3472     if (result.minimumScale > 0)
3473         setMinimumScale(result.minimumScale * devicePixelRatio);
3474     if (result.maximumScale > 0)
3475         setMaximumScale(result.maximumScale * devicePixelRatio);
3476
3477     return Platform::IntSize(result.layoutSize.width(), result.layoutSize.height());
3478 }
3479
3480 #if ENABLE(EVENT_MODE_METATAGS)
3481 void WebPagePrivate::didReceiveCursorEventMode(CursorEventMode mode)
3482 {
3483     if (mode != m_cursorEventMode)
3484         m_client->cursorEventModeChanged(toPlatformCursorEventMode(mode));
3485     m_cursorEventMode = mode;
3486 }
3487
3488 void WebPagePrivate::didReceiveTouchEventMode(TouchEventMode mode)
3489 {
3490     if (mode != m_touchEventMode)
3491         m_client->touchEventModeChanged(toPlatformTouchEventMode(mode));
3492     m_touchEventMode = mode;
3493 }
3494 #endif
3495
3496 void WebPagePrivate::dispatchViewportPropertiesDidChange(const ViewportArguments& arguments)
3497 {
3498     if (arguments == m_viewportArguments)
3499         return;
3500
3501     // If the caller is trying to reset to default arguments, use the user supplied ones instead.
3502     static const ViewportArguments defaultViewportArguments;
3503     if (arguments == defaultViewportArguments) {
3504         m_viewportArguments = m_userViewportArguments;
3505         m_forceRespectViewportArguments = m_userViewportArguments != defaultViewportArguments;
3506     } else
3507         m_viewportArguments = arguments;
3508
3509     // 0 width or height in viewport arguments makes no sense, and results in a very large initial scale.
3510     // In real world, a 0 width or height is usually caused by a syntax error in "content" field of viewport
3511     // meta tag, for example, using semicolon instead of comma as separator ("width=device-width; initial-scale=1.0").
3512     // We don't have a plan to tolerate the semicolon separator, but we can avoid applying 0 width/height.
3513     // I default it to ValueDeviceWidth rather than ValueAuto because in more cases the web site wants "device-width"
3514     // when they specify the viewport width.
3515     if (!m_viewportArguments.width)