OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/search/iframe_source.h" | 5 #include "chrome/browser/search/iframe_source.h" |
6 | 6 |
7 #include "base/memory/ref_counted_memory.h" | 7 #include "base/memory/ref_counted_memory.h" |
8 #include "base/strings/string_piece.h" | 8 #include "base/strings/string_piece.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "chrome/browser/search/instant_io_context.h" | 10 #include "chrome/browser/search/instant_io_context.h" |
11 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
12 #include "content/public/browser/render_view_host.h" | 12 #include "content/public/browser/navigation_entry.h" |
| 13 #include "content/public/browser/render_frame_host.h" |
13 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
14 #include "grit/browser_resources.h" | 15 #include "grit/browser_resources.h" |
15 #include "net/url_request/url_request.h" | 16 #include "net/url_request/url_request.h" |
16 #include "ui/base/resource/resource_bundle.h" | 17 #include "ui/base/resource/resource_bundle.h" |
17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
18 | 19 |
19 IframeSource::IframeSource() { | 20 IframeSource::IframeSource() { |
20 } | 21 } |
21 | 22 |
22 IframeSource::~IframeSource() { | 23 IframeSource::~IframeSource() { |
(...skipping 21 matching lines...) Expand all Loading... |
44 request->url().host() == GetSource() && | 45 request->url().host() == GetSource() && |
45 ServesPath(path); | 46 ServesPath(path); |
46 } | 47 } |
47 | 48 |
48 bool IframeSource::ShouldDenyXFrameOptions() const { | 49 bool IframeSource::ShouldDenyXFrameOptions() const { |
49 return false; | 50 return false; |
50 } | 51 } |
51 | 52 |
52 bool IframeSource::GetOrigin( | 53 bool IframeSource::GetOrigin( |
53 int render_process_id, | 54 int render_process_id, |
54 int render_view_id, | 55 int render_frame_id, |
55 std::string* origin) const { | 56 std::string* origin) const { |
56 content::RenderViewHost* rvh = | 57 content::RenderFrameHost* rfh = |
57 content::RenderViewHost::FromID(render_process_id, render_view_id); | 58 content::RenderFrameHost::FromID(render_process_id, render_frame_id); |
58 if (rvh == NULL) | 59 if (rfh == NULL) |
59 return false; | 60 return false; |
60 content::WebContents* contents = | 61 content::WebContents* contents = |
61 content::WebContents::FromRenderViewHost(rvh); | 62 content::WebContents::FromRenderFrameHost(rfh); |
62 if (contents == NULL) | 63 if (contents == NULL) |
63 return false; | 64 return false; |
64 *origin = contents->GetURL().GetOrigin().spec(); | 65 const content::NavigationEntry* entry = |
| 66 contents->GetController().GetVisibleEntry(); |
| 67 if (entry == NULL) |
| 68 return false; |
| 69 |
| 70 *origin = entry->GetURL().GetOrigin().spec(); |
65 // Origin should not include a trailing slash. That is part of the path. | 71 // Origin should not include a trailing slash. That is part of the path. |
66 base::TrimString(*origin, "/", origin); | 72 base::TrimString(*origin, "/", origin); |
67 return true; | 73 return true; |
68 } | 74 } |
69 | 75 |
70 void IframeSource::SendResource( | 76 void IframeSource::SendResource( |
71 int resource_id, | 77 int resource_id, |
72 const content::URLDataSource::GotDataCallback& callback) { | 78 const content::URLDataSource::GotDataCallback& callback) { |
73 scoped_refptr<base::RefCountedStaticMemory> response( | 79 scoped_refptr<base::RefCountedStaticMemory> response( |
74 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id)); | 80 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id)); |
75 callback.Run(response.get()); | 81 callback.Run(response.get()); |
76 } | 82 } |
77 | 83 |
78 void IframeSource::SendJSWithOrigin( | 84 void IframeSource::SendJSWithOrigin( |
79 int resource_id, | 85 int resource_id, |
80 int render_process_id, | 86 int render_process_id, |
81 int render_view_id, | 87 int render_frame_id, |
82 const content::URLDataSource::GotDataCallback& callback) { | 88 const content::URLDataSource::GotDataCallback& callback) { |
83 std::string origin; | 89 std::string origin; |
84 if (!GetOrigin(render_process_id, render_view_id, &origin)) { | 90 if (!GetOrigin(render_process_id, render_frame_id, &origin)) { |
85 callback.Run(NULL); | 91 callback.Run(NULL); |
86 return; | 92 return; |
87 } | 93 } |
88 | 94 |
89 base::StringPiece template_js = | 95 base::StringPiece template_js = |
90 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); | 96 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); |
91 std::string response(template_js.as_string()); | 97 std::string response(template_js.as_string()); |
92 ReplaceFirstSubstringAfterOffset(&response, 0, "{{ORIGIN}}", origin); | 98 ReplaceFirstSubstringAfterOffset(&response, 0, "{{ORIGIN}}", origin); |
93 callback.Run(base::RefCountedString::TakeString(&response)); | 99 callback.Run(base::RefCountedString::TakeString(&response)); |
94 } | 100 } |
OLD | NEW |