Chromium Code Reviews| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 41 const char kHtmlFooter[] = "</body>\n</html>\n"; | 41 const char kHtmlFooter[] = "</body>\n</html>\n"; |
| 42 | 42 |
| 43 const char kRefreshPath[] = "refresh"; | 43 const char kRefreshPath[] = "refresh"; |
| 44 | 44 |
| 45 std::string GetRefreshHtml(bool is_refresh) { | 45 std::string GetRefreshHtml(bool is_refresh) { |
| 46 if (is_refresh) | 46 if (is_refresh) |
| 47 return "<p>Refreshing in the background, reload to see new data.</p>\n"; | 47 return "<p>Refreshing in the background, reload to see new data.</p>\n"; |
| 48 return std::string("<p><a href=\"") + chrome::kChromeUISuggestionsURL + | 48 return std::string("<p><a href=\"") + chrome::kChromeUISuggestionsURL + |
| 49 kRefreshPath + "\">Refresh</a></p>\n"; | 49 kRefreshPath + "\">Refresh</a></p>\n"; |
| 50 } | 50 } |
| 51 | 51 // Returns the HTML needed to display the suggestions. |
| 52 // Fills |output| with the HTML needed to display the suggestions. | 52 std::string RenderOutputHtml( |
|
Marc Treib
2016/03/29 12:14:26
Unrelated cleanup: With C++11, there's no need to
Bernhard Bauer
2016/03/29 12:31:47
Hooray!
| |
| 53 void RenderOutputHtml(bool is_refresh, | 53 bool is_refresh, |
| 54 const SuggestionsProfile& profile, | 54 const SuggestionsProfile& profile, |
| 55 const std::map<GURL, std::string>& base64_encoded_pngs, | 55 const std::map<GURL, std::string>& base64_encoded_pngs) { |
| 56 std::string* output) { | |
| 57 std::vector<std::string> out; | 56 std::vector<std::string> out; |
| 58 out.push_back(kHtmlHeader); | 57 out.push_back(kHtmlHeader); |
| 59 out.push_back(kHtmlBody); | 58 out.push_back(kHtmlBody); |
| 60 out.push_back("<h1>Suggestions</h1>\n"); | 59 out.push_back("<h1>Suggestions</h1>\n"); |
| 61 out.push_back(GetRefreshHtml(is_refresh)); | 60 out.push_back(GetRefreshHtml(is_refresh)); |
| 62 out.push_back("<ul>"); | 61 out.push_back("<ul>"); |
| 63 int64_t now = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch()) | 62 int64_t now = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch()) |
| 64 .ToInternalValue(); | 63 .ToInternalValue(); |
| 65 size_t size = profile.suggestions_size(); | 64 size_t size = profile.suggestions_size(); |
| 66 for (size_t i = 0; i < size; ++i) { | 65 for (size_t i = 0; i < size; ++i) { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 87 line += base::UTF16ToUTF8(remaining_time_formatted); | 86 line += base::UTF16ToUTF8(remaining_time_formatted); |
| 88 std::vector<std::string> providers; | 87 std::vector<std::string> providers; |
| 89 for (int p = 0; p < suggestion.providers_size(); ++p) | 88 for (int p = 0; p < suggestion.providers_size(); ++p) |
| 90 providers.push_back(base::IntToString(suggestion.providers(p))); | 89 providers.push_back(base::IntToString(suggestion.providers(p))); |
| 91 line += ". Provider IDs: " + base::JoinString(providers, ", "); | 90 line += ". Provider IDs: " + base::JoinString(providers, ", "); |
| 92 line += "</li>\n"; | 91 line += "</li>\n"; |
| 93 out.push_back(line); | 92 out.push_back(line); |
| 94 } | 93 } |
| 95 out.push_back("</ul>"); | 94 out.push_back("</ul>"); |
| 96 out.push_back(kHtmlFooter); | 95 out.push_back(kHtmlFooter); |
| 97 *output = base::JoinString(out, base::StringPiece()); | 96 return base::JoinString(out, base::StringPiece()); |
| 98 } | 97 } |
| 99 | 98 |
| 100 // Fills |output| with the HTML needed to display that no suggestions are | 99 // Returns the HTML needed to display that no suggestions are available. |
| 101 // available. | 100 std::string RenderOutputHtmlNoSuggestions(bool is_refresh) { |
| 102 void RenderOutputHtmlNoSuggestions(bool is_refresh, std::string* output) { | |
| 103 std::vector<std::string> out; | 101 std::vector<std::string> out; |
| 104 out.push_back(kHtmlHeader); | 102 out.push_back(kHtmlHeader); |
| 105 out.push_back(kHtmlBody); | 103 out.push_back(kHtmlBody); |
| 106 out.push_back("<h1>Suggestions</h1>\n"); | 104 out.push_back("<h1>Suggestions</h1>\n"); |
| 107 out.push_back("<p>You have no suggestions.</p>\n"); | 105 out.push_back("<p>You have no suggestions.</p>\n"); |
| 108 out.push_back(GetRefreshHtml(is_refresh)); | 106 out.push_back(GetRefreshHtml(is_refresh)); |
| 109 out.push_back(kHtmlFooter); | 107 out.push_back(kHtmlFooter); |
| 110 *output = base::JoinString(out, base::StringPiece()); | 108 return base::JoinString(out, base::StringPiece()); |
| 111 } | 109 } |
| 112 | 110 |
| 113 } // namespace | 111 } // namespace |
| 114 | 112 |
| 115 SuggestionsSource::SuggestionsSource(Profile* profile) | 113 SuggestionsSource::SuggestionsSource(Profile* profile) |
| 116 : profile_(profile), weak_ptr_factory_(this) {} | 114 : profile_(profile), weak_ptr_factory_(this) {} |
| 117 | 115 |
| 118 SuggestionsSource::~SuggestionsSource() {} | 116 SuggestionsSource::~SuggestionsSource() {} |
| 119 | 117 |
| 120 SuggestionsSource::RequestContext::RequestContext( | 118 SuggestionsSource::RequestContext::RequestContext( |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 135 void SuggestionsSource::StartDataRequest( | 133 void SuggestionsSource::StartDataRequest( |
| 136 const std::string& path, int render_process_id, int render_frame_id, | 134 const std::string& path, int render_process_id, int render_frame_id, |
| 137 const content::URLDataSource::GotDataCallback& callback) { | 135 const content::URLDataSource::GotDataCallback& callback) { |
| 138 // If this was called as "chrome://suggestions/refresh", we also trigger an | 136 // If this was called as "chrome://suggestions/refresh", we also trigger an |
| 139 // async update of the suggestions. | 137 // async update of the suggestions. |
| 140 bool is_refresh = (path == kRefreshPath); | 138 bool is_refresh = (path == kRefreshPath); |
| 141 | 139 |
| 142 SuggestionsService* suggestions_service = | 140 SuggestionsService* suggestions_service = |
| 143 SuggestionsServiceFactory::GetForProfile(profile_); | 141 SuggestionsServiceFactory::GetForProfile(profile_); |
| 144 | 142 |
| 143 // |suggestions_service| is null for guest profiles. | |
| 144 if (!suggestions_service) { | |
| 145 std::string output = RenderOutputHtmlNoSuggestions(is_refresh); | |
| 146 callback.Run(base::RefCountedString::TakeString(&output)); | |
| 147 return; | |
| 148 } | |
| 149 | |
| 145 if (is_refresh) | 150 if (is_refresh) |
| 146 suggestions_service->FetchSuggestionsData(); | 151 suggestions_service->FetchSuggestionsData(); |
| 147 | 152 |
| 148 SuggestionsProfile suggestions_profile = | 153 SuggestionsProfile suggestions_profile = |
| 149 suggestions_service->GetSuggestionsDataFromCache(); | 154 suggestions_service->GetSuggestionsDataFromCache(); |
| 150 size_t size = suggestions_profile.suggestions_size(); | 155 size_t size = suggestions_profile.suggestions_size(); |
| 151 if (!size) { | 156 if (!size) { |
| 152 std::string output; | 157 std::string output = RenderOutputHtmlNoSuggestions(is_refresh); |
| 153 RenderOutputHtmlNoSuggestions(is_refresh, &output); | |
| 154 callback.Run(base::RefCountedString::TakeString(&output)); | 158 callback.Run(base::RefCountedString::TakeString(&output)); |
| 155 } else { | 159 } else { |
| 156 RequestContext* context = | 160 RequestContext* context = |
| 157 new RequestContext(is_refresh, suggestions_profile, callback); | 161 new RequestContext(is_refresh, suggestions_profile, callback); |
| 158 base::Closure barrier = BarrierClosure( | 162 base::Closure barrier = BarrierClosure( |
| 159 size, base::Bind(&SuggestionsSource::OnThumbnailsFetched, | 163 size, base::Bind(&SuggestionsSource::OnThumbnailsFetched, |
| 160 weak_ptr_factory_.GetWeakPtr(), context)); | 164 weak_ptr_factory_.GetWeakPtr(), context)); |
| 161 for (size_t i = 0; i < size; ++i) { | 165 for (size_t i = 0; i < size; ++i) { |
| 162 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); | 166 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); |
| 163 // Fetch the thumbnail for this URL (exercising the fetcher). After all | 167 // Fetch the thumbnail for this URL (exercising the fetcher). After all |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 179 | 183 |
| 180 base::MessageLoop* SuggestionsSource::MessageLoopForRequestPath( | 184 base::MessageLoop* SuggestionsSource::MessageLoopForRequestPath( |
| 181 const std::string& path) const { | 185 const std::string& path) const { |
| 182 // This can be accessed from the IO thread. | 186 // This can be accessed from the IO thread. |
| 183 return content::URLDataSource::MessageLoopForRequestPath(path); | 187 return content::URLDataSource::MessageLoopForRequestPath(path); |
| 184 } | 188 } |
| 185 | 189 |
| 186 void SuggestionsSource::OnThumbnailsFetched(RequestContext* context) { | 190 void SuggestionsSource::OnThumbnailsFetched(RequestContext* context) { |
| 187 scoped_ptr<RequestContext> context_deleter(context); | 191 scoped_ptr<RequestContext> context_deleter(context); |
| 188 | 192 |
| 189 std::string output; | 193 std::string output = |
| 190 RenderOutputHtml(context->is_refresh, context->suggestions_profile, | 194 RenderOutputHtml(context->is_refresh, context->suggestions_profile, |
| 191 context->base64_encoded_pngs, &output); | 195 context->base64_encoded_pngs); |
| 192 context->callback.Run(base::RefCountedString::TakeString(&output)); | 196 context->callback.Run(base::RefCountedString::TakeString(&output)); |
| 193 } | 197 } |
| 194 | 198 |
| 195 void SuggestionsSource::OnThumbnailAvailable(RequestContext* context, | 199 void SuggestionsSource::OnThumbnailAvailable(RequestContext* context, |
| 196 base::Closure barrier, | 200 base::Closure barrier, |
| 197 const GURL& url, | 201 const GURL& url, |
| 198 const SkBitmap* bitmap) { | 202 const SkBitmap* bitmap) { |
| 199 if (bitmap) { | 203 if (bitmap) { |
| 200 std::vector<unsigned char> output; | 204 std::vector<unsigned char> output; |
| 201 gfx::PNGCodec::EncodeBGRASkBitmap(*bitmap, false, &output); | 205 gfx::PNGCodec::EncodeBGRASkBitmap(*bitmap, false, &output); |
| 202 | 206 |
| 203 std::string encoded_output; | 207 std::string encoded_output; |
| 204 base::Base64Encode(std::string(output.begin(), output.end()), | 208 base::Base64Encode(std::string(output.begin(), output.end()), |
| 205 &encoded_output); | 209 &encoded_output); |
| 206 context->base64_encoded_pngs[url] = "data:image/png;base64,"; | 210 context->base64_encoded_pngs[url] = "data:image/png;base64,"; |
| 207 context->base64_encoded_pngs[url] += encoded_output; | 211 context->base64_encoded_pngs[url] += encoded_output; |
| 208 } | 212 } |
| 209 barrier.Run(); | 213 barrier.Run(); |
| 210 } | 214 } |
| 211 | 215 |
| 212 } // namespace suggestions | 216 } // namespace suggestions |
| OLD | NEW |