// 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 <string>

#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.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 scoped_refptr<base::RefCountedMemory> LoadIconBytes(
      float scale_factor,
      int resource_id);

  raw_ptr<Profile, DanglingUntriaged> profile_;

 private:
  enum class DefaultFaviconBehavior {
    kUseGlobeIcon = 0,
    kUseEmptyIcon = 1,
  };

  ui::NativeTheme* GetNativeTheme(
      const content::WebContents::Getter& wc_getter);

  // 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,
      DefaultFaviconBehavior behavior = DefaultFaviconBehavior::kUseGlobeIcon);

  // 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,
                           DefaultFaviconBehavior behavior);

  // Records metrics for the result of a fetched favicon.
  void LogFaviconResult(
      const chrome::ParsedFaviconPath& parsed,
      const content::WebContents::Getter& wc_getter,
      const favicon_base::FaviconRawBitmapResult& bitmap_result);

  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_
