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/site_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/site_tiles_internals_message_handler.h" | |
23 #include "components/ntp_tiles/webui/site_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://site-tiles-internals page. | |
36 class ChromeSiteTilesInternalsMessageHandlerClient | |
37 : public content::WebUIMessageHandler, | |
38 public ntp_tiles::SiteTilesInternalsMessageHandlerClient { | |
39 public: | |
40 ChromeSiteTilesInternalsMessageHandlerClient() {} | |
41 | |
42 private: | |
43 // content::WebUIMessageHandler: | |
44 void RegisterMessages() override; | |
45 | |
46 // ntp_tiles::SiteTilesInternalsMessageHandlerClient | |
47 bool IsSourceEnabled(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::SiteTilesInternalsMessageHandler handler_; | |
59 | |
60 DISALLOW_COPY_AND_ASSIGN(ChromeSiteTilesInternalsMessageHandlerClient); | |
61 }; | |
62 | |
63 void ChromeSiteTilesInternalsMessageHandlerClient::RegisterMessages() { | |
64 handler_.RegisterMessages(this); | |
65 } | |
66 | |
67 bool ChromeSiteTilesInternalsMessageHandlerClient::IsSourceEnabled( | |
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 return ntp_tiles::ShouldShowPopularSites(); | |
76 case ntp_tiles::NTPTileSource::WHITELIST: | |
77 return true; | |
78 #else | |
79 case ntp_tiles::NTPTileSource::POPULAR: | |
80 return false; | |
81 case ntp_tiles::NTPTileSource::WHITELIST: | |
82 // TODO(sfiera): support WHITELIST. | |
Marc Treib
2016/12/12 09:42:18
I guess this is because desktop doesn't pass in th
sfiera
2016/12/12 10:09:50
Right.
I guess we're not going to see WHITELIST t
| |
83 return false; | |
84 #endif | |
85 } | |
86 NOTREACHED(); | |
87 return false; | |
88 } | |
89 | |
90 std::unique_ptr<ntp_tiles::MostVisitedSites> | |
91 ChromeSiteTilesInternalsMessageHandlerClient::MakeMostVisitedSites() { | |
92 // TODO(sfiera): share with Android and Instant in a factory. | |
93 auto* profile = Profile::FromWebUI(web_ui()); | |
94 return base::MakeUnique<ntp_tiles::MostVisitedSites>( | |
95 GetPrefs(), TopSitesFactory::GetForProfile(profile), | |
96 suggestions::SuggestionsServiceFactory::GetForProfile(profile), | |
97 MakePopularSites(), | |
98 base::MakeUnique<ntp_tiles::IconCacher>( | |
99 FaviconServiceFactory::GetForProfile( | |
100 profile, ServiceAccessType::IMPLICIT_ACCESS), | |
101 base::MakeUnique<image_fetcher::ImageFetcherImpl>( | |
102 base::MakeUnique<suggestions::ImageDecoderImpl>(), | |
103 profile->GetRequestContext())), | |
104 /*supervisor=*/nullptr); | |
105 } | |
106 | |
107 std::unique_ptr<ntp_tiles::PopularSites> | |
108 ChromeSiteTilesInternalsMessageHandlerClient::MakePopularSites() { | |
109 #if defined(OS_ANDROID) | |
110 return ChromePopularSites::NewForProfile(Profile::FromWebUI(web_ui())); | |
111 #else | |
112 return nullptr; | |
113 #endif | |
114 } | |
115 | |
116 PrefService* ChromeSiteTilesInternalsMessageHandlerClient::GetPrefs() { | |
117 return Profile::FromWebUI(web_ui())->GetPrefs(); | |
118 } | |
119 | |
120 void ChromeSiteTilesInternalsMessageHandlerClient::RegisterMessageCallback( | |
121 const std::string& message, | |
122 const base::Callback<void(const base::ListValue*)>& callback) { | |
123 web_ui()->RegisterMessageCallback(message, callback); | |
124 } | |
125 | |
126 void ChromeSiteTilesInternalsMessageHandlerClient::CallJavascriptFunctionVector( | |
127 const std::string& name, | |
128 const std::vector<const base::Value*>& values) { | |
129 web_ui()->CallJavascriptFunctionUnsafe(name, values); | |
130 } | |
131 | |
132 content::WebUIDataSource* CreateSiteTilesInternalsHTMLSource() { | |
133 content::WebUIDataSource* source = | |
134 content::WebUIDataSource::Create(chrome::kChromeUISiteTilesInternalsHost); | |
135 | |
136 source->AddResourcePath("site_tiles_internals.js", | |
137 IDR_SITE_TILES_INTERNALS_JS); | |
138 source->AddResourcePath("site_tiles_internals.css", | |
139 IDR_SITE_TILES_INTERNALS_CSS); | |
140 source->SetDefaultResource(IDR_SITE_TILES_INTERNALS_HTML); | |
141 return source; | |
142 } | |
143 | |
144 } // namespace | |
145 | |
146 SiteTilesInternalsUI::SiteTilesInternalsUI(content::WebUI* web_ui) | |
147 : WebUIController(web_ui) { | |
148 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), | |
149 CreateSiteTilesInternalsHTMLSource()); | |
150 web_ui->AddMessageHandler(new ChromeSiteTilesInternalsMessageHandlerClient); | |
151 } | |
152 | |
153 SiteTilesInternalsUI::~SiteTilesInternalsUI() {} | |
OLD | NEW |