OLD | NEW |
---|---|
(Empty) | |
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/suggestion_source.h" | |
6 | |
7 #include "base/json/string_escape.h" | |
8 #include "base/logging.h" | |
9 #include "base/memory/ref_counted_memory.h" | |
10 #include "base/string_util.h" | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "base/strings/string_piece.h" | |
13 #include "chrome/browser/profiles/profile.h" | |
14 #include "chrome/browser/search/instant_io_context.h" | |
15 #include "chrome/browser/search/search.h" | |
16 #include "chrome/common/url_constants.h" | |
17 #include "content/public/browser/render_view_host.h" | |
18 #include "content/public/browser/resource_request_info.h" | |
19 #include "content/public/browser/web_contents.h" | |
20 #include "googleurl/src/gurl.h" | |
21 #include "grit/browser_resources.h" | |
22 #include "net/base/url_util.h" | |
23 #include "net/url_request/url_request.h" | |
24 #include "ui/base/layout.h" | |
25 #include "ui/base/resource/resource_bundle.h" | |
26 | |
27 namespace { | |
28 | |
29 const char kLoaderHtmlPath[] = "/loader.html"; | |
30 const char kLoaderJSPath[] = "/loader.js"; | |
31 const char kResultHtmlPath[] = "/result.html"; | |
32 const char kResultJSPath[] = "/result.js"; | |
33 | |
34 } // namespace | |
35 | |
36 SuggestionSource::SuggestionSource() { | |
37 } | |
38 | |
39 SuggestionSource::~SuggestionSource() { | |
40 } | |
41 | |
42 std::string SuggestionSource::GetSource() const { | |
43 return chrome::kChromeSearchSuggestionHost; | |
44 } | |
45 | |
46 void SuggestionSource::StartDataRequest( | |
47 const std::string& path_and_query, | |
48 int render_process_id, | |
49 int render_view_id, | |
50 const content::URLDataSource::GotDataCallback& callback) { | |
51 std::string path(GURL(chrome::kChromeSearchSuggestionUrl + | |
52 path_and_query).path()); | |
53 if (path == kLoaderHtmlPath) | |
54 SendResource(IDR_OMNIBOX_RESULT_LOADER_HTML, callback); | |
55 else if (path == kLoaderJSPath) | |
56 SendJSWithOrigin(IDR_OMNIBOX_RESULT_LOADER_JS, render_process_id, | |
palmer
2013/04/22 19:38:37
Definitely want to use { ... } when the statement
Jered
2013/04/22 23:02:58
Done.
| |
57 render_view_id, callback); | |
58 else if (path == kResultHtmlPath) | |
59 SendResource(IDR_OMNIBOX_RESULT_HTML, callback); | |
60 else if (path == kResultJSPath) | |
61 SendJSWithOrigin(IDR_OMNIBOX_RESULT_JS, render_process_id, render_view_id, | |
62 callback); | |
63 else | |
64 callback.Run(NULL); | |
65 } | |
66 | |
67 void SuggestionSource::SendResource( | |
68 int resource_id, | |
69 const content::URLDataSource::GotDataCallback& callback) { | |
70 scoped_refptr<base::RefCountedStaticMemory> response( | |
71 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id)); | |
72 callback.Run(response); | |
73 } | |
74 | |
75 void SuggestionSource::SendJSWithOrigin( | |
76 int resource_id, | |
77 int render_process_id, | |
78 int render_view_id, | |
79 const content::URLDataSource::GotDataCallback& callback) { | |
80 std::string origin; | |
81 if (!GetOrigin(render_process_id, render_view_id, &origin)) { | |
82 callback.Run(NULL); | |
83 return; | |
84 } | |
85 | |
86 std::string js_escaped_origin; | |
87 base::JsonDoubleQuote(origin, false, &js_escaped_origin); | |
88 base::StringPiece template_js = | |
89 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); | |
90 std::string response(template_js.as_string()); | |
91 ReplaceFirstSubstringAfterOffset(&response, 0, "{{ORIGIN}}", origin); | |
palmer
2013/04/22 19:38:37
Inside the JS code, an origin check will happen. P
Jered
2013/04/22 23:02:58
No, we do not know the origin for the postMessage(
| |
92 callback.Run(base::RefCountedString::TakeString(&response)); | |
93 } | |
94 | |
95 std::string SuggestionSource::GetMimeType( | |
96 const std::string& path_and_query) const { | |
97 std::string path(GURL(chrome::kChromeSearchSuggestionUrl + | |
98 path_and_query).path()); | |
99 if (path == kLoaderHtmlPath || path == kResultHtmlPath) | |
100 return "text/html"; | |
101 if (path == kLoaderJSPath || path == kResultJSPath) | |
102 return "application/javascript"; | |
103 return ""; | |
104 } | |
105 | |
106 bool SuggestionSource::ShouldServiceRequest( | |
107 const net::URLRequest* request) const { | |
108 const std::string& path = request->url().path(); | |
109 return InstantIOContext::ShouldServiceRequest(request) && | |
110 request->url().SchemeIs(chrome::kChromeSearchScheme) && | |
111 request->url().host() == chrome::kChromeSearchSuggestionHost && | |
112 (path == kLoaderHtmlPath || path == kLoaderJSPath || | |
113 path == kResultHtmlPath || path == kResultJSPath); | |
114 } | |
115 | |
116 bool SuggestionSource::ShouldDenyXFrameOptions() const { | |
117 return false; | |
118 } | |
119 | |
120 bool SuggestionSource::GetOrigin( | |
121 int render_process_id, | |
122 int render_view_id, | |
123 std::string* origin) const { | |
124 content::RenderViewHost* rvh = | |
125 content::RenderViewHost::FromID(render_process_id, render_view_id); | |
126 if (rvh == NULL) | |
127 return false; | |
128 content::WebContents* contents = | |
129 content::WebContents::FromRenderViewHost(rvh); | |
130 if (contents == NULL) | |
131 return false; | |
132 *origin = contents->GetURL().GetOrigin().spec(); | |
133 // Origin should not include a trailing slash. That is part of the path. | |
134 TrimString(*origin, "/", origin); | |
135 return true; | |
136 } | |
OLD | NEW |