Index: chrome/browser/search/suggestion_source.cc |
diff --git a/chrome/browser/search/suggestion_source.cc b/chrome/browser/search/suggestion_source.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2f19a21242ca5006f9cd12e2141edc8f5684884a |
--- /dev/null |
+++ b/chrome/browser/search/suggestion_source.cc |
@@ -0,0 +1,123 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/search/suggestion_source.h" |
+ |
+#include "base/json/string_escape.h" |
+#include "base/logging.h" |
+#include "base/memory/ref_counted_memory.h" |
+#include "base/string_util.h" |
+#include "base/stringprintf.h" |
+#include "base/strings/string_piece.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/search/search.h" |
+#include "chrome/common/url_constants.h" |
+#include "content/public/common/content_client.h" |
+#include "googleurl/src/gurl.h" |
+#include "grit/browser_resources.h" |
+#include "net/base/url_util.h" |
+#include "net/url_request/url_request.h" |
+#include "ui/base/layout.h" |
+ |
+namespace { |
+const char kLoaderHtmlFilename[] = "loader.html"; |
+const char kLoaderJSFilename[] = "loader.js"; |
+const char kResultHtmlFilename[] = "result.html"; |
+const char kResultJSFilename[] = "result.js"; |
+const char kOriginParam[] = "origin"; |
+ |
+std::string StripQueryString(const std::string& path) { |
samarth
2013/04/02 23:26:58
Is there an easy way of doing with some existing u
Jered
2013/04/04 17:30:10
Seems wiser. Done.
|
+ size_t pos = path.find('?'); |
+ if (pos != path.npos) |
+ return path.substr(0, pos); |
+ return path; |
+} |
+} // namespace |
+ |
+SuggestionSource::SuggestionSource() { |
+} |
+ |
+SuggestionSource::~SuggestionSource() { |
+} |
+ |
+std::string SuggestionSource::GetSource() { |
+ return chrome::kChromeSearchSuggestionHost; |
+} |
+ |
+void SuggestionSource::StartDataRequest( |
+ const std::string& path, |
+ bool is_incognito, |
+ const content::URLDataSource::GotDataCallback& callback) { |
+ std::string filename(StripQueryString(path)); |
+ if (filename == kLoaderHtmlFilename) |
+ SendResource(IDR_OMNIBOX_RESULT_LOADER_HTML, callback); |
+ else if (filename == kLoaderJSFilename) |
+ SendJSWithOrigin(IDR_OMNIBOX_RESULT_LOADER_JS, path, callback); |
+ else if (filename == kResultHtmlFilename) |
+ SendResource(IDR_OMNIBOX_RESULT_HTML, callback); |
+ else if (filename == kResultJSFilename) |
+ SendJSWithOrigin(IDR_OMNIBOX_RESULT_JS, path, callback); |
+ else |
+ callback.Run(NULL); |
+} |
+ |
+void SuggestionSource::SendResource( |
+ int resource_id, |
+ const content::URLDataSource::GotDataCallback& callback) { |
+ scoped_refptr<base::RefCountedStaticMemory> response( |
+ content::GetContentClient()->GetDataResourceBytes(resource_id)); |
+ callback.Run(response); |
+} |
+ |
+void SuggestionSource::SendJSWithOrigin( |
+ int resource_id, |
+ const std::string& path, |
+ const content::URLDataSource::GotDataCallback& callback) { |
+ // Expect an &origin param which gives the origin of embedding page. This |
+ // param is always set by the renderer for requests to this host. |
+ std::string origin; |
+ if (!net::GetValueForKeyInQuery( |
+ GURL(chrome::kChromeSearchSuggestionURL + path), |
+ kOriginParam, &origin)) { |
+ callback.Run(NULL); |
+ return; |
+ } |
+ |
+ std::string js_escaped_origin; |
+ base::JsonDoubleQuote(origin, false, &js_escaped_origin); |
+ base::StringPiece template_js = |
+ content::GetContentClient()->GetDataResource(resource_id, |
+ ui::SCALE_FACTOR_NONE); |
+ std::string response(base::StringPrintf(template_js.as_string().c_str(), |
+ js_escaped_origin.c_str())); |
+ callback.Run(base::RefCountedString::TakeString(&response)); |
+} |
+ |
+std::string SuggestionSource::GetMimeType(const std::string& path) const { |
+ std::string filename(StripQueryString(path)); |
+ if (filename == kLoaderHtmlFilename || filename == kResultHtmlFilename) |
+ return "text/html"; |
+ if (filename == kLoaderJSFilename || filename == kResultJSFilename) |
+ return "application/javascript"; |
+ return ""; |
+} |
+ |
+bool SuggestionSource::ShouldServiceRequest( |
+ const net::URLRequest* request) const { |
+ DCHECK(request->url().host() == chrome::kChromeSearchSuggestionHost); |
+ |
+ if (request->url().SchemeIs(chrome::kChromeSearchScheme)) { |
+ DCHECK(StartsWithASCII(request->url().path(), "/", true)); |
+ std::string filename = request->url().path().substr(1); |
+ return filename == kLoaderHtmlFilename || |
+ filename == kLoaderJSFilename || |
+ filename == kResultHtmlFilename || |
+ filename == kResultJSFilename; |
+ } |
+ return false; |
+} |
+ |
+bool SuggestionSource::ShouldDenyXFrameOptions() const { |
+ return false; |
+} |