summaryrefslogtreecommitdiff
path: root/chromium/components/ntp_tiles/webui/ntp_tiles_internals_message_handler.cc
blob: 7cc0047715401e66563dd7bd90028bf13a549b3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright 2016 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/ntp_tiles/webui/ntp_tiles_internals_message_handler.h"

#include "base/bind.h"
#include "base/callback.h"
#include "base/files/file_util.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/task_runner_util.h"
#include "base/values.h"
#include "components/ntp_tiles/most_visited_sites.h"
#include "components/ntp_tiles/pref_names.h"
#include "components/ntp_tiles/webui/ntp_tiles_internals_message_handler_client.h"
#include "components/prefs/pref_service.h"
#include "components/url_formatter/url_fixer.h"
#include "url/gurl.h"

namespace ntp_tiles {

namespace {

std::string FormatJson(const base::Value& value) {
  std::string pretty_printed;
  bool ok = base::JSONWriter::WriteWithOptions(
      value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &pretty_printed);
  DCHECK(ok);
  return pretty_printed;
}

}  // namespace

NTPTilesInternalsMessageHandler::NTPTilesInternalsMessageHandler()
    : client_(nullptr), site_count_(8), weak_ptr_factory_(this) {}

NTPTilesInternalsMessageHandler::~NTPTilesInternalsMessageHandler() = default;

void NTPTilesInternalsMessageHandler::RegisterMessages(
    NTPTilesInternalsMessageHandlerClient* client) {
  client_ = client;

  client_->RegisterMessageCallback(
      "registerForEvents",
      base::Bind(&NTPTilesInternalsMessageHandler::HandleRegisterForEvents,
                 base::Unretained(this)));

  client_->RegisterMessageCallback(
      "update", base::Bind(&NTPTilesInternalsMessageHandler::HandleUpdate,
                           base::Unretained(this)));

  client_->RegisterMessageCallback(
      "fetchSuggestions",
      base::Bind(&NTPTilesInternalsMessageHandler::HandleFetchSuggestions,
                 base::Unretained(this)));

  client_->RegisterMessageCallback(
      "viewPopularSitesJson",
      base::Bind(&NTPTilesInternalsMessageHandler::HandleViewPopularSitesJson,
                 base::Unretained(this)));
}

void NTPTilesInternalsMessageHandler::HandleRegisterForEvents(
    const base::ListValue* args) {
  if (!client_->SupportsNTPTiles()) {
    base::DictionaryValue disabled;
    disabled.SetBoolean("topSites", false);
    disabled.SetBoolean("suggestionsService", false);
    disabled.SetBoolean("popular", false);
    disabled.SetBoolean("whitelist", false);
    client_->CallJavascriptFunction(
        "chrome.ntp_tiles_internals.receiveSourceInfo", disabled);
    SendTiles(NTPTilesVector());
    return;
  }
  DCHECK(args->empty());

  suggestions_status_.clear();
  popular_sites_json_.clear();
  most_visited_sites_ = client_->MakeMostVisitedSites();
  most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_);
  SendSourceInfo();
}

void NTPTilesInternalsMessageHandler::HandleUpdate(
    const base::ListValue* args) {
  if (!client_->SupportsNTPTiles()) {
    return;
  }

  const base::DictionaryValue* dict = nullptr;
  DCHECK_EQ(1u, args->GetSize());
  args->GetDictionary(0, &dict);
  DCHECK(dict);

  PrefService* prefs = client_->GetPrefs();

  if (most_visited_sites_->DoesSourceExist(ntp_tiles::TileSource::POPULAR)) {
    popular_sites_json_.clear();

    std::string url;
    dict->GetString("popular.overrideURL", &url);
    if (url.empty()) {
      prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideURL);
    } else {
      prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideURL,
                       url_formatter::FixupURL(url, std::string()).spec());
    }

    std::string country;
    dict->GetString("popular.overrideCountry", &country);
    if (country.empty()) {
      prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideCountry);
    } else {
      prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideCountry, country);
    }

    std::string version;
    dict->GetString("popular.overrideVersion", &version);
    if (version.empty()) {
      prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion);
    } else {
      prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideVersion, version);
    }
  }

  // Recreate to pick up new values.
  // TODO(sfiera): refresh MostVisitedSites without re-creating it, as soon as
  // that will pick up changes to the Popular Sites overrides.
  most_visited_sites_ = client_->MakeMostVisitedSites();
  most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_);
  SendSourceInfo();
}

void NTPTilesInternalsMessageHandler::HandleFetchSuggestions(
    const base::ListValue* args) {
  DCHECK_EQ(0u, args->GetSize());
  if (!most_visited_sites_->DoesSourceExist(
          ntp_tiles::TileSource::SUGGESTIONS_SERVICE)) {
    return;
  }

  if (most_visited_sites_->suggestions()->FetchSuggestionsData()) {
    suggestions_status_ = "fetching...";
  } else {
    suggestions_status_ = "history sync is disabled, or not yet initialized";
  }
  SendSourceInfo();
}

void NTPTilesInternalsMessageHandler::HandleViewPopularSitesJson(
    const base::ListValue* args) {
  DCHECK_EQ(0u, args->GetSize());
  if (!most_visited_sites_->DoesSourceExist(ntp_tiles::TileSource::POPULAR)) {
    return;
  }

  popular_sites_json_ =
      FormatJson(*most_visited_sites_->popular_sites()->GetCachedJson());
  SendSourceInfo();
}

void NTPTilesInternalsMessageHandler::SendSourceInfo() {
  PrefService* prefs = client_->GetPrefs();
  base::DictionaryValue value;

  value.SetBoolean("topSites",
                   most_visited_sites_->DoesSourceExist(TileSource::TOP_SITES));
  value.SetBoolean("whitelist",
                   most_visited_sites_->DoesSourceExist(TileSource::WHITELIST));

  if (most_visited_sites_->DoesSourceExist(TileSource::SUGGESTIONS_SERVICE)) {
    value.SetString("suggestionsService.status", suggestions_status_);
  } else {
    value.SetBoolean("suggestionsService", false);
  }

  if (most_visited_sites_->DoesSourceExist(TileSource::POPULAR)) {
    auto* popular_sites = most_visited_sites_->popular_sites();
    value.SetString("popular.url", popular_sites->GetURLToFetch().spec());
    value.SetString("popular.country", popular_sites->GetCountryToFetch());
    value.SetString("popular.version", popular_sites->GetVersionToFetch());

    value.SetString(
        "popular.overrideURL",
        prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL));
    value.SetString(
        "popular.overrideCountry",
        prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry));
    value.SetString(
        "popular.overrideVersion",
        prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion));

    value.SetString("popular.json", popular_sites_json_);
  } else {
    value.SetBoolean("popular", false);
  }

  client_->CallJavascriptFunction(
      "chrome.ntp_tiles_internals.receiveSourceInfo", value);
}

void NTPTilesInternalsMessageHandler::SendTiles(const NTPTilesVector& tiles) {
  auto sites_list = base::MakeUnique<base::ListValue>();
  for (const NTPTile& tile : tiles) {
    auto entry = base::MakeUnique<base::DictionaryValue>();
    entry->SetString("title", tile.title);
    entry->SetString("url", tile.url.spec());
    entry->SetInteger("source", static_cast<int>(tile.source));
    entry->SetString("whitelistIconPath",
                     tile.whitelist_icon_path.LossyDisplayName());
    sites_list->Append(std::move(entry));
  }

  base::DictionaryValue result;
  result.Set("sites", std::move(sites_list));
  client_->CallJavascriptFunction("chrome.ntp_tiles_internals.receiveSites",
                                  result);
}

void NTPTilesInternalsMessageHandler::OnMostVisitedURLsAvailable(
    const NTPTilesVector& tiles) {
  SendTiles(tiles);
}

void NTPTilesInternalsMessageHandler::OnIconMadeAvailable(
    const GURL& site_url) {}

}  // namespace ntp_tiles