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.
5 #include "chrome/browser/signin/signin_header_helper.h"
7 #include "chrome/browser/extensions/extension_renderer_state.h"
8 #include "chrome/browser/profiles/profile_io_data.h"
9 #include "chrome/browser/tab_contents/tab_util.h"
10 #include "chrome/browser/ui/browser_finder.h"
11 #include "chrome/browser/ui/browser_window.h"
12 #include "chrome/common/profile_management_switches.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/web_contents.h"
15 #include "google_apis/gaia/gaia_auth_util.h"
16 #include "net/http/http_response_headers.h"
17 #include "net/url_request/url_request.h"
19 #if defined(OS_ANDROID)
20 #include "chrome/browser/android/signin/account_management_screen_helper.h"
21 #endif // defined(OS_ANDROID)
25 const char kChromeConnectedHeader
[] = "X-Chrome-Connected";
26 const char kChromeManageAccountsHeader
[] = "X-Chrome-Manage-Accounts";
27 const char kGaiaAuthExtensionID
[] = "mfffpogegjflfpflabcdkioaeobkgjik";
29 // Show profile avatar bubble on UI thread. Must be called on the UI thread.
30 void ShowAvatarBubbleUIThread(int child_id
, int route_id
) {
31 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
33 #if !defined(OS_ANDROID)
34 content::WebContents
* web_contents
=
35 tab_util::GetWebContentsByID(child_id
, route_id
);
39 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents
);
41 browser
->window()->ShowAvatarBubbleFromAvatarButton();
42 // TODO(guohui): need to handle the case when avatar button is not available.
43 #else // defined(OS_ANDROID)
44 AccountManagementScreenHelper::OpenAccountManagementScreen();
48 bool IsDriveOrigin(const GURL
& url
) {
49 if (!url
.SchemeIsSecure())
52 const GURL
kGoogleDriveURL("https://drive.google.com");
53 const GURL
kGoogleDocsURL("https://docs.google.com");
54 return url
== kGoogleDriveURL
|| url
== kGoogleDocsURL
;
61 void AppendMirrorRequestHeaderIfPossible(
62 net::URLRequest
* request
,
63 const GURL
& redirect_url
,
64 ProfileIOData
* io_data
,
67 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO
));
69 if (io_data
->is_incognito() ||
70 io_data
->google_services_username()->GetValue().empty()) {
74 // Only set the header for Gaia (in the mirror world) and Drive. Gaia needs
75 // the header to redirect certain user actions to Chrome native UI. Drive
76 // needs the header to tell if the current user is connected. The drive path
77 // is a temporary workaround until the more generic chrome.principals API is
79 const GURL
& url
= redirect_url
.is_empty() ? request
->url() : redirect_url
;
80 GURL
origin(url
.GetOrigin());
81 bool is_gaia_origin
= !switches::IsEnableWebBasedSignin() &&
82 switches::IsNewProfileManagement() &&
83 gaia::IsGaiaSignonRealm(origin
);
84 if (!is_gaia_origin
&& !IsDriveOrigin(origin
))
87 ExtensionRendererState
* renderer_state
=
88 ExtensionRendererState::GetInstance();
89 ExtensionRendererState::WebViewInfo webview_info
;
90 bool is_guest
= renderer_state
->GetWebViewInfo(
91 child_id
, route_id
, &webview_info
);
92 if (is_guest
&& webview_info
.extension_id
== kGaiaAuthExtensionID
){
96 std::string
account_id(io_data
->google_services_account_id()->GetValue());
97 if (account_id
.empty())
98 account_id
= "1"; // Dummy value if focus ID not available yet.
100 request
->SetExtraRequestHeaderByName(
101 kChromeConnectedHeader
, account_id
, false);
104 void ProcessMirrorResponseHeaderIfExists(
105 net::URLRequest
* request
,
106 ProfileIOData
* io_data
,
109 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO
));
111 if (gaia::IsGaiaSignonRealm(request
->url().GetOrigin()) &&
112 request
->response_headers()->HasHeader(kChromeManageAccountsHeader
)) {
113 DCHECK(switches::IsNewProfileManagement() &&
114 !io_data
->is_incognito());
115 content::BrowserThread::PostTask(
116 content::BrowserThread::UI
, FROM_HERE
,
117 base::Bind(ShowAvatarBubbleUIThread
, child_id
, route_id
));
121 } // namespace signin