| OLD | NEW |
| 1 // Copyright 2014 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_source.h" | 5 #include "chrome/browser/search/suggestions/suggestions_ui.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <map> |
| 8 #include <stdint.h> | 8 #include <string> |
| 9 | |
| 10 #include <vector> | |
| 11 | 9 |
| 12 #include "base/barrier_closure.h" | 10 #include "base/barrier_closure.h" |
| 13 #include "base/base64.h" | 11 #include "base/base64.h" |
| 14 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback.h" |
| 15 #include "base/memory/ref_counted_memory.h" | 14 #include "base/memory/ref_counted_memory.h" |
| 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
| 17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/string_piece.h" | 18 #include "base/strings/string_piece.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 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/suggestions_service.h" | 26 #include "components/suggestions/suggestions_service.h" |
| 27 #include "content/public/browser/url_data_source.h" |
| 26 #include "net/base/escape.h" | 28 #include "net/base/escape.h" |
| 27 #include "ui/base/l10n/time_format.h" | 29 #include "ui/base/l10n/time_format.h" |
| 28 #include "ui/gfx/codec/png_codec.h" | 30 #include "ui/gfx/codec/png_codec.h" |
| 29 #include "ui/gfx/image/image_skia.h" | 31 #include "ui/gfx/image/image_skia.h" |
| 30 #include "url/gurl.h" | 32 #include "url/gurl.h" |
| 31 | 33 |
| 32 namespace suggestions { | 34 namespace suggestions { |
| 33 | 35 |
| 34 namespace { | 36 namespace { |
| 35 | 37 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 std::vector<std::string> out; | 103 std::vector<std::string> out; |
| 102 out.push_back(kHtmlHeader); | 104 out.push_back(kHtmlHeader); |
| 103 out.push_back(kHtmlBody); | 105 out.push_back(kHtmlBody); |
| 104 out.push_back("<h1>Suggestions</h1>\n"); | 106 out.push_back("<h1>Suggestions</h1>\n"); |
| 105 out.push_back("<p>You have no suggestions.</p>\n"); | 107 out.push_back("<p>You have no suggestions.</p>\n"); |
| 106 out.push_back(GetRefreshHtml(is_refresh)); | 108 out.push_back(GetRefreshHtml(is_refresh)); |
| 107 out.push_back(kHtmlFooter); | 109 out.push_back(kHtmlFooter); |
| 108 return base::JoinString(out, base::StringPiece()); | 110 return base::JoinString(out, base::StringPiece()); |
| 109 } | 111 } |
| 110 | 112 |
| 111 } // namespace | 113 // SuggestionsSource renders a webpage to list SuggestionsService data. |
| 114 class SuggestionsSource : public content::URLDataSource { |
| 115 public: |
| 116 explicit SuggestionsSource(Profile* profile); |
| 117 |
| 118 // content::URLDataSource implementation. |
| 119 std::string GetSource() const override; |
| 120 void StartDataRequest( |
| 121 const std::string& path, |
| 122 int render_process_id, |
| 123 int render_frame_id, |
| 124 const content::URLDataSource::GotDataCallback& callback) override; |
| 125 std::string GetMimeType(const std::string& path) const override; |
| 126 base::MessageLoop* MessageLoopForRequestPath( |
| 127 const std::string& path) const override; |
| 128 |
| 129 private: |
| 130 ~SuggestionsSource() override; |
| 131 |
| 132 // Container for the state of a request. |
| 133 struct RequestContext { |
| 134 RequestContext( |
| 135 bool is_refresh_in, |
| 136 const suggestions::SuggestionsProfile& suggestions_profile_in, |
| 137 const content::URLDataSource::GotDataCallback& callback_in); |
| 138 ~RequestContext(); |
| 139 |
| 140 const bool is_refresh; |
| 141 const suggestions::SuggestionsProfile suggestions_profile; |
| 142 const content::URLDataSource::GotDataCallback callback; |
| 143 std::map<GURL, std::string> base64_encoded_pngs; |
| 144 }; |
| 145 |
| 146 // Callback for responses from each Thumbnail request. |
| 147 void OnThumbnailAvailable(RequestContext* context, base::Closure barrier, |
| 148 const GURL& url, const SkBitmap* bitmap); |
| 149 |
| 150 // Callback for when all requests are complete. Renders the output webpage and |
| 151 // passes the result to the original caller. |
| 152 void OnThumbnailsFetched(RequestContext* context); |
| 153 |
| 154 // Only used when servicing requests on the UI thread. |
| 155 Profile* const profile_; |
| 156 |
| 157 // For callbacks may be run after destruction. |
| 158 base::WeakPtrFactory<SuggestionsSource> weak_ptr_factory_; |
| 159 |
| 160 DISALLOW_COPY_AND_ASSIGN(SuggestionsSource); |
| 161 }; |
| 112 | 162 |
| 113 SuggestionsSource::SuggestionsSource(Profile* profile) | 163 SuggestionsSource::SuggestionsSource(Profile* profile) |
| 114 : profile_(profile), weak_ptr_factory_(this) {} | 164 : profile_(profile), weak_ptr_factory_(this) {} |
| 115 | 165 |
| 116 SuggestionsSource::~SuggestionsSource() {} | 166 SuggestionsSource::~SuggestionsSource() {} |
| 117 | 167 |
| 118 SuggestionsSource::RequestContext::RequestContext( | 168 SuggestionsSource::RequestContext::RequestContext( |
| 119 bool is_refresh_in, | 169 bool is_refresh_in, |
| 120 const SuggestionsProfile& suggestions_profile_in, | 170 const SuggestionsProfile& suggestions_profile_in, |
| 121 const content::URLDataSource::GotDataCallback& callback_in) | 171 const content::URLDataSource::GotDataCallback& callback_in) |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 base::Base64Encode( | 258 base::Base64Encode( |
| 209 base::StringPiece(reinterpret_cast<const char*>(output.data()), | 259 base::StringPiece(reinterpret_cast<const char*>(output.data()), |
| 210 output.size()), | 260 output.size()), |
| 211 &encoded_output); | 261 &encoded_output); |
| 212 context->base64_encoded_pngs[url] = "data:image/png;base64,"; | 262 context->base64_encoded_pngs[url] = "data:image/png;base64,"; |
| 213 context->base64_encoded_pngs[url] += encoded_output; | 263 context->base64_encoded_pngs[url] += encoded_output; |
| 214 } | 264 } |
| 215 barrier.Run(); | 265 barrier.Run(); |
| 216 } | 266 } |
| 217 | 267 |
| 268 } // namespace |
| 269 |
| 270 SuggestionsUI::SuggestionsUI(content::WebUI* web_ui) |
| 271 : content::WebUIController(web_ui) { |
| 272 Profile* profile = Profile::FromWebUI(web_ui); |
| 273 content::URLDataSource::Add(profile, new SuggestionsSource(profile)); |
| 274 } |
| 275 |
| 276 SuggestionsUI::~SuggestionsUI() {} |
| 277 |
| 218 } // namespace suggestions | 278 } // namespace suggestions |
| OLD | NEW |