hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 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 | |
Evan Stade | fdc58a8 | 2018-04-23 19:00:47 | [diff] [blame] | 5 | #include "chrome/browser/notifications/arc_application_notifier_controller.h" |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 6 | |
| 7 | #include <set> |
| 8 | |
Sebastien Marchand | f1349f5 | 2019-01-25 03:16:41 | [diff] [blame^] | 9 | #include "base/bind.h" |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 10 | #include "base/strings/utf_string_conversions.h" |
| 11 | #include "chrome/browser/profiles/profile.h" |
Evan Stade | fdc58a8 | 2018-04-23 19:00:47 | [diff] [blame] | 12 | #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs_factory.h" |
| 13 | #include "components/keyed_service/content/browser_context_keyed_service_shutdown_notifier_factory.h" |
hirono | 0bed5da | 2016-06-22 01:35:45 | [diff] [blame] | 14 | #include "ui/base/layout.h" |
| 15 | #include "ui/display/display.h" |
| 16 | #include "ui/display/screen.h" |
Evan Stade | 889ce471 | 2018-01-28 15:26:26 | [diff] [blame] | 17 | #include "ui/message_center/public/cpp/notifier_id.h" |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 18 | |
| 19 | namespace arc { |
| 20 | |
| 21 | namespace { |
hirono | 0bed5da | 2016-06-22 01:35:45 | [diff] [blame] | 22 | constexpr int kArcAppIconSizeInDp = 48; |
Evan Stade | fdc58a8 | 2018-04-23 19:00:47 | [diff] [blame] | 23 | |
| 24 | class ArcAppNotifierShutdownNotifierFactory |
| 25 | : public BrowserContextKeyedServiceShutdownNotifierFactory { |
| 26 | public: |
| 27 | static ArcAppNotifierShutdownNotifierFactory* GetInstance() { |
| 28 | return base::Singleton<ArcAppNotifierShutdownNotifierFactory>::get(); |
| 29 | } |
| 30 | |
| 31 | private: |
| 32 | friend struct base::DefaultSingletonTraits< |
| 33 | ArcAppNotifierShutdownNotifierFactory>; |
| 34 | |
| 35 | ArcAppNotifierShutdownNotifierFactory() |
| 36 | : BrowserContextKeyedServiceShutdownNotifierFactory("ArcAppNotifier") { |
| 37 | DependsOn(ArcAppListPrefsFactory::GetInstance()); |
| 38 | } |
| 39 | |
| 40 | ~ArcAppNotifierShutdownNotifierFactory() override {} |
| 41 | |
| 42 | DISALLOW_COPY_AND_ASSIGN(ArcAppNotifierShutdownNotifierFactory); |
| 43 | }; |
| 44 | |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 45 | } // namespace |
| 46 | |
Evan Stade | fdc58a8 | 2018-04-23 19:00:47 | [diff] [blame] | 47 | ArcApplicationNotifierController::ArcApplicationNotifierController( |
| 48 | NotifierController::Observer* observer) |
hirono | e39df25 | 2016-06-24 03:17:14 | [diff] [blame] | 49 | : observer_(observer), last_profile_(nullptr) {} |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 50 | |
Evan Stade | fdc58a8 | 2018-04-23 19:00:47 | [diff] [blame] | 51 | ArcApplicationNotifierController::~ArcApplicationNotifierController() { |
hirono | e39df25 | 2016-06-24 03:17:14 | [diff] [blame] | 52 | StopObserving(); |
| 53 | } |
hirono | 0bed5da | 2016-06-22 01:35:45 | [diff] [blame] | 54 | |
Evan Stade | 55575d8 | 2017-11-02 00:52:36 | [diff] [blame] | 55 | std::vector<ash::mojom::NotifierUiDataPtr> |
Evan Stade | fdc58a8 | 2018-04-23 19:00:47 | [diff] [blame] | 56 | ArcApplicationNotifierController::GetNotifierList(Profile* profile) { |
Tetsui Ohkubo | d91be84 | 2018-07-03 01:35:22 | [diff] [blame] | 57 | // In Guest mode, it can be called but there's no ARC apps to return. |
| 58 | if (profile->IsOffTheRecord()) |
| 59 | return std::vector<ash::mojom::NotifierUiDataPtr>(); |
| 60 | |
hirono | e39df25 | 2016-06-24 03:17:14 | [diff] [blame] | 61 | package_to_app_ids_.clear(); |
| 62 | icons_.clear(); |
| 63 | StopObserving(); |
| 64 | |
| 65 | ArcAppListPrefs* const app_list = ArcAppListPrefs::Get(profile); |
Evan Stade | 55575d8 | 2017-11-02 00:52:36 | [diff] [blame] | 66 | std::vector<ash::mojom::NotifierUiDataPtr> results; |
Evan Stade | 51f736fc | 2017-10-25 05:26:18 | [diff] [blame] | 67 | // The app list can be null in unit tests. |
| 68 | if (!app_list) |
| 69 | return results; |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 70 | const std::vector<std::string>& app_ids = app_list->GetAppIds(); |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 71 | |
hirono | e39df25 | 2016-06-24 03:17:14 | [diff] [blame] | 72 | last_profile_ = profile; |
Evan Stade | fdc58a8 | 2018-04-23 19:00:47 | [diff] [blame] | 73 | StartObserving(); |
hirono | e39df25 | 2016-06-24 03:17:14 | [diff] [blame] | 74 | |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 75 | for (const std::string& app_id : app_ids) { |
| 76 | const auto app = app_list->GetApp(app_id); |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 77 | // Handle packages having multiple launcher activities. |
hirono | 1f91865 | 2016-07-05 09:54:54 | [diff] [blame] | 78 | if (!app || package_to_app_ids_.count(app->package_name)) |
| 79 | continue; |
| 80 | |
| 81 | const auto package = app_list->GetPackage(app->package_name); |
| 82 | if (!package || package->system) |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 83 | continue; |
| 84 | |
hirono | 0bed5da | 2016-06-22 01:35:45 | [diff] [blame] | 85 | // Load icons for notifier. |
Vladislav Kaznacheev | cea52b09 | 2018-07-25 22:37:27 | [diff] [blame] | 86 | std::unique_ptr<ArcAppIcon> icon = |
| 87 | std::make_unique<ArcAppIcon>(profile, app_id, |
hirono | 0bed5da | 2016-06-22 01:35:45 | [diff] [blame] | 88 | // ARC icon is available only for 48x48 dips. |
| 89 | kArcAppIconSizeInDp, |
| 90 | // The life time of icon must shorter than |this|. |
Vladislav Kaznacheev | cea52b09 | 2018-07-25 22:37:27 | [diff] [blame] | 91 | this); |
khmel | 832d8b7 | 2017-05-08 18:38:11 | [diff] [blame] | 92 | // Apply icon now to set the default image. |
| 93 | OnIconUpdated(icon.get()); |
hirono | 0bed5da | 2016-06-22 01:35:45 | [diff] [blame] | 94 | |
| 95 | // Add notifiers. |
hirono | e39df25 | 2016-06-24 03:17:14 | [diff] [blame] | 96 | package_to_app_ids_.insert(std::make_pair(app->package_name, app_id)); |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 97 | message_center::NotifierId notifier_id( |
Daniel Cheng | a925cbb5 | 2018-11-06 21:52:35 | [diff] [blame] | 98 | message_center::NotifierType::ARC_APPLICATION, app_id); |
Evan Stade | 55575d8 | 2017-11-02 00:52:36 | [diff] [blame] | 99 | auto ui_data = ash::mojom::NotifierUiData::New( |
Tetsui Ohkubo | c27a5dc | 2018-01-11 05:56:52 | [diff] [blame] | 100 | notifier_id, base::UTF8ToUTF16(app->name), app->notifications_enabled, |
| 101 | false /* enforced */, icon->image_skia()); |
hirono | 0bed5da | 2016-06-22 01:35:45 | [diff] [blame] | 102 | icons_.push_back(std::move(icon)); |
Evan Stade | 51f736fc | 2017-10-25 05:26:18 | [diff] [blame] | 103 | results.push_back(std::move(ui_data)); |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return results; |
| 107 | } |
| 108 | |
Evan Stade | fdc58a8 | 2018-04-23 19:00:47 | [diff] [blame] | 109 | void ArcApplicationNotifierController::SetNotifierEnabled( |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 110 | Profile* profile, |
Evan Stade | 844ad7f | 2017-09-20 18:05:31 | [diff] [blame] | 111 | const message_center::NotifierId& notifier_id, |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 112 | bool enabled) { |
Evan Stade | fdc58a8 | 2018-04-23 19:00:47 | [diff] [blame] | 113 | ArcAppListPrefs::Get(profile)->SetNotificationsEnabled(notifier_id.id, |
| 114 | enabled); |
hirono | e39df25 | 2016-06-24 03:17:14 | [diff] [blame] | 115 | // OnNotifierEnabledChanged will be invoked via ArcAppListPrefs::Observer. |
| 116 | } |
| 117 | |
Evan Stade | fdc58a8 | 2018-04-23 19:00:47 | [diff] [blame] | 118 | void ArcApplicationNotifierController::OnIconUpdated(ArcAppIcon* icon) { |
| 119 | observer_->OnIconImageUpdated( |
Daniel Cheng | a925cbb5 | 2018-11-06 21:52:35 | [diff] [blame] | 120 | message_center::NotifierId(message_center::NotifierType::ARC_APPLICATION, |
Evan Stade | fdc58a8 | 2018-04-23 19:00:47 | [diff] [blame] | 121 | icon->app_id()), |
| 122 | icon->image_skia()); |
| 123 | } |
| 124 | |
| 125 | void ArcApplicationNotifierController::OnNotificationsEnabledChanged( |
hirono | e39df25 | 2016-06-24 03:17:14 | [diff] [blame] | 126 | const std::string& package_name, |
| 127 | bool enabled) { |
| 128 | auto it = package_to_app_ids_.find(package_name); |
| 129 | if (it == package_to_app_ids_.end()) |
| 130 | return; |
| 131 | observer_->OnNotifierEnabledChanged( |
Daniel Cheng | a925cbb5 | 2018-11-06 21:52:35 | [diff] [blame] | 132 | message_center::NotifierId(message_center::NotifierType::ARC_APPLICATION, |
hirono | e39df25 | 2016-06-24 03:17:14 | [diff] [blame] | 133 | it->second), |
| 134 | enabled); |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 135 | } |
| 136 | |
Evan Stade | fdc58a8 | 2018-04-23 19:00:47 | [diff] [blame] | 137 | void ArcApplicationNotifierController::StartObserving() { |
| 138 | ArcAppListPrefs::Get(last_profile_)->AddObserver(this); |
| 139 | shutdown_notifier_ = ArcAppNotifierShutdownNotifierFactory::GetInstance() |
| 140 | ->Get(last_profile_) |
| 141 | ->Subscribe(base::BindRepeating( |
| 142 | &ArcApplicationNotifierController::StopObserving, |
| 143 | base::Unretained(this))); |
hirono | 0bed5da | 2016-06-22 01:35:45 | [diff] [blame] | 144 | } |
| 145 | |
Evan Stade | fdc58a8 | 2018-04-23 19:00:47 | [diff] [blame] | 146 | void ArcApplicationNotifierController::StopObserving() { |
hirono | e39df25 | 2016-06-24 03:17:14 | [diff] [blame] | 147 | if (!last_profile_) |
| 148 | return; |
Evan Stade | fdc58a8 | 2018-04-23 19:00:47 | [diff] [blame] | 149 | shutdown_notifier_.reset(); |
| 150 | ArcAppListPrefs::Get(last_profile_)->RemoveObserver(this); |
hirono | e39df25 | 2016-06-24 03:17:14 | [diff] [blame] | 151 | last_profile_ = nullptr; |
| 152 | } |
| 153 | |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 154 | } // namespace arc |