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 #include "chrome/browser/updates/announcement_notification/announcement_notification_service_factory.h"
6 
7 #include <memory>
8 
9 #include "base/time/default_clock.h"
10 #include "build/build_config.h"
11 #include "chrome/browser/notifications/notification_display_service_factory.h"
12 #include "chrome/browser/profiles/incognito_helpers.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/updates/announcement_notification/announcement_notification_delegate.h"
15 #include "chrome/browser/updates/announcement_notification/announcement_notification_service.h"
16 #include "chrome/browser/updates/announcement_notification/empty_announcement_notification_service.h"
17 #include "components/keyed_service/content/browser_context_dependency_manager.h"
18 
19 #if defined(OS_ANDROID)
20 #include "chrome/browser/updates/announcement_notification/announcement_notification_delegate_android.h"
21 #else
22 #include "chrome/browser/updates/announcement_notification/announcement_notification_delegate.h"
23 #endif  // OS_ANDROID
24 
25 // static
26 AnnouncementNotificationServiceFactory*
GetInstance()27 AnnouncementNotificationServiceFactory::GetInstance() {
28   static base::NoDestructor<AnnouncementNotificationServiceFactory> instance;
29   return instance.get();
30 }
31 
32 // static
33 AnnouncementNotificationService*
GetForProfile(Profile * profile)34 AnnouncementNotificationServiceFactory::GetForProfile(Profile* profile) {
35   return static_cast<AnnouncementNotificationService*>(
36       GetInstance()->GetServiceForBrowserContext(profile, true /* create */));
37 }
38 
BuildServiceInstanceFor(content::BrowserContext * context) const39 KeyedService* AnnouncementNotificationServiceFactory::BuildServiceInstanceFor(
40     content::BrowserContext* context) const {
41   if (context->IsOffTheRecord()) {
42     return new EmptyAnnouncementNotificationService();
43   }
44 
45   Profile* profile = Profile::FromBrowserContext(context);
46   PrefService* pref = profile->GetPrefs();
47 #if defined(OS_ANDROID)
48   auto delegate = std::make_unique<AnnouncementNotificationDelegateAndroid>();
49 #else
50   NotificationDisplayService* display_service =
51       static_cast<NotificationDisplayService*>(
52           NotificationDisplayServiceFactory::GetInstance()->GetForProfile(
53               profile));
54   auto delegate =
55       std::make_unique<AnnouncementNotificationDelegate>(display_service);
56 #endif  // OS_ANDROID
57   return AnnouncementNotificationService::Create(
58       profile, pref, std::move(delegate), base::DefaultClock::GetInstance());
59 }
60 
61 content::BrowserContext*
GetBrowserContextToUse(content::BrowserContext * context) const62 AnnouncementNotificationServiceFactory::GetBrowserContextToUse(
63     content::BrowserContext* context) const {
64   return chrome::GetBrowserContextOwnInstanceInIncognito(context);
65 }
66 
AnnouncementNotificationServiceFactory()67 AnnouncementNotificationServiceFactory::AnnouncementNotificationServiceFactory()
68     : BrowserContextKeyedServiceFactory(
69           "AnnouncementNotificationService",
70           BrowserContextDependencyManager::GetInstance()) {
71   DependsOn(NotificationDisplayServiceFactory::GetInstance());
72 }
73 
74 AnnouncementNotificationServiceFactory::
75     ~AnnouncementNotificationServiceFactory() = default;
76