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