iframe_source.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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/iframe_source.h"
6
7#include "base/memory/ref_counted_memory.h"
8#include "base/strings/string_piece.h"
9#include "base/strings/string_util.h"
10#include "chrome/browser/search/instant_io_context.h"
11#include "chrome/common/url_constants.h"
12#include "content/public/browser/navigation_entry.h"
13#include "content/public/browser/render_frame_host.h"
14#include "content/public/browser/web_contents.h"
15#include "grit/browser_resources.h"
16#include "net/url_request/url_request.h"
17#include "ui/base/resource/resource_bundle.h"
18#include "url/gurl.h"
19
20IframeSource::IframeSource() {
21}
22
23IframeSource::~IframeSource() {
24}
25
26std::string IframeSource::GetMimeType(
27    const std::string& path_and_query) const {
28  std::string path(GURL("chrome-search://host/" + path_and_query).path());
29  if (EndsWith(path, ".js", false))
30    return "application/javascript";
31  if (EndsWith(path, ".png", false))
32    return "image/png";
33  if (EndsWith(path, ".css", false))
34    return "text/css";
35  if (EndsWith(path, ".html", false))
36    return "text/html";
37  return "";
38}
39
40bool IframeSource::ShouldServiceRequest(
41    const net::URLRequest* request) const {
42  const std::string& path = request->url().path();
43  return InstantIOContext::ShouldServiceRequest(request) &&
44      request->url().SchemeIs(chrome::kChromeSearchScheme) &&
45      request->url().host() == GetSource() &&
46      ServesPath(path);
47}
48
49bool IframeSource::ShouldDenyXFrameOptions() const {
50  return false;
51}
52
53bool IframeSource::GetOrigin(
54    int render_process_id,
55    int render_frame_id,
56    std::string* origin) const {
57  content::RenderFrameHost* rfh =
58      content::RenderFrameHost::FromID(render_process_id, render_frame_id);
59  content::WebContents* contents =
60      content::WebContents::FromRenderFrameHost(rfh);
61  if (contents == NULL)
62    return false;
63  const content::NavigationEntry* entry =
64      contents->GetController().GetVisibleEntry();
65  if (entry == NULL)
66    return false;
67
68  *origin = entry->GetURL().GetOrigin().spec();
69  // Origin should not include a trailing slash. That is part of the path.
70  base::TrimString(*origin, "/", origin);
71  return true;
72}
73
74void IframeSource::SendResource(
75    int resource_id,
76    const content::URLDataSource::GotDataCallback& callback) {
77  scoped_refptr<base::RefCountedStaticMemory> response(
78      ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id));
79  callback.Run(response.get());
80}
81
82void IframeSource::SendJSWithOrigin(
83    int resource_id,
84    int render_process_id,
85    int render_frame_id,
86    const content::URLDataSource::GotDataCallback& callback) {
87  std::string origin;
88  if (!GetOrigin(render_process_id, render_frame_id, &origin)) {
89    callback.Run(NULL);
90    return;
91  }
92
93  base::StringPiece template_js =
94      ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id);
95  std::string response(template_js.as_string());
96  ReplaceFirstSubstringAfterOffset(&response, 0, "{{ORIGIN}}", origin);
97  callback.Run(base::RefCountedString::TakeString(&response));
98}
99