Changeset View
Changeset View
Standalone View
Standalone View
webenginepart/src/webenginepartdownloadmanager.cpp
Show All 29 Lines | |||||
30 | WebEnginePartDownloadManager::WebEnginePartDownloadManager() | 30 | WebEnginePartDownloadManager::WebEnginePartDownloadManager() | ||
31 | : QObject() | 31 | : QObject() | ||
32 | { | 32 | { | ||
33 | connect(QWebEngineProfile::defaultProfile(), &QWebEngineProfile::downloadRequested, this, &WebEnginePartDownloadManager::performDownload); | 33 | connect(QWebEngineProfile::defaultProfile(), &QWebEngineProfile::downloadRequested, this, &WebEnginePartDownloadManager::performDownload); | ||
34 | } | 34 | } | ||
35 | 35 | | |||
36 | WebEnginePartDownloadManager::~WebEnginePartDownloadManager() | 36 | WebEnginePartDownloadManager::~WebEnginePartDownloadManager() | ||
37 | { | 37 | { | ||
38 | #ifndef DOWNLOADITEM_KNOWS_PAGE | ||||
38 | m_requests.clear(); | 39 | m_requests.clear(); | ||
40 | #endif | ||||
39 | } | 41 | } | ||
40 | 42 | | |||
41 | WebEnginePartDownloadManager * WebEnginePartDownloadManager::instance() | 43 | WebEnginePartDownloadManager * WebEnginePartDownloadManager::instance() | ||
42 | { | 44 | { | ||
43 | static WebEnginePartDownloadManager inst; | 45 | static WebEnginePartDownloadManager inst; | ||
44 | return &inst; | 46 | return &inst; | ||
45 | } | 47 | } | ||
46 | 48 | | |||
47 | void WebEnginePartDownloadManager::addPage(WebEnginePage* page) | 49 | void WebEnginePartDownloadManager::addPage(WebEnginePage* page) | ||
48 | { | 50 | { | ||
49 | if (!m_pages.contains(page)) { | 51 | if (!m_pages.contains(page)) { | ||
50 | m_pages.append(page); | 52 | m_pages.append(page); | ||
51 | } | 53 | } | ||
54 | #ifndef DOWNLOADITEM_KNOWS_PAGE | ||||
52 | connect(page, &WebEnginePage::navigationRequested, this, &WebEnginePartDownloadManager::recordNavigationRequest); | 55 | connect(page, &WebEnginePage::navigationRequested, this, &WebEnginePartDownloadManager::recordNavigationRequest); | ||
56 | #endif | ||||
53 | connect(page, &QObject::destroyed, this, &WebEnginePartDownloadManager::removePage); | 57 | connect(page, &QObject::destroyed, this, &WebEnginePartDownloadManager::removePage); | ||
54 | } | 58 | } | ||
55 | 59 | | |||
56 | void WebEnginePartDownloadManager::removePage(QObject* page) | 60 | void WebEnginePartDownloadManager::removePage(QObject* page) | ||
57 | { | 61 | { | ||
62 | #ifndef DOWNLOADITEM_KNOWS_PAGE | ||||
58 | const QUrl url = m_requests.key(static_cast<WebEnginePage *>(page)); | 63 | const QUrl url = m_requests.key(static_cast<WebEnginePage *>(page)); | ||
59 | m_requests.remove(url); | 64 | m_requests.remove(url); | ||
65 | #endif | ||||
60 | m_pages.removeOne(static_cast<WebEnginePage*>(page)); | 66 | m_pages.removeOne(static_cast<WebEnginePage*>(page)); | ||
61 | } | 67 | } | ||
62 | 68 | | |||
63 | void WebEnginePartDownloadManager::performDownload(QWebEngineDownloadItem* it) | 69 | void WebEnginePartDownloadManager::performDownload(QWebEngineDownloadItem* it) | ||
64 | { | 70 | { | ||
71 | #ifdef DOWNLOADITEM_KNOWS_PAGE | ||||
72 | WebEnginePage *page = qobject_cast<WebEnginePage*>(it->page()); | ||||
73 | #else | ||||
65 | WebEnginePage *page = m_requests.take(it->url()); | 74 | WebEnginePage *page = m_requests.take(it->url()); | ||
75 | #endif | ||||
66 | bool forceNew = false; | 76 | bool forceNew = false; | ||
77 | //According to the documentation, QWebEngineDownloadItem::page() can return nullptr "if the download was not triggered by content in a page" | ||||
67 | if (!page && !m_pages.isEmpty()) { | 78 | if (!page && !m_pages.isEmpty()) { | ||
68 | qCDebug(WEBENGINEPART_LOG) << "downloading" << it->url() << "in new window or tab"; | 79 | qCDebug(WEBENGINEPART_LOG) << "downloading" << it->url() << "in new window or tab"; | ||
69 | page = m_pages.first(); | 80 | page = m_pages.first(); | ||
70 | forceNew = true; | 81 | forceNew = true; | ||
71 | } else if (!page) { | 82 | } else if (!page) { | ||
72 | qCDebug(WEBENGINEPART_LOG) << "Couldn't find a part wanting to download" << it->url(); | 83 | qCDebug(WEBENGINEPART_LOG) << "Couldn't find a part wanting to download" << it->url(); | ||
73 | return; | 84 | return; | ||
74 | } | 85 | } | ||
75 | page->download(it->url(), forceNew); | 86 | page->download(it->url(), forceNew); | ||
76 | } | 87 | } | ||
77 | 88 | | |||
89 | #ifndef DOWNLOADITEM_KNOWS_PAGE | ||||
90 | | ||||
78 | void WebEnginePartDownloadManager::recordNavigationRequest(WebEnginePage *page, const QUrl& url) | 91 | void WebEnginePartDownloadManager::recordNavigationRequest(WebEnginePage *page, const QUrl& url) | ||
79 | { | 92 | { | ||
80 | // qCDebug(WEBENGINEPART_LOG) << url; | 93 | // qCDebug(WEBENGINEPART_LOG) << url; | ||
81 | m_requests.insert(url, page); | 94 | m_requests.insert(url, page); | ||
82 | } | 95 | } | ||
83 | 96 | | |||
84 | WebEnginePage* WebEnginePartDownloadManager::pageForDownload(QWebEngineDownloadItem* it) | 97 | WebEnginePage* WebEnginePartDownloadManager::pageForDownload(QWebEngineDownloadItem* it) | ||
85 | { | 98 | { | ||
86 | WebEnginePage *page = m_requests.value(it->url()); | 99 | WebEnginePage *page = m_requests.value(it->url()); | ||
87 | if (!page && !m_pages.isEmpty()) { | 100 | if (!page && !m_pages.isEmpty()) { | ||
88 | page = m_pages.first(); | 101 | page = m_pages.first(); | ||
89 | } | 102 | } | ||
90 | return page; | 103 | return page; | ||
91 | } | 104 | } | ||
105 | #endif |