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 "grit/browser_resources.h"
25 #if defined(OS_CHROMEOS)
26 #include "chrome/browser/chromeos/login/startup_utils.h"
27 #include "chrome/browser/ui/webui/chromeos/login/inline_login_handler_chromeos.h"
29 #include "chrome/browser/ui/webui/signin/inline_login_handler_impl.h"
34 bool HandleTestFileRequestCallback(
35 const std::string
& path
,
36 const content::WebUIDataSource::GotDataCallback
& callback
) {
37 std::vector
<std::string
> url_substr
;
38 base::SplitString(path
, '/', &url_substr
);
39 if (url_substr
.size() != 2 || url_substr
[0] != "test")
43 base::FilePath test_data_dir
;
44 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
45 if (!base::ReadFileToString(
46 test_data_dir
.AppendASCII("webui").AppendASCII(url_substr
[1]),
47 &contents
, std::string::npos
))
50 base::RefCountedString
* ref_contents
= new base::RefCountedString();
51 ref_contents
->data() = contents
;
52 callback
.Run(ref_contents
);
56 content::WebUIDataSource
* CreateWebUIDataSource() {
57 content::WebUIDataSource
* source
=
58 content::WebUIDataSource::Create(chrome::kChromeUIChromeSigninHost
);
59 source
->OverrideContentSecurityPolicyFrameSrc("frame-src chrome-extension:;");
60 source
->OverrideContentSecurityPolicyObjectSrc("object-src *;");
61 source
->SetJsonPath("strings.js");
63 bool is_webview_signin_enabled
= switches::IsEnableWebviewBasedSignin();
64 source
->SetDefaultResource(is_webview_signin_enabled
?
65 IDR_NEW_INLINE_LOGIN_HTML
: IDR_INLINE_LOGIN_HTML
);
67 // Only add a filter when runing as test.
68 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
69 const bool is_running_test
= command_line
->HasSwitch(::switches::kTestName
) ||
70 command_line
->HasSwitch(::switches::kTestType
);
72 source
->SetRequestFilter(base::Bind(&HandleTestFileRequestCallback
));
74 source
->AddResourcePath("inline_login.css", IDR_INLINE_LOGIN_CSS
);
75 source
->AddResourcePath("inline_login.js", IDR_INLINE_LOGIN_JS
);
76 source
->AddResourcePath("gaia_auth_host.js", is_webview_signin_enabled
?
77 IDR_GAIA_AUTH_AUTHENTICATOR_JS
: IDR_GAIA_AUTH_HOST_JS
);
79 source
->AddLocalizedString("title", IDS_CHROME_SIGNIN_TITLE
);
83 void AddToSetIfIsAuthIframe(std::set
<content::RenderFrameHost
*>* frame_set
,
84 const GURL
& parent_origin
,
85 const std::string
& parent_frame_name
,
86 content::RenderFrameHost
* frame
) {
87 content::RenderFrameHost
* parent
= frame
->GetParent();
88 if (parent
&& parent
->GetFrameName() == parent_frame_name
&&
89 (parent_origin
.is_empty() ||
90 parent
->GetLastCommittedURL().GetOrigin() == parent_origin
)) {
91 frame_set
->insert(frame
);
95 bool AddToSetIfSigninWebview(std::set
<content::RenderFrameHost
*>* frame_set
,
96 content::WebContents
* web_contents
) {
97 frame_set
->insert(web_contents
->GetMainFrame());
103 InlineLoginUI::InlineLoginUI(content::WebUI
* web_ui
)
104 : WebDialogUI(web_ui
),
105 auth_extension_(Profile::FromWebUI(web_ui
)) {
106 Profile
* profile
= Profile::FromWebUI(web_ui
);
107 content::WebUIDataSource::Add(profile
, CreateWebUIDataSource());
109 #if defined(OS_CHROMEOS)
110 web_ui
->AddMessageHandler(new chromeos::InlineLoginHandlerChromeOS());
112 web_ui
->AddMessageHandler(new InlineLoginHandlerImpl());
114 content::WebContents
* contents
= web_ui
->GetWebContents();
115 // Required for intercepting extension function calls when the page is loaded
116 // in a bubble (not a full tab, thus tab helpers are not registered
118 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
120 extensions::TabHelper::CreateForWebContents(contents
);
121 // Ensure that the login UI has a tab ID, which will allow the GAIA auth
122 // extension's background script to tell it apart from iframes injected by
124 SessionTabHelper::CreateForWebContents(contents
);
127 InlineLoginUI::~InlineLoginUI() {}
129 // Gets the Gaia iframe within a WebContents.
130 content::RenderFrameHost
* InlineLoginUI::GetAuthFrame(
131 content::WebContents
* web_contents
,
132 const GURL
& parent_origin
,
133 const std::string
& parent_frame_name
) {
134 std::set
<content::RenderFrameHost
*> frame_set
;
135 bool is_webview
= switches::IsEnableWebviewBasedSignin();
136 #if defined(OS_CHROMEOS)
137 is_webview
= is_webview
|| chromeos::StartupUtils::IsWebviewSigninEnabled();
140 guest_view::GuestViewManager
* manager
=
141 guest_view::GuestViewManager::FromBrowserContext(
142 web_contents
->GetBrowserContext());
144 manager
->ForEachGuest(web_contents
,
145 base::Bind(&AddToSetIfSigninWebview
, &frame_set
));
148 web_contents
->ForEachFrame(
149 base::Bind(&AddToSetIfIsAuthIframe
, &frame_set
,
150 parent_origin
, parent_frame_name
));
152 DCHECK_GE(1U, frame_set
.size());
153 if (!frame_set
.empty())
154 return *frame_set
.begin();