OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/signin/inline_login_ui.h" | 5 #include "chrome/browser/ui/webui/signin/inline_login_ui.h" |
6 | 6 |
7 #include "base/command_line.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 "build/build_config.h" | 8 #include "build/build_config.h" |
12 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" | 9 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" |
13 #include "chrome/browser/extensions/tab_helper.h" | 10 #include "chrome/browser/extensions/tab_helper.h" |
14 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/sessions/session_tab_helper.h" | 12 #include "chrome/browser/sessions/session_tab_helper.h" |
16 #include "chrome/browser/ui/webui/metrics_handler.h" | 13 #include "chrome/browser/ui/webui/metrics_handler.h" |
17 #include "chrome/common/chrome_paths.h" | 14 #include "chrome/browser/ui/webui/test_files_request_filter.h" |
18 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
20 #include "chrome/grit/chromium_strings.h" | 17 #include "chrome/grit/chromium_strings.h" |
21 #include "components/guest_view/browser/guest_view_manager.h" | |
22 #include "components/signin/core/common/profile_management_switches.h" | 18 #include "components/signin/core/common/profile_management_switches.h" |
23 #include "content/public/browser/render_frame_host.h" | |
24 #include "content/public/browser/web_ui.h" | 19 #include "content/public/browser/web_ui.h" |
25 #include "content/public/browser/web_ui_data_source.h" | 20 #include "content/public/browser/web_ui_data_source.h" |
26 #include "content/public/common/content_switches.h" | 21 #include "content/public/common/content_switches.h" |
27 #include "extensions/browser/guest_view/web_view/web_view_guest.h" | |
28 #include "grit/browser_resources.h" | 22 #include "grit/browser_resources.h" |
29 #if defined(OS_CHROMEOS) | |
30 #include "chrome/browser/chromeos/login/startup_utils.h" | |
31 #include "chrome/browser/ui/webui/chromeos/login/inline_login_handler_chromeos.h
" | |
32 #else | |
33 #include "chrome/browser/ui/webui/signin/inline_login_handler_impl.h" | 23 #include "chrome/browser/ui/webui/signin/inline_login_handler_impl.h" |
34 #endif | |
35 | 24 |
36 namespace { | 25 namespace { |
37 | 26 |
38 bool HandleTestFileRequestCallback( | |
39 const std::string& path, | |
40 const content::WebUIDataSource::GotDataCallback& callback) { | |
41 std::vector<std::string> url_substr = base::SplitString( | |
42 path, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
43 if (url_substr.size() != 2 || url_substr[0] != "test") | |
44 return false; | |
45 | |
46 std::string contents; | |
47 base::FilePath test_data_dir; | |
48 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); | |
49 if (!base::ReadFileToString( | |
50 test_data_dir.AppendASCII("webui").AppendASCII(url_substr[1]), | |
51 &contents, std::string::npos)) | |
52 return false; | |
53 | |
54 base::RefCountedString* ref_contents = new base::RefCountedString(); | |
55 ref_contents->data() = contents; | |
56 callback.Run(ref_contents); | |
57 return true; | |
58 } | |
59 | |
60 content::WebUIDataSource* CreateWebUIDataSource() { | 27 content::WebUIDataSource* CreateWebUIDataSource() { |
61 content::WebUIDataSource* source = | 28 content::WebUIDataSource* source = |
62 content::WebUIDataSource::Create(chrome::kChromeUIChromeSigninHost); | 29 content::WebUIDataSource::Create(chrome::kChromeUIChromeSigninHost); |
63 source->OverrideContentSecurityPolicyFrameSrc("frame-src chrome-extension:;"); | 30 source->OverrideContentSecurityPolicyFrameSrc("frame-src chrome-extension:;"); |
64 source->OverrideContentSecurityPolicyObjectSrc("object-src *;"); | 31 source->OverrideContentSecurityPolicyObjectSrc("object-src *;"); |
65 source->SetJsonPath("strings.js"); | 32 source->SetJsonPath("strings.js"); |
66 | 33 |
67 source->SetDefaultResource(IDR_NEW_INLINE_LOGIN_HTML); | 34 source->SetDefaultResource(IDR_NEW_INLINE_LOGIN_HTML); |
68 | 35 |
69 // Only add a filter when runing as test. | 36 // Only add a filter when runing as test. |
70 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 37 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
71 const bool is_running_test = command_line->HasSwitch(::switches::kTestName) || | 38 const bool is_running_test = command_line->HasSwitch(::switches::kTestName) || |
72 command_line->HasSwitch(::switches::kTestType); | 39 command_line->HasSwitch(::switches::kTestType); |
73 if (is_running_test) | 40 if (is_running_test) |
74 source->SetRequestFilter(base::Bind(&HandleTestFileRequestCallback)); | 41 source->SetRequestFilter(test::GetTestFilesRequestFilter()); |
75 | 42 |
76 source->AddResourcePath("inline_login.css", IDR_INLINE_LOGIN_CSS); | 43 source->AddResourcePath("inline_login.css", IDR_INLINE_LOGIN_CSS); |
77 source->AddResourcePath("inline_login.js", IDR_INLINE_LOGIN_JS); | 44 source->AddResourcePath("inline_login.js", IDR_INLINE_LOGIN_JS); |
78 source->AddResourcePath("gaia_auth_host.js", IDR_GAIA_AUTH_AUTHENTICATOR_JS); | 45 source->AddResourcePath("gaia_auth_host.js", IDR_GAIA_AUTH_AUTHENTICATOR_JS); |
79 | 46 |
80 source->AddLocalizedString("title", IDS_CHROME_SIGNIN_TITLE); | 47 source->AddLocalizedString("title", IDS_CHROME_SIGNIN_TITLE); |
81 return source; | 48 return source; |
82 } | 49 } |
83 | 50 |
84 bool AddWebContentsToSet(std::set<content::WebContents*>* frame_set, | |
85 const std::string& web_view_name, | |
86 content::WebContents* web_contents) { | |
87 auto* web_view = extensions::WebViewGuest::FromWebContents(web_contents); | |
88 if (web_view && web_view->name() == web_view_name) | |
89 frame_set->insert(web_contents); | |
90 return false; | |
91 } | |
92 | |
93 } // empty namespace | 51 } // empty namespace |
94 | 52 |
95 InlineLoginUI::InlineLoginUI(content::WebUI* web_ui) | 53 InlineLoginUI::InlineLoginUI(content::WebUI* web_ui) |
96 : WebDialogUI(web_ui), | 54 : WebDialogUI(web_ui), |
97 auth_extension_(Profile::FromWebUI(web_ui)) { | 55 auth_extension_(Profile::FromWebUI(web_ui)) { |
98 Profile* profile = Profile::FromWebUI(web_ui); | 56 Profile* profile = Profile::FromWebUI(web_ui); |
99 content::WebUIDataSource::Add(profile, CreateWebUIDataSource()); | 57 content::WebUIDataSource::Add(profile, CreateWebUIDataSource()); |
100 | |
101 #if defined(OS_CHROMEOS) | |
102 web_ui->AddMessageHandler(new chromeos::InlineLoginHandlerChromeOS()); | |
103 #else | |
104 web_ui->AddMessageHandler(new InlineLoginHandlerImpl()); | 58 web_ui->AddMessageHandler(new InlineLoginHandlerImpl()); |
105 #endif | |
106 web_ui->AddMessageHandler(new MetricsHandler()); | 59 web_ui->AddMessageHandler(new MetricsHandler()); |
107 | 60 |
108 content::WebContents* contents = web_ui->GetWebContents(); | 61 content::WebContents* contents = web_ui->GetWebContents(); |
109 // Required for intercepting extension function calls when the page is loaded | 62 // Required for intercepting extension function calls when the page is loaded |
110 // in a bubble (not a full tab, thus tab helpers are not registered | 63 // in a bubble (not a full tab, thus tab helpers are not registered |
111 // automatically). | 64 // automatically). |
112 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents( | 65 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents( |
113 contents); | 66 contents); |
114 extensions::TabHelper::CreateForWebContents(contents); | 67 extensions::TabHelper::CreateForWebContents(contents); |
115 // Ensure that the login UI has a tab ID, which will allow the GAIA auth | 68 // Ensure that the login UI has a tab ID, which will allow the GAIA auth |
116 // extension's background script to tell it apart from iframes injected by | 69 // extension's background script to tell it apart from iframes injected by |
117 // other extensions. | 70 // other extensions. |
118 SessionTabHelper::CreateForWebContents(contents); | 71 SessionTabHelper::CreateForWebContents(contents); |
119 } | 72 } |
120 | 73 |
121 InlineLoginUI::~InlineLoginUI() {} | 74 InlineLoginUI::~InlineLoginUI() {} |
122 | 75 |
123 // Gets the Gaia iframe within a WebContents. | |
124 content::RenderFrameHost* InlineLoginUI::GetAuthFrame( | |
125 content::WebContents* web_contents, | |
126 const GURL& parent_origin, | |
127 const std::string& parent_frame_name) { | |
128 content::WebContents* auth_web_contents = GetAuthFrameWebContents( | |
129 web_contents, parent_origin, parent_frame_name); | |
130 return auth_web_contents ? auth_web_contents->GetMainFrame() : nullptr; | |
131 } | |
132 | |
133 content::WebContents* InlineLoginUI::GetAuthFrameWebContents( | |
134 content::WebContents* web_contents, | |
135 const GURL& parent_origin, | |
136 const std::string& parent_frame_name) { | |
137 std::set<content::WebContents*> frame_set; | |
138 auto* manager = | |
139 guest_view::GuestViewManager::FromBrowserContext( | |
140 web_contents->GetBrowserContext()); | |
141 if (manager) { | |
142 manager->ForEachGuest( | |
143 web_contents, | |
144 base::Bind(&AddWebContentsToSet, &frame_set, parent_frame_name)); | |
145 } | |
146 DCHECK_GE(1U, frame_set.size()); | |
147 if (!frame_set.empty()) | |
148 return *frame_set.begin(); | |
149 | |
150 return nullptr; | |
151 } | |
OLD | NEW |