1 /* |
1 /* |
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
3 * All rights reserved. |
3 * All rights reserved. |
4 * This component and the accompanying materials are made available |
4 * |
5 * under the terms of "Eclipse Public License v1.0" |
5 * This program is free software: you can redistribute it and/or modify |
6 * which accompanies this distribution, and is available |
6 * it under the terms of the GNU Lesser General Public License as published by |
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
7 * the Free Software Foundation, version 2.1 of the License. |
8 * |
8 * |
9 * Initial Contributors: |
9 * This program is distributed in the hope that it will be useful, |
10 * Nokia Corporation - initial contribution. |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * Contributors: |
12 * GNU Lesser General Public License for more details. |
13 * |
13 * |
14 * Description: |
14 * You should have received a copy of the GNU Lesser General Public License |
|
15 * along with this program. If not, |
|
16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/". |
|
17 * |
|
18 * Description: |
15 * |
19 * |
16 */ |
20 */ |
17 |
|
18 |
21 |
19 #include <QNetworkRequest> |
22 #include <QNetworkRequest> |
20 #include <QNetworkReply> |
23 #include <QNetworkReply> |
21 #include <QAuthenticator> |
24 #include <QAuthenticator> |
22 |
25 #include <QNetworkInterface> |
23 #if QT_VERSION >= 0x040500 |
26 #if QT_VERSION >= 0x040500 |
24 #include <QNetworkDiskCache> |
27 #include <QNetworkDiskCache> |
|
28 // #include "networkdiskcache.h" |
25 #endif |
29 #endif |
26 |
30 |
27 //#include "wrtsettings.h" |
|
28 #include "bedrockprovisioning.h" |
31 #include "bedrockprovisioning.h" |
29 #include "wrtbrowsercontainer.h" |
32 #include "wrtbrowsercontainer.h" |
30 #include "webcookiejar.h" |
33 #include "webcookiejar.h" |
31 #include "webnetworkaccessmanager.h" |
34 #include "webnetworkaccessmanager.h" |
32 |
35 |
33 #include "WebDialogProvider.h" |
36 #include "WebDialogProvider.h" |
34 |
37 |
35 #include "networkerrorreply.h" |
38 #include "networkerrorreply.h" |
36 #include "SchemeHandlerBr.h" |
39 #include "SchemeHandlerBr.h" |
37 #include "qwebframe.h" |
40 #include <QWebFrame> |
38 |
41 |
39 namespace WRT { |
42 namespace WRT { |
40 |
43 |
41 WebNetworkAccessManager::WebNetworkAccessManager(WrtBrowserContainer* container, QObject* parent) : QNetworkAccessManager(container), m_browserContainer(container) |
44 WebNetworkAccessManager::WebNetworkAccessManager(WrtBrowserContainer* container, QObject* parent) : QNetworkAccessManager(container), m_browserContainer(container) |
42 { |
45 { |
43 m_cookieJar = new CookieJar(); |
46 m_cookieJar = new CookieJar(); |
44 m_reply = NULL; |
47 m_reply = NULL; |
45 this->setCookieJar(m_cookieJar); |
48 this->setCookieJar(m_cookieJar); |
46 connect(this, SIGNAL(authenticationRequired(QNetworkReply *, QAuthenticator *)), m_browserContainer, SLOT(slotAuthenticationRequired(QNetworkReply *, QAuthenticator *))); |
49 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 *))); |
50 connect(this, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy & , QAuthenticator * )), m_browserContainer, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *))); |
|
51 connect(this, SIGNAL(finished(QNetworkReply *)), this, SLOT(onfinished(QNetworkReply *))); |
49 setupCache(); |
52 setupCache(); |
50 setupNetworkProxy(); |
53 setupNetworkProxy(); |
51 } |
54 } |
52 |
55 |
53 #ifdef NETWORK_DEBUG |
56 int WebNetworkAccessManager::activeNetworkInterfaces() |
54 void WebNetworkAccessManager::error(QNetworkReply::NetworkError) |
57 { |
55 { |
58 int count = 0; |
56 // qDebug() << "Network::error:" << n_reply->error() << " String:" << n_reply->errorString(); |
59 QList<QNetworkInterface> allNIFs = QNetworkInterface::allInterfaces(); |
57 // QString errorNumber = QString::number(n_reply->error() ); |
60 foreach (QNetworkInterface aNIF, allNIFs) { |
58 QNetworkReply::NetworkError error = n_reply->error(); |
61 if (aNIF.isValid() && aNIF.flags() & QNetworkInterface::IsUp && !(aNIF.flags() & QNetworkInterface::IsLoopBack)) |
59 |
62 count++; |
60 if ( error != 5 && error != 0 ) |
63 } |
61 { |
64 return count; |
62 switch ( error ) |
65 } |
63 { |
66 |
64 case QNetworkReply::HostNotFoundError: |
67 void WebNetworkAccessManager::onfinished(QNetworkReply* reply) |
65 WebDialogProvider::showTimedMessage(NULL, "No Connection ", 2000); |
68 { |
66 break; |
69 QNetworkReply::NetworkError networkError = reply->error(); |
67 /* |
70 QString requestUrl = reply->request().url().toString(); |
68 // ContentNotFoundError cause many problems. For now, we ignore |
71 |
69 // TODO: We will find more graceful way to handle this error |
72 if ( networkError != QNetworkReply::OperationCanceledError && |
70 case QNetworkReply::ContentNotFoundError: |
73 networkError != QNetworkReply::NoError ) |
71 WebDialogProvider::showTimedMessage(NULL, "Content Not Found", 2000); |
74 { |
72 break; |
75 QString errorMsg = reply->errorString(); |
73 */ |
76 if ( activeNetworkInterfaces() == 0 ) { |
74 case QNetworkReply::ProtocolUnknownError: |
77 errorMsg = "Network not available"; |
75 WebDialogProvider::showTimedMessage(NULL, "Protocol Unknown", 2000); |
78 } else { |
76 break; |
79 int httpErrorCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); |
77 |
80 if ( httpErrorCode ) { |
78 default: |
81 QString httpErrorStr = QString ("HTTP %1 ").arg(httpErrorCode); |
79 break; // Ignore other error msgs |
82 QString httpReasonStr = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString(); |
80 } |
83 errorMsg = httpErrorStr + httpReasonStr; |
81 } |
84 } |
82 return; |
85 } |
83 } |
86 emit networkErrorHappened(errorMsg); |
84 #endif |
87 emit networkErrorUrl(requestUrl); |
|
88 } |
|
89 } |
85 |
90 |
86 WebNetworkAccessManager::~WebNetworkAccessManager() |
91 WebNetworkAccessManager::~WebNetworkAccessManager() |
87 { |
92 { |
88 delete m_cookieJar; |
93 delete m_cookieJar; |
89 //setCache(NULL); |
94 setCache(NULL); |
90 delete m_reply; |
|
91 } |
95 } |
92 |
96 |
93 QNetworkReply* WebNetworkAccessManager::createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData) |
97 QNetworkReply* WebNetworkAccessManager::createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData) |
94 { |
98 { |
95 QNetworkRequest req = request; |
99 QNetworkRequest req = request; |
96 |
100 |
97 req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); |
101 req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); |
|
102 // improve performance by |
|
103 // load from cache if available, otherwise load from network. |
|
104 // Note that this can return possibly stale (but not expired) items from cache. |
|
105 // QNetworkRequest::PreferNetwork is default value |
|
106 req.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); |
98 |
107 |
99 if (m_reply != NULL) { |
108 if (m_reply != NULL) { |
100 delete m_reply; |
109 delete m_reply; |
101 m_reply = NULL; |
110 m_reply = NULL; |
102 } |
111 } |
103 QNetworkReply* reply = NULL; |
112 QNetworkReply* reply = NULL; |
104 |
113 |
105 |
114 |
106 if(m_browserContainer->mainFrame()) { |
115 if(m_browserContainer->mainFrame()) { |
107 if(m_browserContainer->mainFrame()->url().scheme().contains("https")) { |
116 if(m_browserContainer->mainFrame()->url().scheme() == "https") { |
108 |
117 |
109 if (op == QNetworkAccessManager::PostOperation && req.url().scheme().contains("http")) { |
118 if (op == QNetworkAccessManager::PostOperation && req.url().scheme() == "http") { |
110 |
119 |
111 m_text = tr("Secure Page Warning:"); |
120 m_text = tr("Secure Page Warning:"); |
112 m_informativeText = tr("Do you want to continue?"); |
121 m_informativeText = tr("Do you want to continue?"); |
113 m_detailedText = tr("Submit your data over an unencrypted connection."); |
122 m_detailedText = tr("Submit your data over an unencrypted connection."); |
114 m_buttons = QMessageBox::Yes | QMessageBox::No; |
123 m_buttons = QMessageBox::Yes | QMessageBox::No; |