Chromium Code Reviews| 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/search/local_ntp_source.h" | 5 #include "chrome/browser/search/local_ntp_source.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ref_counted_memory.h" | 8 #include "base/memory/ref_counted_memory.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| 11 #include "base/values.h" | |
| 12 #include "chrome/browser/search/search.h" | |
| 11 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
| 12 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
| 13 #include "grit/browser_resources.h" | 15 #include "grit/browser_resources.h" |
| 16 #include "grit/generated_resources.h" | |
| 14 #include "grit/ui_resources.h" | 17 #include "grit/ui_resources.h" |
| 15 #include "net/url_request/url_request.h" | 18 #include "net/url_request/url_request.h" |
| 19 #include "ui/base/l10n/l10n_util.h" | |
| 16 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 21 #include "ui/webui/jstemplate_builder.h" | |
| 17 | 22 |
| 18 namespace { | 23 namespace { |
| 19 | 24 |
| 20 const struct Resource{ | 25 const struct Resource{ |
| 21 const char* filename; | 26 const char* filename; |
| 22 int identifier; | 27 int identifier; |
| 23 const char* mime_type; | 28 const char* mime_type; |
| 24 } kResources[] = { | 29 } kResources[] = { |
| 25 { "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" }, | 30 { "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" }, |
| 26 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" }, | 31 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" }, |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 43 IDR_LOCAL_NTP_IMAGES_WHITE_LOGO_PNG, "image/png" }, | 48 IDR_LOCAL_NTP_IMAGES_WHITE_LOGO_PNG, "image/png" }, |
| 44 { "images/2x/white_google_logo.png", | 49 { "images/2x/white_google_logo.png", |
| 45 IDR_LOCAL_NTP_IMAGES_2X_WHITE_LOGO_PNG, "image/png" }, | 50 IDR_LOCAL_NTP_IMAGES_2X_WHITE_LOGO_PNG, "image/png" }, |
| 46 }; | 51 }; |
| 47 | 52 |
| 48 // Strips any query parameters from the specified path. | 53 // Strips any query parameters from the specified path. |
| 49 std::string StripParameters(const std::string& path) { | 54 std::string StripParameters(const std::string& path) { |
| 50 return path.substr(0, path.find("?")); | 55 return path.substr(0, path.find("?")); |
| 51 } | 56 } |
| 52 | 57 |
| 58 // Adds a localized string keyed by resource id to the dictionary. | |
| 59 void AddString(base::DictionaryValue* dictionary, | |
| 60 const std::string& key, | |
| 61 int resource_id) { | |
| 62 dictionary->SetString(key, l10n_util::GetStringUTF16(resource_id)); | |
| 63 } | |
| 64 | |
| 53 } // namespace | 65 } // namespace |
| 54 | 66 |
| 55 LocalNtpSource::LocalNtpSource() { | 67 LocalNtpSource::LocalNtpSource() { |
| 56 } | 68 } |
| 57 | 69 |
| 58 LocalNtpSource::~LocalNtpSource() { | 70 LocalNtpSource::~LocalNtpSource() { |
| 59 } | 71 } |
| 60 | 72 |
| 61 std::string LocalNtpSource::GetSource() const { | 73 std::string LocalNtpSource::GetSource() const { |
| 62 return chrome::kChromeSearchLocalNtpHost; | 74 return chrome::kChromeSearchLocalNtpHost; |
| 63 } | 75 } |
| 64 | 76 |
| 65 void LocalNtpSource::StartDataRequest( | 77 void LocalNtpSource::StartDataRequest( |
| 66 const std::string& path, | 78 const std::string& path, |
| 67 int render_process_id, | 79 int render_process_id, |
| 68 int render_view_id, | 80 int render_view_id, |
| 69 const content::URLDataSource::GotDataCallback& callback) { | 81 const content::URLDataSource::GotDataCallback& callback) { |
| 70 const std::string stripped_path = StripParameters(path); | 82 const std::string stripped_path = StripParameters(path); |
| 71 for (size_t i = 0; i < arraysize(kResources); ++i) { | 83 for (size_t i = 0; i < arraysize(kResources); ++i) { |
| 72 if (stripped_path == kResources[i].filename) { | 84 if (stripped_path == kResources[i].filename) { |
| 73 scoped_refptr<base::RefCountedStaticMemory> response( | 85 if (strcmp(kResources[i].mime_type, "text/html") == 0) { |
|
samarth
2013/05/03 04:52:45
Check the IDR or filename instead of relying on a
samarth
2013/05/03 04:52:45
Some sort of test for the new code could be nice.
jeremycho
2013/05/03 06:30:29
Done.
jeremycho
2013/05/03 06:30:29
Will work on this.
| |
| 74 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( | 86 base::DictionaryValue translated_strings; |
| 75 kResources[i].identifier)); | 87 if (chrome::IsRecentlyClosedNTPLinkEnabled()) { |
| 76 callback.Run(response); | 88 AddString(&translated_strings, "recentlyClosed", |
| 89 IDS_NEW_TAB_RECENTLY_CLOSED); | |
| 90 } | |
| 91 static const base::StringPiece html(ResourceBundle::GetSharedInstance() | |
|
samarth
2013/05/03 04:52:45
Does this (static stringpiece) work? Is the memor
jeremycho
2013/05/03 06:30:29
This idiom is used in other StartDataRequest funct
| |
| 92 .GetRawDataResource(kResources[i].identifier)); | |
| 93 std::string i18n_html = | |
| 94 webui::GetI18nTemplateHtml(html, &translated_strings); | |
| 95 callback.Run(base::RefCountedString::TakeString(&i18n_html)); | |
| 96 } else { | |
| 97 scoped_refptr<base::RefCountedStaticMemory> response( | |
| 98 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( | |
| 99 kResources[i].identifier)); | |
| 100 callback.Run(response); | |
| 101 } | |
| 77 return; | 102 return; |
| 78 } | 103 } |
| 79 } | 104 } |
| 80 callback.Run(NULL); | 105 callback.Run(NULL); |
| 81 }; | 106 }; |
| 82 | 107 |
| 83 std::string LocalNtpSource::GetMimeType( | 108 std::string LocalNtpSource::GetMimeType( |
| 84 const std::string& path) const { | 109 const std::string& path) const { |
| 85 const std::string stripped_path = StripParameters(path); | 110 const std::string stripped_path = StripParameters(path); |
| 86 for (size_t i = 0; i < arraysize(kResources); ++i) { | 111 for (size_t i = 0; i < arraysize(kResources); ++i) { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 98 DCHECK(StartsWithASCII(request->url().path(), "/", true)); | 123 DCHECK(StartsWithASCII(request->url().path(), "/", true)); |
| 99 std::string filename = request->url().path().substr(1); | 124 std::string filename = request->url().path().substr(1); |
| 100 for (size_t i = 0; i < arraysize(kResources); ++i) { | 125 for (size_t i = 0; i < arraysize(kResources); ++i) { |
| 101 if (filename == kResources[i].filename) | 126 if (filename == kResources[i].filename) |
| 102 return true; | 127 return true; |
| 103 } | 128 } |
| 104 } | 129 } |
| 105 return false; | 130 return false; |
| 106 } | 131 } |
| 107 | 132 |
| 133 bool LocalNtpSource::ShouldAddContentSecurityPolicy() const { | |
|
samarth
2013/05/03 04:52:45
Umm, this is almost certainly a bad idea. I assum
jeremycho
2013/05/03 06:30:29
This is needed for webui::GetI18nTemplateHtml, whi
| |
| 134 return false; | |
| 135 } | |
| 136 | |
| 108 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const { | 137 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const { |
| 109 // Allow embedding of chrome search suggestion host. | 138 // Allow embedding of chrome search suggestion host. |
| 110 return base::StringPrintf("frame-src %s://%s/;", | 139 return base::StringPrintf("frame-src %s://%s/;", |
| 111 chrome::kChromeSearchScheme, | 140 chrome::kChromeSearchScheme, |
| 112 chrome::kChromeSearchSuggestionHost); | 141 chrome::kChromeSearchSuggestionHost); |
| 113 } | 142 } |
| OLD | NEW |