1// Copyright 2020 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import "ios/web_view/internal/sync/web_view_instance_id_profile_service_factory.h"
6
7#include "base/memory/ptr_util.h"
8#include "base/no_destructor.h"
9#include "components/gcm_driver/gcm_profile_service.h"
10#include "components/gcm_driver/instance_id/instance_id_profile_service.h"
11#include "components/keyed_service/ios/browser_state_dependency_manager.h"
12#include "ios/web_view/internal/sync/web_view_gcm_profile_service_factory.h"
13#include "ios/web_view/internal/web_view_browser_state.h"
14
15#if !defined(__has_feature) || !__has_feature(objc_arc)
16#error "This file requires ARC support."
17#endif
18
19namespace ios_web_view {
20
21// static
22instance_id::InstanceIDProfileService*
23WebViewInstanceIDProfileServiceFactory::GetForBrowserState(
24    WebViewBrowserState* browser_state) {
25  return static_cast<instance_id::InstanceIDProfileService*>(
26      GetInstance()->GetServiceForBrowserState(browser_state, true));
27}
28
29// static
30WebViewInstanceIDProfileServiceFactory*
31WebViewInstanceIDProfileServiceFactory::GetInstance() {
32  static base::NoDestructor<WebViewInstanceIDProfileServiceFactory> instance;
33  return instance.get();
34}
35
36WebViewInstanceIDProfileServiceFactory::WebViewInstanceIDProfileServiceFactory()
37    : BrowserStateKeyedServiceFactory(
38          "InstanceIDProfileService",
39          BrowserStateDependencyManager::GetInstance()) {
40  DependsOn(WebViewGCMProfileServiceFactory::GetInstance());
41}
42
43WebViewInstanceIDProfileServiceFactory::
44    ~WebViewInstanceIDProfileServiceFactory() {}
45
46std::unique_ptr<KeyedService>
47WebViewInstanceIDProfileServiceFactory::BuildServiceInstanceFor(
48    web::BrowserState* context) const {
49  DCHECK(!context->IsOffTheRecord());
50
51  WebViewBrowserState* browser_state =
52      WebViewBrowserState::FromBrowserState(context);
53  return std::make_unique<instance_id::InstanceIDProfileService>(
54      WebViewGCMProfileServiceFactory::GetForBrowserState(browser_state)
55          ->driver(),
56      browser_state->IsOffTheRecord());
57}
58
59}  // namespace ios_web_view
60