Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: components/ntp_tiles/webui/popular_sites_internals_message_handler.cc

Issue 2457033003: Add chrome://popular-sites-internals/ to iOS. (Closed)
Patch Set: Minimize ios/content diff. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 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 "chrome/browser/ui/webui/popular_sites_internals_message_handler.h" 5 #include "components/ntp_tiles/webui/popular_sites_internals_message_handler.h"
6
7 #include <utility>
8 6
9 #include "base/bind.h" 7 #include "base/bind.h"
10 #include "base/files/file_path.h" 8 #include "base/callback.h"
11 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
12 #include "base/memory/ref_counted.h" 10 #include "base/logging.h"
11 #include "base/memory/ptr_util.h"
13 #include "base/task_runner_util.h" 12 #include "base/task_runner_util.h"
14 #include "base/values.h" 13 #include "base/values.h"
15 #include "chrome/browser/android/ntp/popular_sites.h" 14 #include "components/ntp_tiles/popular_sites.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h"
19 #include "components/ntp_tiles/pref_names.h" 15 #include "components/ntp_tiles/pref_names.h"
20 #include "components/prefs/pref_service.h" 16 #include "components/prefs/pref_service.h"
21 #include "components/safe_json/safe_json_parser.h"
22 #include "components/url_formatter/url_fixer.h" 17 #include "components/url_formatter/url_fixer.h"
23 #include "content/public/browser/browser_thread.h" 18 #include "url/gurl.h"
24 #include "content/public/browser/web_ui.h"
25
26 using ntp_tiles::PopularSites;
27 19
28 namespace { 20 namespace {
29 21
30 std::string ReadFileToString(const base::FilePath& path) { 22 std::string ReadFileToString(const base::FilePath& path) {
31 std::string result; 23 std::string result;
32 if (!base::ReadFileToString(path, &result)) 24 if (!base::ReadFileToString(path, &result))
33 result.clear(); 25 result.clear();
34 return result; 26 return result;
35 } 27 }
36 28
37 } // namespace 29 } // namespace
38 30
39 PopularSitesInternalsMessageHandler::PopularSitesInternalsMessageHandler() 31 namespace ntp_tiles {
40 : weak_ptr_factory_(this) {}
41 32
42 PopularSitesInternalsMessageHandler::~PopularSitesInternalsMessageHandler() {} 33 PopularSitesInternalsMessageHandlerClient::
34 PopularSitesInternalsMessageHandlerClient() = default;
35 PopularSitesInternalsMessageHandlerClient::
36 ~PopularSitesInternalsMessageHandlerClient() = default;
37
38 PopularSitesInternalsMessageHandler::PopularSitesInternalsMessageHandler(
39 PopularSitesInternalsMessageHandlerClient* web_ui)
40 : web_ui_(web_ui), weak_ptr_factory_(this) {}
41
42 PopularSitesInternalsMessageHandler::~PopularSitesInternalsMessageHandler() =
43 default;
43 44
44 void PopularSitesInternalsMessageHandler::RegisterMessages() { 45 void PopularSitesInternalsMessageHandler::RegisterMessages() {
45 web_ui()->RegisterMessageCallback("registerForEvents", 46 web_ui_->RegisterMessageCallback(
47 "registerForEvents",
46 base::Bind(&PopularSitesInternalsMessageHandler::HandleRegisterForEvents, 48 base::Bind(&PopularSitesInternalsMessageHandler::HandleRegisterForEvents,
47 base::Unretained(this))); 49 base::Unretained(this)));
48 50
49 web_ui()->RegisterMessageCallback("update", 51 web_ui_->RegisterMessageCallback(
50 base::Bind(&PopularSitesInternalsMessageHandler::HandleUpdate, 52 "update", base::Bind(&PopularSitesInternalsMessageHandler::HandleUpdate,
51 base::Unretained(this))); 53 base::Unretained(this)));
52 54
53 web_ui()->RegisterMessageCallback( 55 web_ui_->RegisterMessageCallback(
54 "viewJson", 56 "viewJson",
55 base::Bind(&PopularSitesInternalsMessageHandler::HandleViewJson, 57 base::Bind(&PopularSitesInternalsMessageHandler::HandleViewJson,
56 base::Unretained(this))); 58 base::Unretained(this)));
57 } 59 }
58 60
59 void PopularSitesInternalsMessageHandler::HandleRegisterForEvents( 61 void PopularSitesInternalsMessageHandler::HandleRegisterForEvents(
60 const base::ListValue* args) { 62 const base::ListValue* args) {
61 DCHECK(args->empty()); 63 DCHECK(args->empty());
62 64
63 SendOverrides(); 65 SendOverrides();
64 66
65 Profile* profile = Profile::FromWebUI(web_ui()); 67 popular_sites_ = web_ui_->MakePopularSites();
66 popular_sites_.reset(new PopularSites(
67 content::BrowserThread::GetBlockingPool(), profile->GetPrefs(),
68 TemplateURLServiceFactory::GetForProfile(profile),
69 g_browser_process->variations_service(), profile->GetRequestContext(),
70 ChromePopularSites::GetDirectory(),
71 base::Bind(safe_json::SafeJsonParser::Parse)));
72 popular_sites_->StartFetch( 68 popular_sites_->StartFetch(
73 false, 69 false,
74 base::Bind(&PopularSitesInternalsMessageHandler::OnPopularSitesAvailable, 70 base::Bind(&PopularSitesInternalsMessageHandler::OnPopularSitesAvailable,
75 base::Unretained(this), false)); 71 base::Unretained(this), false));
76 } 72 }
77 73
78 void PopularSitesInternalsMessageHandler::HandleUpdate( 74 void PopularSitesInternalsMessageHandler::HandleUpdate(
79 const base::ListValue* args) { 75 const base::ListValue* args) {
80 DCHECK_EQ(3u, args->GetSize()); 76 DCHECK_EQ(3u, args->GetSize());
81 Profile* profile = Profile::FromWebUI(web_ui());
82 auto callback =
83 base::Bind(&PopularSitesInternalsMessageHandler::OnPopularSitesAvailable,
84 base::Unretained(this), true);
85 77
86 PrefService* prefs = profile->GetPrefs(); 78 PrefService* prefs = web_ui_->GetPrefs();
87 79
88 std::string url; 80 std::string url;
89 args->GetString(0, &url); 81 args->GetString(0, &url);
90 if (url.empty()) 82 if (url.empty())
91 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideURL); 83 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideURL);
92 else 84 else
93 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideURL, 85 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideURL,
94 url_formatter::FixupURL(url, std::string()).spec()); 86 url_formatter::FixupURL(url, std::string()).spec());
95 87
96 std::string country; 88 std::string country;
97 args->GetString(1, &country); 89 args->GetString(1, &country);
98 if (country.empty()) 90 if (country.empty())
99 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideCountry); 91 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideCountry);
100 else 92 else
101 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideCountry, country); 93 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideCountry, country);
102 94
103 std::string version; 95 std::string version;
104 args->GetString(2, &version); 96 args->GetString(2, &version);
105 if (version.empty()) 97 if (version.empty())
106 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion); 98 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion);
107 else 99 else
108 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideVersion, version); 100 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideVersion, version);
109 101
110 popular_sites_.reset(new PopularSites( 102 popular_sites_ = web_ui_->MakePopularSites();
111 content::BrowserThread::GetBlockingPool(), prefs, 103 popular_sites_->StartFetch(
112 TemplateURLServiceFactory::GetForProfile(profile), 104 true,
113 g_browser_process->variations_service(), profile->GetRequestContext(), 105 base::Bind(&PopularSitesInternalsMessageHandler::OnPopularSitesAvailable,
114 ChromePopularSites::GetDirectory(), 106 base::Unretained(this), true));
115 base::Bind(safe_json::SafeJsonParser::Parse)));
116 popular_sites_->StartFetch(true, callback);
117 } 107 }
118 108
119 void PopularSitesInternalsMessageHandler::HandleViewJson( 109 void PopularSitesInternalsMessageHandler::HandleViewJson(
120 const base::ListValue* args) { 110 const base::ListValue* args) {
121 DCHECK_EQ(0u, args->GetSize()); 111 DCHECK_EQ(0u, args->GetSize());
122 112
123 const base::FilePath& path = popular_sites_->local_path(); 113 const base::FilePath& path = popular_sites_->local_path();
124 base::PostTaskAndReplyWithResult( 114 base::PostTaskAndReplyWithResult(
125 content::BrowserThread::GetBlockingPool() 115 web_ui_->GetBlockingPool()
126 ->GetTaskRunnerWithShutdownBehavior( 116 ->GetTaskRunnerWithShutdownBehavior(
127 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) 117 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)
128 .get(), 118 .get(),
129 FROM_HERE, base::Bind(&ReadFileToString, path), 119 FROM_HERE, base::Bind(&ReadFileToString, path),
130 base::Bind(&PopularSitesInternalsMessageHandler::SendJson, 120 base::Bind(&PopularSitesInternalsMessageHandler::SendJson,
131 weak_ptr_factory_.GetWeakPtr())); 121 weak_ptr_factory_.GetWeakPtr()));
132 } 122 }
133 123
134 void PopularSitesInternalsMessageHandler::SendOverrides() { 124 void PopularSitesInternalsMessageHandler::SendOverrides() {
135 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); 125 PrefService* prefs = web_ui_->GetPrefs();
136 std::string url = 126 std::string url =
137 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL); 127 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL);
138 std::string country = 128 std::string country =
139 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry); 129 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry);
140 std::string version = 130 std::string version =
141 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion); 131 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion);
142 web_ui()->CallJavascriptFunctionUnsafe( 132 web_ui_->CallJavascriptFunction(
143 "chrome.popular_sites_internals.receiveOverrides", base::StringValue(url), 133 "chrome.popular_sites_internals.receiveOverrides", base::StringValue(url),
144 base::StringValue(country), base::StringValue(version)); 134 base::StringValue(country), base::StringValue(version));
145 } 135 }
146 136
147 void PopularSitesInternalsMessageHandler::SendDownloadResult(bool success) { 137 void PopularSitesInternalsMessageHandler::SendDownloadResult(bool success) {
148 base::StringValue result(success ? "Success" : "Fail"); 138 base::StringValue result(success ? "Success" : "Fail");
149 web_ui()->CallJavascriptFunctionUnsafe( 139 web_ui_->CallJavascriptFunction(
150 "chrome.popular_sites_internals.receiveDownloadResult", result); 140 "chrome.popular_sites_internals.receiveDownloadResult", result);
151 } 141 }
152 142
153 void PopularSitesInternalsMessageHandler::SendSites() { 143 void PopularSitesInternalsMessageHandler::SendSites() {
154 std::unique_ptr<base::ListValue> sites_list(new base::ListValue); 144 auto sites_list = base::MakeUnique<base::ListValue>();
155 for (const PopularSites::Site& site : popular_sites_->sites()) { 145 for (const PopularSites::Site& site : popular_sites_->sites()) {
156 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); 146 auto entry = base::MakeUnique<base::DictionaryValue>();
157 entry->SetString("title", site.title); 147 entry->SetString("title", site.title);
158 entry->SetString("url", site.url.spec()); 148 entry->SetString("url", site.url.spec());
159 sites_list->Append(std::move(entry)); 149 sites_list->Append(std::move(entry));
160 } 150 }
161 151
162 base::DictionaryValue result; 152 base::DictionaryValue result;
163 result.Set("sites", std::move(sites_list)); 153 result.Set("sites", std::move(sites_list));
164 result.SetString("url", popular_sites_->LastURL().spec()); 154 result.SetString("url", popular_sites_->LastURL().spec());
165 web_ui()->CallJavascriptFunctionUnsafe( 155 web_ui_->CallJavascriptFunction("chrome.popular_sites_internals.receiveSites",
166 "chrome.popular_sites_internals.receiveSites", result); 156 result);
167 } 157 }
168 158
169 void PopularSitesInternalsMessageHandler::SendJson(const std::string& json) { 159 void PopularSitesInternalsMessageHandler::SendJson(const std::string& json) {
170 web_ui()->CallJavascriptFunctionUnsafe( 160 web_ui_->CallJavascriptFunction("chrome.popular_sites_internals.receiveJson",
171 "chrome.popular_sites_internals.receiveJson", base::StringValue(json)); 161 base::StringValue(json));
172 } 162 }
173 163
174 void PopularSitesInternalsMessageHandler::OnPopularSitesAvailable( 164 void PopularSitesInternalsMessageHandler::OnPopularSitesAvailable(
175 bool explicit_request, bool success) { 165 bool explicit_request,
166 bool success) {
176 if (explicit_request) 167 if (explicit_request)
177 SendDownloadResult(success); 168 SendDownloadResult(success);
178 SendSites(); 169 SendSites();
179 } 170 }
171
172 } // namespace ntp_tiles
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698