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