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 #ifndef CHROME_BROWSER_SIGNIN_DICE_INTERCEPTED_SESSION_STARTUP_HELPER_H_
6 #define CHROME_BROWSER_SIGNIN_DICE_INTERCEPTED_SESSION_STARTUP_HELPER_H_
7 
8 #include "base/callback_forward.h"
9 #include "base/cancelable_callback.h"
10 #include "base/scoped_observer.h"
11 #include "base/time/time.h"
12 #include "components/signin/public/identity_manager/identity_manager.h"
13 #include "content/public/browser/web_contents_observer.h"
14 #include "google_apis/gaia/core_account_id.h"
15 
16 namespace content {
17 class WebContents;
18 }
19 
20 namespace signin {
21 struct AccountsInCookieJarInfo;
22 }
23 
24 class GoogleServiceAuthError;
25 class Profile;
26 
27 // Called when the user accepted the dice signin interception and the new
28 // profile has been created. Creates a new browser and moves the intercepted tab
29 // to the new browser.
30 // It is assumed that the account is already in the profile, but not necessarily
31 // in the content area (cookies).
32 class DiceInterceptedSessionStartupHelper
33     : public content::WebContentsObserver,
34       public signin::IdentityManager::Observer {
35  public:
36   // |profile| is the new profile that was created after signin interception.
37   // |account_id| is the main account for the profile, it's already in the
38   // profile.
39   // |tab_to_move| is the tab where the interception happened, in the source
40   // profile.
41   DiceInterceptedSessionStartupHelper(Profile* profile,
42                                       CoreAccountId account_id,
43                                       content::WebContents* tab_to_move);
44 
45   ~DiceInterceptedSessionStartupHelper() override;
46 
47   DiceInterceptedSessionStartupHelper(
48       const DiceInterceptedSessionStartupHelper&) = delete;
49   DiceInterceptedSessionStartupHelper& operator=(
50       const DiceInterceptedSessionStartupHelper&) = delete;
51 
52   // Start up the session. Can only be called once.
53   void Startup(base::OnceClosure callback);
54 
55   // signin::IdentityManager::Observer:
56   void OnAccountsInCookieUpdated(
57       const signin::AccountsInCookieJarInfo& accounts_in_cookie_jar_info,
58       const GoogleServiceAuthError& error) override;
59 
60  private:
61   // Creates a browser with a new tab, and closes the intercepted tab if it's
62   // still open.
63   void MoveTab();
64 
65   Profile* const profile_;
66   CoreAccountId account_id_;
67   base::OnceClosure callback_;
68   ScopedObserver<signin::IdentityManager, signin::IdentityManager::Observer>
69       accounts_in_cookie_observer_{this};
70   base::TimeTicks session_startup_time_;
71   // Timeout while waiting for the account to be added to the cookies in the new
72   // profile.
73   base::CancelableOnceCallback<void()> on_cookie_update_timeout_;
74 };
75 
76 #endif  // CHROME_BROWSER_SIGNIN_DICE_INTERCEPTED_SESSION_STARTUP_HELPER_H_
77