1 // Copyright 2013 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_CHROME_SIGNIN_HELPER_H_
6 #define CHROME_BROWSER_SIGNIN_CHROME_SIGNIN_HELPER_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/macros.h"
12 #include "base/supports_user_data.h"
13 #include "chrome/browser/prefs/incognito_mode_prefs.h"
14 #include "components/signin/core/browser/signin_header_helper.h"
15 #include "content/public/browser/web_contents.h"
16 #include "third_party/blink/public/mojom/loader/resource_load_info.mojom-shared.h"
17 
18 namespace content_settings {
19 class CookieSettings;
20 }
21 
22 namespace net {
23 class HttpResponseHeaders;
24 }
25 
26 class GURL;
27 
28 // Utility functions for handling Chrome/Gaia headers during signin process.
29 // Chrome identity should always stay in sync with Gaia identity. Therefore
30 // Chrome needs to send Gaia special header for requests from a connected
31 // profile, so that Gaia can modify its response accordingly and let Chrome
32 // handle signin accordingly.
33 namespace signin {
34 
35 // Key for ManageAccountsHeaderReceivedUserData. Exposed for testing.
36 extern const void* const kManageAccountsHeaderReceivedUserDataKey;
37 
38 // The source to use when constructing the Mirror header.
39 extern const char kChromeMirrorHeaderSource[];
40 
41 class ChromeRequestAdapter : public RequestAdapter {
42  public:
43   ChromeRequestAdapter(const GURL& url,
44                        const net::HttpRequestHeaders& original_headers,
45                        net::HttpRequestHeaders* modified_headers,
46                        std::vector<std::string>* headers_to_remove);
47   ~ChromeRequestAdapter() override;
48 
49   virtual content::WebContents::Getter GetWebContentsGetter() const = 0;
50 
51   virtual blink::mojom::ResourceType GetResourceType() const = 0;
52 
53   virtual GURL GetReferrerOrigin() const = 0;
54 
55   // Associate a callback with this request which will be executed when the
56   // request is complete (including any redirects). If a callback was already
57   // registered this function does nothing.
58   virtual void SetDestructionCallback(base::OnceClosure closure) = 0;
59 
60  private:
61   DISALLOW_COPY_AND_ASSIGN(ChromeRequestAdapter);
62 };
63 
64 class ResponseAdapter {
65  public:
66   ResponseAdapter();
67   virtual ~ResponseAdapter();
68 
69   virtual content::WebContents::Getter GetWebContentsGetter() const = 0;
70   virtual bool IsMainFrame() const = 0;
71   virtual GURL GetOrigin() const = 0;
72   virtual const net::HttpResponseHeaders* GetHeaders() const = 0;
73   virtual void RemoveHeader(const std::string& name) = 0;
74 
75   virtual base::SupportsUserData::Data* GetUserData(const void* key) const = 0;
76   virtual void SetUserData(
77       const void* key,
78       std::unique_ptr<base::SupportsUserData::Data> data) = 0;
79 
80  private:
81   DISALLOW_COPY_AND_ASSIGN(ResponseAdapter);
82 };
83 
84 // When Dice is enabled, the AccountReconcilor is blocked for a short delay
85 // after sending requests to Gaia. Exposed for testing.
86 void SetDiceAccountReconcilorBlockDelayForTesting(int delay_ms);
87 
88 // Adds an account consistency header to Gaia requests from a connected profile,
89 // with the exception of requests from gaia webview.
90 // Removes the header if it is already in the headers but should not be there.
91 void FixAccountConsistencyRequestHeader(
92     ChromeRequestAdapter* request,
93     const GURL& redirect_url,
94     bool is_off_the_record,
95     int incognito_availibility,
96     AccountConsistencyMethod account_consistency,
97     std::string gaia_id,
98     const base::Optional<bool>& is_child_account,
99 #if defined(OS_CHROMEOS)
100     bool is_secondary_account_addition_allowed,
101 #endif
102 #if BUILDFLAG(ENABLE_DICE_SUPPORT)
103     bool is_sync_enabled,
104     std::string signin_scoped_device_id,
105 #endif
106     content_settings::CookieSettings* cookie_settings);
107 
108 // Processes account consistency response headers (X-Chrome-Manage-Accounts and
109 // Dice). |redirect_url| is empty if the request is not a redirect.
110 void ProcessAccountConsistencyResponseHeaders(ResponseAdapter* response,
111                                               const GURL& redirect_url,
112                                               bool is_off_the_record);
113 
114 }  // namespace signin
115 
116 #endif  // CHROME_BROWSER_SIGNIN_CHROME_SIGNIN_HELPER_H_
117