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

#include "android_webview/browser/aw_web_ui_controller_factory.h"

#include <string_view>

#include "android_webview/browser/safe_browsing/aw_safe_browsing_local_state_delegate_impl.h"
#include "android_webview/browser/safe_browsing/aw_safe_browsing_ui.h"
#include "base/memory/ptr_util.h"
#include "components/safe_browsing/core/common/web_ui_constants.h"
#include "components/webui/user_actions/user_actions_ui.h"
#include "components/webui/user_actions/user_actions_ui_constants.h"
#include "content/public/browser/web_ui.h"
#include "url/gurl.h"

using content::WebUI;
using content::WebUIController;

namespace {

const WebUI::TypeID kSafeBrowsingID = &kSafeBrowsingID;
const WebUI::TypeID kUserActionsID = &kUserActionsID;

// A function for creating a new WebUI. The caller owns the return value, which
// may be nullptr (for example, if the URL refers to an non-existent extension).
typedef WebUIController* (*WebUIFactoryFunctionPointer)(WebUI* web_ui,
                                                        const GURL& url);

// Template for defining WebUIFactoryFunction.
template <class T>
WebUIController* NewWebUI(WebUI* web_ui, const GURL& url) {
  return new T(web_ui);
}

WebUIFactoryFunctionPointer GetWebUIFactoryFunctionPointer(const GURL& url) {
  // WebUI pages here must remain in the base module instead of being moved to
  // the Developer UI Dynamic Feature Module (DevUI DFM). Therefore the hosts
  // here must not appear in IsWebUiHostInDevUiDfm().
  std::string_view host = url.host();
  if (host == safe_browsing::kChromeUISafeBrowsingHost) {
    return &NewWebUI<safe_browsing::AWSafeBrowsingUI>;
  } else if (host == user_actions_ui::kUserActionsWebUIHost) {
    return &NewWebUI<user_actions_ui::UserActionsUI>;
  }

  return nullptr;
}

WebUI::TypeID GetWebUITypeID(const GURL& url) {
  std::string_view host = url.host();
  if (host == safe_browsing::kChromeUISafeBrowsingHost) {
    return kSafeBrowsingID;
  } else if (host == user_actions_ui::kUserActionsWebUIHost) {
    return kUserActionsID;
  }

  return WebUI::kNoWebUI;
}

}  // namespace

namespace android_webview {

// static
AwWebUIControllerFactory* AwWebUIControllerFactory::GetInstance() {
  return base::Singleton<AwWebUIControllerFactory>::get();
}

AwWebUIControllerFactory::AwWebUIControllerFactory() {}

AwWebUIControllerFactory::~AwWebUIControllerFactory() {}

WebUI::TypeID AwWebUIControllerFactory::GetWebUIType(
    content::BrowserContext* browser_context,
    const GURL& url) {
  return GetWebUITypeID(url);
}

bool AwWebUIControllerFactory::UseWebUIForURL(
    content::BrowserContext* browser_context,
    const GURL& url) {
  return GetWebUIType(browser_context, url) != WebUI::kNoWebUI;
}

std::unique_ptr<WebUIController>
AwWebUIControllerFactory::CreateWebUIControllerForURL(WebUI* web_ui,
                                                      const GURL& url) {
  WebUIFactoryFunctionPointer function = GetWebUIFactoryFunctionPointer(url);
  if (!function)
    return nullptr;

  return base::WrapUnique((*function)(web_ui, url));
}

}  // namespace android_webview
