Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / search / iframe_source.cc
blob285638569c19235779699764060f9e692651060d
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.
5 #include "chrome/browser/search/iframe_source.h"
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"
20 IframeSource::IframeSource() {
23 IframeSource::~IframeSource() {
26 std::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 "";
40 bool 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);
49 bool IframeSource::ShouldDenyXFrameOptions() const {
50 return false;
53 bool 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 if (rfh == NULL)
60 return false;
61 content::WebContents* contents =
62 content::WebContents::FromRenderFrameHost(rfh);
63 if (contents == NULL)
64 return false;
65 const content::NavigationEntry* entry =
66 contents->GetController().GetVisibleEntry();
67 if (entry == NULL)
68 return false;
70 *origin = entry->GetURL().GetOrigin().spec();
71 // Origin should not include a trailing slash. That is part of the path.
72 base::TrimString(*origin, "/", origin);
73 return true;
76 void IframeSource::SendResource(
77 int resource_id,
78 const content::URLDataSource::GotDataCallback& callback) {
79 scoped_refptr<base::RefCountedStaticMemory> response(
80 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id));
81 callback.Run(response.get());
84 void IframeSource::SendJSWithOrigin(
85 int resource_id,
86 int render_process_id,
87 int render_frame_id,
88 const content::URLDataSource::GotDataCallback& callback) {
89 std::string origin;
90 if (!GetOrigin(render_process_id, render_frame_id, &origin)) {
91 callback.Run(NULL);
92 return;
95 base::StringPiece template_js =
96 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id);
97 std::string response(template_js.as_string());
98 ReplaceFirstSubstringAfterOffset(&response, 0, "{{ORIGIN}}", origin);
99 callback.Run(base::RefCountedString::TakeString(&response));