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 }; |
25 | 46 |
26 } // namespace | 47 } // namespace |
27 | 48 |
28 LocalNtpSource::LocalNtpSource() { | 49 LocalNtpSource::LocalNtpSource() { |
29 } | 50 } |
30 | 51 |
31 LocalNtpSource::~LocalNtpSource() { | 52 LocalNtpSource::~LocalNtpSource() { |
32 } | 53 } |
33 | 54 |
34 std::string LocalNtpSource::GetSource() { | 55 std::string LocalNtpSource::GetSource() { |
35 return chrome::kChromeSearchLocalNtpHost; | 56 return chrome::kChromeSearchLocalNtpHost; |
36 } | 57 } |
37 | 58 |
38 void LocalNtpSource::StartDataRequest( | 59 void LocalNtpSource::StartDataRequest( |
39 const std::string& path, | 60 const std::string& path, |
40 bool is_incognito, | 61 bool is_incognito, |
41 const content::URLDataSource::GotDataCallback& callback) { | 62 const content::URLDataSource::GotDataCallback& callback) { |
42 int identifier = -1; | 63 int identifier = -1; |
43 if (path == kHtmlFilename) { | 64 std::string stripped_path = StripParameters(path); |
44 identifier = IDR_LOCAL_NTP_HTML; | 65 for (size_t i = 0; i < arraysize(kResources); ++i) { |
45 } else if (path == kJSFilename) { | 66 if (stripped_path == kResources[i].filename) { |
46 identifier = IDR_LOCAL_NTP_JS; | 67 identifier = kResources[i].identifier; |
47 } else if (path == kCssFilename) { | 68 scoped_refptr<base::RefCountedStaticMemory> response( |
48 identifier = IDR_LOCAL_NTP_CSS; | 69 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( |
49 } else if (path == kCloseBarFilename) { | 70 identifier)); |
50 identifier = IDR_CLOSE_2; | 71 callback.Run(response); |
51 } else if (path == kCloseBarHoverFilename) { | 72 return; |
52 identifier = IDR_CLOSE_2_H; | 73 } |
53 } else if (path == kCloseBarActiveFilename) { | |
54 identifier = IDR_CLOSE_2_P; | |
55 } else { | |
56 callback.Run(NULL); | |
57 return; | |
58 } | 74 } |
59 | 75 callback.Run(NULL); |
60 scoped_refptr<base::RefCountedStaticMemory> response( | 76 }; |
61 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(identifier)); | |
62 callback.Run(response); | |
63 } | |
64 | 77 |
65 std::string LocalNtpSource::GetMimeType( | 78 std::string LocalNtpSource::GetMimeType( |
66 const std::string& path) const { | 79 const std::string& path) const { |
67 if (path == kHtmlFilename) | 80 std::string stripped_path = StripParameters(path); |
68 return "text/html"; | 81 for (size_t i = 0; i < arraysize(kResources); ++i) { |
69 if (path == kJSFilename) | 82 if (stripped_path == kResources[i].filename) { |
70 return "application/javascript"; | 83 return kResources[i].mime_type; |
71 if (path == kCssFilename) | 84 } |
72 return "text/css"; | |
73 if (path == kCloseBarFilename || path == kCloseBarHoverFilename || | |
74 path == kCloseBarActiveFilename) { | |
75 return "image/png"; | |
76 } | 85 } |
77 return std::string(); | 86 return std::string(); |
78 } | 87 } |
79 | 88 |
80 bool LocalNtpSource::ShouldServiceRequest( | 89 bool LocalNtpSource::ShouldServiceRequest( |
81 const net::URLRequest* request) const { | 90 const net::URLRequest* request) const { |
82 DCHECK(request->url().host() == chrome::kChromeSearchLocalNtpHost); | 91 DCHECK(request->url().host() == chrome::kChromeSearchLocalNtpHost); |
83 | 92 |
84 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) { | 93 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) { |
85 DCHECK(StartsWithASCII(request->url().path(), "/", true)); | 94 DCHECK(StartsWithASCII(request->url().path(), "/", true)); |
86 std::string filename = request->url().path().substr(1); | 95 std::string filename = request->url().path().substr(1); |
87 return filename == kHtmlFilename || filename == kJSFilename || | 96 for (size_t i = 0; i < arraysize(kResources); ++i) { |
88 filename == kCssFilename || filename == kCloseBarFilename || | 97 if (filename == kResources[i].filename) { |
89 filename == kCloseBarHoverFilename || | 98 return true; |
90 filename == kCloseBarActiveFilename; | 99 } |
| 100 } |
91 } | 101 } |
92 return false; | 102 return false; |
93 } | 103 } |
| 104 |
| 105 std::string LocalNtpSource::StripParameters(const std::string& path) const{ |
| 106 return path.substr(0, path.find("?")); |
| 107 } |
OLD | NEW |