| 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 "chrome/common/url_constants.h" | 10 #include "chrome/common/url_constants.h" |
| 11 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
| 12 #include "grit/browser_resources.h" | 12 #include "grit/browser_resources.h" |
| 13 #include "grit/ui_resources.h" | 13 #include "grit/ui_resources.h" |
| 14 #include "net/url_request/url_request.h" | 14 #include "net/url_request/url_request.h" |
| 15 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 const char kHtmlFilename[] = "local-ntp.html"; | 19 const struct Resource{ |
| 20 const char kJSFilename[] = "local-ntp.js"; | 20 const char* filename; |
| 21 const char kCssFilename[] = "local-ntp.css"; | 21 int identifier; |
| 22 const char kCloseBarFilename[] = "images/close_2.png"; | 22 const char* mime_type; |
| 23 const char kCloseBarHoverFilename[] = "images/close_2_hover.png"; | 23 } kResources[] = { |
| 24 const char kCloseBarActiveFilename[] = "images/close_2_active.png"; | 24 { "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" }, |
| 25 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" }, |
| 26 { "local-ntp.css", IDR_LOCAL_NTP_CSS, "text/css" }, |
| 27 { "images/close_2.png", IDR_CLOSE_2, "image/png" }, |
| 28 { "images/close_2_hover.png", IDR_CLOSE_2_H, "image/png" }, |
| 29 { "images/close_2_active.png", IDR_CLOSE_2_P, "image/png" }, |
| 30 { "images/page_icon.png", IDR_LOCAL_OMNIBOX_POPUP_IMAGES_PAGE_ICON_PNG, |
| 31 "image/png" }, |
| 32 { "images/2x/page_icon.png", |
| 33 IDR_LOCAL_OMNIBOX_POPUP_IMAGES_2X_PAGE_ICON_PNG, "image/png" }, |
| 34 { "images/search_icon.png", |
| 35 IDR_LOCAL_OMNIBOX_POPUP_IMAGES_SEARCH_ICON_PNG, "image/png" }, |
| 36 { "images/2x/search_icon.png", |
| 37 IDR_LOCAL_OMNIBOX_POPUP_IMAGES_2X_SEARCH_ICON_PNG, "image/png" }, |
| 38 { "images/google_logo.png", IDR_LOCAL_NTP_IMAGES_LOGO_PNG, "image/png" }, |
| 39 { "images/2x/google_logo.png", |
| 40 IDR_LOCAL_NTP_IMAGES_2X_LOGO_PNG, "image/png" }, |
| 41 { "images/white_google_logo.png", |
| 42 IDR_LOCAL_NTP_IMAGES_WHITE_LOGO_PNG, "image/png" }, |
| 43 { "images/2x/white_google_logo.png", |
| 44 IDR_LOCAL_NTP_IMAGES_2X_WHITE_LOGO_PNG, "image/png" }, |
| 45 }; |
| 46 |
| 47 // Strips any query parameters from the specified path. |
| 48 std::string StripParameters(const std::string& path) { |
| 49 return path.substr(0, path.find("?")); |
| 50 } |
| 25 | 51 |
| 26 } // namespace | 52 } // namespace |
| 27 | 53 |
| 28 LocalNtpSource::LocalNtpSource() { | 54 LocalNtpSource::LocalNtpSource() { |
| 29 } | 55 } |
| 30 | 56 |
| 31 LocalNtpSource::~LocalNtpSource() { | 57 LocalNtpSource::~LocalNtpSource() { |
| 32 } | 58 } |
| 33 | 59 |
| 34 std::string LocalNtpSource::GetSource() const { | 60 std::string LocalNtpSource::GetSource() const { |
| 35 return chrome::kChromeSearchLocalNtpHost; | 61 return chrome::kChromeSearchLocalNtpHost; |
| 36 } | 62 } |
| 37 | 63 |
| 38 void LocalNtpSource::StartDataRequest( | 64 void LocalNtpSource::StartDataRequest( |
| 39 const std::string& path, | 65 const std::string& path, |
| 40 bool is_incognito, | 66 bool is_incognito, |
| 41 const content::URLDataSource::GotDataCallback& callback) { | 67 const content::URLDataSource::GotDataCallback& callback) { |
| 42 int identifier = -1; | 68 const std::string stripped_path = StripParameters(path); |
| 43 if (path == kHtmlFilename) { | 69 for (size_t i = 0; i < arraysize(kResources); ++i) { |
| 44 identifier = IDR_LOCAL_NTP_HTML; | 70 if (stripped_path == kResources[i].filename) { |
| 45 } else if (path == kJSFilename) { | 71 scoped_refptr<base::RefCountedStaticMemory> response( |
| 46 identifier = IDR_LOCAL_NTP_JS; | 72 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( |
| 47 } else if (path == kCssFilename) { | 73 kResources[i].identifier)); |
| 48 identifier = IDR_LOCAL_NTP_CSS; | 74 callback.Run(response); |
| 49 } else if (path == kCloseBarFilename) { | 75 return; |
| 50 identifier = IDR_CLOSE_2; | 76 } |
| 51 } else if (path == kCloseBarHoverFilename) { | |
| 52 identifier = IDR_CLOSE_2_H; | |
| 53 } else if (path == kCloseBarActiveFilename) { | |
| 54 identifier = IDR_CLOSE_2_P; | |
| 55 } else { | |
| 56 callback.Run(NULL); | |
| 57 return; | |
| 58 } | 77 } |
| 59 | 78 callback.Run(NULL); |
| 60 scoped_refptr<base::RefCountedStaticMemory> response( | 79 }; |
| 61 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(identifier)); | |
| 62 callback.Run(response); | |
| 63 } | |
| 64 | 80 |
| 65 std::string LocalNtpSource::GetMimeType( | 81 std::string LocalNtpSource::GetMimeType( |
| 66 const std::string& path) const { | 82 const std::string& path) const { |
| 67 if (path == kHtmlFilename) | 83 const std::string stripped_path = StripParameters(path); |
| 68 return "text/html"; | 84 for (size_t i = 0; i < arraysize(kResources); ++i) { |
| 69 if (path == kJSFilename) | 85 if (stripped_path == kResources[i].filename) |
| 70 return "application/javascript"; | 86 return kResources[i].mime_type; |
| 71 if (path == kCssFilename) | |
| 72 return "text/css"; | |
| 73 if (path == kCloseBarFilename || path == kCloseBarHoverFilename || | |
| 74 path == kCloseBarActiveFilename) { | |
| 75 return "image/png"; | |
| 76 } | 87 } |
| 77 return std::string(); | 88 return std::string(); |
| 78 } | 89 } |
| 79 | 90 |
| 80 bool LocalNtpSource::ShouldServiceRequest( | 91 bool LocalNtpSource::ShouldServiceRequest( |
| 81 const net::URLRequest* request) const { | 92 const net::URLRequest* request) const { |
| 82 DCHECK(request->url().host() == chrome::kChromeSearchLocalNtpHost); | 93 DCHECK(request->url().host() == chrome::kChromeSearchLocalNtpHost); |
| 83 | 94 |
| 84 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) { | 95 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) { |
| 85 DCHECK(StartsWithASCII(request->url().path(), "/", true)); | 96 DCHECK(StartsWithASCII(request->url().path(), "/", true)); |
| 86 std::string filename = request->url().path().substr(1); | 97 std::string filename = request->url().path().substr(1); |
| 87 return filename == kHtmlFilename || filename == kJSFilename || | 98 for (size_t i = 0; i < arraysize(kResources); ++i) { |
| 88 filename == kCssFilename || filename == kCloseBarFilename || | 99 if (filename == kResources[i].filename) |
| 89 filename == kCloseBarHoverFilename || | 100 return true; |
| 90 filename == kCloseBarActiveFilename; | 101 } |
| 91 } | 102 } |
| 92 return false; | 103 return false; |
| 93 } | 104 } |
| OLD | NEW |