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/stringprintf.h" | |
12 #include "base/strings/string_piece.h" | |
13 #include "chrome/browser/profiles/profile.h" | |
14 #include "chrome/browser/search/search.h" | |
15 #include "chrome/common/url_constants.h" | |
16 #include "googleurl/src/gurl.h" | |
17 #include "grit/browser_resources.h" | |
18 #include "net/base/url_util.h" | |
19 #include "net/url_request/url_request.h" | |
20 #include "ui/base/layout.h" | |
21 #include "ui/base/resource/resource_bundle.h" | |
22 | |
23 namespace { | |
24 | |
25 const char kLoaderHtmlPath[] = "/loader.html"; | |
26 const char kLoaderJSPath[] = "/loader.js"; | |
27 const char kResultHtmlPath[] = "/result.html"; | |
28 const char kResultJSPath[] = "/result.js"; | |
29 const char kOriginParam[] = "origin"; | |
30 | |
31 } // namespace | |
32 | |
33 SuggestionSource::SuggestionSource() { | |
34 } | |
35 | |
36 SuggestionSource::~SuggestionSource() { | |
37 } | |
38 | |
39 std::string SuggestionSource::GetSource() { | |
40 return chrome::kChromeSearchSuggestionHost; | |
41 } | |
42 | |
43 void SuggestionSource::StartDataRequest( | |
44 const std::string& path_and_query, | |
45 bool is_incognito, | |
46 const content::URLDataSource::GotDataCallback& callback) { | |
47 std::string path(GURL(chrome::kChromeSearchSuggestionURL + | |
48 path_and_query).path()); | |
49 if (path == kLoaderHtmlPath) | |
50 SendResource(IDR_OMNIBOX_RESULT_LOADER_HTML, callback); | |
51 else if (path == kLoaderJSPath) | |
52 SendJSWithOrigin(IDR_OMNIBOX_RESULT_LOADER_JS, path_and_query, callback); | |
53 else if (path == kResultHtmlPath) | |
54 SendResource(IDR_OMNIBOX_RESULT_HTML, callback); | |
55 else if (path == kResultJSPath) | |
56 SendJSWithOrigin(IDR_OMNIBOX_RESULT_JS, path_and_query, callback); | |
57 else | |
58 callback.Run(NULL); | |
59 } | |
60 | |
61 void SuggestionSource::SendResource( | |
62 int resource_id, | |
63 const content::URLDataSource::GotDataCallback& callback) { | |
64 scoped_refptr<base::RefCountedStaticMemory> response( | |
65 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id)); | |
66 callback.Run(response); | |
67 } | |
68 | |
69 void SuggestionSource::SendJSWithOrigin( | |
70 int resource_id, | |
71 const std::string& path_and_query, | |
72 const content::URLDataSource::GotDataCallback& callback) { | |
73 // &origin is used to check the source of postMessage() requests to suggestion | |
74 // iframes. It is set by ChromeContentRendererClient::WillSendRequest() but | |
75 // validate and escape it anyway. | |
76 std::string origin; | |
77 if (!net::GetValueForKeyInQuery( | |
78 GURL(chrome::kChromeSearchSuggestionURL + path_and_query), | |
79 kOriginParam, &origin) || | |
80 !GURL(origin).GetOrigin().is_valid()) { | |
81 callback.Run(NULL); | |
82 return; | |
83 } | |
84 origin = GURL(origin).GetOrigin().spec(); | |
85 TrimString(origin, "/", &origin); | |
86 | |
87 std::string js_escaped_origin; | |
88 base::JsonDoubleQuote(origin, false, &js_escaped_origin); | |
89 base::StringPiece template_js = | |
90 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); | |
91 std::string response(base::StringPrintf(template_js.as_string().c_str(), | |
palmer
2013/04/09 19:09:55
This mechanism seems a bit over-powered. Given tha
Jered
2013/04/09 21:59:38
How about this?
| |
92 js_escaped_origin.c_str())); | |
93 callback.Run(base::RefCountedString::TakeString(&response)); | |
94 } | |
95 | |
96 std::string SuggestionSource::GetMimeType( | |
97 const std::string& path_and_query) const { | |
98 std::string path(GURL(chrome::kChromeSearchSuggestionURL + | |
99 path_and_query).path()); | |
100 if (path == kLoaderHtmlPath || path == kResultHtmlPath) | |
101 return "text/html"; | |
102 if (path == kLoaderJSPath || path == kResultJSPath) | |
103 return "application/javascript"; | |
104 return ""; | |
105 } | |
106 | |
107 bool SuggestionSource::ShouldServiceRequest( | |
108 const net::URLRequest* request) const { | |
109 const std::string& path = request->url().path(); | |
110 return 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 } | |
OLD | NEW |