| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/uber/uber_ui.h" | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "base/stl_util.h" | |
| 9 #include "build/build_config.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" | |
| 12 #include "chrome/browser/ui/webui/extensions/extensions_ui.h" | |
| 13 #include "chrome/browser/ui/webui/log_web_ui_url.h" | |
| 14 #include "chrome/common/chrome_features.h" | |
| 15 #include "chrome/common/chrome_switches.h" | |
| 16 #include "chrome/common/extensions/chrome_manifest_url_handlers.h" | |
| 17 #include "chrome/common/url_constants.h" | |
| 18 #include "chrome/grit/browser_resources.h" | |
| 19 #include "chrome/grit/chromium_strings.h" | |
| 20 #include "chrome/grit/generated_resources.h" | |
| 21 #include "components/strings/grit/components_strings.h" | |
| 22 #include "content/public/browser/browser_context.h" | |
| 23 #include "content/public/browser/navigation_controller.h" | |
| 24 #include "content/public/browser/navigation_entry.h" | |
| 25 #include "content/public/browser/navigation_handle.h" | |
| 26 #include "content/public/browser/notification_source.h" | |
| 27 #include "content/public/browser/web_contents.h" | |
| 28 #include "content/public/browser/web_ui.h" | |
| 29 #include "content/public/browser/web_ui_data_source.h" | |
| 30 #include "content/public/common/browser_side_navigation_policy.h" | |
| 31 | |
| 32 #if defined(OS_CHROMEOS) | |
| 33 #include "chrome/browser/ui/webui/options/options_ui.h" | |
| 34 #endif | |
| 35 | |
| 36 using content::NavigationController; | |
| 37 using content::NavigationEntry; | |
| 38 using content::RenderFrameHost; | |
| 39 using content::WebContents; | |
| 40 | |
| 41 namespace { | |
| 42 | |
| 43 content::WebUIDataSource* CreateUberHTMLSource() { | |
| 44 content::WebUIDataSource* source = | |
| 45 content::WebUIDataSource::Create(chrome::kChromeUIUberHost); | |
| 46 | |
| 47 source->SetJsonPath("strings.js"); | |
| 48 source->AddResourcePath("uber.js", IDR_UBER_JS); | |
| 49 source->AddResourcePath("uber_utils.js", IDR_UBER_UTILS_JS); | |
| 50 source->SetDefaultResource(IDR_UBER_HTML); | |
| 51 source->OverrideContentSecurityPolicyChildSrc("child-src chrome:;"); | |
| 52 | |
| 53 // Hack alert: continue showing "Loading..." until a real title is set. | |
| 54 source->AddLocalizedString("pageTitle", IDS_TAB_LOADING_TITLE); | |
| 55 | |
| 56 source->AddString("extensionsFrameURL", chrome::kChromeUIExtensionsFrameURL); | |
| 57 source->AddString("extensionsHost", chrome::kChromeUIExtensionsHost); | |
| 58 source->AddString("helpFrameURL", chrome::kChromeUIHelpFrameURL); | |
| 59 source->AddString("helpHost", chrome::kChromeUIHelpHost); | |
| 60 source->AddString("settingsFrameURL", chrome::kChromeUISettingsFrameURL); | |
| 61 source->AddString("settingsHost", chrome::kChromeUISettingsHost); | |
| 62 | |
| 63 return source; | |
| 64 } | |
| 65 | |
| 66 content::WebUIDataSource* CreateUberFrameHTMLSource( | |
| 67 content::BrowserContext* browser_context) { | |
| 68 content::WebUIDataSource* source = | |
| 69 content::WebUIDataSource::Create(chrome::kChromeUIUberFrameHost); | |
| 70 Profile* profile = Profile::FromBrowserContext(browser_context); | |
| 71 | |
| 72 source->SetJsonPath("strings.js"); | |
| 73 source->AddResourcePath("uber_frame.js", IDR_UBER_FRAME_JS); | |
| 74 source->SetDefaultResource(IDR_UBER_FRAME_HTML); | |
| 75 | |
| 76 // TODO(jhawkins): Attempt to get rid of IDS_SHORT_PRODUCT_OS_NAME. | |
| 77 #if defined(OS_CHROMEOS) | |
| 78 source->AddLocalizedString("shortProductName", IDS_SHORT_PRODUCT_OS_NAME); | |
| 79 #else | |
| 80 source->AddLocalizedString("shortProductName", IDS_SHORT_PRODUCT_NAME); | |
| 81 #endif // defined(OS_CHROMEOS) | |
| 82 | |
| 83 source->AddBoolean("hideExtensions", | |
| 84 base::FeatureList::IsEnabled(features::kMaterialDesignExtensions)); | |
| 85 // TODO(dbeam): remove hideSettingsAndHelp soon. | |
| 86 source->AddBoolean("hideSettingsAndHelp", true); | |
| 87 source->AddString("extensionsHost", chrome::kChromeUIExtensionsHost); | |
| 88 source->AddLocalizedString("extensionsDisplayName", | |
| 89 IDS_MANAGE_EXTENSIONS_SETTING_WINDOWS_TITLE); | |
| 90 source->AddString("helpHost", chrome::kChromeUIHelpHost); | |
| 91 source->AddLocalizedString("helpDisplayName", IDS_ABOUT_TITLE); | |
| 92 source->AddString("settingsHost", chrome::kChromeUISettingsHost); | |
| 93 source->AddLocalizedString("settingsDisplayName", IDS_SETTINGS_TITLE); | |
| 94 | |
| 95 source->DisableDenyXFrameOptions(); | |
| 96 source->OverrideContentSecurityPolicyChildSrc("child-src chrome:;"); | |
| 97 | |
| 98 source->AddBoolean("profileIsGuest", profile->IsGuestSession()); | |
| 99 | |
| 100 return source; | |
| 101 } | |
| 102 | |
| 103 } // namespace | |
| 104 | |
| 105 SubframeLogger::SubframeLogger(content::WebContents* contents) | |
| 106 : WebContentsObserver(contents) {} | |
| 107 | |
| 108 SubframeLogger::~SubframeLogger() {} | |
| 109 | |
| 110 void SubframeLogger::DidFinishNavigation( | |
| 111 content::NavigationHandle* navigation_handle) { | |
| 112 if (!navigation_handle->HasCommitted()) | |
| 113 return; | |
| 114 | |
| 115 const GURL& url = navigation_handle->GetURL(); | |
| 116 if (url == chrome::kChromeUIExtensionsFrameURL || | |
| 117 url == chrome::kChromeUIHelpFrameURL || | |
| 118 url == chrome::kChromeUISettingsFrameURL || | |
| 119 url == chrome::kChromeUIUberFrameURL) { | |
| 120 webui::LogWebUIUrl(url); | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 UberUI::UberUI(content::WebUI* web_ui) : WebUIController(web_ui) { | |
| 125 subframe_logger_ = base::MakeUnique<SubframeLogger>(web_ui->GetWebContents()); | |
| 126 content::WebUIDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(), | |
| 127 CreateUberHTMLSource()); | |
| 128 | |
| 129 RegisterSubpage(chrome::kChromeUIExtensionsFrameURL, | |
| 130 chrome::kChromeUIExtensionsHost); | |
| 131 RegisterSubpage(chrome::kChromeUIHelpFrameURL, | |
| 132 chrome::kChromeUIHelpHost); | |
| 133 #if defined(OS_CHROMEOS) | |
| 134 RegisterSubpage(chrome::kChromeUISettingsFrameURL, | |
| 135 chrome::kChromeUISettingsHost); | |
| 136 #endif | |
| 137 RegisterSubpage(chrome::kChromeUIUberFrameURL, | |
| 138 chrome::kChromeUIUberHost); | |
| 139 } | |
| 140 | |
| 141 UberUI::~UberUI() { | |
| 142 } | |
| 143 | |
| 144 void UberUI::RegisterSubpage(const std::string& page_url, | |
| 145 const std::string& page_host) { | |
| 146 sub_uis_[page_url] = web_ui()->GetWebContents()->CreateSubframeWebUI( | |
| 147 GURL(page_url), page_host); | |
| 148 } | |
| 149 | |
| 150 content::WebUI* UberUI::GetSubpage(const std::string& page_url) { | |
| 151 if (!base::ContainsKey(sub_uis_, page_url)) | |
| 152 return nullptr; | |
| 153 return sub_uis_[page_url].get(); | |
| 154 } | |
| 155 | |
| 156 void UberUI::RenderFrameCreated(RenderFrameHost* render_frame_host) { | |
| 157 for (auto iter = sub_uis_.begin(); iter != sub_uis_.end(); ++iter) { | |
| 158 iter->second->GetController()->RenderFrameCreated(render_frame_host); | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 bool UberUI::OverrideHandleWebUIMessage(const GURL& source_url, | |
| 163 const std::string& message, | |
| 164 const base::ListValue& args) { | |
| 165 // Find the appropriate subpage and forward the message. | |
| 166 auto subpage = sub_uis_.find(source_url.GetOrigin().spec()); | |
| 167 if (subpage == sub_uis_.end()) { | |
| 168 // The message was sent from the uber page itself. | |
| 169 DCHECK_EQ(std::string(chrome::kChromeUIUberHost), source_url.host()); | |
| 170 return false; | |
| 171 } | |
| 172 | |
| 173 // The message was sent from a subpage. | |
| 174 // TODO(jam) fix this to use interface | |
| 175 // return subpage->second->GetController()->OverrideHandleWebUIMessage( | |
| 176 // source_url, message, args); | |
| 177 subpage->second->ProcessWebUIMessage(source_url, message, args); | |
| 178 return true; | |
| 179 } | |
| 180 | |
| 181 // UberFrameUI | |
| 182 | |
| 183 UberFrameUI::UberFrameUI(content::WebUI* web_ui) : WebUIController(web_ui) { | |
| 184 content::BrowserContext* browser_context = | |
| 185 web_ui->GetWebContents()->GetBrowserContext(); | |
| 186 content::WebUIDataSource::Add(browser_context, | |
| 187 CreateUberFrameHTMLSource(browser_context)); | |
| 188 } | |
| 189 | |
| 190 UberFrameUI::~UberFrameUI() { | |
| 191 } | |
| OLD | NEW |