Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/popular_sites_handler.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/files/file_util.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/task_runner_util.h" | |
| 11 #include "base/values.h" | |
| 12 #include "components/ntp_tiles/popular_sites.h" | |
| 13 #include "components/ntp_tiles/pref_names.h" | |
| 14 #include "components/prefs/pref_service.h" | |
| 15 #include "components/url_formatter/url_fixer.h" | |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 std::string ReadFileToString(const base::FilePath& path) { | |
| 21 std::string result; | |
| 22 if (!base::ReadFileToString(path, &result)) | |
| 23 result.clear(); | |
| 24 return result; | |
| 25 } | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 namespace ntp_tiles { | |
| 30 | |
| 31 PopularSitesInternalsMessageHandlerImpl:: | |
| 32 PopularSitesInternalsMessageHandlerImpl( | |
| 33 PopularSitesInternalsMessageHandlerInterface* web_ui) | |
| 34 : web_ui_(web_ui), weak_ptr_factory_(this) {} | |
| 35 | |
| 36 PopularSitesInternalsMessageHandlerImpl:: | |
| 37 ~PopularSitesInternalsMessageHandlerImpl() = default; | |
| 38 | |
| 39 void PopularSitesInternalsMessageHandlerImpl::RegisterMessages() { | |
| 40 web_ui_->RegisterMessageCallback( | |
| 41 "registerForEvents", | |
| 42 base::Bind( | |
| 43 &PopularSitesInternalsMessageHandlerImpl::HandleRegisterForEvents, | |
| 44 base::Unretained(this))); | |
| 45 | |
| 46 web_ui_->RegisterMessageCallback( | |
| 47 "update", | |
| 48 base::Bind(&PopularSitesInternalsMessageHandlerImpl::HandleUpdate, | |
| 49 base::Unretained(this))); | |
| 50 | |
| 51 web_ui_->RegisterMessageCallback( | |
| 52 "viewJson", | |
| 53 base::Bind(&PopularSitesInternalsMessageHandlerImpl::HandleViewJson, | |
| 54 base::Unretained(this))); | |
| 55 } | |
| 56 | |
| 57 void PopularSitesInternalsMessageHandlerImpl::HandleRegisterForEvents( | |
| 58 const base::ListValue* args) { | |
| 59 DCHECK(args->empty()); | |
| 60 | |
| 61 SendOverrides(); | |
| 62 | |
| 63 popular_sites_ = web_ui_->MakePopularSites(); | |
| 64 popular_sites_->StartFetch( | |
| 65 false, | |
| 66 base::Bind( | |
| 67 &PopularSitesInternalsMessageHandlerImpl::OnPopularSitesAvailable, | |
| 68 base::Unretained(this), false)); | |
| 69 } | |
| 70 | |
| 71 void PopularSitesInternalsMessageHandlerImpl::HandleUpdate( | |
| 72 const base::ListValue* args) { | |
| 73 DCHECK_EQ(3u, args->GetSize()); | |
| 74 | |
| 75 PrefService* prefs = web_ui_->GetPrefs(); | |
| 76 | |
| 77 std::string url; | |
| 78 args->GetString(0, &url); | |
| 79 if (url.empty()) | |
| 80 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideURL); | |
| 81 else | |
| 82 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideURL, | |
| 83 url_formatter::FixupURL(url, std::string()).spec()); | |
| 84 | |
| 85 std::string country; | |
| 86 args->GetString(1, &country); | |
| 87 if (country.empty()) | |
| 88 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideCountry); | |
| 89 else | |
| 90 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideCountry, country); | |
| 91 | |
| 92 std::string version; | |
| 93 args->GetString(2, &version); | |
| 94 if (version.empty()) | |
| 95 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion); | |
| 96 else | |
| 97 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideVersion, version); | |
| 98 | |
| 99 popular_sites_ = web_ui_->MakePopularSites(); | |
| 100 popular_sites_->StartFetch( | |
| 101 true, | |
| 102 base::Bind( | |
| 103 &PopularSitesInternalsMessageHandlerImpl::OnPopularSitesAvailable, | |
| 104 base::Unretained(this), true)); | |
| 105 } | |
| 106 | |
| 107 void PopularSitesInternalsMessageHandlerImpl::HandleViewJson( | |
| 108 const base::ListValue* args) { | |
| 109 DCHECK_EQ(0u, args->GetSize()); | |
| 110 | |
| 111 const base::FilePath& path = popular_sites_->local_path(); | |
| 112 base::PostTaskAndReplyWithResult( | |
| 113 web_ui_->GetBlockingPool() | |
| 114 ->GetTaskRunnerWithShutdownBehavior( | |
| 115 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) | |
| 116 .get(), | |
| 117 FROM_HERE, base::Bind(&ReadFileToString, path), | |
| 118 base::Bind(&PopularSitesInternalsMessageHandlerImpl::SendJson, | |
| 119 weak_ptr_factory_.GetWeakPtr())); | |
| 120 } | |
| 121 | |
| 122 void PopularSitesInternalsMessageHandlerImpl::SendOverrides() { | |
| 123 PrefService* prefs = web_ui_->GetPrefs(); | |
| 124 std::string url = | |
| 125 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL); | |
| 126 std::string country = | |
| 127 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry); | |
| 128 std::string version = | |
| 129 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion); | |
| 130 web_ui_->CallJavascriptFunction( | |
| 131 "chrome.popular_sites_internals.receiveOverrides", base::StringValue(url), | |
| 132 base::StringValue(country), base::StringValue(version)); | |
| 133 } | |
| 134 | |
| 135 void PopularSitesInternalsMessageHandlerImpl::SendDownloadResult(bool success) { | |
| 136 base::StringValue result(success ? "Success" : "Fail"); | |
| 137 web_ui_->CallJavascriptFunction( | |
| 138 "chrome.popular_sites_internals.receiveDownloadResult", result); | |
| 139 } | |
| 140 | |
| 141 void PopularSitesInternalsMessageHandlerImpl::SendSites() { | |
| 142 std::unique_ptr<base::ListValue> sites_list(new base::ListValue); | |
|
sdefresne
2016/10/28 13:34:02
Use base::MakeUnique<>
sfiera
2016/10/28 14:12:42
Done. (this is moved code, but might as well impro
| |
| 143 for (const PopularSites::Site& site : popular_sites_->sites()) { | |
| 144 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); | |
|
Bernhard Bauer
2016/10/28 13:23:07
Use base::MakeUnique<>?
sfiera
2016/10/28 14:12:42
Done.
| |
| 145 entry->SetString("title", site.title); | |
| 146 entry->SetString("url", site.url.spec()); | |
| 147 sites_list->Append(std::move(entry)); | |
| 148 } | |
| 149 | |
| 150 base::DictionaryValue result; | |
| 151 result.Set("sites", std::move(sites_list)); | |
| 152 result.SetString("url", popular_sites_->LastURL().spec()); | |
| 153 web_ui_->CallJavascriptFunction("chrome.popular_sites_internals.receiveSites", | |
| 154 result); | |
| 155 } | |
| 156 | |
| 157 void PopularSitesInternalsMessageHandlerImpl::SendJson( | |
| 158 const std::string& json) { | |
| 159 web_ui_->CallJavascriptFunction("chrome.popular_sites_internals.receiveJson", | |
| 160 base::StringValue(json)); | |
| 161 } | |
| 162 | |
| 163 void PopularSitesInternalsMessageHandlerImpl::OnPopularSitesAvailable( | |
| 164 bool explicit_request, | |
| 165 bool success) { | |
| 166 if (explicit_request) | |
| 167 SendDownloadResult(success); | |
| 168 SendSites(); | |
| 169 } | |
| 170 | |
| 171 } // namespace ntp_tiles | |
| OLD | NEW |