diff options
Diffstat (limited to 'src/core/web_contents_adapter.cpp')
-rw-r--r-- | src/core/web_contents_adapter.cpp | 118 |
1 files changed, 96 insertions, 22 deletions
diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp index cff8a025a..7b0712143 100644 --- a/src/core/web_contents_adapter.cpp +++ b/src/core/web_contents_adapter.cpp @@ -77,6 +77,7 @@ #include "content/public/common/page_state.h" #include "content/public/common/page_zoom.h" #include "content/public/common/renderer_preferences.h" +#include "content/public/common/resource_request_body.h" #include "content/public/common/url_constants.h" #include "content/public/common/web_preferences.h" #include "third_party/WebKit/public/web/WebFindOptions.h" @@ -183,12 +184,23 @@ static void callbackOnEvaluateJS(WebContentsAdapterClient *adapterClient, quint6 adapterClient->didRunJavaScript(requestId, fromJSValue(result)); } -static void callbackOnPrintingFinished(WebContentsAdapterClient *adapterClient, int requestId, const std::vector<char>& result) +#if defined(ENABLE_BASIC_PRINTING) +static void callbackOnPrintingFinished(WebContentsAdapterClient *adapterClient, + int requestId, + const std::vector<char>& result) { if (requestId) adapterClient->didPrintPage(requestId, QByteArray(result.data(), result.size())); } +static void callbackOnPdfSavingFinished(WebContentsAdapterClient *adapterClient, + const QString& filePath, + bool success) +{ + adapterClient->didPrintPageToPdf(filePath, success); +} +#endif + static content::WebContents *createBlankWebContents(WebContentsAdapterClient *adapterClient, content::BrowserContext *browserContext) { content::WebContents::CreateParams create_params(browserContext, NULL); @@ -358,7 +370,7 @@ QSharedPointer<WebContentsAdapter> WebContentsAdapter::createFromSerializedNavig // Unlike WebCore, Chromium only supports Restoring to a new WebContents instance. content::WebContents* newWebContents = createBlankWebContents(adapterClient, adapterClient->browserContextAdapter()->browserContext()); content::NavigationController &controller = newWebContents->GetController(); - controller.Restore(currentIndex, content::NavigationController::RESTORE_LAST_SESSION_EXITED_CLEANLY, &entries); + controller.Restore(currentIndex, content::RestoreType::LAST_SESSION_EXITED_CLEANLY, &entries); if (controller.GetActiveEntry()) { // Set up the file access rights for the selected navigation entry. @@ -485,6 +497,12 @@ void WebContentsAdapter::reloadAndBypassCache() void WebContentsAdapter::load(const QUrl &url) { + QWebEngineHttpRequest request(url); + load(request); +} + +void WebContentsAdapter::load(const QWebEngineHttpRequest &request) +{ // The situation can occur when relying on the editingFinished signal in QML to set the url // of the WebView. // When enter is pressed, onEditingFinished fires and the url of the webview is set, which @@ -499,21 +517,55 @@ void WebContentsAdapter::load(const QUrl &url) Q_UNUSED(guard); Q_D(WebContentsAdapter); - GURL gurl = toGurl(url); + GURL gurl = toGurl(request.url()); // Add URL scheme if missing from view-source URL. - if (url.scheme() == content::kViewSourceScheme) { - QUrl pageUrl = QUrl(url.toString().remove(0, strlen(content::kViewSourceScheme) + 1)); + if (request.url().scheme() == content::kViewSourceScheme) { + QUrl pageUrl = QUrl(request.url().toString().remove(0, + strlen(content::kViewSourceScheme) + 1)); if (pageUrl.scheme().isEmpty()) { QUrl extendedUrl = QUrl::fromUserInput(pageUrl.toString()); - extendedUrl = QUrl(QString("%1:%2").arg(content::kViewSourceScheme, extendedUrl.toString())); + extendedUrl = QUrl(QString("%1:%2").arg(content::kViewSourceScheme, + extendedUrl.toString())); gurl = toGurl(extendedUrl); } } content::NavigationController::LoadURLParams params(gurl); - params.transition_type = ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); + params.transition_type = ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED + | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE; + + switch (request.method()) { + case QWebEngineHttpRequest::Get: + params.load_type = content::NavigationController::LOAD_TYPE_DEFAULT; + break; + + case QWebEngineHttpRequest::Post: + params.load_type = content::NavigationController::LOAD_TYPE_HTTP_POST; + // chromium accepts LOAD_TYPE_HTTP_POST only for the HTTP and HTTPS protocols + if (!params.url.SchemeIsHTTPOrHTTPS()) { + d->adapterClient->loadFinished(false, request.url(), false, + net::ERR_DISALLOWED_URL_SCHEME, + QCoreApplication::translate("WebContentsAdapter", + "HTTP-POST data can only be sent over HTTP(S) protocol")); + return; + } + break; + } + + params.post_data = content::ResourceRequestBody::CreateFromBytes( + (const char*)request.postData().constData(), + request.postData().length()); + + // convert the custom headers into the format that chromium expects + QVector<QByteArray> headers = request.headers(); + for (QVector<QByteArray>::const_iterator it = headers.cbegin(); it != headers.cend(); ++it) { + if (params.extra_headers.length() > 0) + params.extra_headers += '\n'; + params.extra_headers += (*it).toStdString() + ": " + request.header(*it).toStdString(); + } + d->webContents->GetController().LoadURLWithParams(params); focusIfNecessary(); } @@ -560,15 +612,17 @@ QUrl WebContentsAdapter::activeUrl() const QUrl WebContentsAdapter::requestedUrl() const { Q_D(const WebContentsAdapter); - content::NavigationEntry* entry = d->webContents->GetController().GetVisibleEntry(); - content::NavigationEntry* pendingEntry = d->webContents->GetController().GetPendingEntry(); + if (d->webContents) { + content::NavigationEntry* entry = d->webContents->GetController().GetVisibleEntry(); + content::NavigationEntry* pendingEntry = d->webContents->GetController().GetPendingEntry(); - if (entry) { - if (!entry->GetOriginalRequestURL().is_empty()) - return toQt(entry->GetOriginalRequestURL()); + if (entry) { + if (!entry->GetOriginalRequestURL().is_empty()) + return toQt(entry->GetOriginalRequestURL()); - if (pendingEntry && pendingEntry == entry) - return toQt(entry->GetURL()); + if (pendingEntry && pendingEntry == entry) + return toQt(entry->GetURL()); + } } return QUrl(); } @@ -917,7 +971,7 @@ void WebContentsAdapter::inspectElementAt(const QPoint &location) { Q_D(WebContentsAdapter); if (content::DevToolsAgentHost::HasFor(d->webContents.get())) { - content::DevToolsAgentHost::GetOrCreateFor(d->webContents.get())->InspectElement(location.x(), location.y()); + content::DevToolsAgentHost::GetOrCreateFor(d->webContents.get())->InspectElement(nullptr, location.x(), location.y()); } } @@ -956,19 +1010,28 @@ void WebContentsAdapter::wasHidden() void WebContentsAdapter::printToPDF(const QPageLayout &pageLayout, const QString &filePath) { #if defined(ENABLE_BASIC_PRINTING) - PrintViewManagerQt::FromWebContents(webContents())->PrintToPDF(pageLayout, true, filePath); + Q_D(WebContentsAdapter); + PrintViewManagerQt::PrintToPDFFileCallback callback = base::Bind(&callbackOnPdfSavingFinished, + d->adapterClient, + filePath); + PrintViewManagerQt::FromWebContents(webContents())->PrintToPDFFileWithCallback(pageLayout, + true, + filePath, + callback); #endif // if defined(ENABLE_BASIC_PRINTING) } -quint64 WebContentsAdapter::printToPDFCallbackResult(const QPageLayout &pageLayout, const bool colorMode) +quint64 WebContentsAdapter::printToPDFCallbackResult(const QPageLayout &pageLayout, + const bool colorMode) { #if defined(ENABLE_BASIC_PRINTING) Q_D(WebContentsAdapter); - PrintViewManagerQt::PrintToPDFCallback callback = base::Bind(&callbackOnPrintingFinished - , d->adapterClient - , d->nextRequestId); - PrintViewManagerQt::FromWebContents(webContents())->PrintToPDFWithCallback(pageLayout, colorMode - , callback); + PrintViewManagerQt::PrintToPDFCallback callback = base::Bind(&callbackOnPrintingFinished, + d->adapterClient, + d->nextRequestId); + PrintViewManagerQt::FromWebContents(webContents())->PrintToPDFWithCallback(pageLayout, + colorMode, + callback); return d->nextRequestId++; #else return 0; @@ -1348,4 +1411,15 @@ bool WebContentsAdapter::canViewSource() return d->webContents->GetController().CanViewSource(); } +ASSERT_ENUMS_MATCH(WebContentsAdapterClient::UnknownDisposition, WindowOpenDisposition::UNKNOWN) +ASSERT_ENUMS_MATCH(WebContentsAdapterClient::CurrentTabDisposition, WindowOpenDisposition::CURRENT_TAB) +ASSERT_ENUMS_MATCH(WebContentsAdapterClient::SingletonTabDisposition, WindowOpenDisposition::SINGLETON_TAB) +ASSERT_ENUMS_MATCH(WebContentsAdapterClient::NewForegroundTabDisposition, WindowOpenDisposition::NEW_FOREGROUND_TAB) +ASSERT_ENUMS_MATCH(WebContentsAdapterClient::NewBackgroundTabDisposition, WindowOpenDisposition::NEW_BACKGROUND_TAB) +ASSERT_ENUMS_MATCH(WebContentsAdapterClient::NewPopupDisposition, WindowOpenDisposition::NEW_POPUP) +ASSERT_ENUMS_MATCH(WebContentsAdapterClient::NewWindowDisposition, WindowOpenDisposition::NEW_WINDOW) +ASSERT_ENUMS_MATCH(WebContentsAdapterClient::SaveToDiskDisposition, WindowOpenDisposition::SAVE_TO_DISK) +ASSERT_ENUMS_MATCH(WebContentsAdapterClient::OffTheRecordDisposition, WindowOpenDisposition::OFF_THE_RECORD) +ASSERT_ENUMS_MATCH(WebContentsAdapterClient::IgnoreActionDisposition, WindowOpenDisposition::IGNORE_ACTION) + } // namespace QtWebEngineCore |