Matt Reynolds | b07027a | 2019-05-21 23:43:26 | [diff] [blame] | 1 | // Copyright 2019 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/hid/hid_chooser_context_factory.h" |
| 6 | |
David Sanders | de533fce | 2021-12-15 14:59:43 | [diff] [blame^] | 7 | #include "base/no_destructor.h" |
Matt Reynolds | b07027a | 2019-05-21 23:43:26 | [diff] [blame] | 8 | #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 9 | #include "chrome/browser/hid/hid_chooser_context.h" |
| 10 | #include "chrome/browser/profiles/incognito_helpers.h" |
| 11 | #include "chrome/browser/profiles/profile.h" |
| 12 | #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 13 | |
| 14 | // static |
| 15 | HidChooserContextFactory* HidChooserContextFactory::GetInstance() { |
| 16 | static base::NoDestructor<HidChooserContextFactory> factory; |
| 17 | return factory.get(); |
| 18 | } |
| 19 | |
| 20 | // static |
| 21 | HidChooserContext* HidChooserContextFactory::GetForProfile(Profile* profile) { |
| 22 | return static_cast<HidChooserContext*>( |
| 23 | GetInstance()->GetServiceForBrowserContext(profile, true)); |
| 24 | } |
| 25 | |
Andy Paicu | fa96c83 | 2021-06-24 16:17:15 | [diff] [blame] | 26 | // static |
| 27 | HidChooserContext* HidChooserContextFactory::GetForProfileIfExists( |
| 28 | Profile* profile) { |
| 29 | return static_cast<HidChooserContext*>( |
| 30 | GetInstance()->GetServiceForBrowserContext(profile, /*create=*/false)); |
| 31 | } |
| 32 | |
Matt Reynolds | b07027a | 2019-05-21 23:43:26 | [diff] [blame] | 33 | HidChooserContextFactory::HidChooserContextFactory() |
| 34 | : BrowserContextKeyedServiceFactory( |
| 35 | "HidChooserContext", |
| 36 | BrowserContextDependencyManager::GetInstance()) { |
| 37 | DependsOn(HostContentSettingsMapFactory::GetInstance()); |
| 38 | } |
| 39 | |
| 40 | HidChooserContextFactory::~HidChooserContextFactory() = default; |
| 41 | |
| 42 | KeyedService* HidChooserContextFactory::BuildServiceInstanceFor( |
| 43 | content::BrowserContext* context) const { |
| 44 | return new HidChooserContext(Profile::FromBrowserContext(context)); |
| 45 | } |
| 46 | |
| 47 | content::BrowserContext* HidChooserContextFactory::GetBrowserContextToUse( |
| 48 | content::BrowserContext* context) const { |
| 49 | return chrome::GetBrowserContextOwnInstanceInIncognito(context); |
| 50 | } |
Andy Paicu | fa96c83 | 2021-06-24 16:17:15 | [diff] [blame] | 51 | |
| 52 | void HidChooserContextFactory::BrowserContextShutdown( |
| 53 | content::BrowserContext* context) { |
| 54 | auto* hid_chooser_context = |
| 55 | GetForProfileIfExists(Profile::FromBrowserContext(context)); |
| 56 | if (hid_chooser_context) |
| 57 | hid_chooser_context->FlushScheduledSaveSettingsCalls(); |
| 58 | } |