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