browsercore/core/network/webnetworkaccessmanager.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 <QNetworkRequest>
       
    20 #include <QNetworkReply>
       
    21 #include <QAuthenticator>
       
    22 
       
    23 #if QT_VERSION >= 0x040500
       
    24 #include <QNetworkDiskCache>
       
    25 #endif
       
    26 
       
    27 //#include "wrtsettings.h"
       
    28 #include "bedrockprovisioning.h"
       
    29 #include "wrtbrowsercontainer.h"
       
    30 #include "webcookiejar.h"
       
    31 #include "webnetworkaccessmanager.h"
       
    32 
       
    33 #include "WebDialogProvider.h"
       
    34 
       
    35 #include "networkerrorreply.h"
       
    36 #include "SchemeHandlerBr.h"
       
    37 #include "qwebframe.h"
       
    38 
       
    39 namespace WRT {
       
    40 
       
    41 WebNetworkAccessManager::WebNetworkAccessManager(WrtBrowserContainer* container, QObject* parent) : QNetworkAccessManager(container), m_browserContainer(container)
       
    42 {
       
    43     m_cookieJar = new CookieJar();
       
    44     m_reply = NULL;
       
    45     this->setCookieJar(m_cookieJar);
       
    46     connect(this, SIGNAL(authenticationRequired(QNetworkReply *, QAuthenticator *)), m_browserContainer, SLOT(slotAuthenticationRequired(QNetworkReply *, QAuthenticator *)));
       
    47 
       
    48     connect(this, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy & , QAuthenticator * )), m_browserContainer, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)));
       
    49     setupCache();
       
    50     setupNetworkProxy();
       
    51 }
       
    52 
       
    53 #ifdef NETWORK_DEBUG
       
    54 void WebNetworkAccessManager::error(QNetworkReply::NetworkError)
       
    55 {
       
    56 //    qDebug() << "Network::error:" << n_reply->error() << " String:" << n_reply->errorString();   
       
    57 //    QString errorNumber = QString::number(n_reply->error() );
       
    58     QNetworkReply::NetworkError error = n_reply->error(); 
       
    59     		 
       
    60     if ( error != 5 && error != 0 )
       
    61     {      
       
    62     switch ( error ) 
       
    63     {     	
       
    64         case QNetworkReply::HostNotFoundError: 
       
    65         WebDialogProvider::showTimedMessage(NULL, "No Connection ", 2000);
       
    66         break; 
       
    67  /*    
       
    68  // ContentNotFoundError cause many problems. For now, we ignore 
       
    69  // TODO: We will find more graceful way to handle this error
       
    70         case QNetworkReply::ContentNotFoundError: 
       
    71         WebDialogProvider::showTimedMessage(NULL, "Content Not Found", 2000);
       
    72         break; 
       
    73  */   
       
    74         case QNetworkReply::ProtocolUnknownError: 
       
    75         WebDialogProvider::showTimedMessage(NULL, "Protocol Unknown", 2000);
       
    76         break; 
       
    77     
       
    78         default: 
       
    79     	  break;    	 // Ignore other error msgs 
       
    80     }
       
    81    }
       
    82    return; 
       
    83 }
       
    84 #endif 
       
    85 
       
    86 WebNetworkAccessManager::~WebNetworkAccessManager()
       
    87 {
       
    88     delete m_cookieJar;
       
    89     //setCache(NULL);
       
    90     delete m_reply;
       
    91 }
       
    92 
       
    93 QNetworkReply* WebNetworkAccessManager::createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData)
       
    94 {
       
    95     QNetworkRequest req = request;
       
    96     
       
    97     req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
       
    98 
       
    99     if (m_reply != NULL) {
       
   100         delete m_reply;
       
   101         m_reply = NULL;
       
   102     }
       
   103     QNetworkReply* reply = NULL;
       
   104 
       
   105 
       
   106     if(m_browserContainer->mainFrame()) {
       
   107         if(m_browserContainer->mainFrame()->url().scheme().contains("https")) {
       
   108 
       
   109             if (op == QNetworkAccessManager::PostOperation && req.url().scheme().contains("http")) {
       
   110 
       
   111                 m_text = tr("Secure Page Warning:");
       
   112                 m_informativeText = tr("Do you want to continue?");
       
   113                 m_detailedText = tr("Submit your data over an unencrypted connection.");
       
   114                 m_buttons = QMessageBox::Yes | QMessageBox::No;
       
   115                 m_defaultButton = QMessageBox::Yes;
       
   116                 m_icon = QMessageBox::Warning;
       
   117 
       
   118                 m_req = &req;
       
   119 
       
   120                 emit showMessageBox(this);
       
   121                 reply =  m_reply;
       
   122             }
       
   123         }
       
   124     }
       
   125     if (reply == NULL) {
       
   126 		reply = createRequestHelper(op, req, outgoingData);
       
   127     }
       
   128 #ifdef NETWORK_DEBUG	
       
   129     n_reply = reply; 
       
   130     connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
       
   131 	    this, SLOT(error(QNetworkReply::NetworkError))); 
       
   132 #endif 		
       
   133     return reply;
       
   134 }
       
   135 
       
   136 void WebNetworkAccessManager::onMessageBoxResponse(int retValue)
       
   137 {
       
   138 
       
   139     if (retValue == QMessageBox::No) {
       
   140         m_reply = new NetworkErrorReply(QNetworkReply::ContentAccessDenied, "Not allowed", m_req->url());
       
   141         //a finished signal will be sent and the member is invoked as soon as the application enters the main event loop
       
   142         QMetaObject::invokeMethod(m_reply, "finished", Qt::QueuedConnection);
       
   143     }
       
   144 }
       
   145 
       
   146 
       
   147 QNetworkReply* WebNetworkAccessManager::createRequestHelper(Operation op, const QNetworkRequest &request, QIODevice *outgoingData)
       
   148 {
       
   149     QNetworkRequest req = request;
       
   150 
       
   151     // Don't load the content if we are restoring the session 
       
   152     if (m_browserContainer->restoreSession()) {
       
   153         // handled in scheme handler - block network access
       
   154         QNetworkReply* reply = new NetworkErrorReply(QNetworkReply::OperationCanceledError, "Page Restored", req.url());
       
   155         QMetaObject::invokeMethod(reply, "finished", Qt::QueuedConnection);
       
   156         return reply;
       
   157     }
       
   158     
       
   159     if (m_browserContainer->schemeHandler()->HandleSpecialScheme(req.url())) {
       
   160         // handled in scheme handler - block network access
       
   161         QNetworkReply* reply = new NetworkErrorReply(QNetworkReply::OperationCanceledError, "Scheme Handled", req.url());
       
   162         QMetaObject::invokeMethod(reply, "finished", Qt::QueuedConnection);
       
   163         return reply;
       
   164     }
       
   165 
       
   166     //Accept-Language header
       
   167     QLocale language;
       
   168     QString langCountryCode = language.name();
       
   169     //QLocale::name returns the language and country as a  string of the form "language_country", where
       
   170     //language is a lowercase, two-letter ISO 639 language code, and country is an uppercase, two-letter 
       
   171     //ISO 3166 country code. But the format is expected to be "language-country". Note that hyphen is expected 
       
   172     //instead of underscore.
       
   173     langCountryCode.replace(QString("_"), QString("-"));
       
   174     req.setRawHeader("Accept-Language", langCountryCode.toUtf8());
       
   175 
       
   176     return QNetworkAccessManager::createRequest(op, req, outgoingData);
       
   177 }
       
   178 
       
   179 void WebNetworkAccessManager::setupNetworkProxy()
       
   180 {
       
   181    QNetworkProxy proxy;
       
   182 	 
       
   183    QString proxyString = BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning()->valueAsString("NetworkProxy");
       
   184    
       
   185    if (proxyString.isEmpty())
       
   186    	{
       
   187       proxy.setType(QNetworkProxy::NoProxy);
       
   188       proxy.setHostName("");
       
   189       proxy.setPort(0);
       
   190    	}
       
   191    	else
       
   192 		{
       
   193       proxy.setType(QNetworkProxy::HttpProxy);
       
   194       proxy.setHostName(proxyString);
       
   195       proxy.setPort(8080);
       
   196  		}
       
   197 
       
   198    	setProxy(proxy);
       
   199 }
       
   200 // Setup cache
       
   201 // Need to use WrtSettingsUI to setup Disk Cache Directory Path
       
   202 void WebNetworkAccessManager::setupCache()
       
   203 {
       
   204 
       
   205 #if QT_VERSION >= 0x040500
       
   206     qDiskCache = new QNetworkDiskCache(this);
       
   207     
       
   208     if ( !BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning()->value("DiskCacheEnabled").toBool() ) 
       
   209 		return;
       
   210 
       
   211     QString diskCacheDir = BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning()->value("DiskCacheDirectoryPath").toString();
       
   212     if(diskCacheDir.isEmpty()) return;
       
   213     // setup cache
       
   214     qDiskCache->setCacheDirectory(diskCacheDir);
       
   215 
       
   216     int cacheMaxSize = BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning()->value("DiskCacheMaxSize").toInt();
       
   217     qDiskCache->setMaximumCacheSize(cacheMaxSize);
       
   218 
       
   219     setCache(qDiskCache);
       
   220 
       
   221 #endif
       
   222 }
       
   223 
       
   224 }