// Copyright 2013 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_ANDROID_FAVICON_HELPER_H_
#define CHROME_BROWSER_ANDROID_FAVICON_HELPER_H_

#include <jni.h>

#include <memory>

#include "base/android/scoped_java_ref.h"
#include "base/memory/weak_ptr.h"
#include "base/task/cancelable_task_tracker.h"
#include "components/favicon/core/favicon_service.h"
#include "components/favicon_base/favicon_types.h"
#include "url/gurl.h"

class Profile;

class FaviconHelper {
 public:
  FaviconHelper();
  void Destroy();

  FaviconHelper(const FaviconHelper&) = delete;
  FaviconHelper& operator=(const FaviconHelper&) = delete;

  bool GetLocalFaviconImageForURL(
      Profile* profile,
      const GURL& page_url,
      int32_t j_desired_size_in_pixel,
      bool fallback_to_host,
      const base::android::JavaRef<jobject>& j_favicon_image_callback);
  bool GetForeignFaviconImageForURL(
      Profile* profile,
      const GURL& page_url,
      int32_t j_desired_size_in_pixel,
      bool fallback_to_host,
      const base::android::JavaRef<jobject>& j_favicon_image_callback);

  void GetLocalFaviconImageForURLInternal(
      favicon::FaviconService* favicon_service,
      GURL url,
      int desired_size_in_pixel,
      bool fallback_to_host,
      favicon_base::FaviconRawBitmapCallback callback_runner);
  void OnJobFinished(int job_id);

 private:
  virtual ~FaviconHelper();

  // This function is expected to be bound to a WeakPtr<FaviconHelper>, so that
  // it won't be run if the FaviconHelper is deleted and
  // |j_favicon_image_callback| isn't executed in that case.
  void OnFaviconBitmapResultAvailable(
      const base::android::JavaRef<jobject>& j_favicon_image_callback,
      bool original_fallback_to_host,
      bool off_the_record,
      const favicon_base::FaviconRawBitmapResult& result);

  std::unique_ptr<base::CancelableTaskTracker> cancelable_task_tracker_;

  base::WeakPtrFactory<FaviconHelper> weak_ptr_factory_{this};
};

#endif  // CHROME_BROWSER_ANDROID_FAVICON_HELPER_H_
