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