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/json/string_escape.h" 8 #include "base/memory/ref_counted_memory.h" 9 #include "base/strings/string_piece.h" 10 #include "base/strings/string_util.h" 11 #include "chrome/browser/search/instant_io_context.h" 12 #include "chrome/common/url_constants.h" 13 #include "content/public/browser/render_view_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 20 IframeSource::IframeSource() { 21 } 22 23 IframeSource::~IframeSource() { 24 } 25 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 ""; 38 } 39 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); 47 } 48 49 bool IframeSource::ShouldDenyXFrameOptions() const { 50 return false; 51 } 52 53 bool IframeSource::GetOrigin( 54 int render_process_id, 55 int render_view_id, 56 std::string* origin) const { 57 content::RenderViewHost* rvh = 58 content::RenderViewHost::FromID(render_process_id, render_view_id); 59 if (rvh == NULL) 60 return false; 61 content::WebContents* contents = 62 content::WebContents::FromRenderViewHost(rvh); 63 if (contents == NULL) 64 return false; 65 *origin = contents->GetURL().GetOrigin().spec(); 66 // Origin should not include a trailing slash. That is part of the path. 67 TrimString(*origin, "/", origin); 68 return true; 69 } 70 71 void IframeSource::SendResource( 72 int resource_id, 73 const content::URLDataSource::GotDataCallback& callback) { 74 scoped_refptr<base::RefCountedStaticMemory> response( 75 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id)); 76 callback.Run(response.get()); 77 } 78 79 void IframeSource::SendJSWithOrigin( 80 int resource_id, 81 int render_process_id, 82 int render_view_id, 83 const content::URLDataSource::GotDataCallback& callback) { 84 std::string origin; 85 if (!GetOrigin(render_process_id, render_view_id, &origin)) { 86 callback.Run(NULL); 87 return; 88 } 89 90 std::string js_escaped_origin; 91 base::JsonDoubleQuote(origin, false, &js_escaped_origin); 92 base::StringPiece template_js = 93 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); 94 std::string response(template_js.as_string()); 95 ReplaceFirstSubstringAfterOffset(&response, 0, "{{ORIGIN}}", origin); 96 callback.Run(base::RefCountedString::TakeString(&response)); 97 } 98