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