blob: 16a9bb6b34799b80a6e331947f8feea1a20604cb [file] [log] [blame]
hironod36c21d2016-06-21 01:25:221// 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
Evan Stadefdc58a82018-04-23 19:00:475#include "chrome/browser/notifications/arc_application_notifier_controller.h"
hironod36c21d2016-06-21 01:25:226
7#include <set>
8
9#include "base/strings/utf_string_conversions.h"
10#include "chrome/browser/profiles/profile.h"
Evan Stadefdc58a82018-04-23 19:00:4711#include "chrome/browser/ui/app_list/arc/arc_app_list_prefs_factory.h"
12#include "components/keyed_service/content/browser_context_keyed_service_shutdown_notifier_factory.h"
hirono0bed5da2016-06-22 01:35:4513#include "ui/base/layout.h"
14#include "ui/display/display.h"
15#include "ui/display/screen.h"
Evan Stade889ce4712018-01-28 15:26:2616#include "ui/message_center/public/cpp/notifier_id.h"
hironod36c21d2016-06-21 01:25:2217
18namespace arc {
19
20namespace {
hirono0bed5da2016-06-22 01:35:4521constexpr int kArcAppIconSizeInDp = 48;
Evan Stadefdc58a82018-04-23 19:00:4722
23class ArcAppNotifierShutdownNotifierFactory
24 : public BrowserContextKeyedServiceShutdownNotifierFactory {
25 public:
26 static ArcAppNotifierShutdownNotifierFactory* GetInstance() {
27 return base::Singleton<ArcAppNotifierShutdownNotifierFactory>::get();
28 }
29
30 private:
31 friend struct base::DefaultSingletonTraits<
32 ArcAppNotifierShutdownNotifierFactory>;
33
34 ArcAppNotifierShutdownNotifierFactory()
35 : BrowserContextKeyedServiceShutdownNotifierFactory("ArcAppNotifier") {
36 DependsOn(ArcAppListPrefsFactory::GetInstance());
37 }
38
39 ~ArcAppNotifierShutdownNotifierFactory() override {}
40
41 DISALLOW_COPY_AND_ASSIGN(ArcAppNotifierShutdownNotifierFactory);
42};
43
hironod36c21d2016-06-21 01:25:2244} // namespace
45
Evan Stadefdc58a82018-04-23 19:00:4746ArcApplicationNotifierController::ArcApplicationNotifierController(
47 NotifierController::Observer* observer)
hironoe39df252016-06-24 03:17:1448 : observer_(observer), last_profile_(nullptr) {}
hironod36c21d2016-06-21 01:25:2249
Evan Stadefdc58a82018-04-23 19:00:4750ArcApplicationNotifierController::~ArcApplicationNotifierController() {
hironoe39df252016-06-24 03:17:1451 StopObserving();
52}
hirono0bed5da2016-06-22 01:35:4553
Evan Stade55575d82017-11-02 00:52:3654std::vector<ash::mojom::NotifierUiDataPtr>
Evan Stadefdc58a82018-04-23 19:00:4755ArcApplicationNotifierController::GetNotifierList(Profile* profile) {
Tetsui Ohkubod91be842018-07-03 01:35:2256 // In Guest mode, it can be called but there's no ARC apps to return.
57 if (profile->IsOffTheRecord())
58 return std::vector<ash::mojom::NotifierUiDataPtr>();
59
hironoe39df252016-06-24 03:17:1460 package_to_app_ids_.clear();
61 icons_.clear();
62 StopObserving();
63
64 ArcAppListPrefs* const app_list = ArcAppListPrefs::Get(profile);
Evan Stade55575d82017-11-02 00:52:3665 std::vector<ash::mojom::NotifierUiDataPtr> results;
Evan Stade51f736fc2017-10-25 05:26:1866 // The app list can be null in unit tests.
67 if (!app_list)
68 return results;
hironod36c21d2016-06-21 01:25:2269 const std::vector<std::string>& app_ids = app_list->GetAppIds();
hironod36c21d2016-06-21 01:25:2270
hironoe39df252016-06-24 03:17:1471 last_profile_ = profile;
Evan Stadefdc58a82018-04-23 19:00:4772 StartObserving();
hironoe39df252016-06-24 03:17:1473
hironod36c21d2016-06-21 01:25:2274 for (const std::string& app_id : app_ids) {
75 const auto app = app_list->GetApp(app_id);
hironod36c21d2016-06-21 01:25:2276 // Handle packages having multiple launcher activities.
hirono1f918652016-07-05 09:54:5477 if (!app || package_to_app_ids_.count(app->package_name))
78 continue;
79
80 const auto package = app_list->GetPackage(app->package_name);
81 if (!package || package->system)
hironod36c21d2016-06-21 01:25:2282 continue;
83
hirono0bed5da2016-06-22 01:35:4584 // Load icons for notifier.
Vladislav Kaznacheevcea52b092018-07-25 22:37:2785 std::unique_ptr<ArcAppIcon> icon =
86 std::make_unique<ArcAppIcon>(profile, app_id,
hirono0bed5da2016-06-22 01:35:4587 // ARC icon is available only for 48x48 dips.
88 kArcAppIconSizeInDp,
89 // The life time of icon must shorter than |this|.
Vladislav Kaznacheevcea52b092018-07-25 22:37:2790 this);
khmel832d8b72017-05-08 18:38:1191 // Apply icon now to set the default image.
92 OnIconUpdated(icon.get());
hirono0bed5da2016-06-22 01:35:4593
94 // Add notifiers.
hironoe39df252016-06-24 03:17:1495 package_to_app_ids_.insert(std::make_pair(app->package_name, app_id));
hironod36c21d2016-06-21 01:25:2296 message_center::NotifierId notifier_id(
Daniel Chenga925cbb52018-11-06 21:52:3597 message_center::NotifierType::ARC_APPLICATION, app_id);
Evan Stade55575d82017-11-02 00:52:3698 auto ui_data = ash::mojom::NotifierUiData::New(
Tetsui Ohkuboc27a5dc2018-01-11 05:56:5299 notifier_id, base::UTF8ToUTF16(app->name), app->notifications_enabled,
100 false /* enforced */, icon->image_skia());
hirono0bed5da2016-06-22 01:35:45101 icons_.push_back(std::move(icon));
Evan Stade51f736fc2017-10-25 05:26:18102 results.push_back(std::move(ui_data));
hironod36c21d2016-06-21 01:25:22103 }
104
105 return results;
106}
107
Evan Stadefdc58a82018-04-23 19:00:47108void ArcApplicationNotifierController::SetNotifierEnabled(
hironod36c21d2016-06-21 01:25:22109 Profile* profile,
Evan Stade844ad7f2017-09-20 18:05:31110 const message_center::NotifierId& notifier_id,
hironod36c21d2016-06-21 01:25:22111 bool enabled) {
Evan Stadefdc58a82018-04-23 19:00:47112 ArcAppListPrefs::Get(profile)->SetNotificationsEnabled(notifier_id.id,
113 enabled);
hironoe39df252016-06-24 03:17:14114 // OnNotifierEnabledChanged will be invoked via ArcAppListPrefs::Observer.
115}
116
Evan Stadefdc58a82018-04-23 19:00:47117void ArcApplicationNotifierController::OnIconUpdated(ArcAppIcon* icon) {
118 observer_->OnIconImageUpdated(
Daniel Chenga925cbb52018-11-06 21:52:35119 message_center::NotifierId(message_center::NotifierType::ARC_APPLICATION,
Evan Stadefdc58a82018-04-23 19:00:47120 icon->app_id()),
121 icon->image_skia());
122}
123
124void ArcApplicationNotifierController::OnNotificationsEnabledChanged(
hironoe39df252016-06-24 03:17:14125 const std::string& package_name,
126 bool enabled) {
127 auto it = package_to_app_ids_.find(package_name);
128 if (it == package_to_app_ids_.end())
129 return;
130 observer_->OnNotifierEnabledChanged(
Daniel Chenga925cbb52018-11-06 21:52:35131 message_center::NotifierId(message_center::NotifierType::ARC_APPLICATION,
hironoe39df252016-06-24 03:17:14132 it->second),
133 enabled);
hironod36c21d2016-06-21 01:25:22134}
135
Evan Stadefdc58a82018-04-23 19:00:47136void ArcApplicationNotifierController::StartObserving() {
137 ArcAppListPrefs::Get(last_profile_)->AddObserver(this);
138 shutdown_notifier_ = ArcAppNotifierShutdownNotifierFactory::GetInstance()
139 ->Get(last_profile_)
140 ->Subscribe(base::BindRepeating(
141 &ArcApplicationNotifierController::StopObserving,
142 base::Unretained(this)));
hirono0bed5da2016-06-22 01:35:45143}
144
Evan Stadefdc58a82018-04-23 19:00:47145void ArcApplicationNotifierController::StopObserving() {
hironoe39df252016-06-24 03:17:14146 if (!last_profile_)
147 return;
Evan Stadefdc58a82018-04-23 19:00:47148 shutdown_notifier_.reset();
149 ArcAppListPrefs::Get(last_profile_)->RemoveObserver(this);
hironoe39df252016-06-24 03:17:14150 last_profile_ = nullptr;
151}
152
hironod36c21d2016-06-21 01:25:22153} // namespace arc