OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/webui/ntp_tiles_internals_ui.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 10 #include "chrome/browser/history/top_sites_factory.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/search/suggestions/image_decoder_impl.h" |
| 13 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
| 14 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 15 #include "chrome/common/url_constants.h" |
| 16 #include "components/grit/components_resources.h" |
| 17 #include "components/history/core/browser/top_sites.h" |
| 18 #include "components/image_fetcher/image_fetcher_impl.h" |
| 19 #include "components/ntp_tiles/field_trial.h" |
| 20 #include "components/ntp_tiles/icon_cacher.h" |
| 21 #include "components/ntp_tiles/most_visited_sites.h" |
| 22 #include "components/ntp_tiles/webui/ntp_tiles_internals_message_handler.h" |
| 23 #include "components/ntp_tiles/webui/ntp_tiles_internals_message_handler_client.
h" |
| 24 #include "content/public/browser/browser_thread.h" |
| 25 #include "content/public/browser/web_ui.h" |
| 26 #include "content/public/browser/web_ui_data_source.h" |
| 27 #include "content/public/browser/web_ui_message_handler.h" |
| 28 |
| 29 #if defined(OS_ANDROID) |
| 30 #include "chrome/browser/android/ntp/popular_sites.h" |
| 31 #endif |
| 32 |
| 33 namespace { |
| 34 |
| 35 // The implementation for the chrome://ntp-tiles-internals page. |
| 36 class ChromeNTPTilesInternalsMessageHandlerClient |
| 37 : public content::WebUIMessageHandler, |
| 38 public ntp_tiles::NTPTilesInternalsMessageHandlerClient { |
| 39 public: |
| 40 ChromeNTPTilesInternalsMessageHandlerClient() {} |
| 41 |
| 42 private: |
| 43 // content::WebUIMessageHandler: |
| 44 void RegisterMessages() override; |
| 45 |
| 46 // ntp_tiles::NTPTilesInternalsMessageHandlerClient |
| 47 bool DoesSourceExist(ntp_tiles::NTPTileSource source) override; |
| 48 std::unique_ptr<ntp_tiles::MostVisitedSites> MakeMostVisitedSites() override; |
| 49 std::unique_ptr<ntp_tiles::PopularSites> MakePopularSites() override; |
| 50 PrefService* GetPrefs() override; |
| 51 void RegisterMessageCallback( |
| 52 const std::string& message, |
| 53 const base::Callback<void(const base::ListValue*)>& callback) override; |
| 54 void CallJavascriptFunctionVector( |
| 55 const std::string& name, |
| 56 const std::vector<const base::Value*>& values) override; |
| 57 |
| 58 ntp_tiles::NTPTilesInternalsMessageHandler handler_; |
| 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(ChromeNTPTilesInternalsMessageHandlerClient); |
| 61 }; |
| 62 |
| 63 void ChromeNTPTilesInternalsMessageHandlerClient::RegisterMessages() { |
| 64 handler_.RegisterMessages(this); |
| 65 } |
| 66 |
| 67 bool ChromeNTPTilesInternalsMessageHandlerClient::DoesSourceExist( |
| 68 ntp_tiles::NTPTileSource source) { |
| 69 switch (source) { |
| 70 case ntp_tiles::NTPTileSource::TOP_SITES: |
| 71 case ntp_tiles::NTPTileSource::SUGGESTIONS_SERVICE: |
| 72 return true; |
| 73 #if defined(OS_ANDROID) |
| 74 case ntp_tiles::NTPTileSource::POPULAR: |
| 75 case ntp_tiles::NTPTileSource::WHITELIST: |
| 76 return true; |
| 77 #else |
| 78 case ntp_tiles::NTPTileSource::POPULAR: |
| 79 return false; |
| 80 case ntp_tiles::NTPTileSource::WHITELIST: |
| 81 // TODO(sfiera): support WHITELIST. |
| 82 return false; |
| 83 #endif |
| 84 } |
| 85 NOTREACHED(); |
| 86 return false; |
| 87 } |
| 88 |
| 89 std::unique_ptr<ntp_tiles::MostVisitedSites> |
| 90 ChromeNTPTilesInternalsMessageHandlerClient::MakeMostVisitedSites() { |
| 91 // TODO(sfiera): share with Android and Instant in a factory. |
| 92 auto* profile = Profile::FromWebUI(web_ui()); |
| 93 return base::MakeUnique<ntp_tiles::MostVisitedSites>( |
| 94 GetPrefs(), TopSitesFactory::GetForProfile(profile), |
| 95 suggestions::SuggestionsServiceFactory::GetForProfile(profile), |
| 96 MakePopularSites(), |
| 97 base::MakeUnique<ntp_tiles::IconCacher>( |
| 98 FaviconServiceFactory::GetForProfile( |
| 99 profile, ServiceAccessType::IMPLICIT_ACCESS), |
| 100 base::MakeUnique<image_fetcher::ImageFetcherImpl>( |
| 101 base::MakeUnique<suggestions::ImageDecoderImpl>(), |
| 102 profile->GetRequestContext())), |
| 103 /*supervisor=*/nullptr); |
| 104 } |
| 105 |
| 106 std::unique_ptr<ntp_tiles::PopularSites> |
| 107 ChromeNTPTilesInternalsMessageHandlerClient::MakePopularSites() { |
| 108 #if defined(OS_ANDROID) |
| 109 return ChromePopularSites::NewForProfile(Profile::FromWebUI(web_ui())); |
| 110 #else |
| 111 return nullptr; |
| 112 #endif |
| 113 } |
| 114 |
| 115 PrefService* ChromeNTPTilesInternalsMessageHandlerClient::GetPrefs() { |
| 116 return Profile::FromWebUI(web_ui())->GetPrefs(); |
| 117 } |
| 118 |
| 119 void ChromeNTPTilesInternalsMessageHandlerClient::RegisterMessageCallback( |
| 120 const std::string& message, |
| 121 const base::Callback<void(const base::ListValue*)>& callback) { |
| 122 web_ui()->RegisterMessageCallback(message, callback); |
| 123 } |
| 124 |
| 125 void ChromeNTPTilesInternalsMessageHandlerClient::CallJavascriptFunctionVector( |
| 126 const std::string& name, |
| 127 const std::vector<const base::Value*>& values) { |
| 128 web_ui()->CallJavascriptFunctionUnsafe(name, values); |
| 129 } |
| 130 |
| 131 content::WebUIDataSource* CreateNTPTilesInternalsHTMLSource() { |
| 132 content::WebUIDataSource* source = |
| 133 content::WebUIDataSource::Create(chrome::kChromeUINTPTilesInternalsHost); |
| 134 |
| 135 source->AddResourcePath("ntp_tiles_internals.js", IDR_NTP_TILES_INTERNALS_JS); |
| 136 source->AddResourcePath("ntp_tiles_internals.css", |
| 137 IDR_NTP_TILES_INTERNALS_CSS); |
| 138 source->SetDefaultResource(IDR_NTP_TILES_INTERNALS_HTML); |
| 139 return source; |
| 140 } |
| 141 |
| 142 } // namespace |
| 143 |
| 144 NTPTilesInternalsUI::NTPTilesInternalsUI(content::WebUI* web_ui) |
| 145 : WebUIController(web_ui) { |
| 146 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), |
| 147 CreateNTPTilesInternalsHTMLSource()); |
| 148 web_ui->AddMessageHandler(new ChromeNTPTilesInternalsMessageHandlerClient); |
| 149 } |
| 150 |
| 151 NTPTilesInternalsUI::~NTPTilesInternalsUI() {} |
OLD | NEW |