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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_WEBUI_FAVICON_SOURCE_H_
#define CHROME_BROWSER_UI_WEBUI_FAVICON_SOURCE_H_
#include <map>
#include <string>
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/task/cancelable_task_tracker.h"
#include "components/favicon/core/favicon_service.h"
#include "content/public/browser/url_data_source.h"
class Profile;
namespace base {
class RefCountedMemory;
}
namespace chrome {
enum class FaviconUrlFormat;
struct ParsedFaviconPath;
} // namespace chrome
namespace ui {
class NativeTheme;
}
// FaviconSource is the gateway between network-level chrome:
// requests for favicons and the history backend that serves these.
// Two possible formats are allowed: chrome://favicon, kept only for backwards
// compatibility for extensions, and chrome://favicon2. Formats are described in
// favicon_url_parser.h.
class FaviconSource : public content::URLDataSource {
public:
// By default, favicons are served via a chrome trusted URL (chrome://). If
// |serve_untrusted| is set to true, favicons will be served via
// chrome-untrusted://. Note that chrome-untrusted:// only supports the
// favicon2 URL format and does not support the legacy URL format.
explicit FaviconSource(Profile* profile,
chrome::FaviconUrlFormat format,
bool serve_untrusted = false);
FaviconSource(const FaviconSource&) = delete;
FaviconSource& operator=(const FaviconSource&) = delete;
~FaviconSource() override;
// content::URLDataSource implementation.
std::string GetSource() override;
void StartDataRequest(
const GURL& url,
const content::WebContents::Getter& wc_getter,
content::URLDataSource::GotDataCallback callback) override;
std::string GetMimeType(const GURL&) override;
bool AllowCaching() override;
bool ShouldReplaceExistingSource() override;
bool ShouldServiceRequest(const GURL& url,
content::BrowserContext* browser_context,
int render_process_id) override;
protected:
// Exposed for testing.
virtual ui::NativeTheme* GetNativeTheme(
const content::WebContents::Getter& wc_getter);
virtual base::RefCountedMemory* LoadIconBytes(float scale_factor,
int resource_id);
raw_ptr<Profile, DanglingUntriaged> profile_;
private:
// Defines the allowed pixel sizes for requested favicons.
enum IconSize { SIZE_16, SIZE_32, SIZE_64, NUM_SIZES };
// Called when favicon data is available from the history backend. If
// |bitmap_result| is valid, returns it to caller using |callback|. Otherwise
// will send appropriate default icon for |size_in_dip| and |scale_factor|.
void OnFaviconDataAvailable(
content::URLDataSource::GotDataCallback callback,
const chrome::ParsedFaviconPath& parsed,
const content::WebContents::Getter& wc_getter,
const favicon_base::FaviconRawBitmapResult& bitmap_result);
// Sends the 16x16 DIP 1x default favicon.
void SendDefaultResponse(content::URLDataSource::GotDataCallback callback,
const content::WebContents::Getter& wc_getter,
bool force_light_mode = false);
// Sends back default favicon or fallback monogram.
void SendDefaultResponse(content::URLDataSource::GotDataCallback callback,
const chrome::ParsedFaviconPath& parsed,
const content::WebContents::Getter& wc_getter);
// Sends the default favicon.
void SendDefaultResponse(content::URLDataSource::GotDataCallback callback,
int size_in_dip,
float scale_factor,
bool dark_mode);
chrome::FaviconUrlFormat url_format_;
base::CancelableTaskTracker cancelable_task_tracker_;
bool serve_untrusted_;
base::WeakPtrFactory<FaviconSource> weak_ptr_factory_{this};
};
#endif // CHROME_BROWSER_UI_WEBUI_FAVICON_SOURCE_H_
|