OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/ash/launcher/arc_app_deferred_launcher_controller.h" | 5 #include "chrome/browser/ui/ash/launcher/arc_app_deferred_launcher_controller.h" |
6 | 6 |
7 #include "chrome/browser/chromeos/arc/arc_support_host.h" | 7 #include "chrome/browser/chromeos/arc/arc_support_host.h" |
8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/ui/app_list/arc/arc_app_utils.h" | 9 #include "chrome/browser/ui/app_list/arc/arc_app_utils.h" |
10 #include "chrome/browser/ui/ash/launcher/arc_app_deferred_launcher_item_controll
er.h" | 10 #include "chrome/browser/ui/ash/launcher/arc_app_deferred_launcher_item_controll
er.h" |
11 #include "chrome/browser/ui/ash/launcher/arc_app_window_launcher_controller.h" | 11 #include "chrome/browser/ui/ash/launcher/arc_app_window_launcher_controller.h" |
12 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller_impl.h" | 12 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller_impl.h" |
13 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
14 #include "ui/gfx/image/canvas_image_source.h" | 14 #include "ui/gfx/image/canvas_image_source.h" |
15 #include "ui/gfx/image/image_skia_operations.h" | 15 #include "ui/gfx/image/image_skia_operations.h" |
16 #include "ui/gfx/paint_throbber.h" | 16 #include "ui/gfx/paint_throbber.h" |
17 | 17 |
18 namespace { | 18 namespace { |
| 19 |
19 constexpr int kUpdateIconIntervalMs = 40; // 40ms for 25 frames per second. | 20 constexpr int kUpdateIconIntervalMs = 40; // 40ms for 25 frames per second. |
20 constexpr int kSpinningGapPercent = 25; | 21 constexpr int kSpinningGapPercent = 25; |
21 | 22 |
22 class SpinningEffectSource : public gfx::CanvasImageSource { | 23 class SpinningEffectSource : public gfx::CanvasImageSource { |
23 public: | 24 public: |
24 SpinningEffectSource( | 25 SpinningEffectSource( |
25 const base::WeakPtr<ArcAppDeferredLauncherController>& host, | 26 const base::WeakPtr<ArcAppDeferredLauncherController>& host, |
26 const std::string& shelf_app_id, | 27 const std::string& shelf_app_id, |
27 const gfx::ImageSkia& image) | 28 const gfx::ImageSkia& image) |
28 : gfx::CanvasImageSource(image.size(), false /* is opaque */), | 29 : gfx::CanvasImageSource(image.size(), false /* is opaque */), |
(...skipping 26 matching lines...) Expand all Loading... |
55 }; | 56 }; |
56 } // namespace | 57 } // namespace |
57 | 58 |
58 ArcAppDeferredLauncherController::ArcAppDeferredLauncherController( | 59 ArcAppDeferredLauncherController::ArcAppDeferredLauncherController( |
59 ChromeLauncherControllerImpl* owner) | 60 ChromeLauncherControllerImpl* owner) |
60 : owner_(owner), weak_ptr_factory_(this) { | 61 : owner_(owner), weak_ptr_factory_(this) { |
61 if (arc::ArcAuthService::IsAllowedForProfile(owner->GetProfile())) { | 62 if (arc::ArcAuthService::IsAllowedForProfile(owner->GetProfile())) { |
62 observed_profile_ = owner->GetProfile(); | 63 observed_profile_ = owner->GetProfile(); |
63 ArcAppListPrefs::Get(observed_profile_)->AddObserver(this); | 64 ArcAppListPrefs::Get(observed_profile_)->AddObserver(this); |
64 } | 65 } |
| 66 arc::ArcAuthService* auth_service = arc::ArcAuthService::Get(); |
| 67 // arc::ArcAuthService might not be set in tests. |
| 68 if (auth_service) |
| 69 auth_service->AddObserver(this); |
65 } | 70 } |
66 | 71 |
67 ArcAppDeferredLauncherController::~ArcAppDeferredLauncherController() { | 72 ArcAppDeferredLauncherController::~ArcAppDeferredLauncherController() { |
| 73 arc::ArcAuthService* auth_service = arc::ArcAuthService::Get(); |
| 74 // arc::ArcAuthService may be released first. |
| 75 if (auth_service) |
| 76 auth_service->RemoveObserver(this); |
68 if (observed_profile_) | 77 if (observed_profile_) |
69 ArcAppListPrefs::Get(observed_profile_)->RemoveObserver(this); | 78 ArcAppListPrefs::Get(observed_profile_)->RemoveObserver(this); |
70 } | 79 } |
71 | 80 |
72 void ArcAppDeferredLauncherController::MaybeApplySpinningEffect( | 81 void ArcAppDeferredLauncherController::MaybeApplySpinningEffect( |
73 const std::string& shelf_app_id, | 82 const std::string& shelf_app_id, |
74 gfx::ImageSkia* image) { | 83 gfx::ImageSkia* image) { |
75 DCHECK(image); | 84 DCHECK(image); |
76 if (app_controller_map_.find(shelf_app_id) == app_controller_map_.end()) | 85 if (app_controller_map_.find(shelf_app_id) == app_controller_map_.end()) |
77 return; | 86 return; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 | 125 |
117 Close(app_id); | 126 Close(app_id); |
118 | 127 |
119 arc::LaunchApp(owner_->GetProfile(), app_id); | 128 arc::LaunchApp(owner_->GetProfile(), app_id); |
120 } | 129 } |
121 | 130 |
122 void ArcAppDeferredLauncherController::OnAppRemoved(const std::string& app_id) { | 131 void ArcAppDeferredLauncherController::OnAppRemoved(const std::string& app_id) { |
123 Close(app_id); | 132 Close(app_id); |
124 } | 133 } |
125 | 134 |
| 135 void ArcAppDeferredLauncherController::OnOptInEnabled(bool enabled) { |
| 136 if (enabled) |
| 137 return; |
| 138 |
| 139 // If Arc was disabled, remove all deferred launch requests. |
| 140 while (!app_controller_map_.empty()) |
| 141 Close(app_controller_map_.begin()->first); |
| 142 } |
| 143 |
| 144 bool ArcAppDeferredLauncherController::HasApp(const std::string& app_id) const { |
| 145 const std::string shelf_app_id = |
| 146 ArcAppWindowLauncherController::GetShelfAppIdFromArcAppId(app_id); |
| 147 return app_controller_map_.count(shelf_app_id); |
| 148 } |
| 149 |
126 base::TimeDelta ArcAppDeferredLauncherController::GetActiveTime( | 150 base::TimeDelta ArcAppDeferredLauncherController::GetActiveTime( |
127 const std::string& shelf_app_id) const { | 151 const std::string& shelf_app_id) const { |
128 AppControllerMap::const_iterator it = app_controller_map_.find(shelf_app_id); | 152 AppControllerMap::const_iterator it = app_controller_map_.find(shelf_app_id); |
129 if (it == app_controller_map_.end()) | 153 if (it == app_controller_map_.end()) |
130 return base::TimeDelta(); | 154 return base::TimeDelta(); |
131 | 155 |
132 return it->second->GetActiveTime(); | 156 return it->second->GetActiveTime(); |
133 } | 157 } |
134 | 158 |
135 void ArcAppDeferredLauncherController::UpdateApps() { | 159 void ArcAppDeferredLauncherController::UpdateApps() { |
136 if (app_controller_map_.empty()) | 160 if (app_controller_map_.empty()) |
137 return; | 161 return; |
138 | 162 |
139 RegisterNextUpdate(); | 163 RegisterNextUpdate(); |
140 for (const auto pair : app_controller_map_) | 164 for (const auto pair : app_controller_map_) |
141 owner_->OnAppUpdated(owner_->GetProfile(), pair.first); | 165 owner_->OnAppUpdated(owner_->GetProfile(), pair.first); |
142 } | 166 } |
143 | 167 |
144 void ArcAppDeferredLauncherController::RegisterNextUpdate() { | 168 void ArcAppDeferredLauncherController::RegisterNextUpdate() { |
145 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 169 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
146 FROM_HERE, base::Bind(&ArcAppDeferredLauncherController::UpdateApps, | 170 FROM_HERE, base::Bind(&ArcAppDeferredLauncherController::UpdateApps, |
147 weak_ptr_factory_.GetWeakPtr()), | 171 weak_ptr_factory_.GetWeakPtr()), |
148 base::TimeDelta::FromMilliseconds(kUpdateIconIntervalMs)); | 172 base::TimeDelta::FromMilliseconds(kUpdateIconIntervalMs)); |
149 } | 173 } |
150 | 174 |
151 void ArcAppDeferredLauncherController::RegisterDeferredLaunch( | 175 void ArcAppDeferredLauncherController::RegisterDeferredLaunch( |
152 const std::string& app_id) { | 176 const std::string& app_id) { |
| 177 const arc::ArcAuthService* auth_service = arc::ArcAuthService::Get(); |
| 178 DCHECK(auth_service); |
| 179 DCHECK(auth_service->state() != arc::ArcAuthService::State::STOPPED); |
| 180 DCHECK(auth_service->state() != arc::ArcAuthService::State::NOT_INITIALIZED); |
| 181 |
153 const std::string shelf_app_id = | 182 const std::string shelf_app_id = |
154 ArcAppWindowLauncherController::GetShelfAppIdFromArcAppId(app_id); | 183 ArcAppWindowLauncherController::GetShelfAppIdFromArcAppId(app_id); |
155 const ash::ShelfID shelf_id = owner_->GetShelfIDForAppID(shelf_app_id); | 184 const ash::ShelfID shelf_id = owner_->GetShelfIDForAppID(shelf_app_id); |
156 | 185 |
157 if (shelf_id) { | 186 if (shelf_id) { |
158 LauncherItemController* controller = | 187 LauncherItemController* controller = |
159 owner_->GetLauncherItemController(shelf_id); | 188 owner_->GetLauncherItemController(shelf_id); |
160 if (controller && | 189 if (controller && |
161 controller->type() != LauncherItemController::TYPE_SHORTCUT) { | 190 controller->type() != LauncherItemController::TYPE_SHORTCUT) { |
162 // We are allowed to apply new deferred controller only over shortcut. | 191 // We are allowed to apply new deferred controller only over shortcut. |
(...skipping 10 matching lines...) Expand all Loading... |
173 } else { | 202 } else { |
174 owner_->SetItemController(shelf_id, controller); | 203 owner_->SetItemController(shelf_id, controller); |
175 owner_->SetItemStatus(shelf_id, ash::STATUS_RUNNING); | 204 owner_->SetItemStatus(shelf_id, ash::STATUS_RUNNING); |
176 } | 205 } |
177 | 206 |
178 if (app_controller_map_.empty()) | 207 if (app_controller_map_.empty()) |
179 RegisterNextUpdate(); | 208 RegisterNextUpdate(); |
180 | 209 |
181 app_controller_map_[shelf_app_id] = controller; | 210 app_controller_map_[shelf_app_id] = controller; |
182 } | 211 } |
OLD | NEW |