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" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/path_service.h" | |
| 13 #include "base/task_runner_util.h" | 14 #include "base/task_runner_util.h" |
| 14 #include "base/values.h" | 15 #include "base/values.h" |
| 15 #include "chrome/browser/android/ntp/popular_sites.h" | 16 #include "chrome/browser/android/ntp/popular_sites.h" |
| 16 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/search_engines/template_url_service_factory.h" | 19 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 20 #include "chrome/common/chrome_paths.h" | |
| 19 #include "components/ntp_tiles/pref_names.h" | 21 #include "components/ntp_tiles/pref_names.h" |
| 20 #include "components/prefs/pref_service.h" | 22 #include "components/prefs/pref_service.h" |
| 21 #include "components/url_formatter/url_fixer.h" | 23 #include "components/url_formatter/url_fixer.h" |
| 22 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/browser/web_ui.h" | 25 #include "content/public/browser/web_ui.h" |
| 24 | 26 |
| 25 namespace { | 27 namespace { |
| 26 | 28 |
| 27 std::string ReadFileToString(const base::FilePath& path) { | 29 std::string ReadFileToString(const base::FilePath& path) { |
| 28 std::string result; | 30 std::string result; |
| 29 if (!base::ReadFileToString(path, &result)) | 31 if (!base::ReadFileToString(path, &result)) |
| 30 result.clear(); | 32 result.clear(); |
| 31 return result; | 33 return result; |
| 32 } | 34 } |
| 33 | 35 |
| 36 base::FilePath GetPopularSitesDirectory() { | |
|
Marc Treib
2016/05/10 08:32:43
Can we move this to a common place somewhere? Mayb
| |
| 37 base::FilePath dir; | |
| 38 PathService::Get(chrome::DIR_USER_DATA, &dir); | |
| 39 return dir; // empty if PathService::Get() failed. | |
| 40 } | |
| 41 | |
| 34 } // namespace | 42 } // namespace |
| 35 | 43 |
| 36 PopularSitesInternalsMessageHandler::PopularSitesInternalsMessageHandler() | 44 PopularSitesInternalsMessageHandler::PopularSitesInternalsMessageHandler() |
| 37 : weak_ptr_factory_(this) {} | 45 : weak_ptr_factory_(this) {} |
| 38 | 46 |
| 39 PopularSitesInternalsMessageHandler::~PopularSitesInternalsMessageHandler() {} | 47 PopularSitesInternalsMessageHandler::~PopularSitesInternalsMessageHandler() {} |
| 40 | 48 |
| 41 void PopularSitesInternalsMessageHandler::RegisterMessages() { | 49 void PopularSitesInternalsMessageHandler::RegisterMessages() { |
| 42 web_ui()->RegisterMessageCallback("registerForEvents", | 50 web_ui()->RegisterMessageCallback("registerForEvents", |
| 43 base::Bind(&PopularSitesInternalsMessageHandler::HandleRegisterForEvents, | 51 base::Bind(&PopularSitesInternalsMessageHandler::HandleRegisterForEvents, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 54 } | 62 } |
| 55 | 63 |
| 56 void PopularSitesInternalsMessageHandler::HandleRegisterForEvents( | 64 void PopularSitesInternalsMessageHandler::HandleRegisterForEvents( |
| 57 const base::ListValue* args) { | 65 const base::ListValue* args) { |
| 58 DCHECK(args->empty()); | 66 DCHECK(args->empty()); |
| 59 | 67 |
| 60 SendOverrides(); | 68 SendOverrides(); |
| 61 | 69 |
| 62 Profile* profile = Profile::FromWebUI(web_ui()); | 70 Profile* profile = Profile::FromWebUI(web_ui()); |
| 63 popular_sites_.reset(new PopularSites( | 71 popular_sites_.reset(new PopularSites( |
| 64 profile->GetPrefs(), | 72 profile->GetPrefs(), TemplateURLServiceFactory::GetForProfile(profile), |
| 65 TemplateURLServiceFactory::GetForProfile(profile), | 73 g_browser_process->variations_service(), profile->GetRequestContext(), |
| 66 g_browser_process->variations_service(), | 74 GetPopularSitesDirectory(), std::string(), std::string(), false, |
| 67 profile->GetRequestContext(), | |
| 68 std::string(), std::string(), false, | |
| 69 base::Bind(&PopularSitesInternalsMessageHandler::OnPopularSitesAvailable, | 75 base::Bind(&PopularSitesInternalsMessageHandler::OnPopularSitesAvailable, |
| 70 base::Unretained(this), false))); | 76 base::Unretained(this), false))); |
| 71 } | 77 } |
| 72 | 78 |
| 73 void PopularSitesInternalsMessageHandler::HandleUpdate( | 79 void PopularSitesInternalsMessageHandler::HandleUpdate( |
| 74 const base::ListValue* args) { | 80 const base::ListValue* args) { |
| 75 DCHECK_EQ(3u, args->GetSize()); | 81 DCHECK_EQ(3u, args->GetSize()); |
| 76 Profile* profile = Profile::FromWebUI(web_ui()); | 82 Profile* profile = Profile::FromWebUI(web_ui()); |
| 77 auto callback = | 83 auto callback = |
| 78 base::Bind(&PopularSitesInternalsMessageHandler::OnPopularSitesAvailable, | 84 base::Bind(&PopularSitesInternalsMessageHandler::OnPopularSitesAvailable, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 91 args->GetString(2, &version); | 97 args->GetString(2, &version); |
| 92 if (version.empty()) | 98 if (version.empty()) |
| 93 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion); | 99 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion); |
| 94 else | 100 else |
| 95 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideVersion, version); | 101 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideVersion, version); |
| 96 | 102 |
| 97 std::string url; | 103 std::string url; |
| 98 args->GetString(0, &url); | 104 args->GetString(0, &url); |
| 99 if (!url.empty()) { | 105 if (!url.empty()) { |
| 100 popular_sites_.reset(new PopularSites( | 106 popular_sites_.reset(new PopularSites( |
| 101 profile->GetPrefs(), | 107 profile->GetPrefs(), TemplateURLServiceFactory::GetForProfile(profile), |
| 102 TemplateURLServiceFactory::GetForProfile(profile), | 108 profile->GetRequestContext(), GetPopularSitesDirectory(), |
| 103 profile->GetRequestContext(), | |
| 104 url_formatter::FixupURL(url, std::string()), callback)); | 109 url_formatter::FixupURL(url, std::string()), callback)); |
| 105 return; | 110 return; |
| 106 } | 111 } |
| 107 | 112 |
| 108 popular_sites_.reset(new PopularSites( | 113 popular_sites_.reset(new PopularSites( |
| 109 profile->GetPrefs(), | 114 profile->GetPrefs(), TemplateURLServiceFactory::GetForProfile(profile), |
| 110 TemplateURLServiceFactory::GetForProfile(profile), | 115 g_browser_process->variations_service(), profile->GetRequestContext(), |
| 111 g_browser_process->variations_service(), | 116 GetPopularSitesDirectory(), std::string(), std::string(), true, |
| 112 profile->GetRequestContext(), | 117 callback)); |
| 113 std::string(), std::string(), true, callback)); | |
| 114 } | 118 } |
| 115 | 119 |
| 116 void PopularSitesInternalsMessageHandler::HandleViewJson( | 120 void PopularSitesInternalsMessageHandler::HandleViewJson( |
| 117 const base::ListValue* args) { | 121 const base::ListValue* args) { |
| 118 DCHECK_EQ(0u, args->GetSize()); | 122 DCHECK_EQ(0u, args->GetSize()); |
| 119 | 123 |
| 120 const base::FilePath& path = popular_sites_->local_path(); | 124 const base::FilePath& path = popular_sites_->local_path(); |
| 121 base::PostTaskAndReplyWithResult( | 125 base::PostTaskAndReplyWithResult( |
| 122 content::BrowserThread::GetBlockingPool() | 126 content::BrowserThread::GetBlockingPool() |
| 123 ->GetTaskRunnerWithShutdownBehavior( | 127 ->GetTaskRunnerWithShutdownBehavior( |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 web_ui()->CallJavascriptFunction("chrome.popular_sites_internals.receiveJson", | 170 web_ui()->CallJavascriptFunction("chrome.popular_sites_internals.receiveJson", |
| 167 base::StringValue(json)); | 171 base::StringValue(json)); |
| 168 } | 172 } |
| 169 | 173 |
| 170 void PopularSitesInternalsMessageHandler::OnPopularSitesAvailable( | 174 void PopularSitesInternalsMessageHandler::OnPopularSitesAvailable( |
| 171 bool explicit_request, bool success) { | 175 bool explicit_request, bool success) { |
| 172 if (explicit_request) | 176 if (explicit_request) |
| 173 SendDownloadResult(success); | 177 SendDownloadResult(success); |
| 174 SendSites(); | 178 SendSites(); |
| 175 } | 179 } |
| OLD | NEW |