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