// 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 "components/autofill/content/browser/autofill_log_router_factory.h"

#include "components/autofill/core/browser/logging/log_router.h"
#include "components/autofill/core/common/autofill_debug_features.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"

namespace autofill {

// static
LogRouter* AutofillLogRouterFactory::GetForBrowserContext(
    content::BrowserContext* context) {
  LogRouter* log_router = static_cast<LogRouter*>(
      GetInstance()->GetServiceForBrowserContext(context, /* create = */ true));
  if (base::FeatureList::IsEnabled(features::debug::kAutofillLogToTerminal)) {
    log_router->LogToTerminal();
  }
  return log_router;
}

// static
AutofillLogRouterFactory* AutofillLogRouterFactory::GetInstance() {
  static base::NoDestructor<AutofillLogRouterFactory> instance;
  return instance.get();
}

AutofillLogRouterFactory::AutofillLogRouterFactory()
    : BrowserContextKeyedServiceFactory(
          "AutofillInternalsService",
          BrowserContextDependencyManager::GetInstance()) {}

AutofillLogRouterFactory::~AutofillLogRouterFactory() = default;

std::unique_ptr<KeyedService>
AutofillLogRouterFactory::BuildServiceInstanceForBrowserContext(
    content::BrowserContext* /* context */) const {
  return std::make_unique<LogRouter>();
}

}  // namespace autofill
