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