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