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