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 #include "chrome/browser/ui/webui/signin/inline_login_ui.h"
6
7 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/sessions/session_tab_helper.h"
10 #include "chrome/common/url_constants.h"
11 #include "chrome/grit/chromium_strings.h"
12 #include "content/public/browser/render_frame_host.h"
13 #include "content/public/browser/web_ui.h"
14 #include "content/public/browser/web_ui_data_source.h"
15 #include "grit/browser_resources.h"
16 #if defined(OS_CHROMEOS)
17 #include "chrome/browser/ui/webui/chromeos/login/inline_login_handler_chromeos.h"
18 #else
19 #include "chrome/browser/ui/webui/signin/inline_login_handler_impl.h"
20 #endif
21
22 namespace {
23
CreateWebUIDataSource()24 content::WebUIDataSource* CreateWebUIDataSource() {
25 content::WebUIDataSource* source =
26 content::WebUIDataSource::Create(chrome::kChromeUIChromeSigninHost);
27 source->OverrideContentSecurityPolicyFrameSrc("frame-src chrome-extension:;");
28 source->OverrideContentSecurityPolicyObjectSrc("object-src *;");
29 source->SetUseJsonJSFormatV2();
30 source->SetJsonPath("strings.js");
31
32 source->SetDefaultResource(IDR_INLINE_LOGIN_HTML);
33 source->AddResourcePath("inline_login.css", IDR_INLINE_LOGIN_CSS);
34 source->AddResourcePath("inline_login.js", IDR_INLINE_LOGIN_JS);
35
36 source->AddLocalizedString("title", IDS_CHROME_SIGNIN_TITLE);
37 return source;
38 }
39
AddToSetIfIsAuthIframe(std::set<content::RenderFrameHost * > * frame_set,const GURL & parent_origin,const std::string & parent_frame_name,content::RenderFrameHost * frame)40 void AddToSetIfIsAuthIframe(std::set<content::RenderFrameHost*>* frame_set,
41 const GURL& parent_origin,
42 const std::string& parent_frame_name,
43 content::RenderFrameHost* frame) {
44 content::RenderFrameHost* parent = frame->GetParent();
45 if (parent && parent->GetFrameName() == parent_frame_name &&
46 (parent_origin.is_empty() ||
47 parent->GetLastCommittedURL().GetOrigin() == parent_origin)) {
48 frame_set->insert(frame);
49 }
50 }
51
52 } // empty namespace
53
InlineLoginUI(content::WebUI * web_ui)54 InlineLoginUI::InlineLoginUI(content::WebUI* web_ui)
55 : WebDialogUI(web_ui),
56 auth_extension_(Profile::FromWebUI(web_ui)) {
57 Profile* profile = Profile::FromWebUI(web_ui);
58 content::WebUIDataSource::Add(profile, CreateWebUIDataSource());
59
60 #if defined(OS_CHROMEOS)
61 web_ui->AddMessageHandler(new chromeos::InlineLoginHandlerChromeOS());
62 #else
63 web_ui->AddMessageHandler(new InlineLoginHandlerImpl());
64 #endif
65 content::WebContents* contents = web_ui->GetWebContents();
66 // Required for intercepting extension function calls when the page is loaded
67 // in a bubble (not a full tab, thus tab helpers are not registered
68 // automatically).
69 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
70 contents);
71 // Ensure that the login UI has a tab ID, which will allow the GAIA auth
72 // extension's background script to tell it apart from iframes injected by
73 // other extensions.
74 SessionTabHelper::CreateForWebContents(contents);
75 }
76
~InlineLoginUI()77 InlineLoginUI::~InlineLoginUI() {}
78
79 // Gets the Gaia iframe within a WebContents.
GetAuthIframe(content::WebContents * web_contents,const GURL & parent_origin,const std::string & parent_frame_name)80 content::RenderFrameHost* InlineLoginUI::GetAuthIframe(
81 content::WebContents* web_contents,
82 const GURL& parent_origin,
83 const std::string& parent_frame_name) {
84 std::set<content::RenderFrameHost*> frame_set;
85 web_contents->ForEachFrame(
86 base::Bind(&AddToSetIfIsAuthIframe, &frame_set,
87 parent_origin, parent_frame_name));
88 DCHECK_GE(1U, frame_set.size());
89 if (!frame_set.empty())
90 return *frame_set.begin();
91
92 return NULL;
93 }
94