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 "base/command_line.h"
8 #include "base/files/file_util.h"
9 #include "base/path_service.h"
10 #include "base/strings/string_split.h"
11 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
12 #include "chrome/browser/extensions/tab_helper.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/sessions/session_tab_helper.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/url_constants.h"
18 #include "chrome/grit/chromium_strings.h"
19 #include "components/guest_view/browser/guest_view_manager.h"
20 #include "components/signin/core/common/profile_management_switches.h"
21 #include "content/public/browser/render_frame_host.h"
22 #include "content/public/browser/web_ui.h"
23 #include "content/public/browser/web_ui_data_source.h"
24 #include "extensions/browser/guest_view/web_view/web_view_guest.h"
25 #include "grit/browser_resources.h"
26 #if defined(OS_CHROMEOS)
27 #include "chrome/browser/chromeos/login/startup_utils.h"
28 #include "chrome/browser/ui/webui/chromeos/login/inline_login_handler_chromeos.h"
30 #include "chrome/browser/ui/webui/signin/inline_login_handler_impl.h"
35 bool HandleTestFileRequestCallback(
36 const std::string
& path
,
37 const content::WebUIDataSource::GotDataCallback
& callback
) {
38 std::vector
<std::string
> url_substr
= base::SplitString(
39 path
, "/", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
40 if (url_substr
.size() != 2 || url_substr
[0] != "test")
44 base::FilePath test_data_dir
;
45 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
46 if (!base::ReadFileToString(
47 test_data_dir
.AppendASCII("webui").AppendASCII(url_substr
[1]),
48 &contents
, std::string::npos
))
51 base::RefCountedString
* ref_contents
= new base::RefCountedString();
52 ref_contents
->data() = contents
;
53 callback
.Run(ref_contents
);
57 content::WebUIDataSource
* CreateWebUIDataSource() {
58 content::WebUIDataSource
* source
=
59 content::WebUIDataSource::Create(chrome::kChromeUIChromeSigninHost
);
60 source
->OverrideContentSecurityPolicyFrameSrc("frame-src chrome-extension:;");
61 source
->OverrideContentSecurityPolicyObjectSrc("object-src *;");
62 source
->SetJsonPath("strings.js");
64 bool is_webview_signin_enabled
= switches::IsEnableWebviewBasedSignin();
65 source
->SetDefaultResource(is_webview_signin_enabled
?
66 IDR_NEW_INLINE_LOGIN_HTML
: IDR_INLINE_LOGIN_HTML
);
68 // Only add a filter when runing as test.
69 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
70 const bool is_running_test
= command_line
->HasSwitch(::switches::kTestName
) ||
71 command_line
->HasSwitch(::switches::kTestType
);
73 source
->SetRequestFilter(base::Bind(&HandleTestFileRequestCallback
));
75 source
->AddResourcePath("inline_login.css", IDR_INLINE_LOGIN_CSS
);
76 source
->AddResourcePath("inline_login.js", IDR_INLINE_LOGIN_JS
);
77 source
->AddResourcePath("gaia_auth_host.js", is_webview_signin_enabled
?
78 IDR_GAIA_AUTH_AUTHENTICATOR_JS
: IDR_GAIA_AUTH_HOST_JS
);
80 source
->AddLocalizedString("title", IDS_CHROME_SIGNIN_TITLE
);
84 void AddToSetIfIsAuthIframe(std::set
<content::RenderFrameHost
*>* frame_set
,
85 const GURL
& parent_origin
,
86 const std::string
& parent_frame_name
,
87 content::RenderFrameHost
* frame
) {
88 content::RenderFrameHost
* parent
= frame
->GetParent();
89 if (parent
&& parent
->GetFrameName() == parent_frame_name
&&
90 (parent_origin
.is_empty() ||
91 parent
->GetLastCommittedURL().GetOrigin() == parent_origin
)) {
92 frame_set
->insert(frame
);
96 bool AddToSetIfSigninWebview(std::set
<content::RenderFrameHost
*>* frame_set
,
97 const std::string
& web_view_name
,
98 content::WebContents
* web_contents
) {
99 auto* web_view
= extensions::WebViewGuest::FromWebContents(web_contents
);
100 if (web_view
&& web_view
->name() == web_view_name
)
101 frame_set
->insert(web_contents
->GetMainFrame());
107 InlineLoginUI::InlineLoginUI(content::WebUI
* web_ui
)
108 : WebDialogUI(web_ui
),
109 auth_extension_(Profile::FromWebUI(web_ui
)) {
110 Profile
* profile
= Profile::FromWebUI(web_ui
);
111 content::WebUIDataSource::Add(profile
, CreateWebUIDataSource());
113 #if defined(OS_CHROMEOS)
114 web_ui
->AddMessageHandler(new chromeos::InlineLoginHandlerChromeOS());
116 web_ui
->AddMessageHandler(new InlineLoginHandlerImpl());
118 content::WebContents
* contents
= web_ui
->GetWebContents();
119 // Required for intercepting extension function calls when the page is loaded
120 // in a bubble (not a full tab, thus tab helpers are not registered
122 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
124 extensions::TabHelper::CreateForWebContents(contents
);
125 // Ensure that the login UI has a tab ID, which will allow the GAIA auth
126 // extension's background script to tell it apart from iframes injected by
128 SessionTabHelper::CreateForWebContents(contents
);
131 InlineLoginUI::~InlineLoginUI() {}
133 // Gets the Gaia iframe within a WebContents.
134 content::RenderFrameHost
* InlineLoginUI::GetAuthFrame(
135 content::WebContents
* web_contents
,
136 const GURL
& parent_origin
,
137 const std::string
& parent_frame_name
) {
138 std::set
<content::RenderFrameHost
*> frame_set
;
139 bool is_webview
= switches::IsEnableWebviewBasedSignin();
140 #if defined(OS_CHROMEOS)
141 is_webview
= is_webview
|| chromeos::StartupUtils::IsWebviewSigninEnabled();
144 guest_view::GuestViewManager
* manager
=
145 guest_view::GuestViewManager::FromBrowserContext(
146 web_contents
->GetBrowserContext());
148 manager
->ForEachGuest(
150 base::Bind(&AddToSetIfSigninWebview
, &frame_set
, parent_frame_name
));
153 web_contents
->ForEachFrame(
154 base::Bind(&AddToSetIfIsAuthIframe
, &frame_set
,
155 parent_origin
, parent_frame_name
));
157 DCHECK_GE(1U, frame_set
.size());
158 if (!frame_set
.empty())
159 return *frame_set
.begin();