OLD | NEW |
---|---|
(Empty) | |
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 | |
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/browser_process.h" | |
Marc Treib
2016/12/08 15:41:49
Needed?
sfiera
2016/12/08 16:44:34
Removed.
| |
10 #include "chrome/browser/favicon/favicon_service_factory.h" | |
11 #include "chrome/browser/history/top_sites_factory.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/browser/search/suggestions/image_decoder_impl.h" | |
14 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | |
15 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
16 #include "chrome/common/url_constants.h" | |
17 #include "components/grit/components_resources.h" | |
18 #include "components/history/core/browser/top_sites.h" | |
19 #include "components/image_fetcher/image_fetcher_impl.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 ChromeSiteTilesInternalsMessageHandlerBridge | |
Marc Treib
2016/12/08 15:41:49
s/Bridge/Client/ ?
sfiera
2016/12/08 16:44:34
Done.
| |
37 : public content::WebUIMessageHandler, | |
38 public ntp_tiles::SiteTilesInternalsMessageHandlerClient { | |
39 public: | |
40 ChromeSiteTilesInternalsMessageHandlerBridge() : handler_(this) {} | |
Marc Treib
2016/12/08 15:41:49
This is a bit dangerous - if the handler does anyt
sfiera
2016/12/08 16:44:34
Sure, done.
| |
41 | |
42 private: | |
43 // content::WebUIMessageHandler: | |
44 void RegisterMessages() override; | |
45 | |
46 // ntp_tiles::SiteTilesInternalsMessageHandlerClient | |
47 base::SequencedWorkerPool* GetBlockingPool() override; | |
48 bool IsSourceEnabled(ntp_tiles::NTPTileSource source) override; | |
49 std::unique_ptr<ntp_tiles::MostVisitedSites> MakeMostVisitedSites() override; | |
50 std::unique_ptr<ntp_tiles::PopularSites> MakePopularSites() override; | |
51 PrefService* GetPrefs() override; | |
52 void RegisterMessageCallback( | |
53 const std::string& message, | |
54 const base::Callback<void(const base::ListValue*)>& callback) override; | |
55 void CallJavascriptFunctionVector( | |
56 const std::string& name, | |
57 const std::vector<const base::Value*>& values) override; | |
58 | |
59 ntp_tiles::SiteTilesInternalsMessageHandler handler_; | |
60 | |
61 DISALLOW_COPY_AND_ASSIGN(ChromeSiteTilesInternalsMessageHandlerBridge); | |
62 }; | |
63 | |
64 void ChromeSiteTilesInternalsMessageHandlerBridge::RegisterMessages() { | |
65 handler_.RegisterMessages(); | |
66 } | |
67 | |
68 base::SequencedWorkerPool* | |
69 ChromeSiteTilesInternalsMessageHandlerBridge::GetBlockingPool() { | |
70 return content::BrowserThread::GetBlockingPool(); | |
71 } | |
72 | |
73 bool ChromeSiteTilesInternalsMessageHandlerBridge::IsSourceEnabled( | |
74 ntp_tiles::NTPTileSource source) { | |
75 switch (source) { | |
76 case ntp_tiles::NTPTileSource::TOP_SITES: | |
77 case ntp_tiles::NTPTileSource::SUGGESTIONS_SERVICE: | |
78 #if defined(OS_ANDROID) | |
79 case ntp_tiles::NTPTileSource::POPULAR: | |
80 #endif | |
81 return true; | |
82 default: | |
Marc Treib
2016/12/08 15:41:49
What about WHITELIST?
Can we get rid of the defaul
sfiera
2016/12/08 16:44:34
Done.
| |
83 return false; | |
84 } | |
85 } | |
86 | |
87 std::unique_ptr<ntp_tiles::MostVisitedSites> | |
88 ChromeSiteTilesInternalsMessageHandlerBridge::MakeMostVisitedSites() { | |
89 // TODO(sfiera): share with Android and Instant in a factory. | |
90 auto* profile = Profile::FromWebUI(web_ui()); | |
91 return base::MakeUnique<ntp_tiles::MostVisitedSites>( | |
92 GetPrefs(), TopSitesFactory::GetForProfile(profile), | |
93 suggestions::SuggestionsServiceFactory::GetForProfile(profile), | |
94 MakePopularSites(), | |
95 base::MakeUnique<ntp_tiles::IconCacher>( | |
96 FaviconServiceFactory::GetForProfile( | |
97 profile, ServiceAccessType::IMPLICIT_ACCESS), | |
98 base::MakeUnique<image_fetcher::ImageFetcherImpl>( | |
99 base::MakeUnique<suggestions::ImageDecoderImpl>(), | |
100 profile->GetRequestContext())), | |
101 /*supervisor=*/nullptr); | |
102 } | |
103 | |
104 std::unique_ptr<ntp_tiles::PopularSites> | |
105 ChromeSiteTilesInternalsMessageHandlerBridge::MakePopularSites() { | |
106 #if defined(OS_ANDROID) | |
107 return ChromePopularSites::NewForProfile(Profile::FromWebUI(web_ui())); | |
108 #else | |
109 return nullptr; | |
110 #endif | |
111 } | |
112 | |
113 PrefService* ChromeSiteTilesInternalsMessageHandlerBridge::GetPrefs() { | |
114 return Profile::FromWebUI(web_ui())->GetPrefs(); | |
115 } | |
116 | |
117 void ChromeSiteTilesInternalsMessageHandlerBridge::RegisterMessageCallback( | |
118 const std::string& message, | |
119 const base::Callback<void(const base::ListValue*)>& callback) { | |
120 web_ui()->RegisterMessageCallback(message, callback); | |
121 } | |
122 | |
123 void ChromeSiteTilesInternalsMessageHandlerBridge::CallJavascriptFunctionVector( | |
124 const std::string& name, | |
125 const std::vector<const base::Value*>& values) { | |
126 web_ui()->CallJavascriptFunctionUnsafe(name, values); | |
Marc Treib
2016/12/08 15:41:49
CallJavascriptFunction from WebUIMessageHandler se
sfiera
2016/12/08 16:44:34
Unfortunately it only seems to take arguments by t
Marc Treib
2016/12/08 17:33:22
Maybe the right solution would be to add a vector
| |
127 } | |
128 | |
129 } // namespace | |
130 | |
131 content::WebUIDataSource* CreateSiteTilesInternalsHTMLSource() { | |
Marc Treib
2016/12/08 15:41:49
Should this be in the anon namespace?
sfiera
2016/12/08 16:44:34
Done.
| |
132 content::WebUIDataSource* source = | |
133 content::WebUIDataSource::Create(chrome::kChromeUISiteTilesInternalsHost); | |
134 | |
135 source->AddResourcePath("site_tiles_internals.js", | |
136 IDR_SITE_TILES_INTERNALS_JS); | |
137 source->AddResourcePath("site_tiles_internals.css", | |
138 IDR_SITE_TILES_INTERNALS_CSS); | |
139 source->SetDefaultResource(IDR_SITE_TILES_INTERNALS_HTML); | |
140 return source; | |
141 } | |
142 | |
143 SiteTilesInternalsUI::SiteTilesInternalsUI(content::WebUI* web_ui) | |
144 : WebUIController(web_ui) { | |
145 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), | |
146 CreateSiteTilesInternalsHTMLSource()); | |
147 web_ui->AddMessageHandler(new ChromeSiteTilesInternalsMessageHandlerBridge); | |
148 } | |
149 | |
150 SiteTilesInternalsUI::~SiteTilesInternalsUI() {} | |
OLD | NEW |