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