OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/contextual_search_promo_source.h" | 5 #include "chrome/browser/search/contextual_search_promo_source.h" |
6 | 6 |
7 #include "base/memory/ref_counted_memory.h" | 7 #include "base/memory/ref_counted_memory.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "chrome/common/url_constants.h" | 10 #include "chrome/common/url_constants.h" |
11 #include "grit/browser_resources.h" | 11 #include "grit/browser_resources.h" |
12 #include "ui/base/resource/resource_bundle.h" | 12 #include "ui/base/resource/resource_bundle.h" |
13 #include "url/gurl.h" | 13 #include "url/gurl.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 const char kPromoHTMLPath[] = "/promo.html"; | 17 const char kPromoHTMLPath[] = "/promo.html"; |
18 const char kPromoCSSPath[] = "/promo.css"; | 18 const char kPromoCSSPath[] = "/promo.css"; |
19 const char kPromoJSPath[] = "/promo.js"; | 19 const char kPromoJSPath[] = "/promo.js"; |
20 const char kPromoOptInHTMLPath[] = "/optin.html"; | |
21 const char kPromoOptOutHTMLPath[] = "/optout.html"; | |
22 | 20 |
23 } // namespace | 21 } // namespace |
24 | 22 |
25 ContextualSearchPromoSource::ContextualSearchPromoSource() {} | 23 ContextualSearchPromoSource::ContextualSearchPromoSource() {} |
26 | 24 |
27 ContextualSearchPromoSource::~ContextualSearchPromoSource() {} | 25 ContextualSearchPromoSource::~ContextualSearchPromoSource() {} |
28 | 26 |
29 void ContextualSearchPromoSource::StartDataRequest( | 27 void ContextualSearchPromoSource::StartDataRequest( |
30 const std::string& path_and_query, int render_process_id, | 28 const std::string& path_and_query, int render_process_id, |
31 int render_frame_id, | 29 int render_frame_id, |
32 const content::URLDataSource::GotDataCallback& callback) { | 30 const content::URLDataSource::GotDataCallback& callback) { |
33 GURL url(std::string(chrome::kChromeUIContextualSearchPromoURL) + "/" + | 31 GURL url(std::string(chrome::kChromeUIContextualSearchPromoURL) + "/" + |
34 path_and_query); | 32 path_and_query); |
35 std::string path(url.path()); | 33 std::string path(url.path()); |
36 if (path == kPromoHTMLPath) { | 34 if (path == kPromoHTMLPath) { |
37 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_HTML, callback); | 35 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_HTML, callback); |
38 } else if (path == kPromoCSSPath) { | 36 } else if (path == kPromoCSSPath) { |
39 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_CSS, callback); | 37 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_CSS, callback); |
40 } else if (path == kPromoJSPath) { | 38 } else if (path == kPromoJSPath) { |
41 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_JS, callback); | 39 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_JS, callback); |
42 } else if (path == kPromoOptInHTMLPath) { | |
43 std::string empty; | |
44 callback.Run(base::RefCountedString::TakeString(&empty)); | |
45 } else if (path == kPromoOptOutHTMLPath) { | |
46 std::string empty; | |
47 callback.Run(base::RefCountedString::TakeString(&empty)); | |
48 } else { | 40 } else { |
49 callback.Run(NULL); | 41 callback.Run(NULL); |
50 } | 42 } |
51 } | 43 } |
52 | 44 |
53 std::string ContextualSearchPromoSource::GetSource() const { | 45 std::string ContextualSearchPromoSource::GetSource() const { |
54 return chrome::kChromeUIContextualSearchPromoHost; | 46 return chrome::kChromeUIContextualSearchPromoHost; |
55 } | 47 } |
56 | 48 |
57 std::string ContextualSearchPromoSource::GetMimeType( | 49 std::string ContextualSearchPromoSource::GetMimeType( |
58 const std::string& path_and_query) const { | 50 const std::string& path_and_query) const { |
59 std::string path(GURL("chrome://host/" + path_and_query).path()); | 51 std::string path(GURL("chrome://host/" + path_and_query).path()); |
60 if (EndsWith(path, ".js", false)) return "application/javascript"; | 52 if (EndsWith(path, ".js", false)) return "application/javascript"; |
61 if (EndsWith(path, ".png", false)) return "image/png"; | 53 if (EndsWith(path, ".png", false)) return "image/png"; |
62 if (EndsWith(path, ".css", false)) return "text/css"; | 54 if (EndsWith(path, ".css", false)) return "text/css"; |
63 if (EndsWith(path, ".html", false)) return "text/html"; | 55 if (EndsWith(path, ".html", false)) return "text/html"; |
64 return ""; | 56 return ""; |
65 } | 57 } |
66 | 58 |
67 bool ContextualSearchPromoSource::ShouldDenyXFrameOptions() const { | 59 bool ContextualSearchPromoSource::ShouldDenyXFrameOptions() const { |
68 return false; | 60 return false; |
69 } | 61 } |
70 | 62 |
71 void ContextualSearchPromoSource::SendResource( | 63 void ContextualSearchPromoSource::SendResource( |
72 int resource_id, const content::URLDataSource::GotDataCallback& callback) { | 64 int resource_id, const content::URLDataSource::GotDataCallback& callback) { |
73 scoped_refptr<base::RefCountedStaticMemory> response( | 65 scoped_refptr<base::RefCountedStaticMemory> response( |
74 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id)); | 66 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id)); |
75 callback.Run(response.get()); | 67 callback.Run(response.get()); |
76 } | 68 } |
OLD | NEW |