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/json/json_string_value_serializer.h" | |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
| 9 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 10 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "chrome/browser/search/instant_io_context.h" | 13 #include "chrome/browser/search/instant_io_context.h" |
| 13 #include "chrome/browser/search/search.h" | 14 #include "chrome/browser/search/search.h" |
| 14 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
| 15 #include "grit/browser_resources.h" | 16 #include "grit/browser_resources.h" |
| 16 #include "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
| 17 #include "grit/ui_resources.h" | 18 #include "grit/ui_resources.h" |
| 18 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
| 19 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
| 20 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
| 21 #include "ui/webui/jstemplate_builder.h" | 22 #include "ui/webui/jstemplate_builder.h" |
| 22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 // Signifies a locally constructed resource, i.e. not from grit/. | 27 // Signifies a locally constructed resource, i.e. not from grit/. |
| 27 const int kLocalResource = -1; | 28 const int kLocalResource = -1; |
| 28 | 29 |
| 29 const char kTranslatedStringsFilename[] = "translated-strings.js"; | 30 const char kConfigDataFilename[] = "config.js"; |
| 30 | 31 |
| 31 const struct Resource{ | 32 const struct Resource{ |
| 32 const char* filename; | 33 const char* filename; |
| 33 int identifier; | 34 int identifier; |
| 34 const char* mime_type; | 35 const char* mime_type; |
| 35 } kResources[] = { | 36 } kResources[] = { |
| 36 { "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" }, | 37 { "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" }, |
| 37 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" }, | 38 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" }, |
| 38 { kTranslatedStringsFilename, kLocalResource, "application/javascript" }, | 39 { kConfigDataFilename, kLocalResource, "application/javascript" }, |
| 39 { "local-ntp.css", IDR_LOCAL_NTP_CSS, "text/css" }, | 40 { "local-ntp.css", IDR_LOCAL_NTP_CSS, "text/css" }, |
| 40 { "images/close_2.png", IDR_CLOSE_2, "image/png" }, | 41 { "images/close_2.png", IDR_CLOSE_2, "image/png" }, |
| 41 { "images/close_2_hover.png", IDR_CLOSE_2_H, "image/png" }, | 42 { "images/close_2_hover.png", IDR_CLOSE_2_H, "image/png" }, |
| 42 { "images/close_2_active.png", IDR_CLOSE_2_P, "image/png" }, | 43 { "images/close_2_active.png", IDR_CLOSE_2_P, "image/png" }, |
| 43 { "images/close_2_white.png", IDR_CLOSE_2_MASK, "image/png" }, | 44 { "images/close_2_white.png", IDR_CLOSE_2_MASK, "image/png" }, |
| 44 { "images/page_icon.png", IDR_LOCAL_OMNIBOX_POPUP_IMAGES_PAGE_ICON_PNG, | 45 { "images/page_icon.png", IDR_LOCAL_OMNIBOX_POPUP_IMAGES_PAGE_ICON_PNG, |
| 45 "image/png" }, | 46 "image/png" }, |
| 46 { "images/2x/page_icon.png", | 47 { "images/2x/page_icon.png", |
| 47 IDR_LOCAL_OMNIBOX_POPUP_IMAGES_2X_PAGE_ICON_PNG, "image/png" }, | 48 IDR_LOCAL_OMNIBOX_POPUP_IMAGES_2X_PAGE_ICON_PNG, "image/png" }, |
| 48 { "images/search_icon.png", | 49 { "images/search_icon.png", |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 60 return path.substr(0, path.find("?")); | 61 return path.substr(0, path.find("?")); |
| 61 } | 62 } |
| 62 | 63 |
| 63 // Adds a localized string keyed by resource id to the dictionary. | 64 // Adds a localized string keyed by resource id to the dictionary. |
| 64 void AddString(base::DictionaryValue* dictionary, | 65 void AddString(base::DictionaryValue* dictionary, |
| 65 const std::string& key, | 66 const std::string& key, |
| 66 int resource_id) { | 67 int resource_id) { |
| 67 dictionary->SetString(key, l10n_util::GetStringUTF16(resource_id)); | 68 dictionary->SetString(key, l10n_util::GetStringUTF16(resource_id)); |
| 68 } | 69 } |
| 69 | 70 |
| 70 // Returns a JS dictionary of translated strings for the local NTP. | 71 // Populates |translated_strings| dictionary for the local NTP. |
| 71 std::string GetTranslatedStrings() { | 72 void GetTranslatedStrings(base::DictionaryValue* translated_strings) { |
| 72 base::DictionaryValue translated_strings; | |
| 73 if (chrome::ShouldShowRecentTabsOnNTP()) | 73 if (chrome::ShouldShowRecentTabsOnNTP()) |
| 74 AddString(&translated_strings, "recentTabs", IDS_RECENT_TABS_MENU); | 74 AddString(translated_strings, "recentTabs", IDS_RECENT_TABS_MENU); |
| 75 AddString(&translated_strings, "thumbnailRemovedNotification", | 75 |
| 76 AddString(translated_strings, "thumbnailRemovedNotification", | |
| 76 IDS_NEW_TAB_THUMBNAIL_REMOVED_NOTIFICATION); | 77 IDS_NEW_TAB_THUMBNAIL_REMOVED_NOTIFICATION); |
| 77 AddString(&translated_strings, "removeThumbnailTooltip", | 78 AddString(translated_strings, "removeThumbnailTooltip", |
| 78 IDS_NEW_TAB_REMOVE_THUMBNAIL_TOOLTIP); | 79 IDS_NEW_TAB_REMOVE_THUMBNAIL_TOOLTIP); |
| 79 AddString(&translated_strings, "undoThumbnailRemove", | 80 AddString(translated_strings, "undoThumbnailRemove", |
| 80 IDS_NEW_TAB_UNDO_THUMBNAIL_REMOVE); | 81 IDS_NEW_TAB_UNDO_THUMBNAIL_REMOVE); |
| 81 AddString(&translated_strings, "restoreThumbnailsShort", | 82 AddString(translated_strings, "restoreThumbnailsShort", |
| 82 IDS_NEW_TAB_RESTORE_THUMBNAILS_SHORT_LINK); | 83 IDS_NEW_TAB_RESTORE_THUMBNAILS_SHORT_LINK); |
| 83 AddString(&translated_strings, "attributionIntro", | 84 AddString(translated_strings, "attributionIntro", |
| 84 IDS_NEW_TAB_ATTRIBUTION_INTRO); | 85 IDS_NEW_TAB_ATTRIBUTION_INTRO); |
| 85 AddString(&translated_strings, "title", IDS_NEW_TAB_TITLE); | 86 AddString(translated_strings, "title", IDS_NEW_TAB_TITLE); |
| 86 std::string translated_strings_js; | 87 } |
| 87 webui::AppendJsonJS(&translated_strings, &translated_strings_js); | 88 |
| 88 return translated_strings_js; | 89 // Returns a JS dictionary of configuration data for the local NTP. |
| 90 std::string GetConfigData(Profile* profile) { | |
| 91 base::DictionaryValue* translated_strings = new base::DictionaryValue(); | |
|
samarth
2013/07/17 16:39:45
Use a scoped_ptr here and then use .release() when
kmadhusu
2013/07/17 17:58:03
Done.
| |
| 92 GetTranslatedStrings(translated_strings); | |
| 93 | |
| 94 // Configuration data. | |
| 95 base::DictionaryValue config_data; | |
| 96 config_data.Set("translatedStrings", translated_strings); | |
| 97 config_data.SetBoolean("isGooglePage", | |
| 98 chrome::DefaultSearchProviderIsGoogle(profile)); | |
| 99 | |
| 100 // Serialize the dictionary. | |
| 101 std::string js_text; | |
| 102 JSONStringValueSerializer serializer(&js_text); | |
| 103 serializer.Serialize(config_data); | |
| 104 | |
| 105 std::string config_data_js; | |
| 106 config_data_js.append("var configData = "); | |
| 107 config_data_js.append(js_text); | |
| 108 config_data_js.append(";"); | |
| 109 return config_data_js; | |
| 89 } | 110 } |
| 90 | 111 |
| 91 } // namespace | 112 } // namespace |
| 92 | 113 |
| 93 LocalNtpSource::LocalNtpSource() { | 114 LocalNtpSource::LocalNtpSource(Profile* profile) : profile_(profile) { |
| 94 } | 115 } |
| 95 | 116 |
| 96 LocalNtpSource::~LocalNtpSource() { | 117 LocalNtpSource::~LocalNtpSource() { |
| 97 } | 118 } |
| 98 | 119 |
| 99 std::string LocalNtpSource::GetSource() const { | 120 std::string LocalNtpSource::GetSource() const { |
| 100 return chrome::kChromeSearchLocalNtpHost; | 121 return chrome::kChromeSearchLocalNtpHost; |
| 101 } | 122 } |
| 102 | 123 |
| 103 void LocalNtpSource::StartDataRequest( | 124 void LocalNtpSource::StartDataRequest( |
| 104 const std::string& path, | 125 const std::string& path, |
| 105 int render_process_id, | 126 int render_process_id, |
| 106 int render_view_id, | 127 int render_view_id, |
| 107 const content::URLDataSource::GotDataCallback& callback) { | 128 const content::URLDataSource::GotDataCallback& callback) { |
| 108 const std::string stripped_path = StripParameters(path); | 129 const std::string stripped_path = StripParameters(path); |
| 109 if (stripped_path == kTranslatedStringsFilename) { | 130 if (stripped_path == kConfigDataFilename) { |
| 110 std::string translated_strings_js = GetTranslatedStrings(); | 131 std::string config_data_js = GetConfigData(profile_); |
| 111 callback.Run(base::RefCountedString::TakeString(&translated_strings_js)); | 132 callback.Run(base::RefCountedString::TakeString(&config_data_js)); |
| 112 return; | 133 return; |
| 113 } | 134 } |
| 114 for (size_t i = 0; i < arraysize(kResources); ++i) { | 135 for (size_t i = 0; i < arraysize(kResources); ++i) { |
| 115 if (stripped_path == kResources[i].filename) { | 136 if (stripped_path == kResources[i].filename) { |
| 116 scoped_refptr<base::RefCountedStaticMemory> response( | 137 scoped_refptr<base::RefCountedStaticMemory> response( |
| 117 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( | 138 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( |
| 118 kResources[i].identifier)); | 139 kResources[i].identifier)); |
| 119 callback.Run(response.get()); | 140 callback.Run(response.get()); |
| 120 return; | 141 return; |
| 121 } | 142 } |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 148 } | 169 } |
| 149 } | 170 } |
| 150 return false; | 171 return false; |
| 151 } | 172 } |
| 152 | 173 |
| 153 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const { | 174 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const { |
| 154 // Allow embedding of most visited iframes. | 175 // Allow embedding of most visited iframes. |
| 155 return base::StringPrintf("frame-src %s;", | 176 return base::StringPrintf("frame-src %s;", |
| 156 chrome::kChromeSearchMostVisitedUrl); | 177 chrome::kChromeSearchMostVisitedUrl); |
| 157 } | 178 } |
| OLD | NEW |