// Copyright 2014 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 "libcef/browser/component_updater/cef_component_updater_configurator.h"
#include "include/cef_version.h"

#include "base/version.h"
#include "components/component_updater/configurator_impl.h"
#include "components/update_client/component_patcher_operation.h"
#include "content/public/browser/browser_thread.h"

namespace component_updater {

namespace {

class CefConfigurator : public update_client::Configurator {
 public:
  CefConfigurator(const base::CommandLine* cmdline,
                  net::URLRequestContextGetter* url_request_getter,
                  PrefService* pref_service);

  int InitialDelay() const override;
  int NextCheckDelay() const override;
  int StepDelay() const override;
  int OnDemandDelay() const override;
  int UpdateDelay() const override;
  std::vector<GURL> UpdateUrl() const override;
  std::vector<GURL> PingUrl() const override;
  base::Version GetBrowserVersion() const override;
  std::string GetChannel() const override;
  std::string GetBrand() const override;
  std::string GetLang() const override;
  std::string GetOSLongName() const override;
  std::string ExtraRequestParams() const override;
  std::string GetDownloadPreference() const override;
  net::URLRequestContextGetter* RequestContext() const override;
  scoped_refptr<update_client::OutOfProcessPatcher>
      CreateOutOfProcessPatcher() const override;
  bool DeltasEnabled() const override;
  bool UseBackgroundDownloader() const override;
  bool UseCupSigning() const override;
  scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner()
      const override;
  PrefService* GetPrefService() const override;

 private:
  friend class base::RefCountedThreadSafe<CefConfigurator>;

  ~CefConfigurator() override {}

  ConfiguratorImpl configurator_impl_;
  PrefService* pref_service_;
};

CefConfigurator::CefConfigurator(
    const base::CommandLine* cmdline,
    net::URLRequestContextGetter* url_request_getter,
    PrefService* pref_service)
    : configurator_impl_(cmdline, url_request_getter, false),
      pref_service_(pref_service) {
}

int CefConfigurator::InitialDelay() const {
  return configurator_impl_.InitialDelay();
}

int CefConfigurator::NextCheckDelay() const {
  return configurator_impl_.NextCheckDelay();
}

int CefConfigurator::StepDelay() const {
  return configurator_impl_.StepDelay();
}

int CefConfigurator::OnDemandDelay() const {
  return configurator_impl_.OnDemandDelay();
}

int CefConfigurator::UpdateDelay() const {
  return configurator_impl_.UpdateDelay();
}

std::vector<GURL> CefConfigurator::UpdateUrl() const {
  return configurator_impl_.UpdateUrl();
}

std::vector<GURL> CefConfigurator::PingUrl() const {
  return configurator_impl_.PingUrl();
}

base::Version CefConfigurator::GetBrowserVersion() const {
  return configurator_impl_.GetBrowserVersion();
}

std::string CefConfigurator::GetChannel() const {
  return std::string();
}

std::string CefConfigurator::GetBrand() const {
  return std::string();
}

std::string CefConfigurator::GetLang() const {
  return std::string();
}

std::string CefConfigurator::GetOSLongName() const {
  return configurator_impl_.GetOSLongName();
}

std::string CefConfigurator::ExtraRequestParams() const {
  return configurator_impl_.ExtraRequestParams();
}

std::string CefConfigurator::GetDownloadPreference() const {
  return std::string();
}

net::URLRequestContextGetter* CefConfigurator::RequestContext() const {
  return configurator_impl_.RequestContext();
}

scoped_refptr<update_client::OutOfProcessPatcher>
CefConfigurator::CreateOutOfProcessPatcher() const {
  return nullptr;
}

bool CefConfigurator::DeltasEnabled() const {
  return configurator_impl_.DeltasEnabled();
}

bool CefConfigurator::UseBackgroundDownloader() const {
  return configurator_impl_.UseBackgroundDownloader();
}

bool CefConfigurator::UseCupSigning() const {
  return configurator_impl_.UseCupSigning();
}

// Returns a task runner to run blocking tasks. The task runner continues to run
// after the browser shuts down, until the OS terminates the process. This
// imposes certain requirements for the code using the task runner, such as
// not accessing any global browser state while the code is running.
scoped_refptr<base::SequencedTaskRunner>
CefConfigurator::GetSequencedTaskRunner() const {
  return content::BrowserThread::GetBlockingPool()
      ->GetSequencedTaskRunnerWithShutdownBehavior(
          base::SequencedWorkerPool::GetSequenceToken(),
          base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
}

PrefService* CefConfigurator::GetPrefService() const {
  return pref_service_;
}

}  // namespace

scoped_refptr<update_client::Configurator>
MakeCefComponentUpdaterConfigurator(
    const base::CommandLine* cmdline,
    net::URLRequestContextGetter* context_getter,
    PrefService* pref_service) {
  return new CefConfigurator(cmdline, context_getter, pref_service);
}

}  // namespace component_updater
