browsercore/core/wrtbrowsercontainer.cpp
changeset 0 1450b09d0cfd
child 3 0954f5dd2cd0
child 5 0f2326c2a325
equal deleted inserted replaced
-1:000000000000 0:1450b09d0cfd
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "browserpagefactory.h"
       
    20 #include "webcontentview.h"
       
    21 #include "wrtpage.h"
       
    22 #include "wrtbrowsercontainer_p.h"
       
    23 #include "wrtbrowsercontainer.h"
       
    24 #include "webpagedata.h"
       
    25 #include "qwebhistory.h"
       
    26 #include "qwebframe.h"
       
    27 #include "SchemeHandlerBr.h"
       
    28 #include "webnetworkaccessmanager.h"
       
    29 #ifdef QT_MOBILITY_BEARER_MANAGEMENT
       
    30 #include "WebNetworkConnectionManager.h"
       
    31 #endif // QT_MOBILITY_BEARER_MANAGEMENT
       
    32 #include "webcookiejar.h"
       
    33 #include "webnavigation.h"
       
    34 #include "secureuicontroller.h"
       
    35 #include "LoadController.h"
       
    36 #include "WebDialogProvider.h"
       
    37 #include "bedrockprovisioning.h"
       
    38 #include <QPainter>
       
    39 #include <QAuthenticator>
       
    40 #include <QFile>
       
    41 #include <QMessageBox>
       
    42 
       
    43 namespace WRT
       
    44 {
       
    45 
       
    46 const int historyItemsCount = 20;
       
    47 
       
    48 WrtBrowserContainerPrivate::WrtBrowserContainerPrivate(QObject* parent,
       
    49 		WrtBrowserContainer* page/*never NULL*/) : m_schemeHandler(new SchemeHandler())
       
    50 ,   m_pageFactory(0)
       
    51 ,   m_widget(0)
       
    52 {
       
    53     assert(page);
       
    54     m_page = page;
       
    55 
       
    56     m_secureController = new WRT::SecureUIController(parent);
       
    57     m_loadController = new  WRT::LoadController();
       
    58  
       
    59     // Turn off the scroll bars of main frame
       
    60     m_page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
       
    61     m_page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
       
    62     
       
    63 }
       
    64 
       
    65 WrtBrowserContainerPrivate::~WrtBrowserContainerPrivate()
       
    66 {
       
    67 	m_page->setView(0);
       
    68 	if (m_page)
       
    69 		m_page->disconnect();
       
    70 	delete m_schemeHandler;
       
    71   delete m_secureController;
       
    72   delete m_loadController;
       
    73 }
       
    74 
       
    75 /*!
       
    76  * Static function which creates Wrt page with parent Widget
       
    77  * @param parent   : parent widget for the new page
       
    78  * @return WrtBrowserContainer : Page handle to the newly created page
       
    79  */
       
    80 WrtBrowserContainer* WrtBrowserContainer::createPageWithWidgetParent(
       
    81 		QObject* parent, WrtBrowserContainer* page)
       
    82 {
       
    83     if (page)
       
    84     {
       
    85         page->setParent(parent);
       
    86         return page;
       
    87     }
       
    88     else
       
    89         return new WrtBrowserContainer(parent);
       
    90 }
       
    91 
       
    92 /*!
       
    93  * \class WrtBrowserContainer
       
    94  * \brief Description: This class is a handle to open page on which user can
       
    95  * load an url , view history items, load an history item into the page etc.
       
    96  * @see WrtPageManager
       
    97  */
       
    98 
       
    99 /*!
       
   100  * WrtBrowserContainer Constructor
       
   101  * @param parent : Widget parent
       
   102  */
       
   103 WrtBrowserContainer::WrtBrowserContainer(QObject* parent) :
       
   104 	WrtPage(parent), d(new WrtBrowserContainerPrivate(this, this))
       
   105 {
       
   106 	// Download related enable "forwardUnsupportedContent" to redirect unsupported content to download manager
       
   107 	setForwardUnsupportedContent(true);
       
   108 
       
   109 #ifndef NO_NETWORK_ACCESS_MANAGER	
       
   110 	setNetworkAccessManager(new WebNetworkAccessManager(this,this));
       
   111 #endif
       
   112 	
       
   113 #ifdef QT_MOBILITY_BEARER_MANAGEMENT
       
   114 	setNetworkConnectionManager(new WebNetworkConnectionManager(this));
       
   115 #endif // QT_MOBILITY_BEARER_MANAGEMENT
       
   116 	history()->setMaximumItemCount(historyItemsCount);
       
   117 
       
   118 	connect(this,
       
   119 			SIGNAL(saveFrameStateRequested(QWebFrame*, QWebHistoryItem*)),
       
   120 			this, SLOT(savePageDataToHistoryItem(QWebFrame*, QWebHistoryItem*)));
       
   121 
       
   122     /* Connect secure related signals and slots */
       
   123     connect(mainFrame(), SIGNAL(urlChanged(QUrl)), d->m_secureController, SLOT(setTopLevelScheme(const QUrl &)));
       
   124     connect(this, SIGNAL(loadFinished(bool)), d->m_secureController, SLOT( endSecureCheck(bool) ) );
       
   125     connect(d->m_secureController, SIGNAL(pageSecureState(int)), this, SLOT(pageSecureState(int)));
       
   126     connect(networkAccessManager(), SIGNAL(sslErrors(QNetworkReply *, const QList<QSslError> &)), d->m_secureController, SLOT(onSslErrors(QNetworkReply *, const QList<QSslError> &)));
       
   127 
       
   128     connect(this, SIGNAL(loadStarted()), d->m_loadController, SLOT(loadStarted()));
       
   129     connect(this, SIGNAL(loadProgress(int)), d->m_loadController, SLOT(loadProgress(int)));
       
   130     connect(this, SIGNAL(loadFinished(bool)), d->m_loadController, SLOT(loadFinished(bool)));
       
   131     connect(mainFrame(), SIGNAL(urlChanged(QUrl)), d->m_loadController, SLOT(urlChanged(QUrl)));
       
   132     
       
   133 	// All pages in the browser share the same pageGroup.
       
   134 	d->m_pageGroupName = "nokia_page_group";
       
   135 	setPageGroupName(d->m_pageGroupName);
       
   136 }
       
   137 
       
   138 /*!
       
   139  * WrtBrowserContainer destructor
       
   140  */
       
   141 WrtBrowserContainer::~WrtBrowserContainer()
       
   142 {
       
   143 	// gracefully but warn, disconnect anything connected to this page
       
   144 	if (!disconnect())
       
   145 		qWarning("WrtPage destructor blanket disconnect failed");
       
   146 
       
   147     //setNetworkAccessManager(NULL);
       
   148    delete d;
       
   149 }
       
   150 
       
   151 QGraphicsWidget* WrtBrowserContainer::webWidget() const
       
   152 {
       
   153     return d->m_widget;
       
   154 }
       
   155 
       
   156 void WrtBrowserContainer::setWebWidget(QGraphicsWidget* view)
       
   157 {
       
   158     if(d->m_widget) {
       
   159         disconnect(static_cast<WebContentWidget*>(webWidget()), SIGNAL(pageZoomMetaDataChange(QWebFrame*, ZoomMetaData)), this, SLOT(pageZoomMetaDataChange(QWebFrame*, ZoomMetaData)));
       
   160     }
       
   161 
       
   162     d->m_widget = view;
       
   163 
       
   164     if (view)
       
   165     {
       
   166         QGraphicsWebView* webView = static_cast<QGraphicsWebView*>(view);
       
   167         if(webView)
       
   168             webView->setPage(this);
       
   169 
       
   170         /* Currently connecting it here. Need to revisit if the dialog is enabled in the
       
   171          * context of multiple windows */
       
   172         connect( d->m_secureController, SIGNAL( showMessageBox(WRT::MessageBoxProxy*)), static_cast<WebContentWidget*>(webWidget())->view(), SLOT( showMessageBox(WRT::MessageBoxProxy*)));
       
   173 
       
   174         connect(static_cast<WebContentWidget*>(webWidget()), SIGNAL(pageZoomMetaDataChange(QWebFrame*, ZoomMetaData)), this, SLOT(pageZoomMetaDataChange(QWebFrame*, ZoomMetaData)));
       
   175     }
       
   176 }
       
   177  
       
   178 /*!
       
   179  * Handles pageSecureState state 
       
   180  */
       
   181 void WrtBrowserContainer::pageSecureState(int state) {
       
   182 
       
   183     emit secureStateChange(state);
       
   184 
       
   185 }
       
   186 
       
   187 /*!
       
   188  * Returns schemeHandler
       
   189  */
       
   190 SchemeHandler* WrtBrowserContainer::schemeHandler() const
       
   191 {
       
   192    return d->m_schemeHandler;
       
   193 }
       
   194 
       
   195 /*!
       
   196  * Gets the history count for this page, i.e gets the number of urls loaded
       
   197  * onto this page
       
   198  * @return  History count of this page
       
   199  */
       
   200 int WrtBrowserContainer::getHistoryCount() const
       
   201 {
       
   202 	return history()->count();
       
   203 }
       
   204 
       
   205 /*!
       
   206  * Triggers public Action
       
   207  * @param WebAction : web action to be triggred
       
   208  * @param checked  : true/false
       
   209  * @see   WebAction
       
   210  */
       
   211 /*void WrtBrowserContainer::triggerAction(QWebPage::WebAction action, bool checked)
       
   212  {
       
   213  triggerAction(action, checked);
       
   214  }
       
   215  */
       
   216 
       
   217 /*!
       
   218  * clears cookies
       
   219  */
       
   220 void WrtBrowserContainer::clearCookies()
       
   221 {
       
   222 	return ((CookieJar*) networkAccessManager()->cookieJar())->clear();
       
   223 }
       
   224 
       
   225 bool WrtBrowserContainer::clearNetworkCache()
       
   226 {
       
   227    bool ok = false;
       
   228     QAbstractNetworkCache* diskCache = networkAccessManager()->cache();
       
   229     diskCache->clear();
       
   230     if (!diskCache->cacheSize()) { 
       
   231        // Unless there was failures clearing the cache cacheSize() should return 0 after a call to clear 
       
   232        ok = true;
       
   233    }
       
   234    return ok;
       
   235 }
       
   236 
       
   237 /*!
       
   238  * This function page thumbnail for this page as specified by X & Y co-ordinate scale factors
       
   239  * @param  scaleX :  X Co-ordinate scale factor for the page thumbnail
       
   240  * @param  scaleY :  y Co-ordinate scale factor for the page thumbnail
       
   241  */
       
   242 QImage WrtBrowserContainer::pageThumbnail(qreal scaleX, qreal scaleY)
       
   243 {
       
   244     qDebug() << "WrtBrowserContainer::pageThumbnail:" << webWidget()->size();
       
   245     QSize size = webWidget()->size().toSize();
       
   246     QImage image(size, QImage::Format_RGB32);
       
   247 
       
   248     QPainter painter(&image);
       
   249     QRect r(0, 0, size.width(), size.height());
       
   250     QRegion clip(r);
       
   251     painter.save();
       
   252     painter.setBrush(Qt::white);
       
   253     painter.drawRect(r);
       
   254     painter.restore();
       
   255     mainFrame()->render(&painter, clip);
       
   256     return image;
       
   257 }
       
   258 
       
   259 /*!
       
   260  * sets the page zoom factor as specified by zoom
       
   261  * @param zoom : page zoom factor to be set
       
   262  */
       
   263 void WrtBrowserContainer::setPageZoomFactor(qreal zoom)
       
   264 {
       
   265    // FiX ME ... canvas zoom doesn't seem to support text-only zooming.
       
   266    //static_cast<WebCanvasWidget*>(webWidget())->canvas()->setPageZoomFactor(zoom);
       
   267 
       
   268 	bool isZoomTextOnly = BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning()->value(
       
   269 			"ZoomTextOnly").toBool();
       
   270 
       
   271 	if (!isZoomTextOnly)
       
   272 	{
       
   273 		// mainFrame()->setZoomFactor(zoom);
       
   274 		static_cast<WebContentWidget*> (webWidget())->setZoomFactor(zoom);
       
   275 	}
       
   276 	else
       
   277 	{
       
   278 		//mainFrame()->setTextSizeMultiplier(zoom);
       
   279 		static_cast<WebContentWidget*> (webWidget())->setTextSizeMultiplier(
       
   280 				zoom);
       
   281     }
       
   282 }
       
   283 
       
   284 /*!
       
   285  * setpageDirtyZoomFactor
       
   286  * @param zoom: page zoom factor to be set
       
   287  */
       
   288 void WrtBrowserContainer::setPageDirtyZoomFactor(qreal zoom)
       
   289 {
       
   290    // FIXME: dirty page zoom is not supported yet in single-buffer mode.
       
   291    static_cast<WebContentWidget*> (webWidget())->setDirtyZoomFactor(zoom);
       
   292 }
       
   293 
       
   294 /*!
       
   295  * setpageCenterZoomFactor
       
   296  * @param zoom: center page zoom factor to be set
       
   297  */
       
   298 // TODO: hold on this change until zoom/scrolling improvement 
       
   299 /*
       
   300  void WrtBrowserContainer::setPageCenterZoomFactor(qreal zoom)
       
   301  {
       
   302  static_cast<WebCanvasWidget*>(webWidget())->canvas()->setPageCenterZoomFactor(zoom);
       
   303  }
       
   304  */
       
   305 /*!
       
   306  * returns the current page zoom factor of this page
       
   307  * @return current zoom factor
       
   308  */
       
   309 qreal WrtBrowserContainer::pageZoomFactor() const
       
   310 {
       
   311    return static_cast<WebContentWidget*> (webWidget())->zoomFactor();
       
   312 }
       
   313 
       
   314 /*!
       
   315  * sets the Canvas ScaleFactor  for this page
       
   316  * @param  scaleX :  X Co-ordinate scale factor
       
   317  * @param  scaleY :  Y Co-ordinate scale factor
       
   318  */
       
   319 void WrtBrowserContainer::setCanvasScaleFactor(qreal scaleX, qreal scaleY)
       
   320 {
       
   321    QTransform transform;
       
   322    transform.scale(scaleX, scaleY);
       
   323    //     static_cast<ContentView*>(webWidget())->setTransform(transform);
       
   324 }
       
   325 
       
   326 /*!
       
   327  * allows offlineStorage for url
       
   328  * @param url : url for which offlinestorage has to be enabled
       
   329  */
       
   330 bool WrtBrowserContainer::allowOfflineStorage(const QUrl& url)
       
   331 {
       
   332    if (BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning()->value("SecurityLevel").toInt()
       
   333          != SecurityHigh)
       
   334       return true;
       
   335 
       
   336 	QStringList yesno;
       
   337 	yesno << "Yes" << "No";
       
   338 	return (WebDialogProvider::getButtonResponse(
       
   339 			0, /* webWidget(), Check, that widget is required */
       
   340 			QString(
       
   341 					"Offline Database: This host asking for storing data in offline database, allow?"),
       
   342 			yesno));
       
   343 }
       
   344 
       
   345 
       
   346 /*!
       
   347  Pulic Slots:
       
   348  void savePageDataToHistoryItem(QWebFrame*, QWebHistoryItem* item);
       
   349  void slotAuthenticationRequired(QNetworkReply *, QAuthenticator *);
       
   350  void slotProxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *);
       
   351  */
       
   352 
       
   353 /*!
       
   354  * Public slot, saves the page data (like thumbail, current position, zoom factor etc ) to WebHistoryItem
       
   355  * @frame  main frame for which the data has to be stored
       
   356  * @item   handle to QWebHistoryItem into which the page data has to be saved.
       
   357  */
       
   358 
       
   359 void WrtBrowserContainer::savePageDataToHistoryItem(QWebFrame* frame,
       
   360       QWebHistoryItem* item)
       
   361 {
       
   362     if (frame != mainFrame() || !item->isValid() || !webWidget())
       
   363     {
       
   364         return;
       
   365     }
       
   366     if (restoreSession()) return;
       
   367     	
       
   368     WebPageData data(this);
       
   369     //   WebPageData data = item->userData().value<WebPageData>();
       
   370     data.m_zoomFactor = pageZoomFactor();
       
   371     data.m_thumbnail = pageThumbnail(1.0, 1.0);//data.m_zoomFactor, data.m_zoomFactor);
       
   372 
       
   373     QPoint pos(0, 0);
       
   374     //    pos = static_cast<WebCanvasWidget*>(webWidget())->canvas()->canvasToDocument(pos);
       
   375     pos = mainFrame()->scrollPosition();
       
   376     data.m_contentsPos = pos;
       
   377     QVariant variant;
       
   378     variant.setValue(data);
       
   379     item->setUserData(variant);
       
   380     //ii++;
       
   381 }
       
   382 
       
   383 /*!
       
   384  * Public slot  AuthenticationRequired
       
   385  * Launches dialog for user name and password if Authentication is required for page load.
       
   386  * @param reply     : network reply
       
   387  * @param  athenticator : athenticator
       
   388  */
       
   389 void WrtBrowserContainer::slotAuthenticationRequired(QNetworkReply* reply,
       
   390       QAuthenticator* authenticator)
       
   391 {
       
   392 	QString username, password;
       
   393 	if (WebDialogProvider::getUsernamePassword(0/*webWidget()*/, username, password))
       
   394 	{
       
   395 		authenticator->setUser(username);
       
   396 		authenticator->setPassword(password);
       
   397     }
       
   398 }
       
   399 
       
   400 /*!
       
   401  * public slot for setting proxy when Authentication is Required
       
   402  * @param networkProxy : network Proxy
       
   403  * @param authenticator : authenticator
       
   404  */
       
   405 void WrtBrowserContainer::slotProxyAuthenticationRequired(
       
   406       const QNetworkProxy& networkProxy, QAuthenticator* authenticator)
       
   407 {
       
   408 	QString username, password;
       
   409 	if (WebDialogProvider::getUsernamePassword(0/*webWidget()*/, username, password))
       
   410 	{
       
   411 		authenticator->setUser(username);
       
   412 		authenticator->setPassword(password);
       
   413     }
       
   414 }
       
   415 
       
   416 QWebPage* WrtBrowserContainer::createWindow(
       
   417     QWebPage::WebWindowType webWindowType)
       
   418 {
       
   419 
       
   420     /* When WrtPage is created, QWebSettings::JavascriptCanOpenWindows is initialized
       
   421      * to popup setting value. Need not do any check here 
       
   422      */
       
   423     if (d->m_pageFactory)
       
   424     {
       
   425         // no need to signal in this case
       
   426         return d->m_pageFactory->openPage();
       
   427     }
       
   428 
       
   429     WrtBrowserContainer* wrtPage = new WrtBrowserContainer();
       
   430     emit createNewWindow(wrtPage);
       
   431     return wrtPage;
       
   432 }
       
   433 
       
   434 void WrtBrowserContainer::setPageFactory(BrowserPageFactory* f)
       
   435 {
       
   436     d->m_pageFactory = f;
       
   437 }
       
   438 
       
   439 void WrtBrowserContainer::setElementType(
       
   440       wrtBrowserDefs::BrowserElementType& aElType)
       
   441 {
       
   442     m_elementType = aElType;
       
   443 }
       
   444 wrtBrowserDefs::BrowserElementType WrtBrowserContainer::getElementType()
       
   445 {
       
   446    return m_elementType;
       
   447 }
       
   448 
       
   449 QString WrtBrowserContainer::pageTitle(){
       
   450 
       
   451     QString title = mainFrame()->title();
       
   452     
       
   453     // If mainFrame title is empty we may be restoring session
       
   454     // Check history title
       
   455     if (title.isEmpty()){ 
       
   456     	title = history()->currentItem().title();
       
   457     }
       
   458     
       
   459     /* If there is no title, provide the partial url */
       
   460     if (title.isEmpty()) {
       
   461         QUrl url  = mainFrame()->url(); 
       
   462         // If mainframe url is empty, we may be restoring session
       
   463         // check history url
       
   464         if (url.isEmpty()) {
       
   465         	url = history()->currentItem().url();
       
   466         }
       
   467         title = url.toString();
       
   468         QString scheme=url.scheme();
       
   469         title.remove(0, scheme.length() + 3); // remove "scheme://"
       
   470         if (title.startsWith("www.", Qt::CaseInsensitive)) {
       
   471             title.remove(0, 4);
       
   472         }
       
   473 
       
   474     }
       
   475     return title;
       
   476 }
       
   477 
       
   478 int WrtBrowserContainer::secureState() {
       
   479 
       
   480     return d->m_secureController->secureState();
       
   481 }
       
   482 bool WrtBrowserContainer::restoreSession() {
       
   483 	if (d->m_pageFactory)
       
   484     return d->m_pageFactory->m_bRestoreSession;
       
   485   else
       
   486     return true;
       
   487 }
       
   488 
       
   489 bool WrtBrowserContainer::emptyWindow() {
       
   490 
       
   491     bool result= false;
       
   492     if (mainFrame()->title()  == "" && mainFrame()->url().toString() == "" )
       
   493         result = true;
       
   494 
       
   495     return result;
       
   496 }
       
   497 
       
   498 void WrtBrowserContainer::pageZoomMetaDataChange(QWebFrame* frame,  ZoomMetaData  zoomData ){
       
   499 
       
   500     //qDebug() << __func__ << "Frame " << frame << "My Frame " << mainFrame();
       
   501     if (frame == mainFrame()) {
       
   502         setPageZoomMetaData(zoomData);
       
   503     } 
       
   504 }
       
   505 
       
   506 ZoomMetaData WrtBrowserContainer::pageZoomMetaData() {
       
   507     return d->m_zoomData ;
       
   508 
       
   509 }
       
   510 void WrtBrowserContainer::setPageZoomMetaData( ZoomMetaData  zoomData ){
       
   511 
       
   512     d->m_zoomData = zoomData;
       
   513 }
       
   514 
       
   515 
       
   516 }
       
   517 
       
   518