OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/search/suggestion_source.h" |
| 6 |
| 7 #include "base/json/string_escape.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/ref_counted_memory.h" |
| 10 #include "base/string_util.h" |
| 11 #include "base/stringprintf.h" |
| 12 #include "base/strings/string_piece.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/search/search.h" |
| 15 #include "chrome/common/url_constants.h" |
| 16 #include "content/public/common/content_client.h" |
| 17 #include "googleurl/src/gurl.h" |
| 18 #include "grit/browser_resources.h" |
| 19 #include "net/base/url_util.h" |
| 20 #include "net/url_request/url_request.h" |
| 21 #include "ui/base/layout.h" |
| 22 |
| 23 namespace { |
| 24 const char kLoaderHtmlFilename[] = "loader.html"; |
| 25 const char kLoaderJSFilename[] = "loader.js"; |
| 26 const char kResultHtmlFilename[] = "result.html"; |
| 27 const char kResultJSFilename[] = "result.js"; |
| 28 const char kOriginParam[] = "origin"; |
| 29 |
| 30 std::string StripQueryString(const std::string& path) { |
| 31 size_t pos = path.find('?'); |
| 32 if (pos != path.npos) |
| 33 return path.substr(0, pos); |
| 34 return path; |
| 35 } |
| 36 } // namespace |
| 37 |
| 38 SuggestionSource::SuggestionSource() { |
| 39 } |
| 40 |
| 41 SuggestionSource::~SuggestionSource() { |
| 42 } |
| 43 |
| 44 std::string SuggestionSource::GetSource() { |
| 45 return chrome::kChromeSearchSuggestionHost; |
| 46 } |
| 47 |
| 48 void SuggestionSource::StartDataRequest( |
| 49 const std::string& path, |
| 50 bool is_incognito, |
| 51 const content::URLDataSource::GotDataCallback& callback) { |
| 52 std::string filename(StripQueryString(path)); |
| 53 if (filename == kLoaderHtmlFilename) |
| 54 SendResource(IDR_OMNIBOX_RESULT_LOADER_HTML, callback); |
| 55 else if (filename == kLoaderJSFilename) |
| 56 SendJSWithOrigin(IDR_OMNIBOX_RESULT_LOADER_JS, path, callback); |
| 57 else if (filename == kResultHtmlFilename) |
| 58 SendResource(IDR_OMNIBOX_RESULT_HTML, callback); |
| 59 else if (filename == kResultJSFilename) |
| 60 SendJSWithOrigin(IDR_OMNIBOX_RESULT_JS, path, callback); |
| 61 else |
| 62 callback.Run(NULL); |
| 63 } |
| 64 |
| 65 void SuggestionSource::SendResource( |
| 66 int resource_id, |
| 67 const content::URLDataSource::GotDataCallback& callback) { |
| 68 scoped_refptr<base::RefCountedStaticMemory> response( |
| 69 content::GetContentClient()->GetDataResourceBytes(resource_id)); |
| 70 callback.Run(response); |
| 71 } |
| 72 |
| 73 void SuggestionSource::SendJSWithOrigin( |
| 74 int resource_id, |
| 75 const std::string& path, |
| 76 const content::URLDataSource::GotDataCallback& callback) { |
| 77 // Expect an &origin param which gives the origin of embedding page. This |
| 78 // param is always set by the renderer for requests to this host. |
| 79 std::string origin; |
| 80 if (!net::GetValueForKeyInQuery( |
| 81 GURL(chrome::kChromeSearchSuggestionURL + path), |
| 82 kOriginParam, &origin)) { |
| 83 callback.Run(NULL); |
| 84 return; |
| 85 } |
| 86 |
| 87 std::string js_escaped_origin; |
| 88 base::JsonDoubleQuote(origin, false, &js_escaped_origin); |
| 89 base::StringPiece template_js = |
| 90 content::GetContentClient()->GetDataResource(resource_id, |
| 91 ui::SCALE_FACTOR_NONE); |
| 92 std::string response(base::StringPrintf(template_js.as_string().c_str(), |
| 93 js_escaped_origin.c_str())); |
| 94 callback.Run(base::RefCountedString::TakeString(&response)); |
| 95 } |
| 96 |
| 97 std::string SuggestionSource::GetMimeType(const std::string& path) const { |
| 98 std::string filename(StripQueryString(path)); |
| 99 if (filename == kLoaderHtmlFilename || filename == kResultHtmlFilename) |
| 100 return "text/html"; |
| 101 if (filename == kLoaderJSFilename || filename == kResultJSFilename) |
| 102 return "application/javascript"; |
| 103 return ""; |
| 104 } |
| 105 |
| 106 bool SuggestionSource::ShouldServiceRequest( |
| 107 const net::URLRequest* request) const { |
| 108 DCHECK(request->url().host() == chrome::kChromeSearchSuggestionHost); |
| 109 |
| 110 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) { |
| 111 DCHECK(StartsWithASCII(request->url().path(), "/", true)); |
| 112 std::string filename = request->url().path().substr(1); |
| 113 return filename == kLoaderHtmlFilename || |
| 114 filename == kLoaderJSFilename || |
| 115 filename == kResultHtmlFilename || |
| 116 filename == kResultJSFilename; |
| 117 } |
| 118 return false; |
| 119 } |
| 120 |
| 121 bool SuggestionSource::ShouldDenyXFrameOptions() const { |
| 122 return false; |
| 123 } |
OLD | NEW |