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 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 } // namespace |
| 30 |
| 31 SuggestionSource::SuggestionSource() { |
| 32 } |
| 33 |
| 34 SuggestionSource::~SuggestionSource() { |
| 35 } |
| 36 |
| 37 std::string SuggestionSource::GetSource() { |
| 38 return chrome::kChromeSearchSuggestionHost; |
| 39 } |
| 40 |
| 41 void SuggestionSource::StartDataRequest( |
| 42 const std::string& path_and_query, |
| 43 bool is_incognito, |
| 44 const content::URLDataSource::GotDataCallback& callback) { |
| 45 std::string path(GURL(chrome::kChromeSearchSuggestionURL + |
| 46 path_and_query).path()); |
| 47 if (path == kLoaderHtmlPath) |
| 48 SendResource(IDR_OMNIBOX_RESULT_LOADER_HTML, callback); |
| 49 else if (path == kLoaderJSPath) |
| 50 SendJSWithOrigin(IDR_OMNIBOX_RESULT_LOADER_JS, path_and_query, callback); |
| 51 else if (path == kResultHtmlPath) |
| 52 SendResource(IDR_OMNIBOX_RESULT_HTML, callback); |
| 53 else if (path == kResultJSPath) |
| 54 SendJSWithOrigin(IDR_OMNIBOX_RESULT_JS, path_and_query, callback); |
| 55 else |
| 56 callback.Run(NULL); |
| 57 } |
| 58 |
| 59 void SuggestionSource::SendResource( |
| 60 int resource_id, |
| 61 const content::URLDataSource::GotDataCallback& callback) { |
| 62 scoped_refptr<base::RefCountedStaticMemory> response( |
| 63 content::GetContentClient()->GetDataResourceBytes(resource_id)); |
| 64 callback.Run(response); |
| 65 } |
| 66 |
| 67 void SuggestionSource::SendJSWithOrigin( |
| 68 int resource_id, |
| 69 const std::string& path_and_query, |
| 70 const content::URLDataSource::GotDataCallback& callback) { |
| 71 // Expect an &origin param which gives the origin of embedding page. This |
| 72 // param is always set by the renderer for requests to this host. |
| 73 std::string origin; |
| 74 if (!net::GetValueForKeyInQuery( |
| 75 GURL(chrome::kChromeSearchSuggestionURL + path_and_query), |
| 76 kOriginParam, &origin)) { |
| 77 callback.Run(NULL); |
| 78 return; |
| 79 } |
| 80 |
| 81 std::string js_escaped_origin; |
| 82 base::JsonDoubleQuote(origin, false, &js_escaped_origin); |
| 83 base::StringPiece template_js = |
| 84 content::GetContentClient()->GetDataResource(resource_id, |
| 85 ui::SCALE_FACTOR_NONE); |
| 86 std::string response(base::StringPrintf(template_js.as_string().c_str(), |
| 87 js_escaped_origin.c_str())); |
| 88 callback.Run(base::RefCountedString::TakeString(&response)); |
| 89 } |
| 90 |
| 91 std::string SuggestionSource::GetMimeType( |
| 92 const std::string& path_and_query) const { |
| 93 std::string path(GURL(chrome::kChromeSearchSuggestionURL + |
| 94 path_and_query).path()); |
| 95 if (path == kLoaderHtmlPath || path == kResultHtmlPath) |
| 96 return "text/html"; |
| 97 if (path == kLoaderJSPath || path == kResultJSPath) |
| 98 return "application/javascript"; |
| 99 return ""; |
| 100 } |
| 101 |
| 102 bool SuggestionSource::ShouldServiceRequest( |
| 103 const net::URLRequest* request) const { |
| 104 const std::string& path = request->url().path(); |
| 105 return request->url().SchemeIs(chrome::kChromeSearchScheme) && |
| 106 request->url().host() == chrome::kChromeSearchSuggestionHost && |
| 107 (path == kLoaderHtmlPath || path == kLoaderJSPath || |
| 108 path == kResultHtmlPath || path == kResultJSPath); |
| 109 } |
| 110 |
| 111 bool SuggestionSource::ShouldDenyXFrameOptions() const { |
| 112 return false; |
| 113 } |
OLD | NEW |