diff options
Diffstat (limited to 'src/core/web_contents_adapter.cpp')
-rw-r--r-- | src/core/web_contents_adapter.cpp | 155 |
1 files changed, 115 insertions, 40 deletions
diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp index fc54c98ed..fadbd6d2e 100644 --- a/src/core/web_contents_adapter.cpp +++ b/src/core/web_contents_adapter.cpp @@ -46,14 +46,16 @@ #include "browser_accessibility_qt.h" #include "browser_context_adapter.h" +#include "browser_context_adapter_client.h" #include "browser_context_qt.h" #include "download_manager_delegate_qt.h" #include "media_capture_devices_dispatcher.h" +#include "pdfium_printing_wrapper_qt.h" #include "print_view_manager_qt.h" #include "qwebenginecallback_p.h" +#include "renderer_host/web_channel_ipc_transport_host.h" #include "render_view_observer_host_qt.h" #include "type_conversion.h" -#include "web_channel_ipc_transport_host.h" #include "web_contents_adapter_client.h" #include "web_contents_view_qt.h" #include "web_engine_context.h" @@ -90,6 +92,9 @@ #include <QtGui/qaccessible.h> #include <QtGui/qdrag.h> #include <QtGui/qpixmap.h> +#if !defined(QT_NO_WIDGETS) && !defined(QT_NO_PRINTER) +#include <QtPrintSupport/qprinter.h> +#endif // QT_NO_PRINTER #include <QtWebChannel/QWebChannel> namespace QtWebEngineCore { @@ -188,6 +193,17 @@ static void callbackOnPrintingFinished(WebContentsAdapterClient *adapterClient, } } +#if !defined(QT_NO_WIDGETS) && !defined(QT_NO_PRINTER) +static void callbackOnPrintingOnPrinterFinished(WebContentsAdapterClient *adapterClient, int requestId, QPrinter *printer, const std::vector<char> &result) +{ + if (requestId) { + PdfiumPrintingWrapperQt printWrapper(result.data(), result.size()); + bool printerResult = printWrapper.printOnPrinter(*printer); + adapterClient->didPrintPageOnPrinter(requestId, printerResult); + } +} +#endif // QT_NO_PRINTER + static content::WebContents *createBlankWebContents(WebContentsAdapterClient *adapterClient, content::BrowserContext *browserContext) { content::WebContents::CreateParams create_params(browserContext, NULL); @@ -231,7 +247,7 @@ static void serializeNavigationHistory(const content::NavigationController &cont } } -static void deserializeNavigationHistory(QDataStream &input, int *currentIndex, std::vector<scoped_ptr<content::NavigationEntry>> *entries, content::BrowserContext *browserContext) +static void deserializeNavigationHistory(QDataStream &input, int *currentIndex, std::vector<std::unique_ptr<content::NavigationEntry>> *entries, content::BrowserContext *browserContext) { int version; input >> version; @@ -280,7 +296,7 @@ static void deserializeNavigationHistory(QDataStream &input, int *currentIndex, return; } - scoped_ptr<content::NavigationEntry> entry = content::NavigationController::CreateNavigationEntry( + std::unique_ptr<content::NavigationEntry> entry = content::NavigationController::CreateNavigationEntry( toGurl(virtualUrl), content::Referrer(toGurl(referrerUrl), static_cast<blink::WebReferrerPolicy>(referrerPolicy)), // Use a transition type of reload so that we don't incorrectly @@ -335,7 +351,6 @@ WebContentsAdapterPrivate::WebContentsAdapterPrivate() , adapterClient(0) , nextRequestId(CallbackDirectory::ReservedCallbackIdsEnd) , lastFindRequestId(0) - , currentDropData(nullptr) , currentDropAction(Qt::IgnoreAction) , inDragUpdateLoop(false) , updateDragCursorMessagePollingTimer(new QTimer) @@ -351,7 +366,7 @@ WebContentsAdapterPrivate::~WebContentsAdapterPrivate() QSharedPointer<WebContentsAdapter> WebContentsAdapter::createFromSerializedNavigationHistory(QDataStream &input, WebContentsAdapterClient *adapterClient) { int currentIndex; - std::vector<scoped_ptr<content::NavigationEntry>> entries; + std::vector<std::unique_ptr<content::NavigationEntry>> entries; deserializeNavigationHistory(input, ¤tIndex, &entries, adapterClient->browserContextAdapter()->browserContext()); if (currentIndex == -1) @@ -469,21 +484,21 @@ void WebContentsAdapter::stop() controller.RemoveEntryAtIndex(index); d->webContents->Stop(); - d->webContents->Focus(); + focusIfNecessary(); } void WebContentsAdapter::reload() { Q_D(WebContentsAdapter); d->webContents->GetController().Reload(/*checkRepost = */false); - d->webContents->Focus(); + focusIfNecessary(); } void WebContentsAdapter::reloadAndBypassCache() { Q_D(WebContentsAdapter); - d->webContents->GetController().ReloadIgnoringCache(/*checkRepost = */false); - d->webContents->Focus(); + d->webContents->GetController().ReloadBypassingCache(/*checkRepost = */false); + focusIfNecessary(); } void WebContentsAdapter::load(const QUrl &url) @@ -502,11 +517,23 @@ void WebContentsAdapter::load(const QUrl &url) Q_UNUSED(guard); Q_D(WebContentsAdapter); - content::NavigationController::LoadURLParams params(toGurl(url)); + GURL gurl = toGurl(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 (pageUrl.scheme().isEmpty()) { + QUrl extendedUrl = QUrl::fromUserInput(pageUrl.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.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE; d->webContents->GetController().LoadURLWithParams(params); - d->webContents->Focus(); + focusIfNecessary(); } void WebContentsAdapter::setContent(const QByteArray &data, const QString &mimeType, const QUrl &baseUrl) @@ -519,7 +546,7 @@ void WebContentsAdapter::setContent(const QByteArray &data, const QString &mimeT urlString.append(encodedData.constData(), encodedData.length()); GURL dataUrlToLoad(urlString); - if (dataUrlToLoad.spec().size() > content::kMaxURLChars) { + if (dataUrlToLoad.spec().size() > url::kMaxURLChars) { d->adapterClient->loadFinished(false, baseUrl, false, net::ERR_ABORTED); return; } @@ -531,13 +558,14 @@ void WebContentsAdapter::setContent(const QByteArray &data, const QString &mimeT params.transition_type = ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_API); params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE; d->webContents->GetController().LoadURLWithParams(params); - d->webContents->Focus(); + focusIfNecessary(); d->webContents->Unselect(); } -void WebContentsAdapter::save() +void WebContentsAdapter::save(const QString &filePath, int savePageFormat) { Q_D(WebContentsAdapter); + d->webContentsDelegate->setSavePageInfo(SavePageInfo(filePath, savePageFormat)); d->webContents->OnSavePage(); } @@ -631,7 +659,7 @@ void WebContentsAdapter::selectAll() void WebContentsAdapter::requestClose() { Q_D(WebContentsAdapter); - d->webContents->DispatchBeforeUnload(false); + d->webContents->DispatchBeforeUnload(); } void WebContentsAdapter::unselect() @@ -644,14 +672,14 @@ void WebContentsAdapter::navigateToIndex(int offset) { Q_D(WebContentsAdapter); d->webContents->GetController().GoToIndex(offset); - d->webContents->Focus(); + focusIfNecessary(); } void WebContentsAdapter::navigateToOffset(int offset) { Q_D(WebContentsAdapter); d->webContents->GetController().GoToOffset(offset); - d->webContents->Focus(); + focusIfNecessary(); } int WebContentsAdapter::navigationEntryCount() @@ -849,13 +877,19 @@ void WebContentsAdapter::updateWebPreferences(const content::WebPreferences & we void WebContentsAdapter::download(const QUrl &url, const QString &suggestedFileName) { + Q_D(WebContentsAdapter); content::BrowserContext *bctx = webContents()->GetBrowserContext(); content::DownloadManager *dlm = content::BrowserContext::GetDownloadManager(bctx); + DownloadManagerDelegateQt *dlmd = d->browserContextAdapter->downloadManagerDelegate(); + if (!dlm) return; - scoped_ptr<content::DownloadUrlParameters> params( - content::DownloadUrlParameters::FromWebContents(webContents(), toGurl(url))); + dlmd->setDownloadType(BrowserContextAdapterClient::UserRequested); + dlm->SetDelegate(dlmd); + + std::unique_ptr<content::DownloadUrlParameters> params( + content::DownloadUrlParameters::CreateForWebContentsMainFrame(webContents(), toGurl(url))); params->set_suggested_name(toString16(suggestedFileName)); dlm->DownloadUrl(std::move(params)); } @@ -881,7 +915,7 @@ bool WebContentsAdapter::recentlyAudible() void WebContentsAdapter::copyImageAt(const QPoint &location) { Q_D(WebContentsAdapter); - d->webContents->GetRenderViewHost()->CopyImageAt(location.x(), location.y()); + d->webContents->GetRenderViewHost()->GetMainFrame()->CopyImageAt(location.x(), location.y()); } ASSERT_ENUMS_MATCH(WebContentsAdapter::MediaPlayerNoAction, blink::WebMediaPlayerAction::Unknown) @@ -916,13 +950,13 @@ bool WebContentsAdapter::hasInspector() const void WebContentsAdapter::exitFullScreen() { Q_D(WebContentsAdapter); - d->webContents->ExitFullscreen(); + d->webContents->ExitFullscreen(false); } void WebContentsAdapter::changedFullScreen() { Q_D(WebContentsAdapter); - d->webContents->NotifyFullscreenChanged(); + d->webContents->NotifyFullscreenChanged(false); } void WebContentsAdapter::wasShown() @@ -940,7 +974,7 @@ void WebContentsAdapter::wasHidden() void WebContentsAdapter::printToPDF(const QPageLayout &pageLayout, const QString &filePath) { #if defined(ENABLE_BASIC_PRINTING) - PrintViewManagerQt::FromWebContents(webContents())->PrintToPDF(pageLayout, filePath); + PrintViewManagerQt::FromWebContents(webContents())->PrintToPDF(pageLayout, true, filePath); #endif // if defined(ENABLE_BASIC_PRINTING) } @@ -948,13 +982,33 @@ quint64 WebContentsAdapter::printToPDFCallbackResult(const QPageLayout &pageLayo { #if defined(ENABLE_BASIC_PRINTING) Q_D(WebContentsAdapter); - PrintViewManagerQt::PrintToPDFCallback callback = base::Bind(&callbackOnPrintingFinished, d->adapterClient, d->nextRequestId); - PrintViewManagerQt::FromWebContents(webContents())->PrintToPDFWithCallback(pageLayout, callback); + PrintViewManagerQt::PrintToPDFCallback callback = base::Bind(&callbackOnPrintingFinished + , d->adapterClient + , d->nextRequestId); + PrintViewManagerQt::FromWebContents(webContents())->PrintToPDFWithCallback(pageLayout, true + , callback); + return d->nextRequestId++; +#else + return 0; +#endif // if defined(ENABLE_BASIC_PRINTING) +} + +#if !defined(QT_NO_WIDGETS) && !defined(QT_NO_PRINTER) +quint64 WebContentsAdapter::printOnPrinterCallbackResult(QPrinter *printer) +{ +#if defined(ENABLE_BASIC_PRINTING) + Q_D(WebContentsAdapter); + PrintViewManagerQt::PrintToPDFCallback callback + = base::Bind(&callbackOnPrintingOnPrinterFinished, d->adapterClient + , d->nextRequestId, printer); + PrintViewManagerQt::FromWebContents(webContents())->PrintToPDFWithCallback( + printer->pageLayout(), printer->colorMode() == QPrinter::Color, callback); return d->nextRequestId++; #else return 0; #endif // if defined(ENABLE_BASIC_PRINTING) } +#endif // QT_NO_PRINTER QPointF WebContentsAdapter::lastScrollOffset() const { @@ -1079,15 +1133,14 @@ void WebContentsAdapter::startDragging(QObject *dragSource, const content::DropD // Clear certain fields of the drop data to not run into DCHECKs // of DropDataToWebDragData in render_view_impl.cc. - content::DropData fixedDropData = dropData; - fixedDropData.download_metadata.clear(); - fixedDropData.file_contents.clear(); - fixedDropData.file_description_filename.clear(); + d->currentDropData.reset(new content::DropData(dropData)); + d->currentDropData->download_metadata.clear(); + d->currentDropData->file_contents.clear(); + d->currentDropData->file_description_filename.clear(); d->currentDropAction = Qt::IgnoreAction; - d->currentDropData = &fixedDropData; QDrag *drag = new QDrag(dragSource); // will be deleted by Qt's DnD implementation - drag->setMimeData(mimeDataFromDropData(fixedDropData)); + drag->setMimeData(mimeDataFromDropData(*d->currentDropData)); if (!pixmap.isNull()) { drag->setPixmap(pixmap); drag->setHotSpot(offset); @@ -1100,7 +1153,7 @@ void WebContentsAdapter::startDragging(QObject *dragSource, const content::DropD content::RenderViewHost *rvh = d->webContents->GetRenderViewHost(); rvh->DragSourceSystemDragEnded(); - d->currentDropData = nullptr; + d->currentDropData.reset(); } static blink::WebDragOperationsMask toWeb(const Qt::DropActions action) @@ -1137,17 +1190,15 @@ void WebContentsAdapter::enterDrag(QDragEnterEvent *e, const QPoint &screenPos) { Q_D(WebContentsAdapter); - scoped_ptr<content::DropData> ownedDropData; - const content::DropData *rvhDropData = d->currentDropData; - if (!rvhDropData) { + if (!d->currentDropData) { // The drag originated outside the WebEngineView. - ownedDropData.reset(new content::DropData); - fillDropDataFromMimeData(ownedDropData.get(), e->mimeData()); - rvhDropData = ownedDropData.get(); + d->currentDropData.reset(new content::DropData); + fillDropDataFromMimeData(d->currentDropData.get(), e->mimeData()); } content::RenderViewHost *rvh = d->webContents->GetRenderViewHost(); - rvh->DragTargetDragEnter(*rvhDropData, toGfx(e->pos()), toGfx(screenPos), + rvh->FilterDropData(d->currentDropData.get()); + rvh->DragTargetDragEnter(*d->currentDropData, toGfx(e->pos()), toGfx(screenPos), toWeb(e->possibleActions()), flagsFromModifiers(e->keyboardModifiers())); } @@ -1203,7 +1254,9 @@ void WebContentsAdapter::endDragging(const QPoint &clientPos, const QPoint &scre Q_D(WebContentsAdapter); finishDragUpdate(); content::RenderViewHost *rvh = d->webContents->GetRenderViewHost(); - rvh->DragTargetDrop(toGfx(clientPos), toGfx(screenPos), 0); + rvh->FilterDropData(d->currentDropData.get()); + rvh->DragTargetDrop(*d->currentDropData, toGfx(clientPos), toGfx(screenPos), 0); + d->currentDropData.reset(); } void WebContentsAdapter::leaveDrag() @@ -1212,6 +1265,7 @@ void WebContentsAdapter::leaveDrag() finishDragUpdate(); content::RenderViewHost *rvh = d->webContents->GetRenderViewHost(); rvh->DragTargetDragLeave(); + d->currentDropData.reset(); } void WebContentsAdapter::initUpdateDragCursorMessagePollingTimer() @@ -1242,6 +1296,15 @@ void WebContentsAdapter::replaceMisspelling(const QString &word) #endif } +void WebContentsAdapter::focusIfNecessary() +{ + Q_D(WebContentsAdapter); + const WebEngineSettings *settings = d->adapterClient->webEngineSettings(); + bool focusOnNavigation = settings->testAttribute(WebEngineSettings::FocusOnNavigationEnabled); + if (focusOnNavigation) + d->webContents->Focus(); +} + WebContentsAdapterClient::RenderProcessTerminationStatus WebContentsAdapterClient::renderProcessExitStatus(int terminationStatus) { auto status = WebContentsAdapterClient::RenderProcessTerminationStatus(-1); @@ -1280,4 +1343,16 @@ FaviconManager *WebContentsAdapter::faviconManager() return d->webContentsDelegate->faviconManager(); } +void WebContentsAdapter::viewSource() +{ + Q_D(WebContentsAdapter); + d->webContents->ViewSource(); +} + +bool WebContentsAdapter::canViewSource() +{ + Q_D(WebContentsAdapter); + return d->webContents->GetController().CanViewSource(); +} + } // namespace QtWebEngineCore |