Chromium Code Reviews| 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..792dade466b4dcc124a5577e9453aa0a0ef1f924 |
| --- /dev/null |
| +++ b/chrome/browser/search/suggestion_source.cc |
| @@ -0,0 +1,118 @@ |
| +// 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 kLoaderHtmlPath[] = "/loader.html"; |
| +const char kLoaderJSPath[] = "/loader.js"; |
| +const char kResultHtmlPath[] = "/result.html"; |
| +const char kResultJSPath[] = "/result.js"; |
| +const char kOriginParam[] = "origin"; |
| +} // namespace |
| + |
| +SuggestionSource::SuggestionSource() { |
| +} |
| + |
| +SuggestionSource::~SuggestionSource() { |
| +} |
| + |
| +std::string SuggestionSource::GetSource() { |
| + return chrome::kChromeSearchSuggestionHost; |
| +} |
| + |
| +void SuggestionSource::StartDataRequest( |
| + const std::string& path_and_query, |
| + bool is_incognito, |
| + const content::URLDataSource::GotDataCallback& callback) { |
| + std::string path(GURL(chrome::kChromeSearchSuggestionURL + |
| + path_and_query).path()); |
| + if (path == kLoaderHtmlPath) |
| + SendResource(IDR_OMNIBOX_RESULT_LOADER_HTML, callback); |
| + else if (path == kLoaderJSPath) |
| + SendJSWithOrigin(IDR_OMNIBOX_RESULT_LOADER_JS, path_and_query, callback); |
| + else if (path == kResultHtmlPath) |
| + SendResource(IDR_OMNIBOX_RESULT_HTML, callback); |
| + else if (path == kResultJSPath) |
| + SendJSWithOrigin(IDR_OMNIBOX_RESULT_JS, path_and_query, 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_and_query, |
| + 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_and_query), |
| + 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_and_query) const { |
| + std::string path(GURL(chrome::kChromeSearchSuggestionURL + |
| + path_and_query).path()); |
| + if (path == kLoaderHtmlPath || path == kResultHtmlPath) |
| + return "text/html"; |
| + if (path == kLoaderJSPath || path == kResultJSPath) |
| + return "application/javascript"; |
| + return ""; |
| +} |
| + |
| +bool SuggestionSource::ShouldServiceRequest( |
| + const net::URLRequest* request) const { |
| + DCHECK(request->url().host() == chrome::kChromeSearchSuggestionHost); |
| + |
| + if (request->url().SchemeIs(chrome::kChromeSearchScheme)) { |
|
palmer
2013/04/05 01:30:32
NIT: You could rewrite this, with less indentation
Jered
2013/04/05 15:30:23
Done. Actually, strcmp is pretty cheap, so I'll ma
|
| + const std::string& path = request->url().path(); |
| + return path == kLoaderHtmlPath || |
| + path == kLoaderJSPath || |
| + path == kResultHtmlPath || |
| + path == kResultJSPath; |
| + } |
| + return false; |
| +} |
| + |
| +bool SuggestionSource::ShouldDenyXFrameOptions() const { |
| + return false; |
| +} |