| 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/search.h" |
| 14 #include "chrome/common/url_constants.h" |
| 15 #include "googleurl/src/gurl.h" |
| 16 #include "grit/browser_resources.h" |
| 17 #include "net/base/url_util.h" |
| 18 #include "net/url_request/url_request.h" |
| 19 #include "ui/base/layout.h" |
| 20 #include "ui/base/resource/resource_bundle.h" |
| 21 |
| 22 namespace { |
| 23 |
| 24 const char kLoaderHtmlPath[] = "/loader.html"; |
| 25 const char kLoaderJSPath[] = "/loader.js"; |
| 26 const char kResultHtmlPath[] = "/result.html"; |
| 27 const char kResultJSPath[] = "/result.js"; |
| 28 const char kOriginParam[] = "origin"; |
| 29 |
| 30 } // namespace |
| 31 |
| 32 SuggestionSource::SuggestionSource() { |
| 33 } |
| 34 |
| 35 SuggestionSource::~SuggestionSource() { |
| 36 } |
| 37 |
| 38 std::string SuggestionSource::GetSource() { |
| 39 return chrome::kChromeSearchSuggestionHost; |
| 40 } |
| 41 |
| 42 void SuggestionSource::StartDataRequest( |
| 43 const std::string& path_and_query, |
| 44 bool is_incognito, |
| 45 const content::URLDataSource::GotDataCallback& callback) { |
| 46 std::string path(GURL(chrome::kChromeSearchSuggestionUrl + |
| 47 path_and_query).path()); |
| 48 if (path == kLoaderHtmlPath) |
| 49 SendResource(IDR_OMNIBOX_RESULT_LOADER_HTML, callback); |
| 50 else if (path == kLoaderJSPath) |
| 51 SendJSWithOrigin(IDR_OMNIBOX_RESULT_LOADER_JS, path_and_query, callback); |
| 52 else if (path == kResultHtmlPath) |
| 53 SendResource(IDR_OMNIBOX_RESULT_HTML, callback); |
| 54 else if (path == kResultJSPath) |
| 55 SendJSWithOrigin(IDR_OMNIBOX_RESULT_JS, path_and_query, callback); |
| 56 else |
| 57 callback.Run(NULL); |
| 58 } |
| 59 |
| 60 void SuggestionSource::SendResource( |
| 61 int resource_id, |
| 62 const content::URLDataSource::GotDataCallback& callback) { |
| 63 scoped_refptr<base::RefCountedStaticMemory> response( |
| 64 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id)); |
| 65 callback.Run(response); |
| 66 } |
| 67 |
| 68 void SuggestionSource::SendJSWithOrigin( |
| 69 int resource_id, |
| 70 const std::string& path_and_query, |
| 71 const content::URLDataSource::GotDataCallback& callback) { |
| 72 // &origin is used to check the source of postMessage() requests to suggestion |
| 73 // iframes. It is set by ChromeContentRendererClient::WillSendRequest() but |
| 74 // validate and escape it anyway. |
| 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 origin = GURL(origin).GetOrigin().spec(); |
| 84 TrimString(origin, "/", &origin); |
| 85 |
| 86 std::string js_escaped_origin; |
| 87 base::JsonDoubleQuote(origin, false, &js_escaped_origin); |
| 88 base::StringPiece template_js = |
| 89 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); |
| 90 std::string response(template_js.as_string()); |
| 91 ReplaceFirstSubstringAfterOffset(&response, 0, "{{ORIGIN}}", origin); |
| 92 callback.Run(base::RefCountedString::TakeString(&response)); |
| 93 } |
| 94 |
| 95 std::string SuggestionSource::GetMimeType( |
| 96 const std::string& path_and_query) const { |
| 97 std::string path(GURL(chrome::kChromeSearchSuggestionUrl + |
| 98 path_and_query).path()); |
| 99 if (path == kLoaderHtmlPath || path == kResultHtmlPath) |
| 100 return "text/html"; |
| 101 if (path == kLoaderJSPath || path == kResultJSPath) |
| 102 return "application/javascript"; |
| 103 return ""; |
| 104 } |
| 105 |
| 106 bool SuggestionSource::ShouldServiceRequest( |
| 107 const net::URLRequest* request) const { |
| 108 const std::string& path = request->url().path(); |
| 109 return request->url().SchemeIs(chrome::kChromeSearchScheme) && |
| 110 request->url().host() == chrome::kChromeSearchSuggestionHost && |
| 111 (path == kLoaderHtmlPath || path == kLoaderJSPath || |
| 112 path == kResultHtmlPath || path == kResultJSPath); |
| 113 } |
| 114 |
| 115 bool SuggestionSource::ShouldDenyXFrameOptions() const { |
| 116 return false; |
| 117 } |
| OLD | NEW |