| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/ntp/new_tab_ui.h" | 5 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
| 22 #include "components/bookmarks/common/bookmark_pref_names.h" | 22 #include "components/bookmarks/common/bookmark_pref_names.h" |
| 23 #include "components/prefs/pref_service.h" | 23 #include "components/prefs/pref_service.h" |
| 24 #include "components/strings/grit/components_strings.h" | 24 #include "components/strings/grit/components_strings.h" |
| 25 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 26 #include "content/public/browser/render_process_host.h" | 26 #include "content/public/browser/render_process_host.h" |
| 27 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 28 #include "content/public/browser/web_ui.h" | 28 #include "content/public/browser/web_ui.h" |
| 29 #include "extensions/browser/extension_system.h" | 29 #include "extensions/browser/extension_system.h" |
| 30 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
| 31 #include "ui/base/resource/resource_bundle.h" | |
| 32 #include "url/gurl.h" | 31 #include "url/gurl.h" |
| 33 | 32 |
| 34 using content::BrowserThread; | 33 using content::BrowserThread; |
| 35 using content::WebUIController; | 34 using content::WebUIController; |
| 36 | 35 |
| 37 namespace { | 36 namespace { |
| 38 | 37 |
| 39 // Strings sent to the page via jstemplates used to set the direction of the | 38 // Strings sent to the page via jstemplates used to set the direction of the |
| 40 // HTML document based on locale. | 39 // HTML document based on locale. |
| 41 const char kRTLHtmlTextDirection[] = "rtl"; | 40 const char kRTLHtmlTextDirection[] = "rtl"; |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 std::string NewTabUI::NewTabHTMLSource::GetSource() const { | 173 std::string NewTabUI::NewTabHTMLSource::GetSource() const { |
| 175 return chrome::kChromeUINewTabHost; | 174 return chrome::kChromeUINewTabHost; |
| 176 } | 175 } |
| 177 | 176 |
| 178 void NewTabUI::NewTabHTMLSource::StartDataRequest( | 177 void NewTabUI::NewTabHTMLSource::StartDataRequest( |
| 179 const std::string& path, | 178 const std::string& path, |
| 180 const content::ResourceRequestInfo::WebContentsGetter& wc_getter, | 179 const content::ResourceRequestInfo::WebContentsGetter& wc_getter, |
| 181 const content::URLDataSource::GotDataCallback& callback) { | 180 const content::URLDataSource::GotDataCallback& callback) { |
| 182 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 181 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 183 | 182 |
| 184 std::map<std::string, std::pair<std::string, int> >::iterator it = | |
| 185 resource_map_.find(path); | |
| 186 if (it != resource_map_.end()) { | |
| 187 scoped_refptr<base::RefCountedMemory> resource_bytes( | |
| 188 it->second.second ? | |
| 189 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( | |
| 190 it->second.second) : | |
| 191 new base::RefCountedStaticMemory); | |
| 192 callback.Run(resource_bytes.get()); | |
| 193 return; | |
| 194 } | |
| 195 | |
| 196 if (!path.empty() && path[0] != '#') { | 183 if (!path.empty() && path[0] != '#') { |
| 197 // A path under new-tab was requested; it's likely a bad relative | 184 // A path under new-tab was requested; it's likely a bad relative |
| 198 // URL from the new tab page, but in any case it's an error. | 185 // URL from the new tab page, but in any case it's an error. |
| 199 NOTREACHED() << path << " should not have been requested on the NTP"; | 186 NOTREACHED() << path << " should not have been requested on the NTP"; |
| 200 callback.Run(NULL); | 187 callback.Run(NULL); |
| 201 return; | 188 return; |
| 202 } | 189 } |
| 203 | 190 |
| 204 content::WebContents* web_contents = wc_getter.Run(); | 191 content::WebContents* web_contents = wc_getter.Run(); |
| 205 content::RenderProcessHost* render_host = | 192 content::RenderProcessHost* render_host = |
| 206 web_contents ? web_contents->GetRenderProcessHost() : nullptr; | 193 web_contents ? web_contents->GetRenderProcessHost() : nullptr; |
| 207 NTPResourceCache::WindowType win_type = NTPResourceCache::GetWindowType( | 194 NTPResourceCache::WindowType win_type = NTPResourceCache::GetWindowType( |
| 208 profile_, render_host); | 195 profile_, render_host); |
| 209 scoped_refptr<base::RefCountedMemory> html_bytes( | 196 scoped_refptr<base::RefCountedMemory> html_bytes( |
| 210 NTPResourceCacheFactory::GetForProfile(profile_)-> | 197 NTPResourceCacheFactory::GetForProfile(profile_)-> |
| 211 GetNewTabHTML(win_type)); | 198 GetNewTabHTML(win_type)); |
| 212 | 199 |
| 213 callback.Run(html_bytes.get()); | 200 callback.Run(html_bytes.get()); |
| 214 } | 201 } |
| 215 | 202 |
| 216 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string& resource) | 203 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string& resource) |
| 217 const { | 204 const { |
| 218 std::map<std::string, std::pair<std::string, int> >::const_iterator it = | |
| 219 resource_map_.find(resource); | |
| 220 if (it != resource_map_.end()) | |
| 221 return it->second.first; | |
| 222 return "text/html"; | 205 return "text/html"; |
| 223 } | 206 } |
| 224 | 207 |
| 225 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const { | 208 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const { |
| 226 return false; | 209 return false; |
| 227 } | 210 } |
| 228 | 211 |
| 229 std::string NewTabUI::NewTabHTMLSource::GetContentSecurityPolicyScriptSrc() | 212 std::string NewTabUI::NewTabHTMLSource::GetContentSecurityPolicyScriptSrc() |
| 230 const { | 213 const { |
| 231 // 'unsafe-inline' and google resources are added to script-src. | 214 // 'unsafe-inline' and google resources are added to script-src. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 242 const { | 225 const { |
| 243 return "img-src chrome-search://thumb chrome-search://thumb2 " | 226 return "img-src chrome-search://thumb chrome-search://thumb2 " |
| 244 "chrome-search://theme chrome://theme data:;"; | 227 "chrome-search://theme chrome://theme data:;"; |
| 245 } | 228 } |
| 246 | 229 |
| 247 std::string NewTabUI::NewTabHTMLSource::GetContentSecurityPolicyChildSrc() | 230 std::string NewTabUI::NewTabHTMLSource::GetContentSecurityPolicyChildSrc() |
| 248 const { | 231 const { |
| 249 return "child-src chrome-search://most-visited;"; | 232 return "child-src chrome-search://most-visited;"; |
| 250 } | 233 } |
| 251 | 234 |
| 252 void NewTabUI::NewTabHTMLSource::AddResource(const char* resource, | |
| 253 const char* mime_type, | |
| 254 int resource_id) { | |
| 255 DCHECK(resource); | |
| 256 DCHECK(mime_type); | |
| 257 resource_map_[std::string(resource)] = | |
| 258 std::make_pair(std::string(mime_type), resource_id); | |
| 259 } | |
| 260 | |
| 261 NewTabUI::NewTabHTMLSource::~NewTabHTMLSource() {} | 235 NewTabUI::NewTabHTMLSource::~NewTabHTMLSource() {} |
| OLD | NEW |