// Copyright 2017 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "components/suggestions/webui/suggestions_source.h" #include "base/barrier_closure.h" #include "base/base64.h" #include "base/bind.h" #include "base/strings/strcat.h" #include "base/strings/string16.h" #include "base/strings/string_number_conversions.h" #include "base/strings/string_piece.h" #include "base/strings/string_util.h" #include "base/strings/utf_string_conversions.h" #include "base/time/time.h" #include "components/suggestions/proto/suggestions.pb.h" #include "net/base/escape.h" #include "ui/base/l10n/time_format.h" #include "ui/gfx/codec/png_codec.h" #include "ui/gfx/image/image_skia.h" namespace suggestions { namespace { const char kHtmlHeader[] = "\n\n
\nRefreshing in the background, reload to see new data.
\n"; return std::string("\n"; } // Returns the HTML needed to display the suggestions. std::string RenderOutputHtml(const std::string& base_url, bool is_refresh, const SuggestionsProfile& profile) { std::vectorYou have no suggestions.
\n"); out.push_back(GetRefreshHtml(base_url, is_refresh)); out.push_back(kHtmlFooter); return base::StrCat(out); } } // namespace SuggestionsSource::SuggestionsSource(SuggestionsService* suggestions_service, const std::string& base_url) : suggestions_service_(suggestions_service), base_url_(base_url) {} SuggestionsSource::~SuggestionsSource() {} void SuggestionsSource::StartDataRequest(const std::string& path, GotDataCallback callback) { // If this was called as "chrome://suggestions/refresh", we also trigger an // async update of the suggestions. bool is_refresh = (path == kRefreshPath); // |suggestions_service| is null for guest profiles. if (!suggestions_service_) { std::string output = RenderOutputHtmlNoSuggestions(base_url_, is_refresh); std::move(callback).Run(base::RefCountedString::TakeString(&output)); return; } if (is_refresh) suggestions_service_->FetchSuggestionsData(); SuggestionsProfile suggestions_profile = suggestions_service_->GetSuggestionsDataFromCache().value_or( SuggestionsProfile()); size_t size = suggestions_profile.suggestions_size(); std::string output = !size ? RenderOutputHtmlNoSuggestions(base_url_, is_refresh) : RenderOutputHtml(base_url_, is_refresh, suggestions_profile); std::move(callback).Run(base::RefCountedString::TakeString(&output)); } std::string SuggestionsSource::GetMimeType(const std::string& path) const { return "text/html"; } } // namespace suggestions