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 path; |
| 75 args->GetString(1, &path); |
| 76 if (path.empty()) |
| 77 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverridePath); |
| 78 else |
| 79 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverridePath, path); |
| 80 |
74 std::string country; | 81 std::string country; |
75 args->GetString(1, &country); | 82 args->GetString(2, &country); |
76 if (country.empty()) | 83 if (country.empty()) |
77 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideCountry); | 84 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideCountry); |
78 else | 85 else |
79 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideCountry, country); | 86 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideCountry, country); |
80 | 87 |
81 std::string version; | 88 std::string version; |
82 args->GetString(2, &version); | 89 args->GetString(3, &version); |
83 if (version.empty()) | 90 if (version.empty()) |
84 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion); | 91 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion); |
85 else | 92 else |
86 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideVersion, version); | 93 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideVersion, version); |
87 | 94 |
88 popular_sites_ = web_ui_->MakePopularSites(); | 95 popular_sites_ = web_ui_->MakePopularSites(); |
89 popular_sites_->MaybeStartFetch( | 96 popular_sites_->MaybeStartFetch( |
90 true, | 97 true, |
91 base::Bind(&PopularSitesInternalsMessageHandler::OnPopularSitesAvailable, | 98 base::Bind(&PopularSitesInternalsMessageHandler::OnPopularSitesAvailable, |
92 base::Unretained(this))); | 99 base::Unretained(this))); |
93 } | 100 } |
94 | 101 |
95 void PopularSitesInternalsMessageHandler::HandleViewJson( | 102 void PopularSitesInternalsMessageHandler::HandleViewJson( |
96 const base::ListValue* args) { | 103 const base::ListValue* args) { |
97 DCHECK_EQ(0u, args->GetSize()); | 104 DCHECK_EQ(0u, args->GetSize()); |
98 | 105 |
99 const base::ListValue* json = popular_sites_->GetCachedJson(); | 106 const base::ListValue* json = popular_sites_->GetCachedJson(); |
100 std::string json_string; | 107 std::string json_string; |
101 if (json) { | 108 if (json) { |
102 bool success = base::JSONWriter::Write(*json, &json_string); | 109 bool success = base::JSONWriter::Write(*json, &json_string); |
103 DCHECK(success); | 110 DCHECK(success); |
104 } | 111 } |
105 SendJson(json_string); | 112 SendJson(json_string); |
106 } | 113 } |
107 | 114 |
108 void PopularSitesInternalsMessageHandler::SendOverrides() { | 115 void PopularSitesInternalsMessageHandler::SendOverrides() { |
109 PrefService* prefs = web_ui_->GetPrefs(); | 116 PrefService* prefs = web_ui_->GetPrefs(); |
110 std::string url = | 117 std::string url = |
111 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL); | 118 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL); |
| 119 std::string path = |
| 120 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverridePath); |
112 std::string country = | 121 std::string country = |
113 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry); | 122 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry); |
114 std::string version = | 123 std::string version = |
115 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion); | 124 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion); |
116 web_ui_->CallJavascriptFunction( | 125 web_ui_->CallJavascriptFunction( |
117 "chrome.popular_sites_internals.receiveOverrides", base::Value(url), | 126 "chrome.popular_sites_internals.receiveOverrides", base::Value(url), |
118 base::Value(country), base::Value(version)); | 127 base::Value(path), base::Value(country), base::Value(version)); |
119 } | 128 } |
120 | 129 |
121 void PopularSitesInternalsMessageHandler::SendDownloadResult(bool success) { | 130 void PopularSitesInternalsMessageHandler::SendDownloadResult(bool success) { |
122 base::Value result(success ? "Success" : "Fail"); | 131 base::Value result(success ? "Success" : "Fail"); |
123 web_ui_->CallJavascriptFunction( | 132 web_ui_->CallJavascriptFunction( |
124 "chrome.popular_sites_internals.receiveDownloadResult", result); | 133 "chrome.popular_sites_internals.receiveDownloadResult", result); |
125 } | 134 } |
126 | 135 |
127 void PopularSitesInternalsMessageHandler::SendSites() { | 136 void PopularSitesInternalsMessageHandler::SendSites() { |
128 auto sites_list = base::MakeUnique<base::ListValue>(); | 137 auto sites_list = base::MakeUnique<base::ListValue>(); |
(...skipping 16 matching lines...) Expand all Loading... |
145 base::Value(json)); | 154 base::Value(json)); |
146 } | 155 } |
147 | 156 |
148 void PopularSitesInternalsMessageHandler::OnPopularSitesAvailable( | 157 void PopularSitesInternalsMessageHandler::OnPopularSitesAvailable( |
149 bool success) { | 158 bool success) { |
150 SendDownloadResult(success); | 159 SendDownloadResult(success); |
151 SendSites(); | 160 SendSites(); |
152 } | 161 } |
153 | 162 |
154 } // namespace ntp_tiles | 163 } // namespace ntp_tiles |
OLD | NEW |