Chromium Code Reviews| Index: chrome/browser/ui/webui/popular_sites_internals_message_handler.cc |
| diff --git a/chrome/browser/ui/webui/popular_sites_internals_message_handler.cc b/chrome/browser/ui/webui/popular_sites_internals_message_handler.cc |
| index 8bdc53dac364aefbdd267371413f7c0ea9b7870a..a112d4900e47ce8014c8050b00f41ca015d58636 100644 |
| --- a/chrome/browser/ui/webui/popular_sites_internals_message_handler.cc |
| +++ b/chrome/browser/ui/webui/popular_sites_internals_message_handler.cc |
| @@ -6,174 +6,55 @@ |
| #include <utility> |
| -#include "base/bind.h" |
| -#include "base/files/file_path.h" |
| -#include "base/files/file_util.h" |
| -#include "base/memory/ref_counted.h" |
| -#include "base/task_runner_util.h" |
| -#include "base/values.h" |
| #include "chrome/browser/android/ntp/popular_sites.h" |
| #include "chrome/browser/browser_process.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/search_engines/template_url_service_factory.h" |
| -#include "components/ntp_tiles/pref_names.h" |
| -#include "components/prefs/pref_service.h" |
| #include "components/safe_json/safe_json_parser.h" |
| -#include "components/url_formatter/url_fixer.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "content/public/browser/web_ui.h" |
| using ntp_tiles::PopularSites; |
| - |
| -namespace { |
| - |
| -std::string ReadFileToString(const base::FilePath& path) { |
| - std::string result; |
| - if (!base::ReadFileToString(path, &result)) |
| - result.clear(); |
| - return result; |
| -} |
| - |
| -} // namespace |
| +using ntp_tiles::PopularSitesInternalsMessageHandlerImpl; |
|
Bernhard Bauer
2016/10/28 13:23:07
Do you need this?
sfiera
2016/10/28 14:12:42
Removed.
|
| PopularSitesInternalsMessageHandler::PopularSitesInternalsMessageHandler() |
| - : weak_ptr_factory_(this) {} |
| + : impl_(this) {} |
| PopularSitesInternalsMessageHandler::~PopularSitesInternalsMessageHandler() {} |
| void PopularSitesInternalsMessageHandler::RegisterMessages() { |
| - web_ui()->RegisterMessageCallback("registerForEvents", |
| - base::Bind(&PopularSitesInternalsMessageHandler::HandleRegisterForEvents, |
| - base::Unretained(this))); |
| - |
| - web_ui()->RegisterMessageCallback("update", |
| - base::Bind(&PopularSitesInternalsMessageHandler::HandleUpdate, |
| - base::Unretained(this))); |
| - |
| - web_ui()->RegisterMessageCallback( |
| - "viewJson", |
| - base::Bind(&PopularSitesInternalsMessageHandler::HandleViewJson, |
| - base::Unretained(this))); |
| + impl_.RegisterMessages(); |
| } |
| -void PopularSitesInternalsMessageHandler::HandleRegisterForEvents( |
| - const base::ListValue* args) { |
| - DCHECK(args->empty()); |
| - |
| - SendOverrides(); |
| +base::SequencedWorkerPool* |
| +PopularSitesInternalsMessageHandler::GetBlockingPool() { |
| + return content::BrowserThread::GetBlockingPool(); |
| +} |
| +std::unique_ptr<PopularSites> |
| +PopularSitesInternalsMessageHandler::MakePopularSites() { |
| Profile* profile = Profile::FromWebUI(web_ui()); |
| - popular_sites_.reset(new PopularSites( |
| + return base::MakeUnique<PopularSites>( |
| content::BrowserThread::GetBlockingPool(), profile->GetPrefs(), |
| TemplateURLServiceFactory::GetForProfile(profile), |
| g_browser_process->variations_service(), profile->GetRequestContext(), |
| ChromePopularSites::GetDirectory(), |
| - base::Bind(safe_json::SafeJsonParser::Parse))); |
| - popular_sites_->StartFetch( |
| - false, |
| - base::Bind(&PopularSitesInternalsMessageHandler::OnPopularSitesAvailable, |
| - base::Unretained(this), false)); |
| + base::Bind(safe_json::SafeJsonParser::Parse)); |
| } |
| -void PopularSitesInternalsMessageHandler::HandleUpdate( |
| - const base::ListValue* args) { |
| - DCHECK_EQ(3u, args->GetSize()); |
| +PrefService* PopularSitesInternalsMessageHandler::GetPrefs() { |
| Profile* profile = Profile::FromWebUI(web_ui()); |
| - auto callback = |
| - base::Bind(&PopularSitesInternalsMessageHandler::OnPopularSitesAvailable, |
| - base::Unretained(this), true); |
| - |
| - PrefService* prefs = profile->GetPrefs(); |
| - |
| - std::string url; |
| - args->GetString(0, &url); |
| - if (url.empty()) |
| - prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideURL); |
| - else |
| - prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideURL, |
| - url_formatter::FixupURL(url, std::string()).spec()); |
| - |
| - std::string country; |
| - args->GetString(1, &country); |
| - if (country.empty()) |
| - prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideCountry); |
| - else |
| - prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideCountry, country); |
| - |
| - std::string version; |
| - args->GetString(2, &version); |
| - if (version.empty()) |
| - prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion); |
| - else |
| - prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideVersion, version); |
| - |
| - popular_sites_.reset(new PopularSites( |
| - content::BrowserThread::GetBlockingPool(), prefs, |
| - TemplateURLServiceFactory::GetForProfile(profile), |
| - g_browser_process->variations_service(), profile->GetRequestContext(), |
| - ChromePopularSites::GetDirectory(), |
| - base::Bind(safe_json::SafeJsonParser::Parse))); |
| - popular_sites_->StartFetch(true, callback); |
| -} |
| - |
| -void PopularSitesInternalsMessageHandler::HandleViewJson( |
| - const base::ListValue* args) { |
| - DCHECK_EQ(0u, args->GetSize()); |
| - |
| - const base::FilePath& path = popular_sites_->local_path(); |
| - base::PostTaskAndReplyWithResult( |
| - content::BrowserThread::GetBlockingPool() |
| - ->GetTaskRunnerWithShutdownBehavior( |
| - base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) |
| - .get(), |
| - FROM_HERE, base::Bind(&ReadFileToString, path), |
| - base::Bind(&PopularSitesInternalsMessageHandler::SendJson, |
| - weak_ptr_factory_.GetWeakPtr())); |
| -} |
| - |
| -void PopularSitesInternalsMessageHandler::SendOverrides() { |
| - PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); |
| - std::string url = |
| - prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL); |
| - std::string country = |
| - prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry); |
| - std::string version = |
| - prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion); |
| - web_ui()->CallJavascriptFunctionUnsafe( |
| - "chrome.popular_sites_internals.receiveOverrides", base::StringValue(url), |
| - base::StringValue(country), base::StringValue(version)); |
| -} |
| - |
| -void PopularSitesInternalsMessageHandler::SendDownloadResult(bool success) { |
| - base::StringValue result(success ? "Success" : "Fail"); |
| - web_ui()->CallJavascriptFunctionUnsafe( |
| - "chrome.popular_sites_internals.receiveDownloadResult", result); |
| -} |
| - |
| -void PopularSitesInternalsMessageHandler::SendSites() { |
| - std::unique_ptr<base::ListValue> sites_list(new base::ListValue); |
| - for (const PopularSites::Site& site : popular_sites_->sites()) { |
| - std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
| - entry->SetString("title", site.title); |
| - entry->SetString("url", site.url.spec()); |
| - sites_list->Append(std::move(entry)); |
| - } |
| - |
| - base::DictionaryValue result; |
| - result.Set("sites", std::move(sites_list)); |
| - result.SetString("url", popular_sites_->LastURL().spec()); |
| - web_ui()->CallJavascriptFunctionUnsafe( |
| - "chrome.popular_sites_internals.receiveSites", result); |
| + return profile->GetPrefs(); |
| } |
| -void PopularSitesInternalsMessageHandler::SendJson(const std::string& json) { |
| - web_ui()->CallJavascriptFunctionUnsafe( |
| - "chrome.popular_sites_internals.receiveJson", base::StringValue(json)); |
| +void PopularSitesInternalsMessageHandler::RegisterMessageCallback( |
| + const std::string& message, |
| + const base::Callback<void(const base::ListValue*)>& callback) { |
| + web_ui()->RegisterMessageCallback(message, callback); |
| } |
| -void PopularSitesInternalsMessageHandler::OnPopularSitesAvailable( |
| - bool explicit_request, bool success) { |
| - if (explicit_request) |
| - SendDownloadResult(success); |
| - SendSites(); |
| +void PopularSitesInternalsMessageHandler::CallJavascriptFunctionVector( |
| + const std::string& name, |
| + const std::vector<const base::Value*>& values) { |
| + web_ui()->CallJavascriptFunctionUnsafe(name, values); |
| } |