| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/ntp_tiles/webui/popular_sites_internals_message_handler.h" | 5 #include "components/ntp_tiles/webui/popular_sites_internals_message_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 DCHECK(args->empty()); | 52 DCHECK(args->empty()); |
| 53 | 53 |
| 54 SendOverrides(); | 54 SendOverrides(); |
| 55 | 55 |
| 56 popular_sites_ = web_ui_->MakePopularSites(); | 56 popular_sites_ = web_ui_->MakePopularSites(); |
| 57 SendSites(); | 57 SendSites(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 void PopularSitesInternalsMessageHandler::HandleUpdate( | 60 void PopularSitesInternalsMessageHandler::HandleUpdate( |
| 61 const base::ListValue* args) { | 61 const base::ListValue* args) { |
| 62 DCHECK_EQ(3u, args->GetSize()); | 62 DCHECK_EQ(4u, args->GetSize()); |
| 63 | 63 |
| 64 PrefService* prefs = web_ui_->GetPrefs(); | 64 PrefService* prefs = web_ui_->GetPrefs(); |
| 65 | 65 |
| 66 std::string url; | 66 std::string url; |
| 67 args->GetString(0, &url); | 67 args->GetString(0, &url); |
| 68 if (url.empty()) | 68 if (url.empty()) |
| 69 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideURL); | 69 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideURL); |
| 70 else | 70 else |
| 71 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideURL, | 71 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideURL, |
| 72 url_formatter::FixupURL(url, std::string()).spec()); | 72 url_formatter::FixupURL(url, std::string()).spec()); |
| 73 | 73 |
| 74 std::string directory; |
| 75 args->GetString(1, &directory); |
| 76 if (directory.empty()) |
| 77 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideDirectory); |
| 78 else |
| 79 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideDirectory, |
| 80 directory); |
| 81 |
| 74 std::string country; | 82 std::string country; |
| 75 args->GetString(1, &country); | 83 args->GetString(2, &country); |
| 76 if (country.empty()) | 84 if (country.empty()) |
| 77 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideCountry); | 85 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideCountry); |
| 78 else | 86 else |
| 79 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideCountry, country); | 87 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideCountry, country); |
| 80 | 88 |
| 81 std::string version; | 89 std::string version; |
| 82 args->GetString(2, &version); | 90 args->GetString(3, &version); |
| 83 if (version.empty()) | 91 if (version.empty()) |
| 84 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion); | 92 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion); |
| 85 else | 93 else |
| 86 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideVersion, version); | 94 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideVersion, version); |
| 87 | 95 |
| 88 popular_sites_ = web_ui_->MakePopularSites(); | 96 popular_sites_ = web_ui_->MakePopularSites(); |
| 89 popular_sites_->MaybeStartFetch( | 97 popular_sites_->MaybeStartFetch( |
| 90 true, | 98 true, |
| 91 base::Bind(&PopularSitesInternalsMessageHandler::OnPopularSitesAvailable, | 99 base::Bind(&PopularSitesInternalsMessageHandler::OnPopularSitesAvailable, |
| 92 base::Unretained(this))); | 100 base::Unretained(this))); |
| 93 } | 101 } |
| 94 | 102 |
| 95 void PopularSitesInternalsMessageHandler::HandleViewJson( | 103 void PopularSitesInternalsMessageHandler::HandleViewJson( |
| 96 const base::ListValue* args) { | 104 const base::ListValue* args) { |
| 97 DCHECK_EQ(0u, args->GetSize()); | 105 DCHECK_EQ(0u, args->GetSize()); |
| 98 | 106 |
| 99 const base::ListValue* json = popular_sites_->GetCachedJson(); | 107 const base::ListValue* json = popular_sites_->GetCachedJson(); |
| 100 std::string json_string; | 108 std::string json_string; |
| 101 if (json) { | 109 if (json) { |
| 102 bool success = base::JSONWriter::Write(*json, &json_string); | 110 bool success = base::JSONWriter::Write(*json, &json_string); |
| 103 DCHECK(success); | 111 DCHECK(success); |
| 104 } | 112 } |
| 105 SendJson(json_string); | 113 SendJson(json_string); |
| 106 } | 114 } |
| 107 | 115 |
| 108 void PopularSitesInternalsMessageHandler::SendOverrides() { | 116 void PopularSitesInternalsMessageHandler::SendOverrides() { |
| 109 PrefService* prefs = web_ui_->GetPrefs(); | 117 PrefService* prefs = web_ui_->GetPrefs(); |
| 110 std::string url = | 118 std::string url = |
| 111 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL); | 119 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL); |
| 120 std::string directory = |
| 121 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideDirectory); |
| 112 std::string country = | 122 std::string country = |
| 113 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry); | 123 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry); |
| 114 std::string version = | 124 std::string version = |
| 115 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion); | 125 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion); |
| 116 web_ui_->CallJavascriptFunction( | 126 web_ui_->CallJavascriptFunction( |
| 117 "chrome.popular_sites_internals.receiveOverrides", base::Value(url), | 127 "chrome.popular_sites_internals.receiveOverrides", base::Value(url), |
| 118 base::Value(country), base::Value(version)); | 128 base::Value(directory), base::Value(country), base::Value(version)); |
| 119 } | 129 } |
| 120 | 130 |
| 121 void PopularSitesInternalsMessageHandler::SendDownloadResult(bool success) { | 131 void PopularSitesInternalsMessageHandler::SendDownloadResult(bool success) { |
| 122 base::Value result(success ? "Success" : "Fail"); | 132 base::Value result(success ? "Success" : "Fail"); |
| 123 web_ui_->CallJavascriptFunction( | 133 web_ui_->CallJavascriptFunction( |
| 124 "chrome.popular_sites_internals.receiveDownloadResult", result); | 134 "chrome.popular_sites_internals.receiveDownloadResult", result); |
| 125 } | 135 } |
| 126 | 136 |
| 127 void PopularSitesInternalsMessageHandler::SendSites() { | 137 void PopularSitesInternalsMessageHandler::SendSites() { |
| 128 auto sites_list = base::MakeUnique<base::ListValue>(); | 138 auto sites_list = base::MakeUnique<base::ListValue>(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 145 base::Value(json)); | 155 base::Value(json)); |
| 146 } | 156 } |
| 147 | 157 |
| 148 void PopularSitesInternalsMessageHandler::OnPopularSitesAvailable( | 158 void PopularSitesInternalsMessageHandler::OnPopularSitesAvailable( |
| 149 bool success) { | 159 bool success) { |
| 150 SendDownloadResult(success); | 160 SendDownloadResult(success); |
| 151 SendSites(); | 161 SendSites(); |
| 152 } | 162 } |
| 153 | 163 |
| 154 } // namespace ntp_tiles | 164 } // namespace ntp_tiles |
| OLD | NEW |