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_number_conversions.h" |
| 12 #include "base/strings/string_piece.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/search/instant_io_context.h" |
| 15 #include "chrome/browser/search/search.h" |
| 16 #include "chrome/common/url_constants.h" |
| 17 #include "content/public/browser/render_view_host.h" |
| 18 #include "content/public/browser/resource_request_info.h" |
| 19 #include "content/public/browser/web_contents.h" |
| 20 #include "googleurl/src/gurl.h" |
| 21 #include "grit/browser_resources.h" |
| 22 #include "net/base/url_util.h" |
| 23 #include "net/url_request/url_request.h" |
| 24 #include "ui/base/layout.h" |
| 25 #include "ui/base/resource/resource_bundle.h" |
| 26 |
| 27 namespace { |
| 28 |
| 29 const char kLoaderHtmlPath[] = "/loader.html"; |
| 30 const char kLoaderJSPath[] = "/loader.js"; |
| 31 const char kResultHtmlPath[] = "/result.html"; |
| 32 const char kResultJSPath[] = "/result.js"; |
| 33 |
| 34 } // namespace |
| 35 |
| 36 SuggestionSource::SuggestionSource() { |
| 37 } |
| 38 |
| 39 SuggestionSource::~SuggestionSource() { |
| 40 } |
| 41 |
| 42 std::string SuggestionSource::GetSource() const { |
| 43 return chrome::kChromeSearchSuggestionHost; |
| 44 } |
| 45 |
| 46 void SuggestionSource::StartDataRequest( |
| 47 const std::string& path_and_query, |
| 48 int render_process_id, |
| 49 int render_view_id, |
| 50 const content::URLDataSource::GotDataCallback& callback) { |
| 51 std::string path(GURL(chrome::kChromeSearchSuggestionUrl + |
| 52 path_and_query).path()); |
| 53 if (path == kLoaderHtmlPath) { |
| 54 SendResource(IDR_OMNIBOX_RESULT_LOADER_HTML, callback); |
| 55 } else if (path == kLoaderJSPath) { |
| 56 SendJSWithOrigin(IDR_OMNIBOX_RESULT_LOADER_JS, render_process_id, |
| 57 render_view_id, callback); |
| 58 } else if (path == kResultHtmlPath) { |
| 59 SendResource(IDR_OMNIBOX_RESULT_HTML, callback); |
| 60 } else if (path == kResultJSPath) { |
| 61 SendJSWithOrigin(IDR_OMNIBOX_RESULT_JS, render_process_id, render_view_id, |
| 62 callback); |
| 63 } else { |
| 64 callback.Run(NULL); |
| 65 } |
| 66 } |
| 67 |
| 68 void SuggestionSource::SendResource( |
| 69 int resource_id, |
| 70 const content::URLDataSource::GotDataCallback& callback) { |
| 71 scoped_refptr<base::RefCountedStaticMemory> response( |
| 72 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id)); |
| 73 callback.Run(response); |
| 74 } |
| 75 |
| 76 void SuggestionSource::SendJSWithOrigin( |
| 77 int resource_id, |
| 78 int render_process_id, |
| 79 int render_view_id, |
| 80 const content::URLDataSource::GotDataCallback& callback) { |
| 81 std::string origin; |
| 82 if (!GetOrigin(render_process_id, render_view_id, &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 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); |
| 91 std::string response(template_js.as_string()); |
| 92 ReplaceFirstSubstringAfterOffset(&response, 0, "{{ORIGIN}}", origin); |
| 93 callback.Run(base::RefCountedString::TakeString(&response)); |
| 94 } |
| 95 |
| 96 std::string SuggestionSource::GetMimeType( |
| 97 const std::string& path_and_query) const { |
| 98 std::string path(GURL(chrome::kChromeSearchSuggestionUrl + |
| 99 path_and_query).path()); |
| 100 if (path == kLoaderHtmlPath || path == kResultHtmlPath) |
| 101 return "text/html"; |
| 102 if (path == kLoaderJSPath || path == kResultJSPath) |
| 103 return "application/javascript"; |
| 104 return ""; |
| 105 } |
| 106 |
| 107 bool SuggestionSource::ShouldServiceRequest( |
| 108 const net::URLRequest* request) const { |
| 109 const std::string& path = request->url().path(); |
| 110 return InstantIOContext::ShouldServiceRequest(request) && |
| 111 request->url().SchemeIs(chrome::kChromeSearchScheme) && |
| 112 request->url().host() == chrome::kChromeSearchSuggestionHost && |
| 113 (path == kLoaderHtmlPath || path == kLoaderJSPath || |
| 114 path == kResultHtmlPath || path == kResultJSPath); |
| 115 } |
| 116 |
| 117 bool SuggestionSource::ShouldDenyXFrameOptions() const { |
| 118 return false; |
| 119 } |
| 120 |
| 121 bool SuggestionSource::GetOrigin( |
| 122 int render_process_id, |
| 123 int render_view_id, |
| 124 std::string* origin) const { |
| 125 content::RenderViewHost* rvh = |
| 126 content::RenderViewHost::FromID(render_process_id, render_view_id); |
| 127 if (rvh == NULL) |
| 128 return false; |
| 129 content::WebContents* contents = |
| 130 content::WebContents::FromRenderViewHost(rvh); |
| 131 if (contents == NULL) |
| 132 return false; |
| 133 *origin = contents->GetURL().GetOrigin().spec(); |
| 134 // Origin should not include a trailing slash. That is part of the path. |
| 135 TrimString(*origin, "/", origin); |
| 136 return true; |
| 137 } |
OLD | NEW |