Melissa Zhang | ff2bca98 | 2020-06-24 02:54:48 | [diff] [blame] | 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/sharesheet/sharesheet_service_factory.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame] | 9 | #include "build/chromeos_buildflags.h" |
Melissa Zhang | 59ca374f | 2020-07-22 05:55:51 | [diff] [blame] | 10 | #include "chrome/browser/apps/app_service/app_service_proxy_factory.h" |
Melissa Zhang | ff2bca98 | 2020-06-24 02:54:48 | [diff] [blame] | 11 | #include "chrome/browser/profiles/incognito_helpers.h" |
| 12 | #include "chrome/browser/profiles/profile.h" |
| 13 | #include "chrome/browser/sharesheet/sharesheet_service.h" |
| 14 | #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 15 | |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame] | 16 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Yeunjoo Choi | 15ab1ac | 2021-02-04 17:15:07 | [diff] [blame] | 17 | #include "chrome/browser/ash/profiles/profile_helper.h" |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame] | 18 | #endif // BUILDFLAG(IS_CHROMEOS_ASH) |
Melissa Zhang | ff2bca98 | 2020-06-24 02:54:48 | [diff] [blame] | 19 | |
| 20 | namespace sharesheet { |
| 21 | |
| 22 | // static |
| 23 | SharesheetService* SharesheetServiceFactory::GetForProfile(Profile* profile) { |
Melissa Zhang | ff2bca98 | 2020-06-24 02:54:48 | [diff] [blame] | 24 | return static_cast<SharesheetService*>( |
| 25 | SharesheetServiceFactory::GetInstance()->GetServiceForBrowserContext( |
| 26 | profile, true /* create */)); |
| 27 | } |
| 28 | |
| 29 | // static |
| 30 | SharesheetServiceFactory* SharesheetServiceFactory::GetInstance() { |
| 31 | return base::Singleton<SharesheetServiceFactory>::get(); |
| 32 | } |
| 33 | |
| 34 | SharesheetServiceFactory::SharesheetServiceFactory() |
| 35 | : BrowserContextKeyedServiceFactory( |
| 36 | "SharesheetService", |
Melissa Zhang | 59ca374f | 2020-07-22 05:55:51 | [diff] [blame] | 37 | BrowserContextDependencyManager::GetInstance()) { |
| 38 | DependsOn(apps::AppServiceProxyFactory::GetInstance()); |
| 39 | } |
Melissa Zhang | ff2bca98 | 2020-06-24 02:54:48 | [diff] [blame] | 40 | |
| 41 | SharesheetServiceFactory::~SharesheetServiceFactory() = default; |
| 42 | |
| 43 | KeyedService* SharesheetServiceFactory::BuildServiceInstanceFor( |
| 44 | content::BrowserContext* context) const { |
| 45 | return new SharesheetService(Profile::FromBrowserContext(context)); |
| 46 | } |
| 47 | |
| 48 | content::BrowserContext* SharesheetServiceFactory::GetBrowserContextToUse( |
| 49 | content::BrowserContext* context) const { |
| 50 | Profile* const profile = Profile::FromBrowserContext(context); |
| 51 | if (!profile || profile->IsSystemProfile()) { |
| 52 | return nullptr; |
| 53 | } |
| 54 | |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame] | 55 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Yeunjoo Choi | 36f21d54 | 2022-01-13 03:12:33 | [diff] [blame] | 56 | if (ash::ProfileHelper::IsSigninProfile(profile)) { |
Melissa Zhang | ff2bca98 | 2020-06-24 02:54:48 | [diff] [blame] | 57 | return nullptr; |
| 58 | } |
| 59 | |
Melissa Zhang | 52cbd3b6 | 2020-10-21 00:13:34 | [diff] [blame] | 60 | // We allow sharing in guest mode or incognito mode.. |
Melissa Zhang | ff2bca98 | 2020-06-24 02:54:48 | [diff] [blame] | 61 | if (profile->IsGuestSession()) { |
| 62 | return chrome::GetBrowserContextOwnInstanceInIncognito(context); |
| 63 | } |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame] | 64 | #endif // BUILDFLAG(IS_CHROMEOS_ASH) |
Melissa Zhang | ff2bca98 | 2020-06-24 02:54:48 | [diff] [blame] | 65 | |
Melissa Zhang | 52cbd3b6 | 2020-10-21 00:13:34 | [diff] [blame] | 66 | return chrome::GetBrowserContextRedirectedInIncognito(context); |
Melissa Zhang | ff2bca98 | 2020-06-24 02:54:48 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | bool SharesheetServiceFactory::ServiceIsCreatedWithBrowserContext() const { |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | } // namespace sharesheet |