1 // Copyright 2017 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 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_SIGNIN_PARTITION_MANAGER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_SIGNIN_PARTITION_MANAGER_H_
7 
8 #include <string>
9 
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/singleton.h"
14 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
15 #include "components/keyed_service/core/keyed_service.h"
16 
17 namespace content {
18 class BrowserContext;
19 class StoragePartition;
20 class WebContents;
21 }  // namespace content
22 
23 namespace network {
24 namespace mojom {
25 class NetworkContext;
26 }  // namespace mojom
27 }  // namespace network
28 
29 namespace chromeos {
30 namespace login {
31 
32 // Manages storage partitions for sign-in attempts on the sign-in screen and
33 // enrollment screen.
34 class SigninPartitionManager : public KeyedService {
35  public:
36   using ClearStoragePartitionTask =
37       base::RepeatingCallback<void(content::StoragePartition* storage_partition,
38                                    base::OnceClosure data_cleared)>;
39 
40   using GetSystemNetworkContextTask =
41       base::RepeatingCallback<network::mojom::NetworkContext*()>;
42 
43   using OnCreateNewStoragePartition =
44       base::RepeatingCallback<void(content::StoragePartition*)>;
45 
46   using StartSigninSessionDoneCallback =
47       base::OnceCallback<void(const std::string& partition_name)>;
48 
49   explicit SigninPartitionManager(content::BrowserContext* browser_context);
50   ~SigninPartitionManager() override;
51 
52   // Creates a new StoragePartition for a sign-in attempt. If a previous
53   // StoragePartition has been created by this SigninPartitionManager, it is
54   // closed (and cleared).
55   // `embedder_web_contents` is the WebContents instance embedding the webview
56   // which will display the sign-in pages.
57   // `signin_session_started` will be invoked with the partition name of the
58   // started signin session on completion.
59   void StartSigninSession(
60       content::WebContents* embedder_web_contents,
61       StartSigninSessionDoneCallback signin_session_started);
62 
63   // Closes the current StoragePartition. All cached data in the
64   // StoragePartition is cleared. `partition_data_cleared` will be called when
65   // clearing of cached data is finished.
66   void CloseCurrentSigninSession(base::OnceClosure partition_data_cleared);
67 
68   // Returns true if a sign-in session is active, that is between
69   // StartSigninSession and CloseCurrentSigninSession calls.
70   bool IsInSigninSession() const;
71 
72   // Returns the current StoragePartition name. This can be used as a webview's
73   // `partition` attribute. May only be called when a sign-in session is active,
74   // that is between StartSigninSession and CloseCurrentSigninSession calls.
75   const std::string& GetCurrentStoragePartitionName() const;
76 
77   // Returns the current StoragePartition. May only be called when a sign-in
78   // session is active, that is between StartSigninSession and
79   // CloseCurrentSigninSession calls.
80   content::StoragePartition* GetCurrentStoragePartition();
81 
82   // Returns true if `storage_partition` is the partition in use by the current
83   // sign-in session. Returns false if no sign-in session is active.
84   bool IsCurrentSigninStoragePartition(
85       const content::StoragePartition* storage_partition) const;
86 
87   void SetClearStoragePartitionTaskForTesting(
88       ClearStoragePartitionTask clear_storage_partition_task);
89   void SetGetSystemNetworkContextForTesting(
90       GetSystemNetworkContextTask get_system_network_context_task);
91   void SetOnCreateNewStoragePartitionForTesting(
92       OnCreateNewStoragePartition on_create_new_storage_partition);
93 
94   class Factory : public BrowserContextKeyedServiceFactory {
95    public:
96     static SigninPartitionManager* GetForBrowserContext(
97         content::BrowserContext* browser_context);
98 
99     static Factory* GetInstance();
100 
101    private:
102     friend struct base::DefaultSingletonTraits<Factory>;
103 
104     Factory();
105     ~Factory() override;
106 
107     // BrowserContextKeyedServiceFactory:
108     KeyedService* BuildServiceInstanceFor(
109         content::BrowserContext* context) const override;
110     content::BrowserContext* GetBrowserContextToUse(
111         content::BrowserContext* context) const override;
112 
113     DISALLOW_COPY_AND_ASSIGN(Factory);
114   };
115 
116  private:
117   content::BrowserContext* const browser_context_;
118 
119   ClearStoragePartitionTask clear_storage_partition_task_;
120   GetSystemNetworkContextTask get_system_network_context_task_;
121   OnCreateNewStoragePartition on_create_new_storage_partition_;
122 
123   // GuestView StoragePartitions use the host of the embedder site's URL as the
124   // domain of their StoragePartition.
125   std::string storage_partition_domain_;
126   // The random and unique name of the StoragePartition to be used, is generated
127   // by SigninPartitionManager.
128   std::string current_storage_partition_name_;
129   // The StoragePartition identified by `storage_partition_domain_` and
130   // `current_storage_partition_name_`.
131   content::StoragePartition* current_storage_partition_ = nullptr;
132 
133   DISALLOW_COPY_AND_ASSIGN(SigninPartitionManager);
134 };
135 
136 }  // namespace login
137 }  // namespace chromeos
138 
139 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_SIGNIN_PARTITION_MANAGER_H_
140