OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/engagement/site_engagement_ui.h" | 5 #include "chrome/browser/ui/webui/engagement/site_engagement_ui.h" |
6 | 6 |
7 #include "chrome/browser/engagement/site_engagement_service.h" | |
8 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/common/url_constants.h" | 8 #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" | 9 #include "content/public/browser/web_ui_data_source.h" |
13 #include "grit/browser_resources.h" | 10 #include "grit/browser_resources.h" |
14 #include "mojo/common/url_type_converters.h" | |
15 #include "mojo/public/cpp/bindings/strong_binding.h" | |
16 | |
17 namespace { | |
18 | |
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_); | |
29 } | |
30 | |
31 ~SiteEngagementUIHandlerImpl() override {} | |
32 | |
33 // SiteEngagementUIHandler overrides: | |
34 void GetSiteEngagementInfo( | |
35 const GetSiteEngagementInfoCallback& callback) override { | |
36 mojo::Array<SiteEngagementInfoPtr> engagement_info(0); | |
37 | |
38 SiteEngagementService* service = SiteEngagementService::Get(profile_); | |
39 | |
40 for (const std::pair<GURL, double>& 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()); | |
45 } | |
46 | |
47 callback.Run(engagement_info.Pass()); | |
48 } | |
49 | |
50 private: | |
51 // The Profile* handed to us in our constructor. | |
52 Profile* profile_; | |
53 | |
54 mojo::StrongBinding<SiteEngagementUIHandler> binding_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(SiteEngagementUIHandlerImpl); | |
57 }; | |
58 | |
59 } // namespace | |
60 | 11 |
61 SiteEngagementUI::SiteEngagementUI(content::WebUI* web_ui) | 12 SiteEngagementUI::SiteEngagementUI(content::WebUI* web_ui) |
62 : MojoWebUIController<SiteEngagementUIHandler>(web_ui) { | 13 : MojoWebUIController(web_ui) { |
63 // Set up the chrome://site-engagement/ source. | 14 // Set up the chrome://site-engagement/ source. |
64 scoped_ptr<content::WebUIDataSource> source( | 15 scoped_ptr<content::WebUIDataSource> source( |
65 content::WebUIDataSource::Create(chrome::kChromeUISiteEngagementHost)); | 16 content::WebUIDataSource::Create(chrome::kChromeUISiteEngagementHost)); |
66 source->AddResourcePath("engagement_table.html", | 17 source->AddResourcePath("engagement_table.html", |
67 IDR_SITE_ENGAGEMENT_ENGAGEMENT_TABLE_HTML); | 18 IDR_SITE_ENGAGEMENT_ENGAGEMENT_TABLE_HTML); |
68 source->AddResourcePath("engagement_table.css", | 19 source->AddResourcePath("engagement_table.css", |
69 IDR_SITE_ENGAGEMENT_ENGAGEMENT_TABLE_CSS); | 20 IDR_SITE_ENGAGEMENT_ENGAGEMENT_TABLE_CSS); |
70 source->AddResourcePath("engagement_table.js", | 21 source->AddResourcePath("engagement_table.js", |
71 IDR_SITE_ENGAGEMENT_ENGAGEMENT_TABLE_JS); | 22 IDR_SITE_ENGAGEMENT_ENGAGEMENT_TABLE_JS); |
72 source->AddResourcePath("site_engagement.js", IDR_SITE_ENGAGEMENT_JS); | 23 source->AddResourcePath("site_engagement.js", IDR_SITE_ENGAGEMENT_JS); |
73 source->AddResourcePath( | 24 source->AddResourcePath( |
74 "chrome/browser/ui/webui/engagement/site_engagement.mojom", | 25 "chrome/browser/ui/webui/engagement/site_engagement.mojom", |
75 IDR_SITE_ENGAGEMENT_MOJO_JS); | 26 IDR_SITE_ENGAGEMENT_MOJO_JS); |
76 source->AddMojoResources(); | 27 source->AddMojoResources(); |
77 source->SetDefaultResource(IDR_SITE_ENGAGEMENT_HTML); | 28 source->SetDefaultResource(IDR_SITE_ENGAGEMENT_HTML); |
78 | 29 |
79 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), source.release()); | 30 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), source.release()); |
80 } | 31 } |
81 | 32 |
82 SiteEngagementUI::~SiteEngagementUI() {} | 33 SiteEngagementUI::~SiteEngagementUI() {} |
83 | |
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()); | |
88 } | |
OLD | NEW |