summaryrefslogtreecommitdiffstats
path: root/src/core/favicon_driver_qt.cpp
blob: bb4a734b415eb873c9fb126ed5ba63b70f681c61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

// Based on components/favicon/content/content_favicon_driver.cc:
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "favicon_driver_qt.h"
#include "type_conversion.h"
#include "web_contents_adapter_client.h"
#include "web_engine_settings.h"

#include "components/favicon/content/favicon_url_util.h"
#include "components/favicon/core/favicon_service.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/favicon_status.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/page.h"
#include "content/public/browser/render_frame_host.h"
#include "third_party/blink/public/mojom/manifest/manifest.mojom.h"

namespace QtWebEngineCore {

namespace {

int activeHandlersCount(QWebEngineSettings *settings)
{
    bool touchIconsEnabled = settings->testAttribute(QWebEngineSettings::TouchIconsEnabled);
    return touchIconsEnabled ? 2 : 1;
}

} // namespace

FaviconStatusQt::FaviconStatusQt()
    : FaviconStatus(), source(favicon::FaviconDriverObserver::NON_TOUCH_16_DIP)
{
}

// static
void FaviconDriverQt::CreateForWebContents(content::WebContents *webContents,
                                           favicon::CoreFaviconService *faviconService,
                                           WebContentsAdapterClient *viewClient)
{
    if (FromWebContents(webContents))
        return;

    webContents->SetUserData(
            UserDataKey(),
            base::WrapUnique(new FaviconDriverQt(webContents, faviconService, viewClient)));
}

FaviconDriverQt::FaviconDriverQt(content::WebContents *webContents,
                                 favicon::CoreFaviconService *faviconService,
                                 WebContentsAdapterClient *viewClient)
    : content::WebContentsObserver(webContents)
    , content::WebContentsUserData<FaviconDriverQt>(*webContents)
    , m_faviconService(faviconService)
    , m_viewClient(viewClient)
{
    if (!m_faviconService)
        return;

    m_handlers.push_back(std::make_unique<favicon::FaviconHandler>(
            m_faviconService, this, favicon::FaviconDriverObserver::NON_TOUCH_16_DIP));
    m_handlers.push_back(std::make_unique<favicon::FaviconHandler>(
            m_faviconService, this, favicon::FaviconDriverObserver::NON_TOUCH_LARGEST));
    m_handlers.push_back(std::make_unique<favicon::FaviconHandler>(
            m_faviconService, this, favicon::FaviconDriverObserver::TOUCH_LARGEST));
}

void FaviconDriverQt::FetchFavicon(const GURL &page_url, bool is_same_document)
{
    QWebEngineSettings *settings = m_viewClient->webEngineSettings();
    bool iconsEnabled = settings->testAttribute(QWebEngineSettings::AutoLoadIconsForPage);
    bool touchIconsEnabled = settings->testAttribute(QWebEngineSettings::TouchIconsEnabled);

    if (!iconsEnabled)
        return;

    for (const std::unique_ptr<favicon::FaviconHandler> &handler : m_handlers) {
        switch (handler->Type()) {
        case favicon::FaviconDriverObserver::NON_TOUCH_16_DIP:
            if (touchIconsEnabled)
                continue;
            break;
        case favicon::FaviconDriverObserver::NON_TOUCH_LARGEST:
        case favicon::FaviconDriverObserver::TOUCH_LARGEST:
            if (!touchIconsEnabled)
                continue;
            break;
        }

        handler->FetchFavicon(page_url, is_same_document);
    }
}

gfx::Image FaviconDriverQt::GetFavicon() const
{
    // Like GetTitle(), we also want to use the favicon for the last committed
    // entry rather than a pending navigation entry.
    content::NavigationController &controller = web_contents()->GetController();

    content::NavigationEntry *entry = controller.GetLastCommittedEntry();
    if (entry)
        return entry->GetFavicon().image;
    return gfx::Image();
}

bool FaviconDriverQt::FaviconIsValid() const
{
    content::NavigationController &controller = web_contents()->GetController();

    content::NavigationEntry *entry = controller.GetLastCommittedEntry();
    if (entry)
        return entry->GetFavicon().valid;

    return false;
}

GURL FaviconDriverQt::GetActiveURL()
{
    content::NavigationEntry *entry = web_contents()->GetController().GetLastCommittedEntry();
    return entry ? entry->GetURL() : GURL();
}

GURL FaviconDriverQt::GetManifestURL(content::RenderFrameHost *rfh)
{
    DocumentManifestData *document_data = DocumentManifestData::GetOrCreateForCurrentDocument(rfh);
    return document_data->has_manifest_url ? rfh->GetPage().GetManifestUrl().value_or(GURL())
                                           : GURL();
}

GURL FaviconDriverQt::GetFaviconURL() const
{
    content::NavigationController &controller = web_contents()->GetController();

    content::NavigationEntry *entry = controller.GetLastCommittedEntry();
    if (entry)
        return entry->GetFavicon().url;
    return GURL();
}

FaviconDriverQt::DocumentManifestData::DocumentManifestData(content::RenderFrameHost *rfh)
    : content::DocumentUserData<DocumentManifestData>(rfh)
{
}

FaviconDriverQt::DocumentManifestData::~DocumentManifestData() = default;

FaviconDriverQt::NavigationManifestData::NavigationManifestData(
        content::NavigationHandle &navigation_handle)
{
}

FaviconDriverQt::NavigationManifestData::~NavigationManifestData() = default;

void FaviconDriverQt::OnDidDownloadManifest(ManifestDownloadCallback callback,
                                            const GURL &manifest_url,
                                            blink::mojom::ManifestPtr manifest)
{
    Q_UNUSED(manifest_url);

    // ~WebContentsImpl triggers running any pending callbacks for manifests.
    // As we're about to be destroyed ignore the request. To do otherwise may
    // result in calling back to this and attempting to use the WebContents, which
    // will crash.
    if (!web_contents())
        return;

    std::vector<favicon::FaviconURL> candidates;
    if (manifest) {
        for (const auto &icon : manifest->icons) {
            candidates.emplace_back(icon.src, favicon_base::IconType::kWebManifestIcon, icon.sizes);
        }
    }
    std::move(callback).Run(candidates);
}

int FaviconDriverQt::DownloadImage(const GURL &url, int max_image_size,
                                   ImageDownloadCallback callback)
{
    bool bypass_cache = (m_bypassCachePageURL == GetActiveURL());
    m_bypassCachePageURL = GURL();

    const gfx::Size preferred_size(max_image_size, max_image_size);
    return web_contents()->DownloadImage(url, true, preferred_size,
                                         /*max_bitmap_size=*/max_image_size,
                                         bypass_cache, std::move(callback));
}

void FaviconDriverQt::DownloadManifest(const GURL &url, ManifestDownloadCallback callback)
{
    // TODO(crbug.com/1201237): This appears to be reachable from pages other
    // than the primary page. This code should likely be refactored so that either
    // this is unreachable from other pages, or the correct page is plumbed in
    // here.
    web_contents()->GetPrimaryPage().GetManifest(base::BindOnce(
            &FaviconDriverQt::OnDidDownloadManifest, base::Unretained(this), std::move(callback)));
}

bool FaviconDriverQt::IsOffTheRecord()
{
    DCHECK(web_contents());
    return web_contents()->GetBrowserContext()->IsOffTheRecord();
}

void FaviconDriverQt::OnFaviconUpdated(
        const GURL &page_url,
        favicon::FaviconDriverObserver::NotificationIconType notification_icon_type,
        const GURL &icon_url, bool icon_url_changed, const gfx::Image &image)
{
    Q_UNUSED(page_url);

    QWebEngineSettings *settings = m_viewClient->webEngineSettings();
    bool touchIconsEnabled = settings->testAttribute(QWebEngineSettings::TouchIconsEnabled);

    // Prefer touch icons over favicons if touch icons are enabled.
    if (!touchIconsEnabled
        || m_latestFavicon.source != favicon::FaviconDriverObserver::TOUCH_LARGEST
        || notification_icon_type == favicon::FaviconDriverObserver::TOUCH_LARGEST) {
        m_latestFavicon.valid = true;
        m_latestFavicon.url = icon_url;
        m_latestFavicon.image = image;
        m_latestFavicon.source = notification_icon_type;
    }

    NotifyFaviconUpdatedObservers(notification_icon_type, icon_url, icon_url_changed, image);
    emitIconChangedIfNeeded();
}

void FaviconDriverQt::OnFaviconDeleted(
        const GURL &page_url,
        favicon::FaviconDriverObserver::NotificationIconType notification_icon_type)
{
    content::NavigationEntry *entry = web_contents()->GetController().GetLastCommittedEntry();
    DCHECK(entry && entry->GetURL() == page_url);

    entry->GetFavicon() = FaviconStatusQt();
    web_contents()->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB);

    NotifyFaviconUpdatedObservers(notification_icon_type, /*icon_url=*/GURL(),
                                  /*icon_url_changed=*/true, FaviconStatusQt().image);
}

void FaviconDriverQt::OnHandlerCompleted(favicon::FaviconHandler *handler)
{
    Q_UNUSED(handler);

    if (activeHandlersCount(m_viewClient->webEngineSettings()) > m_completedHandlersCount)
        ++m_completedHandlersCount;
    emitIconChangedIfNeeded();
}

void FaviconDriverQt::DidUpdateFaviconURL(
        content::RenderFrameHost *rfh, const std::vector<blink::mojom::FaviconURLPtr> &candidates)
{
    // Ignore the update if there is no last committed navigation entry. This can
    // occur when loading an initially blank page.
    content::NavigationEntry *entry = web_contents()->GetController().GetLastCommittedEntry();

    if (!entry)
        return;

    // We update |m_faviconUrls| even if the list is believed to be partial
    // (checked below), because callers of our getter favicon_urls() expect so.
    std::vector<blink::mojom::FaviconURLPtr> faviconUrls;
    for (const auto &candidate : candidates)
        faviconUrls.push_back(candidate.Clone());
    m_faviconUrls = std::move(faviconUrls);

    if (!rfh->IsDocumentOnLoadCompletedInMainFrame())
        return;

    OnUpdateCandidates(rfh->GetLastCommittedURL(),
                       favicon::FaviconURLsFromContentFaviconURLs(candidates), GetManifestURL(rfh));
}

void FaviconDriverQt::DidUpdateWebManifestURL(content::RenderFrameHost *rfh,
                                              const GURL &manifest_url)
{
    Q_UNUSED(manifest_url);

    // Ignore the update if there is no last committed navigation entry. This can
    // occur when loading an initially blank page.
    content::NavigationEntry *entry = web_contents()->GetController().GetLastCommittedEntry();
    if (!entry || !rfh->IsDocumentOnLoadCompletedInMainFrame())
        return;

    DocumentManifestData *document_data = DocumentManifestData::GetOrCreateForCurrentDocument(rfh);
    document_data->has_manifest_url = true;

    // On regular page loads, DidUpdateManifestURL() is guaranteed to be called
    // before DidUpdateFaviconURL(). However, a page can update the favicons via
    // javascript.
    if (!rfh->FaviconURLs().empty()) {
        OnUpdateCandidates(rfh->GetLastCommittedURL(),
                           favicon::FaviconURLsFromContentFaviconURLs(rfh->FaviconURLs()),
                           GetManifestURL(rfh));
    }
}

void FaviconDriverQt::DidStartNavigation(content::NavigationHandle *navigation_handle)
{
    if (!navigation_handle->IsInPrimaryMainFrame())
        return;

    if (!navigation_handle->IsSameDocument()) {
        m_completedHandlersCount = 0;
        m_latestFavicon = FaviconStatusQt();
        m_viewClient->iconChanged(QUrl());
    }

    content::ReloadType reload_type = navigation_handle->GetReloadType();
    if (reload_type == content::ReloadType::NONE || IsOffTheRecord())
        return;

    if (!navigation_handle->IsSameDocument()) {
        NavigationManifestData *navigation_data =
                NavigationManifestData::GetOrCreateForNavigationHandle(*navigation_handle);
        navigation_data->has_manifest_url = false;
    }

    if (reload_type == content::ReloadType::BYPASSING_CACHE)
        m_bypassCachePageURL = navigation_handle->GetURL();

    SetFaviconOutOfDateForPage(navigation_handle->GetURL(),
                               reload_type == content::ReloadType::BYPASSING_CACHE);
}

void FaviconDriverQt::DidFinishNavigation(content::NavigationHandle *navigation_handle)
{
    if (!navigation_handle->IsInPrimaryMainFrame() || !navigation_handle->HasCommitted()
        || navigation_handle->IsErrorPage()) {
        return;
    }

    // Transfer in-flight navigation data to the document user data.
    NavigationManifestData *navigation_data =
            NavigationManifestData::GetOrCreateForNavigationHandle(*navigation_handle);
    DocumentManifestData *document_data = DocumentManifestData::GetOrCreateForCurrentDocument(
            navigation_handle->GetRenderFrameHost());
    document_data->has_manifest_url = navigation_data->has_manifest_url;

    // Wait till the user navigates to a new URL to start checking the cache
    // again. The cache may be ignored for non-reload navigations (e.g.
    // history.replace() in-page navigation). This is allowed to increase the
    // likelihood that "reloading a page ignoring the cache" redownloads the
    // favicon. In particular, a page may do an in-page navigation before
    // FaviconHandler has the time to determine that the favicon needs to be
    // redownloaded.
    GURL url = navigation_handle->GetURL();
    if (url != m_bypassCachePageURL)
        m_bypassCachePageURL = GURL();

    // Get the favicon, either from history or request it from the net.
    FetchFavicon(url, navigation_handle->IsSameDocument());
}

void FaviconDriverQt::SetFaviconOutOfDateForPage(const GURL &url, bool force_reload)
{
    if (m_faviconService) {
        m_faviconService->SetFaviconOutOfDateForPage(url);
        if (force_reload)
            m_faviconService->ClearUnableToDownloadFavicons();
    }
}

void FaviconDriverQt::OnUpdateCandidates(const GURL &page_url,
                                         const std::vector<favicon::FaviconURL> &candidates,
                                         const GURL &manifest_url)
{
    QWebEngineSettings *settings = m_viewClient->webEngineSettings();
    bool touchIconsEnabled = settings->testAttribute(QWebEngineSettings::TouchIconsEnabled);
    for (const std::unique_ptr<favicon::FaviconHandler> &handler : m_handlers) {
        switch (handler->Type()) {
        case favicon::FaviconDriverObserver::NON_TOUCH_16_DIP:
            if (touchIconsEnabled)
                continue;
            break;
        case favicon::FaviconDriverObserver::NON_TOUCH_LARGEST:
        case favicon::FaviconDriverObserver::TOUCH_LARGEST:
            if (!touchIconsEnabled)
                continue;
            break;
        }

        // We feed in the Web Manifest URL (if any) to the instance handling type
        // kWebManifestIcon, because those compete which each other (i.e. manifest
        // icons override inline touch icons).
        handler->OnUpdateCandidates(
                page_url, candidates,
                (handler->icon_types().count(favicon_base::IconType::kWebManifestIcon) != 0)
                        ? manifest_url
                        : GURL::EmptyGURL());
    }
}

void FaviconDriverQt::emitIconChangedIfNeeded()
{
    if (activeHandlersCount(m_viewClient->webEngineSettings()) != m_completedHandlersCount)
        return;

    content::NavigationEntry *entry = web_contents()->GetController().GetLastCommittedEntry();
    DCHECK(entry);

    if (entry->GetFavicon().url != m_latestFavicon.url) {
        entry->GetFavicon().valid = m_latestFavicon.valid;
        entry->GetFavicon().url = m_latestFavicon.url;
        entry->GetFavicon().image = m_latestFavicon.image;
        web_contents()->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB);
    }

    m_viewClient->iconChanged(toQt(m_latestFavicon.url));
}

NAVIGATION_HANDLE_USER_DATA_KEY_IMPL(FaviconDriverQt::NavigationManifestData);
DOCUMENT_USER_DATA_KEY_IMPL(FaviconDriverQt::DocumentManifestData);
WEB_CONTENTS_USER_DATA_KEY_IMPL(FaviconDriverQt);

} // namespace QtWebEngineCore