OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/suggestions/suggestions_source.h" | 5 #include "chrome/browser/search/suggestions/suggestions_source.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/barrier_closure.h" | 9 #include "base/barrier_closure.h" |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
14 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/search/instant_io_context.h" | |
18 #include "chrome/browser/search/suggestions/suggestions_service.h" | 17 #include "chrome/browser/search/suggestions/suggestions_service.h" |
19 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 18 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
20 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
21 #include "net/base/escape.h" | 20 #include "net/base/escape.h" |
22 #include "net/url_request/url_request.h" | |
23 #include "ui/gfx/codec/png_codec.h" | 21 #include "ui/gfx/codec/png_codec.h" |
24 #include "ui/gfx/image/image_skia.h" | 22 #include "ui/gfx/image/image_skia.h" |
25 #include "url/gurl.h" | 23 #include "url/gurl.h" |
26 | 24 |
27 namespace suggestions { | 25 namespace suggestions { |
28 | 26 |
29 namespace { | 27 namespace { |
30 | 28 |
31 const char kHtmlHeader[] = | 29 const char kHtmlHeader[] = |
32 "<!DOCTYPE html>\n<html>\n<head>\n<title>Suggestions</title>\n" | 30 "<!DOCTYPE html>\n<html>\n<head>\n<title>Suggestions</title>\n" |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 std::string SuggestionsSource::GetMimeType(const std::string& path) const { | 119 std::string SuggestionsSource::GetMimeType(const std::string& path) const { |
122 return "text/html"; | 120 return "text/html"; |
123 } | 121 } |
124 | 122 |
125 base::MessageLoop* SuggestionsSource::MessageLoopForRequestPath( | 123 base::MessageLoop* SuggestionsSource::MessageLoopForRequestPath( |
126 const std::string& path) const { | 124 const std::string& path) const { |
127 // This can be accessed from the IO thread. | 125 // This can be accessed from the IO thread. |
128 return content::URLDataSource::MessageLoopForRequestPath(path); | 126 return content::URLDataSource::MessageLoopForRequestPath(path); |
129 } | 127 } |
130 | 128 |
131 bool SuggestionsSource::ShouldServiceRequest( | |
132 const net::URLRequest* request) const { | |
133 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) | |
134 return InstantIOContext::ShouldServiceRequest(request); | |
135 return URLDataSource::ShouldServiceRequest(request); | |
136 } | |
137 | |
138 void SuggestionsSource::OnSuggestionsAvailable( | 129 void SuggestionsSource::OnSuggestionsAvailable( |
139 const content::URLDataSource::GotDataCallback& callback, | 130 const content::URLDataSource::GotDataCallback& callback, |
140 const SuggestionsProfile& suggestions_profile) { | 131 const SuggestionsProfile& suggestions_profile) { |
141 size_t size = suggestions_profile.suggestions_size(); | 132 size_t size = suggestions_profile.suggestions_size(); |
142 if (!size) { | 133 if (!size) { |
143 std::string output; | 134 std::string output; |
144 RenderOutputHtmlNoSuggestions(&output); | 135 RenderOutputHtmlNoSuggestions(&output); |
145 callback.Run(base::RefCountedString::TakeString(&output)); | 136 callback.Run(base::RefCountedString::TakeString(&output)); |
146 } else { | 137 } else { |
147 RequestContext* context = new RequestContext(suggestions_profile, callback); | 138 RequestContext* context = new RequestContext(suggestions_profile, callback); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 std::string encoded_output; | 174 std::string encoded_output; |
184 base::Base64Encode(std::string(output.begin(), output.end()), | 175 base::Base64Encode(std::string(output.begin(), output.end()), |
185 &encoded_output); | 176 &encoded_output); |
186 context->base64_encoded_pngs[url] = "data:image/png;base64,"; | 177 context->base64_encoded_pngs[url] = "data:image/png;base64,"; |
187 context->base64_encoded_pngs[url] += encoded_output; | 178 context->base64_encoded_pngs[url] += encoded_output; |
188 } | 179 } |
189 barrier.Run(); | 180 barrier.Run(); |
190 } | 181 } |
191 | 182 |
192 } // namespace suggestions | 183 } // namespace suggestions |
OLD | NEW |