| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_ui.h" | 5 #include "chrome/browser/search/suggestions/suggestions_ui.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/barrier_closure.h" | 10 #include "base/barrier_closure.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 22 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 23 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
| 24 #include "chrome/common/url_constants.h" | 24 #include "chrome/common/url_constants.h" |
| 25 #include "components/suggestions/proto/suggestions.pb.h" | 25 #include "components/suggestions/proto/suggestions.pb.h" |
| 26 #include "components/suggestions/suggestions_service.h" | 26 #include "components/suggestions/suggestions_service.h" |
| 27 #include "content/public/browser/url_data_source.h" | 27 #include "content/public/browser/url_data_source.h" |
| 28 #include "net/base/escape.h" | 28 #include "net/base/escape.h" |
| 29 #include "ui/base/l10n/time_format.h" | 29 #include "ui/base/l10n/time_format.h" |
| 30 #include "ui/gfx/codec/png_codec.h" | 30 #include "ui/gfx/codec/png_codec.h" |
| 31 #include "ui/gfx/image/image.h" |
| 31 #include "ui/gfx/image/image_skia.h" | 32 #include "ui/gfx/image/image_skia.h" |
| 32 #include "url/gurl.h" | 33 #include "url/gurl.h" |
| 33 | 34 |
| 34 namespace suggestions { | 35 namespace suggestions { |
| 35 | 36 |
| 36 namespace { | 37 namespace { |
| 37 | 38 |
| 38 const char kHtmlHeader[] = | 39 const char kHtmlHeader[] = |
| 39 "<!DOCTYPE html>\n<html>\n<head>\n<title>Suggestions</title>\n" | 40 "<!DOCTYPE html>\n<html>\n<head>\n<title>Suggestions</title>\n" |
| 40 "<meta charset=\"utf-8\">\n" | 41 "<meta charset=\"utf-8\">\n" |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 ~RequestContext(); | 139 ~RequestContext(); |
| 139 | 140 |
| 140 const bool is_refresh; | 141 const bool is_refresh; |
| 141 const suggestions::SuggestionsProfile suggestions_profile; | 142 const suggestions::SuggestionsProfile suggestions_profile; |
| 142 const content::URLDataSource::GotDataCallback callback; | 143 const content::URLDataSource::GotDataCallback callback; |
| 143 std::map<GURL, std::string> base64_encoded_pngs; | 144 std::map<GURL, std::string> base64_encoded_pngs; |
| 144 }; | 145 }; |
| 145 | 146 |
| 146 // Callback for responses from each Thumbnail request. | 147 // Callback for responses from each Thumbnail request. |
| 147 void OnThumbnailAvailable(RequestContext* context, base::Closure barrier, | 148 void OnThumbnailAvailable(RequestContext* context, base::Closure barrier, |
| 148 const GURL& url, const SkBitmap* bitmap); | 149 const GURL& url, const gfx::Image& image); |
| 149 | 150 |
| 150 // Callback for when all requests are complete. Renders the output webpage and | 151 // Callback for when all requests are complete. Renders the output webpage and |
| 151 // passes the result to the original caller. | 152 // passes the result to the original caller. |
| 152 void OnThumbnailsFetched(RequestContext* context); | 153 void OnThumbnailsFetched(RequestContext* context); |
| 153 | 154 |
| 154 // Only used when servicing requests on the UI thread. | 155 // Only used when servicing requests on the UI thread. |
| 155 Profile* const profile_; | 156 Profile* const profile_; |
| 156 | 157 |
| 157 // For callbacks may be run after destruction. | 158 // For callbacks may be run after destruction. |
| 158 base::WeakPtrFactory<SuggestionsSource> weak_ptr_factory_; | 159 base::WeakPtrFactory<SuggestionsSource> weak_ptr_factory_; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 | 243 |
| 243 std::string output = | 244 std::string output = |
| 244 RenderOutputHtml(context->is_refresh, context->suggestions_profile, | 245 RenderOutputHtml(context->is_refresh, context->suggestions_profile, |
| 245 context->base64_encoded_pngs); | 246 context->base64_encoded_pngs); |
| 246 context->callback.Run(base::RefCountedString::TakeString(&output)); | 247 context->callback.Run(base::RefCountedString::TakeString(&output)); |
| 247 } | 248 } |
| 248 | 249 |
| 249 void SuggestionsSource::OnThumbnailAvailable(RequestContext* context, | 250 void SuggestionsSource::OnThumbnailAvailable(RequestContext* context, |
| 250 base::Closure barrier, | 251 base::Closure barrier, |
| 251 const GURL& url, | 252 const GURL& url, |
| 252 const SkBitmap* bitmap) { | 253 const gfx::Image& image) { |
| 253 if (bitmap) { | 254 if (!image.IsEmpty()) { |
| 254 std::vector<unsigned char> output; | 255 std::vector<unsigned char> output; |
| 255 gfx::PNGCodec::EncodeBGRASkBitmap(*bitmap, false, &output); | 256 gfx::PNGCodec::EncodeBGRASkBitmap(*image.ToSkBitmap(), false, &output); |
| 256 | 257 |
| 257 std::string encoded_output; | 258 std::string encoded_output; |
| 258 base::Base64Encode( | 259 base::Base64Encode( |
| 259 base::StringPiece(reinterpret_cast<const char*>(output.data()), | 260 base::StringPiece(reinterpret_cast<const char*>(output.data()), |
| 260 output.size()), | 261 output.size()), |
| 261 &encoded_output); | 262 &encoded_output); |
| 262 context->base64_encoded_pngs[url] = "data:image/png;base64,"; | 263 context->base64_encoded_pngs[url] = "data:image/png;base64,"; |
| 263 context->base64_encoded_pngs[url] += encoded_output; | 264 context->base64_encoded_pngs[url] += encoded_output; |
| 264 } | 265 } |
| 265 barrier.Run(); | 266 barrier.Run(); |
| 266 } | 267 } |
| 267 | 268 |
| 268 } // namespace | 269 } // namespace |
| 269 | 270 |
| 270 SuggestionsUI::SuggestionsUI(content::WebUI* web_ui) | 271 SuggestionsUI::SuggestionsUI(content::WebUI* web_ui) |
| 271 : content::WebUIController(web_ui) { | 272 : content::WebUIController(web_ui) { |
| 272 Profile* profile = Profile::FromWebUI(web_ui); | 273 Profile* profile = Profile::FromWebUI(web_ui); |
| 273 content::URLDataSource::Add(profile, new SuggestionsSource(profile)); | 274 content::URLDataSource::Add(profile, new SuggestionsSource(profile)); |
| 274 } | 275 } |
| 275 | 276 |
| 276 SuggestionsUI::~SuggestionsUI() {} | 277 SuggestionsUI::~SuggestionsUI() {} |
| 277 | 278 |
| 278 } // namespace suggestions | 279 } // namespace suggestions |
| OLD | NEW |