blob: 670e5f109752ce5ce493140d13e95ea587b85bc7 [file] [log] [blame]
Melissa Zhangff2bca982020-06-24 02:54:481// 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
18namespace sharesheet {
19
20// static
21SharesheetService* 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
35SharesheetServiceFactory* SharesheetServiceFactory::GetInstance() {
36 return base::Singleton<SharesheetServiceFactory>::get();
37}
38
39SharesheetServiceFactory::SharesheetServiceFactory()
40 : BrowserContextKeyedServiceFactory(
41 "SharesheetService",
42 BrowserContextDependencyManager::GetInstance()) {}
43
44SharesheetServiceFactory::~SharesheetServiceFactory() = default;
45
46KeyedService* SharesheetServiceFactory::BuildServiceInstanceFor(
47 content::BrowserContext* context) const {
48 return new SharesheetService(Profile::FromBrowserContext(context));
49}
50
51content::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
72bool SharesheetServiceFactory::ServiceIsCreatedWithBrowserContext() const {
73 return true;
74}
75
76} // namespace sharesheet