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/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/search/instant_io_context.h" | 12 #include "chrome/browser/search/instant_io_context.h" |
| 13 #include "chrome/browser/search/search.h" | 13 #include "chrome/browser/search/search.h" |
| 14 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 15 #include "grit/browser_resources.h" | 15 #include "grit/browser_resources.h" |
| 16 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 17 #include "grit/ui_resources.h" | 17 #include "grit/ui_resources.h" |
| 18 #include "net/url_request/url_request.h" | 18 #include "net/url_request/url_request.h" |
| 19 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
| 20 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 21 #include "ui/webui/jstemplate_builder.h" | 21 #include "ui/webui/jstemplate_builder.h" |
| 22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 // Signifies a locally constructed resource, i.e. not from grit/. | 26 // Signifies a locally constructed resource, i.e. not from grit/. |
| 27 const int kLocalResource = -1; | 27 const int kLocalResource = -1; |
| 28 | 28 |
| 29 const char kTranslatedStringsFilename[] = "translated-strings.js"; | 29 const char kLoadTimeDataFilename[] = "load-time-data.js"; |
| 30 | 30 |
| 31 const struct Resource{ | 31 const struct Resource{ |
| 32 const char* filename; | 32 const char* filename; |
| 33 int identifier; | 33 int identifier; |
| 34 const char* mime_type; | 34 const char* mime_type; |
| 35 } kResources[] = { | 35 } kResources[] = { |
| 36 { "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" }, | 36 { "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" }, |
| 37 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" }, | 37 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" }, |
| 38 { kTranslatedStringsFilename, kLocalResource, "application/javascript" }, | 38 { kLoadTimeDataFilename, kLocalResource, "application/javascript" }, |
| 39 { "local-ntp.css", IDR_LOCAL_NTP_CSS, "text/css" }, | 39 { "local-ntp.css", IDR_LOCAL_NTP_CSS, "text/css" }, |
| 40 { "images/close_2.png", IDR_CLOSE_2, "image/png" }, | 40 { "images/close_2.png", IDR_CLOSE_2, "image/png" }, |
| 41 { "images/close_2_hover.png", IDR_CLOSE_2_H, "image/png" }, | 41 { "images/close_2_hover.png", IDR_CLOSE_2_H, "image/png" }, |
| 42 { "images/close_2_active.png", IDR_CLOSE_2_P, "image/png" }, | 42 { "images/close_2_active.png", IDR_CLOSE_2_P, "image/png" }, |
| 43 { "images/close_2_white.png", IDR_CLOSE_2_MASK, "image/png" }, | 43 { "images/close_2_white.png", IDR_CLOSE_2_MASK, "image/png" }, |
| 44 { "images/page_icon.png", IDR_LOCAL_OMNIBOX_POPUP_IMAGES_PAGE_ICON_PNG, | 44 { "images/page_icon.png", IDR_LOCAL_OMNIBOX_POPUP_IMAGES_PAGE_ICON_PNG, |
| 45 "image/png" }, | 45 "image/png" }, |
| 46 { "images/2x/page_icon.png", | 46 { "images/2x/page_icon.png", |
| 47 IDR_LOCAL_OMNIBOX_POPUP_IMAGES_2X_PAGE_ICON_PNG, "image/png" }, | 47 IDR_LOCAL_OMNIBOX_POPUP_IMAGES_2X_PAGE_ICON_PNG, "image/png" }, |
| 48 { "images/search_icon.png", | 48 { "images/search_icon.png", |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 60 return path.substr(0, path.find("?")); | 60 return path.substr(0, path.find("?")); |
| 61 } | 61 } |
| 62 | 62 |
| 63 // Adds a localized string keyed by resource id to the dictionary. | 63 // Adds a localized string keyed by resource id to the dictionary. |
| 64 void AddString(base::DictionaryValue* dictionary, | 64 void AddString(base::DictionaryValue* dictionary, |
| 65 const std::string& key, | 65 const std::string& key, |
| 66 int resource_id) { | 66 int resource_id) { |
| 67 dictionary->SetString(key, l10n_util::GetStringUTF16(resource_id)); | 67 dictionary->SetString(key, l10n_util::GetStringUTF16(resource_id)); |
| 68 } | 68 } |
| 69 | 69 |
| 70 // Returns a JS dictionary of translated strings for the local NTP. | 70 // Returns a JS dictionary of load time data for the local NTP. |
| 71 std::string GetTranslatedStrings() { | 71 std::string GetLoadTimeData(Profile* profile) { |
| 72 base::DictionaryValue translated_strings; | 72 base::DictionaryValue load_time_data; |
| 73 if (chrome::ShouldShowRecentTabsOnNTP()) | 73 if (chrome::ShouldShowRecentTabsOnNTP()) |
| 74 AddString(&translated_strings, "recentTabs", IDS_RECENT_TABS_MENU); | 74 AddString(&load_time_data, "recentTabs", IDS_RECENT_TABS_MENU); |
| 75 AddString(&translated_strings, "thumbnailRemovedNotification", | 75 AddString(&load_time_data, "thumbnailRemovedNotification", |
| 76 IDS_NEW_TAB_THUMBNAIL_REMOVED_NOTIFICATION); | 76 IDS_NEW_TAB_THUMBNAIL_REMOVED_NOTIFICATION); |
| 77 AddString(&translated_strings, "removeThumbnailTooltip", | 77 AddString(&load_time_data, "removeThumbnailTooltip", |
| 78 IDS_NEW_TAB_REMOVE_THUMBNAIL_TOOLTIP); | 78 IDS_NEW_TAB_REMOVE_THUMBNAIL_TOOLTIP); |
| 79 AddString(&translated_strings, "undoThumbnailRemove", | 79 AddString(&load_time_data, "undoThumbnailRemove", |
| 80 IDS_NEW_TAB_UNDO_THUMBNAIL_REMOVE); | 80 IDS_NEW_TAB_UNDO_THUMBNAIL_REMOVE); |
| 81 AddString(&translated_strings, "restoreThumbnailsShort", | 81 AddString(&load_time_data, "restoreThumbnailsShort", |
| 82 IDS_NEW_TAB_RESTORE_THUMBNAILS_SHORT_LINK); | 82 IDS_NEW_TAB_RESTORE_THUMBNAILS_SHORT_LINK); |
| 83 AddString(&translated_strings, "attributionIntro", | 83 AddString(&load_time_data, "attributionIntro", |
| 84 IDS_NEW_TAB_ATTRIBUTION_INTRO); | 84 IDS_NEW_TAB_ATTRIBUTION_INTRO); |
| 85 AddString(&translated_strings, "title", IDS_NEW_TAB_TITLE); | 85 AddString(&load_time_data, "title", IDS_NEW_TAB_TITLE); |
| 86 std::string translated_strings_js; | 86 load_time_data.SetBoolean("isGooglePage", |
|
samarth
2013/07/12 17:16:06
Rather than mixing these fields, please create a c
kmadhusu
2013/07/15 22:21:51
Done.
| |
| 87 webui::AppendJsonJS(&translated_strings, &translated_strings_js); | 87 chrome::DefaultSearchProviderIsGoogle(profile)); |
| 88 return translated_strings_js; | 88 std::string load_time_data_js; |
| 89 webui::AppendJsonJS(&load_time_data, &load_time_data_js); | |
|
samarth
2013/07/12 17:16:06
AppendJsonJS [1] sets a variable called "templateD
kmadhusu
2013/07/15 22:21:51
Done.
| |
| 90 return load_time_data_js; | |
| 89 } | 91 } |
| 90 | 92 |
| 91 } // namespace | 93 } // namespace |
| 92 | 94 |
| 93 LocalNtpSource::LocalNtpSource() { | 95 LocalNtpSource::LocalNtpSource(Profile* profile) : profile_(profile) { |
| 94 } | 96 } |
| 95 | 97 |
| 96 LocalNtpSource::~LocalNtpSource() { | 98 LocalNtpSource::~LocalNtpSource() { |
| 97 } | 99 } |
| 98 | 100 |
| 99 std::string LocalNtpSource::GetSource() const { | 101 std::string LocalNtpSource::GetSource() const { |
| 100 return chrome::kChromeSearchLocalNtpHost; | 102 return chrome::kChromeSearchLocalNtpHost; |
| 101 } | 103 } |
| 102 | 104 |
| 103 void LocalNtpSource::StartDataRequest( | 105 void LocalNtpSource::StartDataRequest( |
| 104 const std::string& path, | 106 const std::string& path, |
| 105 int render_process_id, | 107 int render_process_id, |
| 106 int render_view_id, | 108 int render_view_id, |
| 107 const content::URLDataSource::GotDataCallback& callback) { | 109 const content::URLDataSource::GotDataCallback& callback) { |
| 108 const std::string stripped_path = StripParameters(path); | 110 const std::string stripped_path = StripParameters(path); |
| 109 if (stripped_path == kTranslatedStringsFilename) { | 111 if (stripped_path == kLoadTimeDataFilename) { |
| 110 std::string translated_strings_js = GetTranslatedStrings(); | 112 std::string load_time_data_js = GetLoadTimeData(profile_); |
| 111 callback.Run(base::RefCountedString::TakeString(&translated_strings_js)); | 113 callback.Run(base::RefCountedString::TakeString(&load_time_data_js)); |
| 112 return; | 114 return; |
| 113 } | 115 } |
| 114 for (size_t i = 0; i < arraysize(kResources); ++i) { | 116 for (size_t i = 0; i < arraysize(kResources); ++i) { |
| 115 if (stripped_path == kResources[i].filename) { | 117 if (stripped_path == kResources[i].filename) { |
| 116 scoped_refptr<base::RefCountedStaticMemory> response( | 118 scoped_refptr<base::RefCountedStaticMemory> response( |
| 117 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( | 119 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( |
| 118 kResources[i].identifier)); | 120 kResources[i].identifier)); |
| 119 callback.Run(response.get()); | 121 callback.Run(response.get()); |
| 120 return; | 122 return; |
| 121 } | 123 } |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 148 } | 150 } |
| 149 } | 151 } |
| 150 return false; | 152 return false; |
| 151 } | 153 } |
| 152 | 154 |
| 153 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const { | 155 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const { |
| 154 // Allow embedding of most visited iframes. | 156 // Allow embedding of most visited iframes. |
| 155 return base::StringPrintf("frame-src %s;", | 157 return base::StringPrintf("frame-src %s;", |
| 156 chrome::kChromeSearchMostVisitedUrl); | 158 chrome::kChromeSearchMostVisitedUrl); |
| 157 } | 159 } |
| OLD | NEW |