// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/input_method/ime_service_connector.h"

#include <memory>
#include <utility>

#include "ash/constants/ash_features.h"
#include "base/feature_list.h"
#include "chromeos/ash/services/ime/constants.h"
#include "chromeos/ash/services/ime/public/mojom/ime_service.mojom.h"
#include "chromeos/ash/services/ime/public/mojom/input_method_user_data.mojom.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "content/public/browser/service_process_host.h"
#include "net/base/load_flags.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/redirect_info.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/mojom/network_context.mojom.h"

namespace ash {
namespace input_method {

namespace {

constexpr net::NetworkTrafficAnnotationTag traffic_annotation =
    net::DefineNetworkTrafficAnnotation("ime_url_downloader", R"(
    semantics {
      sender: "IME Service Downloader"
      description:
        "When user selects a new input method in ChromeOS, it may request a"
        "corresponding language module downloaded if it does not exist."
      trigger: "User switches to an input method without language module."
      data:
        "The language module download URL. No user identifier is sent."
      destination: GOOGLE_OWNED_SERVICE
    }
    policy {
      cookies_allowed: NO
      policy_exception_justification:
        "Not implemented, considered not useful."
    })");

bool IsDownloadPathValid(const base::FilePath& file_path) {
  // Only non-empty, relative path which doesn't reference a parent is allowed.
  if (file_path.empty() || file_path.IsAbsolute() ||
      file_path.ReferencesParent()) {
    return false;
  }

  // Target path must be restricted in the provided path.
  base::FilePath parent(ime::kInputMethodsDirName);
  parent = parent.Append(ime::kLanguageDataDirName);
  return parent.IsParent(file_path);
}

bool IsDownloadURLValid(const GURL& url) {
  return url.SchemeIs(url::kHttpsScheme) &&
         (url.DomainIs("dl.google.com") || url.DomainIs("edgedl.me.gvt1.com"));
}

std::unique_ptr<network::SimpleURLLoader> CreateUrlLoader(const GURL& url) {
  auto resource_request = std::make_unique<network::ResourceRequest>();
  resource_request->url = url;
  resource_request->load_flags =
      net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE;
  // Disable cookies for this request.
  resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;

  auto url_loader = network::SimpleURLLoader::Create(
      std::move(resource_request), traffic_annotation);
  // TODO(https://crbug.com/162559903): Allow the client to specify the timeout.
  url_loader->SetTimeoutDuration(base::Minutes(10));
  return url_loader;
}

}  // namespace

ImeServiceConnector::ImeServiceConnector(Profile* profile)
    : profile_(profile), url_loader_factory_(profile->GetURLLoaderFactory()) {
  profile_observation_.Observe(profile);
}

ImeServiceConnector::~ImeServiceConnector() = default;

void ImeServiceConnector::DownloadImeFileTo(
    const GURL& url,
    const base::FilePath& file_path,
    DownloadImeFileToCallback callback) {
  // Validate url and file_path, return an empty file path if not.
  if (!IsDownloadURLValid(url) || !IsDownloadPathValid(file_path)) {
    base::FilePath empty_path;
    std::move(callback).Run(empty_path);
    return;
  }

  base::FilePath full_path = profile_->GetPath().Append(file_path);
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(&ImeServiceConnector::MaybeTriggerDownload,
                                weak_ptr_factory_.GetWeakPtr(), url, full_path,
                                std::move(callback)));
}

void ImeServiceConnector::OnProfileWillBeDestroyed(Profile* profile) {
  profile_observation_.Reset();
  profile_ = nullptr;
}

void ImeServiceConnector::SetupImeService(
    mojo::PendingReceiver<ime::mojom::InputEngineManager> receiver) {
  if (!remote_service_) {
    content::ServiceProcessHost::Launch(
        remote_service_.BindNewPipeAndPassReceiver(),
        content::ServiceProcessHost::Options()
            .WithDisplayName(IDS_IME_SERVICE_DISPLAY_NAME)
            .Pass());
    remote_service_.reset_on_disconnect();

    platform_access_receiver_.reset();
    remote_service_->SetPlatformAccessProvider(
        platform_access_receiver_.BindNewPipeAndPassRemote());
  }

  remote_service_->BindInputEngineManager(std::move(receiver));
}

void ImeServiceConnector::BindInputMethodUserDataService(
    mojo::PendingReceiver<ime::mojom::InputMethodUserDataService> receiver) {
  if (!remote_service_) {
    content::ServiceProcessHost::Launch(
        remote_service_.BindNewPipeAndPassReceiver(),
        content::ServiceProcessHost::Options()
            .WithDisplayName(IDS_IME_SERVICE_DISPLAY_NAME)
            .Pass());
    remote_service_.reset_on_disconnect();

    platform_access_receiver_.reset();
    remote_service_->SetPlatformAccessProvider(
        platform_access_receiver_.BindNewPipeAndPassRemote());
  }

  remote_service_->BindInputMethodUserDataService(std::move(receiver));
}

void ImeServiceConnector::MaybeTriggerDownload(
    GURL url,
    base::FilePath file_path,
    DownloadImeFileToCallback callback) {
  // Do not trigger a new download if one is already in progress for the same
  // url. Store the callback given and run it when the current download
  // finishes.
  if (url_loader_ && active_request_url_ && active_request_url_ == url.spec()) {
    download_callbacks_.emplace_back(std::move(callback));
    return;
  }

  // If the currently active request does not match the url being requested,
  // then revert to the previous logic of dropping new requests while the
  // current request is in progress.
  if (url_loader_) {
    base::FilePath empty_path;
    std::move(callback).Run(empty_path);
    return;
  }

  // Reset the download context before triggering a new download request.
  active_request_url_ = url.spec();
  download_callbacks_.clear();
  download_callbacks_.emplace_back(std::move(callback));
  url_loader_ = CreateUrlLoader(url);
  url_loader_->SetOnRedirectCallback(base::BindRepeating(
      &ImeServiceConnector::OnRedirect, weak_ptr_factory_.GetWeakPtr()));
  url_loader_->DownloadToFile(
      url_loader_factory_.get(),
      base::BindOnce(&ImeServiceConnector::HandleDownloadResponse,
                     weak_ptr_factory_.GetWeakPtr()),
      file_path);
}

void ImeServiceConnector::HandleDownloadResponse(base::FilePath file_path) {
  // Notify any download callbacks registered for the current request.
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(&ImeServiceConnector::NotifyAllDownloadListeners,
                     weak_ptr_factory_.GetWeakPtr(), file_path));
}

void ImeServiceConnector::NotifyAllDownloadListeners(base::FilePath file_path) {
  while (!download_callbacks_.empty()) {
    std::move(download_callbacks_.back()).Run(file_path);
    download_callbacks_.pop_back();
  }

  // Clear the currently active request info.
  url_loader_.reset();
  active_request_url_ = std::nullopt;
}

void ImeServiceConnector::OnRedirect(
    const GURL& url_before_redirect,
    const net::RedirectInfo& redirect_info,
    const network::mojom::URLResponseHead& response_head,
    std::vector<std::string>* removed_headers) {
  if (!IsDownloadURLValid(redirect_info.new_url)) {
    NotifyAllDownloadListeners(base::FilePath());
  }
}

}  // namespace input_method
}  // namespace ash
