Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: chrome/browser/ui/webui/ntp_tiles_internals_ui.cc

Issue 2557103004: Add chrome://site-tiles-internals/ (Closed)
Patch Set: ntp-tiles-internals Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 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::NTPTilesInternalsMessageHandler handler_;
59
60 DISALLOW_COPY_AND_ASSIGN(ChromeNTPTilesInternalsMessageHandlerClient);
61 };
62
63 void ChromeNTPTilesInternalsMessageHandlerClient::RegisterMessages() {
64 handler_.RegisterMessages(this);
65 }
66
67 bool ChromeNTPTilesInternalsMessageHandlerClient::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.
83 return false;
84 #endif
85 }
86 NOTREACHED();
87 return false;
88 }
89
90 std::unique_ptr<ntp_tiles::MostVisitedSites>
91 ChromeNTPTilesInternalsMessageHandlerClient::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 ChromeNTPTilesInternalsMessageHandlerClient::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* ChromeNTPTilesInternalsMessageHandlerClient::GetPrefs() {
117 return Profile::FromWebUI(web_ui())->GetPrefs();
118 }
119
120 void ChromeNTPTilesInternalsMessageHandlerClient::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 ChromeNTPTilesInternalsMessageHandlerClient::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* CreateNTPTilesInternalsHTMLSource() {
133 content::WebUIDataSource* source =
134 content::WebUIDataSource::Create(chrome::kChromeUINTPTilesInternalsHost);
135
136 source->AddResourcePath("ntp_tiles_internals.js", IDR_NTP_TILES_INTERNALS_JS);
137 source->AddResourcePath("ntp_tiles_internals.css",
138 IDR_NTP_TILES_INTERNALS_CSS);
139 source->SetDefaultResource(IDR_NTP_TILES_INTERNALS_HTML);
140 return source;
141 }
142
143 } // namespace
144
145 NTPTilesInternalsUI::NTPTilesInternalsUI(content::WebUI* web_ui)
146 : WebUIController(web_ui) {
147 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui),
148 CreateNTPTilesInternalsHTMLSource());
149 web_ui->AddMessageHandler(new ChromeNTPTilesInternalsMessageHandlerClient);
150 }
151
152 NTPTilesInternalsUI::~NTPTilesInternalsUI() {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698