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 | |
| 9 | #include "chrome/browser/profiles/incognito_helpers.h" |
| 10 | #include "chrome/browser/profiles/profile.h" |
| 11 | #include "chrome/browser/sharesheet/sharesheet_service.h" |
| 12 | #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 13 | |
| 14 | #if defined(OS_CHROMEOS) |
| 15 | #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 16 | #endif // OS_CHROMEOS |
| 17 | |
| 18 | namespace sharesheet { |
| 19 | |
| 20 | // static |
| 21 | SharesheetService* SharesheetServiceFactory::GetForProfile(Profile* profile) { |
| 22 | // TODO: decide the right behaviour in incognito (non-guest) profiles: |
| 23 | // - return nullptr (means we need to null check the service at call sites |
| 24 | // OR ensure it's never accessed from an incognito profile), |
| 25 | // - return the service attached to the Profile that the incognito profile |
| 26 | // is branched from (i.e. "inherit" the parent service), |
| 27 | // - return a temporary service just for the incognito session (probably |
| 28 | // the least sensible option). |
| 29 | return static_cast<SharesheetService*>( |
| 30 | SharesheetServiceFactory::GetInstance()->GetServiceForBrowserContext( |
| 31 | profile, true /* create */)); |
| 32 | } |
| 33 | |
| 34 | // static |
| 35 | SharesheetServiceFactory* SharesheetServiceFactory::GetInstance() { |
| 36 | return base::Singleton<SharesheetServiceFactory>::get(); |
| 37 | } |
| 38 | |
| 39 | SharesheetServiceFactory::SharesheetServiceFactory() |
| 40 | : BrowserContextKeyedServiceFactory( |
| 41 | "SharesheetService", |
| 42 | BrowserContextDependencyManager::GetInstance()) {} |
| 43 | |
| 44 | SharesheetServiceFactory::~SharesheetServiceFactory() = default; |
| 45 | |
| 46 | KeyedService* SharesheetServiceFactory::BuildServiceInstanceFor( |
| 47 | content::BrowserContext* context) const { |
| 48 | return new SharesheetService(Profile::FromBrowserContext(context)); |
| 49 | } |
| 50 | |
| 51 | content::BrowserContext* SharesheetServiceFactory::GetBrowserContextToUse( |
| 52 | content::BrowserContext* context) const { |
| 53 | Profile* const profile = Profile::FromBrowserContext(context); |
| 54 | if (!profile || profile->IsSystemProfile()) { |
| 55 | return nullptr; |
| 56 | } |
| 57 | |
| 58 | #if defined(OS_CHROMEOS) |
| 59 | if (chromeos::ProfileHelper::IsSigninProfile(profile)) { |
| 60 | return nullptr; |
| 61 | } |
| 62 | |
| 63 | // We allow sharing in guest mode. |
| 64 | if (profile->IsGuestSession()) { |
| 65 | return chrome::GetBrowserContextOwnInstanceInIncognito(context); |
| 66 | } |
| 67 | #endif // OS_CHROMEOS |
| 68 | |
| 69 | return BrowserContextKeyedServiceFactory::GetBrowserContextToUse(context); |
| 70 | } |
| 71 | |
| 72 | bool SharesheetServiceFactory::ServiceIsCreatedWithBrowserContext() const { |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | } // namespace sharesheet |