| 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/strings/string_piece.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/search/instant_io_context.h" |
| 14 #include "chrome/browser/search/search.h" |
| 15 #include "chrome/common/url_constants.h" |
| 16 #include "googleurl/src/gurl.h" |
| 17 #include "grit/browser_resources.h" |
| 18 #include "net/base/url_util.h" |
| 19 #include "net/url_request/url_request.h" |
| 20 #include "ui/base/layout.h" |
| 21 #include "ui/base/resource/resource_bundle.h" |
| 22 |
| 23 namespace { |
| 24 |
| 25 const char kLoaderHtmlPath[] = "/loader.html"; |
| 26 const char kLoaderJSPath[] = "/loader.js"; |
| 27 const char kResultHtmlPath[] = "/result.html"; |
| 28 const char kResultJSPath[] = "/result.js"; |
| 29 const char kOriginParam[] = "origin"; |
| 30 |
| 31 } // namespace |
| 32 |
| 33 SuggestionSource::SuggestionSource() { |
| 34 } |
| 35 |
| 36 SuggestionSource::~SuggestionSource() { |
| 37 } |
| 38 |
| 39 std::string SuggestionSource::GetSource() { |
| 40 return chrome::kChromeSearchSuggestionHost; |
| 41 } |
| 42 |
| 43 void SuggestionSource::StartDataRequest( |
| 44 const std::string& path_and_query, |
| 45 bool is_incognito, |
| 46 const content::URLDataSource::GotDataCallback& callback) { |
| 47 std::string path(GURL(chrome::kChromeSearchSuggestionUrl + |
| 48 path_and_query).path()); |
| 49 if (path == kLoaderHtmlPath) |
| 50 SendResource(IDR_OMNIBOX_RESULT_LOADER_HTML, callback); |
| 51 else if (path == kLoaderJSPath) |
| 52 SendJSWithOrigin(IDR_OMNIBOX_RESULT_LOADER_JS, path_and_query, callback); |
| 53 else if (path == kResultHtmlPath) |
| 54 SendResource(IDR_OMNIBOX_RESULT_HTML, callback); |
| 55 else if (path == kResultJSPath) |
| 56 SendJSWithOrigin(IDR_OMNIBOX_RESULT_JS, path_and_query, callback); |
| 57 else |
| 58 callback.Run(NULL); |
| 59 } |
| 60 |
| 61 void SuggestionSource::SendResource( |
| 62 int resource_id, |
| 63 const content::URLDataSource::GotDataCallback& callback) { |
| 64 scoped_refptr<base::RefCountedStaticMemory> response( |
| 65 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id)); |
| 66 callback.Run(response); |
| 67 } |
| 68 |
| 69 void SuggestionSource::SendJSWithOrigin( |
| 70 int resource_id, |
| 71 const std::string& path_and_query, |
| 72 const content::URLDataSource::GotDataCallback& callback) { |
| 73 // &origin is used to check the source of postMessage() requests to |
| 74 // suggestion iframes. It is added below in WillServiceRequest(). |
| 75 std::string origin; |
| 76 if (!net::GetValueForKeyInQuery( |
| 77 GURL(chrome::kChromeSearchSuggestionUrl + path_and_query), |
| 78 kOriginParam, &origin) || |
| 79 !GURL(origin).GetOrigin().is_valid()) { |
| 80 callback.Run(NULL); |
| 81 return; |
| 82 } |
| 83 |
| 84 std::string js_escaped_origin; |
| 85 base::JsonDoubleQuote(origin, false, &js_escaped_origin); |
| 86 base::StringPiece template_js = |
| 87 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); |
| 88 std::string response(template_js.as_string()); |
| 89 ReplaceFirstSubstringAfterOffset(&response, 0, "{{ORIGIN}}", origin); |
| 90 callback.Run(base::RefCountedString::TakeString(&response)); |
| 91 } |
| 92 |
| 93 std::string SuggestionSource::GetMimeType( |
| 94 const std::string& path_and_query) const { |
| 95 std::string path(GURL(chrome::kChromeSearchSuggestionUrl + |
| 96 path_and_query).path()); |
| 97 if (path == kLoaderHtmlPath || path == kResultHtmlPath) |
| 98 return "text/html"; |
| 99 if (path == kLoaderJSPath || path == kResultJSPath) |
| 100 return "application/javascript"; |
| 101 return ""; |
| 102 } |
| 103 |
| 104 bool SuggestionSource::ShouldServiceRequest( |
| 105 const net::URLRequest* request) const { |
| 106 const std::string& path = request->url().path(); |
| 107 return InstantIOContext::ShouldServiceRequest(request) && |
| 108 request->url().SchemeIs(chrome::kChromeSearchScheme) && |
| 109 request->url().host() == chrome::kChromeSearchSuggestionHost && |
| 110 (path == kLoaderHtmlPath || path == kLoaderJSPath || |
| 111 path == kResultHtmlPath || path == kResultJSPath); |
| 112 } |
| 113 |
| 114 void SuggestionSource::WillServiceRequest( |
| 115 const net::URLRequest* request, std::string* path) const { |
| 116 GURL url(chrome::kChromeSearchSuggestionUrl + (*path)); |
| 117 // Note that this replaces any existing query string parameters, including |
| 118 // any existing &origin parameter. |
| 119 GURL::Replacements set_origin; |
| 120 std::string query_str = "origin="; |
| 121 query_str.append(InstantIOContext::GetExpectedOrigin(request)); |
| 122 set_origin.SetQueryStr(query_str); |
| 123 GURL url_with_origin(url.ReplaceComponents(set_origin)); |
| 124 *path = url_with_origin.path().substr(1) + "?" + url_with_origin.query(); |
| 125 } |
| 126 |
| 127 bool SuggestionSource::ShouldDenyXFrameOptions() const { |
| 128 return false; |
| 129 } |
| OLD | NEW |