Commit 3a17e2f6 authored by mathp@chromium.org's avatar mathp@chromium.org

[Search] Adding a URLDataSource for search feature

This implements the first-run flow for the contextual search feature. Resources are being committed in a different CL.

BUG=374932
TEST=going to chrome://contextual-search/

Review URL: https://codereview.chromium.org/375593004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@281818 0039d316-1c4b-4281-b951-d872f2087c98
parent 75e4e889
// Copyright 2014 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/contextual_search_promo_source.h"
#include "base/memory/ref_counted_memory.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "chrome/common/url_constants.h"
#include "grit/browser_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "url/gurl.h"
namespace {
const char kPromoHTMLPath[] = "/promo.html";
const char kPromoCSSPath[] = "/promo.css";
const char kPromoJSPath[] = "/promo.js";
const char kPromoOptInHTMLPath[] = "/optin.html";
const char kPromoOptOutHTMLPath[] = "/optout.html";
} // namespace
ContextualSearchPromoSource::ContextualSearchPromoSource() {}
ContextualSearchPromoSource::~ContextualSearchPromoSource() {}
void ContextualSearchPromoSource::StartDataRequest(
const std::string& path_and_query, int render_process_id,
int render_frame_id,
const content::URLDataSource::GotDataCallback& callback) {
GURL url(std::string(chrome::kChromeUIContextualSearchPromoURL) + "/" +
path_and_query);
std::string path(url.path());
if (path == kPromoHTMLPath) {
SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_HTML, callback);
} else if (path == kPromoCSSPath) {
SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_CSS, callback);
} else if (path == kPromoJSPath) {
SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_JS, callback);
} else if (path == kPromoOptInHTMLPath) {
std::string empty;
callback.Run(base::RefCountedString::TakeString(&empty));
} else if (path == kPromoOptOutHTMLPath) {
std::string empty;
callback.Run(base::RefCountedString::TakeString(&empty));
} else {
callback.Run(NULL);
}
}
std::string ContextualSearchPromoSource::GetSource() const {
return chrome::kChromeUIContextualSearchPromoHost;
}
std::string ContextualSearchPromoSource::GetMimeType(
const std::string& path_and_query) const {
std::string path(GURL("chrome://host/" + path_and_query).path());
if (EndsWith(path, ".js", false)) return "application/javascript";
if (EndsWith(path, ".png", false)) return "image/png";
if (EndsWith(path, ".css", false)) return "text/css";
if (EndsWith(path, ".html", false)) return "text/html";
return "";
}
bool ContextualSearchPromoSource::ShouldDenyXFrameOptions() const {
return false;
}
void ContextualSearchPromoSource::SendResource(
int resource_id, const content::URLDataSource::GotDataCallback& callback) {
scoped_refptr<base::RefCountedStaticMemory> response(
ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id));
callback.Run(response.get());
}
// Copyright 2014 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.
#ifndef CHROME_BROWSER_SEARCH_CONTEXTUAL_SEARCH_PROMO_SOURCE_H_
#define CHROME_BROWSER_SEARCH_CONTEXTUAL_SEARCH_PROMO_SOURCE_H_
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "content/public/browser/url_data_source.h"
// Serves HTML for displaying the contextual search first-run promo.
class ContextualSearchPromoSource : public content::URLDataSource {
public:
ContextualSearchPromoSource();
virtual ~ContextualSearchPromoSource();
protected:
// Overridden from content::URLDataSource:
virtual void StartDataRequest(
const std::string& path_and_query,
int render_process_id,
int render_frame_id,
const content::URLDataSource::GotDataCallback& callback) OVERRIDE;
virtual std::string GetSource() const OVERRIDE;
virtual std::string GetMimeType(
const std::string& path_and_query) const OVERRIDE;
virtual bool ShouldDenyXFrameOptions() const OVERRIDE;
// Sends unmodified resource bytes.
void SendResource(
int resource_id,
const content::URLDataSource::GotDataCallback& callback);
private:
DISALLOW_COPY_AND_ASSIGN(ContextualSearchPromoSource);
};
#endif // CHROME_BROWSER_SEARCH_CONTEXTUAL_SEARCH_PROMO_SOURCE_H_
......@@ -1123,6 +1123,8 @@
'browser/resources_util.h',
'browser/safe_browsing/safe_browsing_tab_observer.cc',
'browser/safe_browsing/safe_browsing_tab_observer.h',
'browser/search/contextual_search_promo_source.cc',
'browser/search/contextual_search_promo_source.h',
'browser/search/hotword_client.h',
'browser/search/hotword_service.cc',
'browser/search/hotword_service.h',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment