Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: components/ntp_tiles/webui/site_tiles_internals_message_handler.cc

Issue 2557103004: Add chrome://site-tiles-internals/ (Closed)
Patch Set: Fix gn check Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/ntp_tiles/webui/site_tiles_internals_message_handler.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/files/file_util.h"
10 #include "base/logging.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/task_runner_util.h"
13 #include "base/values.h"
14 #include "components/ntp_tiles/most_visited_sites.h"
15 #include "components/ntp_tiles/pref_names.h"
16 #include "components/ntp_tiles/webui/site_tiles_internals_message_handler_client .h"
17 #include "components/prefs/pref_service.h"
18 #include "components/url_formatter/url_fixer.h"
19 #include "url/gurl.h"
20
21 namespace {
22
23 std::string ReadFileToString(const base::FilePath& path) {
24 std::string result;
25 if (!base::ReadFileToString(path, &result))
26 result.clear();
Marc Treib 2016/12/08 15:41:50 nit: Does/should our new "always use braces" rule
sfiera 2016/12/08 16:44:35 Yes, I think so. I copy-pasted this function, but
27 return result;
28 }
29
30 } // namespace
31
32 namespace ntp_tiles {
33
34 SiteTilesInternalsMessageHandlerClient::
35 SiteTilesInternalsMessageHandlerClient() = default;
36 SiteTilesInternalsMessageHandlerClient::
37 ~SiteTilesInternalsMessageHandlerClient() = default;
38
39 SiteTilesInternalsMessageHandler::SiteTilesInternalsMessageHandler(
40 SiteTilesInternalsMessageHandlerClient* web_ui)
41 : web_ui_(web_ui), site_count_(8), weak_ptr_factory_(this) {}
Marc Treib 2016/12/08 15:41:50 Any plans to make site_count_ non-constant? If not
sfiera 2016/12/08 16:44:35 It should have that feature, since we sometimes fe
Marc Treib 2016/12/08 17:33:22 I'd argue that as long as it doesn't have that fea
42
43 SiteTilesInternalsMessageHandler::~SiteTilesInternalsMessageHandler() = default;
44
45 void SiteTilesInternalsMessageHandler::RegisterMessages() {
46 web_ui_->RegisterMessageCallback(
47 "registerForEvents",
48 base::Bind(&SiteTilesInternalsMessageHandler::HandleRegisterForEvents,
49 base::Unretained(this)));
50
51 web_ui_->RegisterMessageCallback(
52 "update", base::Bind(&SiteTilesInternalsMessageHandler::HandleUpdate,
53 base::Unretained(this)));
54
55 web_ui_->RegisterMessageCallback(
56 "viewPopularSitesJson",
57 base::Bind(&SiteTilesInternalsMessageHandler::HandleViewPopularSitesJson,
58 base::Unretained(this)));
59
60 web_ui_->RegisterMessageCallback(
61 "getFavicon",
62 base::Bind(&SiteTilesInternalsMessageHandler::HandleGetFavicon,
63 base::Unretained(this)));
64 }
65
66 void SiteTilesInternalsMessageHandler::HandleRegisterForEvents(
67 const base::ListValue* args) {
68 DCHECK(args->empty());
69
70 SendSourceInfo();
71
72 most_visited_sites_ = web_ui_->MakeMostVisitedSites();
73 most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_);
74 }
75
76 void SiteTilesInternalsMessageHandler::HandleUpdate(
77 const base::ListValue* args) {
78 const base::DictionaryValue* dict;
79 DCHECK_EQ(1u, args->GetSize());
80 DCHECK(args->GetDictionary(0, &dict));
81
82 PrefService* prefs = web_ui_->GetPrefs();
83
84 if (web_ui_->IsSourceEnabled(ntp_tiles::NTPTileSource::POPULAR)) {
85 std::string url;
86 dict->GetString("popular.overrideURL", &url);
87 if (url.empty())
88 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideURL);
89 else
90 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideURL,
91 url_formatter::FixupURL(url, std::string()).spec());
92
93 std::string country;
94 dict->GetString("popular.overrideCountry", &country);
95 if (country.empty())
96 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideCountry);
97 else
98 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideCountry, country);
99
100 std::string version;
101 dict->GetString("popular.overrideVersion", &version);
102 if (version.empty())
103 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion);
104 else
105 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideVersion, version);
106 }
107
108 // Recreate to pick up new values.
109 most_visited_sites_ = web_ui_->MakeMostVisitedSites();
110 most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_);
111 SendSourceInfo();
112 }
113
114 void SiteTilesInternalsMessageHandler::HandleViewPopularSitesJson(
115 const base::ListValue* args) {
116 DCHECK_EQ(0u, args->GetSize());
117
118 const base::FilePath& path = web_ui_->MakePopularSites()->local_path();
Marc Treib 2016/12/08 15:41:50 Should we maybe expose the PopularSites instance f
sfiera 2016/12/08 16:44:35 Well, the alternate plan is to make PopularSites l
Marc Treib 2016/12/08 17:33:22 Well, the alternate alternate plan is to make Most
119 base::PostTaskAndReplyWithResult(
120 web_ui_->GetBlockingPool()
121 ->GetTaskRunnerWithShutdownBehavior(
122 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)
123 .get(),
124 FROM_HERE, base::Bind(&ReadFileToString, path),
125 base::Bind(&SiteTilesInternalsMessageHandler::SendPopularSitesJson,
126 weak_ptr_factory_.GetWeakPtr()));
127 }
128
129 void SiteTilesInternalsMessageHandler::HandleGetFavicon(
130 const base::ListValue* args) {
131 DCHECK_EQ(1u, args->GetSize());
132 // TODO(sfiera): implement
133 }
134
135 void SiteTilesInternalsMessageHandler::SendSourceInfo() {
136 PrefService* prefs = web_ui_->GetPrefs();
137 base::DictionaryValue value;
138
139 value.SetBoolean("topSites",
140 web_ui_->IsSourceEnabled(NTPTileSource::TOP_SITES));
141 value.SetBoolean(
142 "suggestionsService",
143 web_ui_->IsSourceEnabled(NTPTileSource::SUGGESTIONS_SERVICE));
144 value.SetBoolean("whitelist",
145 web_ui_->IsSourceEnabled(NTPTileSource::WHITELIST));
146
147 if (web_ui_->IsSourceEnabled(NTPTileSource::POPULAR)) {
148 auto popular_sites = web_ui_->MakePopularSites();
149 value.SetString("popular.url", popular_sites->GetURLToUse().spec());
150 value.SetString("popular.country", popular_sites->GetCountryToUse());
151 value.SetString("popular.version", popular_sites->GetVersionToUse());
152
153 value.SetString(
154 "popular.overrideURL",
155 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL));
156 value.SetString(
157 "popular.overrideCountry",
158 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry));
159 value.SetString(
160 "popular.overrideVersion",
161 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion));
162 } else {
163 value.SetBoolean("popular", false);
Marc Treib 2016/12/08 15:41:50 It doesn't need to be set to true in the enabled c
sfiera 2016/12/08 16:44:35 It is true in the enabled case, because objects ar
Marc Treib 2016/12/08 17:33:22 Ewwww. Oh well.
sfiera 2016/12/08 18:00:48 (I suppose, if you want, the state could be { "t
Marc Treib 2016/12/09 10:52:11 Naaah, it's okay. I just don't like JS :)
164 }
165
166 web_ui_->CallJavascriptFunction(
167 "chrome.site_tiles_internals.receiveSourceInfo", value);
168 }
169
170 void SiteTilesInternalsMessageHandler::SendTiles(const NTPTilesVector& tiles) {
171 auto sites_list = base::MakeUnique<base::ListValue>();
172 for (const NTPTile& tile : tiles) {
173 auto entry = base::MakeUnique<base::DictionaryValue>();
174 entry->SetString("title", tile.title);
175 entry->SetString("url", tile.url.spec());
176 entry->SetInteger("source", static_cast<int>(tile.source));
177 if (tile.source == NTPTileSource::WHITELIST) {
178 entry->SetString("whitelistIconPath",
179 tile.whitelist_icon_path.AsUTF8Unsafe());
Marc Treib 2016/12/08 15:41:50 Huh, contrary to what the comment on NTPTile says,
sfiera 2016/12/08 16:44:35 Done, not done, and done. (set for other sources,
180 }
181 sites_list->Append(std::move(entry));
182 }
183
184 base::DictionaryValue result;
185 result.Set("sites", std::move(sites_list));
186 web_ui_->CallJavascriptFunction("chrome.site_tiles_internals.receiveSites",
187 result);
188 }
189
190 void SiteTilesInternalsMessageHandler::SendPopularSitesJson(
191 const std::string& json) {
192 web_ui_->CallJavascriptFunction(
193 "chrome.site_tiles_internals.receivePopularSitesJson",
194 base::StringValue(json));
195 }
196
197 void SiteTilesInternalsMessageHandler::OnMostVisitedURLsAvailable(
198 const NTPTilesVector& tiles) {
199 SendTiles(tiles);
200 }
201
202 void SiteTilesInternalsMessageHandler::OnIconMadeAvailable(
203 const GURL& site_url) {}
204
205 } // namespace ntp_tiles
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698