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/ui/webui/signin/inline_login_ui.h"
7 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
8 #include "chrome/browser/extensions/tab_helper.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/sessions/session_tab_helper.h"
11 #include "chrome/common/url_constants.h"
12 #include "chrome/grit/chromium_strings.h"
13 #include "components/signin/core/common/profile_management_switches.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/web_ui.h"
16 #include "content/public/browser/web_ui_data_source.h"
17 #include "extensions/browser/guest_view/guest_view_manager.h"
18 #include "grit/browser_resources.h"
19 #if defined(OS_CHROMEOS)
20 #include "chrome/browser/chromeos/login/startup_utils.h"
21 #include "chrome/browser/ui/webui/chromeos/login/inline_login_handler_chromeos.h"
23 #include "chrome/browser/ui/webui/signin/inline_login_handler_impl.h"
28 content::WebUIDataSource
* CreateWebUIDataSource() {
29 content::WebUIDataSource
* source
=
30 content::WebUIDataSource::Create(chrome::kChromeUIChromeSigninHost
);
31 source
->OverrideContentSecurityPolicyFrameSrc("frame-src chrome-extension:;");
32 source
->OverrideContentSecurityPolicyObjectSrc("object-src *;");
33 source
->SetJsonPath("strings.js");
35 bool is_webview_signin_enabled
= switches::IsEnableWebviewBasedSignin();
36 source
->SetDefaultResource(is_webview_signin_enabled
?
37 IDR_NEW_INLINE_LOGIN_HTML
: IDR_INLINE_LOGIN_HTML
);
38 source
->AddResourcePath("inline_login.css", IDR_INLINE_LOGIN_CSS
);
39 source
->AddResourcePath("inline_login.js", IDR_INLINE_LOGIN_JS
);
40 source
->AddResourcePath("gaia_auth_host.js", is_webview_signin_enabled
?
41 IDR_GAIA_AUTH_AUTHENTICATOR_JS
: IDR_GAIA_AUTH_HOST_JS
);
43 source
->AddLocalizedString("title", IDS_CHROME_SIGNIN_TITLE
);
47 void AddToSetIfIsAuthIframe(std::set
<content::RenderFrameHost
*>* frame_set
,
48 const GURL
& parent_origin
,
49 const std::string
& parent_frame_name
,
50 content::RenderFrameHost
* frame
) {
51 content::RenderFrameHost
* parent
= frame
->GetParent();
52 if (parent
&& parent
->GetFrameName() == parent_frame_name
&&
53 (parent_origin
.is_empty() ||
54 parent
->GetLastCommittedURL().GetOrigin() == parent_origin
)) {
55 frame_set
->insert(frame
);
59 bool AddToSetIfSigninWebview(std::set
<content::RenderFrameHost
*>* frame_set
,
60 content::WebContents
* web_contents
) {
61 frame_set
->insert(web_contents
->GetMainFrame());
67 InlineLoginUI::InlineLoginUI(content::WebUI
* web_ui
)
68 : WebDialogUI(web_ui
),
69 auth_extension_(Profile::FromWebUI(web_ui
)) {
70 Profile
* profile
= Profile::FromWebUI(web_ui
);
71 content::WebUIDataSource::Add(profile
, CreateWebUIDataSource());
73 #if defined(OS_CHROMEOS)
74 web_ui
->AddMessageHandler(new chromeos::InlineLoginHandlerChromeOS());
76 web_ui
->AddMessageHandler(new InlineLoginHandlerImpl());
78 content::WebContents
* contents
= web_ui
->GetWebContents();
79 // Required for intercepting extension function calls when the page is loaded
80 // in a bubble (not a full tab, thus tab helpers are not registered
82 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
84 extensions::TabHelper::CreateForWebContents(contents
);
85 // Ensure that the login UI has a tab ID, which will allow the GAIA auth
86 // extension's background script to tell it apart from iframes injected by
88 SessionTabHelper::CreateForWebContents(contents
);
91 InlineLoginUI::~InlineLoginUI() {}
93 // Gets the Gaia iframe within a WebContents.
94 content::RenderFrameHost
* InlineLoginUI::GetAuthIframe(
95 content::WebContents
* web_contents
,
96 const GURL
& parent_origin
,
97 const std::string
& parent_frame_name
) {
98 std::set
<content::RenderFrameHost
*> frame_set
;
99 bool is_webview
= switches::IsEnableWebviewBasedSignin();
100 #if defined(OS_CHROMEOS)
101 is_webview
= is_webview
|| chromeos::StartupUtils::IsWebviewSigninEnabled();
104 extensions::GuestViewManager
* manager
=
105 extensions::GuestViewManager::FromBrowserContext(
106 web_contents
->GetBrowserContext());
107 manager
->ForEachGuest(web_contents
,
108 base::Bind(&AddToSetIfSigninWebview
, &frame_set
));
110 web_contents
->ForEachFrame(
111 base::Bind(&AddToSetIfIsAuthIframe
, &frame_set
,
112 parent_origin
, parent_frame_name
));
114 DCHECK_GE(1U, frame_set
.size());
115 if (!frame_set
.empty())
116 return *frame_set
.begin();