| 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 131 |
| 123 Close(app_id); | 132 Close(app_id); |
| 124 | 133 |
| 125 arc::LaunchApp(owner_->GetProfile(), app_id); | 134 arc::LaunchApp(owner_->GetProfile(), app_id); |
| 126 } | 135 } |
| 127 | 136 |
| 128 void ArcAppDeferredLauncherController::OnAppRemoved(const std::string& app_id) { | 137 void ArcAppDeferredLauncherController::OnAppRemoved(const std::string& app_id) { |
| 129 Close(app_id); | 138 Close(app_id); |
| 130 } | 139 } |
| 131 | 140 |
| 141 void ArcAppDeferredLauncherController::OnOptInEnabled(bool enabled) { |
| 142 if (enabled) |
| 143 return; |
| 144 |
| 145 // If Arc was disabled, remove all deferred launch requests. |
| 146 while (!app_controller_map_.empty()) |
| 147 Close(app_controller_map_.begin()->first); |
| 148 } |
| 149 |
| 150 bool ArcAppDeferredLauncherController::HasApp(const std::string& app_id) const { |
| 151 const std::string shelf_app_id = |
| 152 ArcAppWindowLauncherController::GetShelfAppIdFromArcAppId(app_id); |
| 153 return app_controller_map_.count(shelf_app_id); |
| 154 } |
| 155 |
| 132 base::TimeDelta ArcAppDeferredLauncherController::GetActiveTime( | 156 base::TimeDelta ArcAppDeferredLauncherController::GetActiveTime( |
| 133 const std::string& shelf_app_id) const { | 157 const std::string& shelf_app_id) const { |
| 134 AppControllerMap::const_iterator it = app_controller_map_.find(shelf_app_id); | 158 AppControllerMap::const_iterator it = app_controller_map_.find(shelf_app_id); |
| 135 if (it == app_controller_map_.end()) | 159 if (it == app_controller_map_.end()) |
| 136 return base::TimeDelta(); | 160 return base::TimeDelta(); |
| 137 | 161 |
| 138 return it->second->GetActiveTime(); | 162 return it->second->GetActiveTime(); |
| 139 } | 163 } |
| 140 | 164 |
| 141 void ArcAppDeferredLauncherController::UpdateApps() { | 165 void ArcAppDeferredLauncherController::UpdateApps() { |
| 142 if (app_controller_map_.empty()) | 166 if (app_controller_map_.empty()) |
| 143 return; | 167 return; |
| 144 | 168 |
| 145 RegisterNextUpdate(); | 169 RegisterNextUpdate(); |
| 146 for (const auto pair : app_controller_map_) | 170 for (const auto pair : app_controller_map_) |
| 147 owner_->OnAppUpdated(owner_->GetProfile(), pair.first); | 171 owner_->OnAppUpdated(owner_->GetProfile(), pair.first); |
| 148 } | 172 } |
| 149 | 173 |
| 150 void ArcAppDeferredLauncherController::RegisterNextUpdate() { | 174 void ArcAppDeferredLauncherController::RegisterNextUpdate() { |
| 151 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 175 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 152 FROM_HERE, base::Bind(&ArcAppDeferredLauncherController::UpdateApps, | 176 FROM_HERE, base::Bind(&ArcAppDeferredLauncherController::UpdateApps, |
| 153 weak_ptr_factory_.GetWeakPtr()), | 177 weak_ptr_factory_.GetWeakPtr()), |
| 154 base::TimeDelta::FromMilliseconds(kUpdateIconIntervalMs)); | 178 base::TimeDelta::FromMilliseconds(kUpdateIconIntervalMs)); |
| 155 } | 179 } |
| 156 | 180 |
| 157 void ArcAppDeferredLauncherController::RegisterDeferredLaunch( | 181 void ArcAppDeferredLauncherController::RegisterDeferredLaunch( |
| 158 const std::string& app_id) { | 182 const std::string& app_id) { |
| 183 const arc::ArcAuthService* auth_service = arc::ArcAuthService::Get(); |
| 184 DCHECK(auth_service); |
| 185 DCHECK(auth_service->state() != arc::ArcAuthService::State::STOPPED); |
| 186 DCHECK(auth_service->state() != arc::ArcAuthService::State::NOT_INITIALIZED); |
| 187 |
| 159 const std::string shelf_app_id = | 188 const std::string shelf_app_id = |
| 160 ArcAppWindowLauncherController::GetShelfAppIdFromArcAppId(app_id); | 189 ArcAppWindowLauncherController::GetShelfAppIdFromArcAppId(app_id); |
| 161 const ash::ShelfID shelf_id = owner_->GetShelfIDForAppID(shelf_app_id); | 190 const ash::ShelfID shelf_id = owner_->GetShelfIDForAppID(shelf_app_id); |
| 162 | 191 |
| 163 if (shelf_id) { | 192 if (shelf_id) { |
| 164 LauncherItemController* controller = | 193 LauncherItemController* controller = |
| 165 owner_->GetLauncherItemController(shelf_id); | 194 owner_->GetLauncherItemController(shelf_id); |
| 166 if (controller && | 195 if (controller && |
| 167 controller->type() != LauncherItemController::TYPE_SHORTCUT) { | 196 controller->type() != LauncherItemController::TYPE_SHORTCUT) { |
| 168 // We are allowed to apply new deferred controller only over shortcut. | 197 // We are allowed to apply new deferred controller only over shortcut. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 179 } else { | 208 } else { |
| 180 owner_->SetItemController(shelf_id, controller); | 209 owner_->SetItemController(shelf_id, controller); |
| 181 owner_->SetItemStatus(shelf_id, ash::STATUS_RUNNING); | 210 owner_->SetItemStatus(shelf_id, ash::STATUS_RUNNING); |
| 182 } | 211 } |
| 183 | 212 |
| 184 if (app_controller_map_.empty()) | 213 if (app_controller_map_.empty()) |
| 185 RegisterNextUpdate(); | 214 RegisterNextUpdate(); |
| 186 | 215 |
| 187 app_controller_map_[shelf_app_id] = controller; | 216 app_controller_map_[shelf_app_id] = controller; |
| 188 } | 217 } |
| OLD | NEW |