Add chrome://site-engagement WebUI.
[chromium-blink-merge.git] / chrome / browser / ui / webui / engagement / site_engagement_ui.cc
blob64cc553338f12254860cc3aabd72083c7f96c3f9
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.
5 #include "chrome/browser/ui/webui/engagement/site_engagement_ui.h"
7 #include "chrome/browser/engagement/site_engagement_service.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/common/url_constants.h"
10 #include "content/public/browser/web_ui.h"
11 #include "content/public/browser/web_ui_controller.h"
12 #include "content/public/browser/web_ui_data_source.h"
13 #include "grit/browser_resources.h"
14 #include "mojo/common/url_type_converters.h"
15 #include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h"
17 namespace {
19 // Implementation of SiteEngagementUIHandler that gets information from the
20 // SiteEngagementService to provide data for the WebUI.
21 class SiteEngagementUIHandlerImpl : public SiteEngagementUIHandler {
22 public:
23 // SiteEngagementUIHandlerImpl is deleted when the supplied pipe is destroyed.
24 SiteEngagementUIHandlerImpl(
25 Profile* profile,
26 mojo::InterfaceRequest<SiteEngagementUIHandler> request)
27 : profile_(profile), binding_(this, request.Pass()) {
28 DCHECK(profile_);
31 ~SiteEngagementUIHandlerImpl() override {}
33 // SiteEngagementUIHandler overrides:
34 void GetSiteEngagementInfo(
35 const GetSiteEngagementInfoCallback& callback) override {
36 mojo::Array<SiteEngagementInfoPtr> engagement_info(0);
38 SiteEngagementService* service = SiteEngagementService::Get(profile_);
40 for (const std::pair<GURL, int>& info : service->GetScoreMap()) {
41 SiteEngagementInfoPtr origin_info(SiteEngagementInfo::New());
42 origin_info->origin = mojo::String::From(info.first);
43 origin_info->score = info.second;
44 engagement_info.push_back(origin_info.Pass());
47 callback.Run(engagement_info.Pass());
50 private:
51 // The Profile* handed to us in our constructor.
52 Profile* profile_;
54 mojo::StrongBinding<SiteEngagementUIHandler> binding_;
56 DISALLOW_COPY_AND_ASSIGN(SiteEngagementUIHandlerImpl);
59 } // namespace
61 SiteEngagementUI::SiteEngagementUI(content::WebUI* web_ui)
62 : MojoWebUIController<SiteEngagementUIHandler>(web_ui) {
63 // Set up the chrome://site-engagement/ source.
64 scoped_ptr<content::WebUIDataSource> source(
65 content::WebUIDataSource::Create(chrome::kChromeUISiteEngagementHost));
66 source->AddResourcePath("engagement_table.html",
67 IDR_SITE_ENGAGEMENT_ENGAGEMENT_TABLE_HTML);
68 source->AddResourcePath("engagement_table.css",
69 IDR_SITE_ENGAGEMENT_ENGAGEMENT_TABLE_CSS);
70 source->AddResourcePath("engagement_table.js",
71 IDR_SITE_ENGAGEMENT_ENGAGEMENT_TABLE_JS);
72 source->AddResourcePath("site_engagement.js", IDR_SITE_ENGAGEMENT_JS);
73 source->SetDefaultResource(IDR_SITE_ENGAGEMENT_HTML);
75 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), source.release());
77 AddMojoResourcePath(
78 "chrome/browser/ui/webui/engagement/site_engagement.mojom",
79 IDR_SITE_ENGAGEMENT_MOJO_JS);
82 SiteEngagementUI::~SiteEngagementUI() {}
84 void SiteEngagementUI::BindUIHandler(
85 mojo::InterfaceRequest<SiteEngagementUIHandler> request) {
86 // SiteEngagementUIHandlerImpl deletes itself when the pipe is closed.
87 new SiteEngagementUIHandlerImpl(Profile::FromWebUI(web_ui()), request.Pass());