Chromium Code Reviews| 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/chromeos/app_mode/arc/arc_kiosk_app_service.h> | 5 #include <chrome/browser/chromeos/app_mode/arc/arc_kiosk_app_service.h> | 
| 6 | 6 | 
| 7 #include "chrome/browser/chromeos/app_mode/arc/arc_kiosk_app_service_factory.h" | 7 #include "chrome/browser/chromeos/app_mode/arc/arc_kiosk_app_service_factory.h" | 
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" | 
| 9 #include "chrome/browser/profiles/profile_manager.h" | 9 #include "chrome/browser/profiles/profile_manager.h" | 
| 10 #include "chrome/browser/ui/app_list/arc/arc_app_utils.h" | 10 #include "chrome/browser/ui/app_list/arc/arc_app_utils.h" | 
| 11 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | 11 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | 
| 12 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" | 
| 13 #include "components/prefs/pref_service.h" | 13 #include "components/prefs/pref_service.h" | 
| 14 | 14 | 
| 15 namespace chromeos { | 15 namespace chromeos { | 
| 16 | 16 | 
| 17 // static | 17 // static | 
| 18 ArcKioskAppService* ArcKioskAppService::Create(Profile* profile) { | 18 ArcKioskAppService* ArcKioskAppService::Create(Profile* profile) { | 
| 19 return new ArcKioskAppService(profile); | 19 return new ArcKioskAppService(profile); | 
| 20 } | 20 } | 
| 21 | 21 | 
| 22 // static | 22 // static | 
| 23 ArcKioskAppService* ArcKioskAppService::Get(content::BrowserContext* context) { | 23 ArcKioskAppService* ArcKioskAppService::Get(content::BrowserContext* context) { | 
| 24 return ArcKioskAppServiceFactory::GetForBrowserContext(context); | 24 return ArcKioskAppServiceFactory::GetForBrowserContext(context); | 
| 25 } | 25 } | 
| 26 | 26 | 
| 27 void ArcKioskAppService::SetDelegate(Delegate* delegate) { | |
| 28 delegate_ = delegate; | |
| 
 
Luis Héctor Chávez
2017/01/24 18:40:24
nit: DCHECK(delegate);
Do you also want to DCHECK
 
Sergey Poromov
2017/01/25 14:29:21
This |delegate| could be null to unregister it.
No
 
 | |
| 29 } | |
| 30 | |
| 27 void ArcKioskAppService::Shutdown() { | 31 void ArcKioskAppService::Shutdown() { | 
| 28 ArcAppListPrefs::Get(profile_)->RemoveObserver(this); | 32 ArcAppListPrefs::Get(profile_)->RemoveObserver(this); | 
| 29 app_manager_->RemoveObserver(this); | 33 app_manager_->RemoveObserver(this); | 
| 30 } | 34 } | 
| 31 | 35 | 
| 32 void ArcKioskAppService::OnAppRegistered( | 36 void ArcKioskAppService::OnAppRegistered( | 
| 33 const std::string& app_id, | 37 const std::string& app_id, | 
| 34 const ArcAppListPrefs::AppInfo& app_info) { | 38 const ArcAppListPrefs::AppInfo& app_info) { | 
| 39 if (!app_id_.empty() && app_id != app_id_) | |
| 40 return; | |
| 35 PreconditionsChanged(); | 41 PreconditionsChanged(); | 
| 36 } | 42 } | 
| 37 | 43 | 
| 38 void ArcKioskAppService::OnAppReadyChanged(const std::string& id, bool ready) { | 44 void ArcKioskAppService::OnAppReadyChanged(const std::string& id, bool ready) { | 
| 45 if (!app_id_.empty() && id != app_id_) | |
| 46 return; | |
| 39 PreconditionsChanged(); | 47 PreconditionsChanged(); | 
| 40 } | 48 } | 
| 41 | 49 | 
| 42 void ArcKioskAppService::OnPackageListInitialRefreshed() { | 50 void ArcKioskAppService::OnPackageListInitialRefreshed() { | 
| 43 // The app could already be registered. | 51 // The app could already be registered. | 
| 44 PreconditionsChanged(); | 52 PreconditionsChanged(); | 
| 45 } | 53 } | 
| 46 | 54 | 
| 47 void ArcKioskAppService::OnArcKioskAppsChanged() { | 55 void ArcKioskAppService::OnArcKioskAppsChanged() { | 
| 48 PreconditionsChanged(); | 56 PreconditionsChanged(); | 
| 49 } | 57 } | 
| 50 | 58 | 
| 51 void ArcKioskAppService::OnTaskCreated(int32_t task_id, | 59 void ArcKioskAppService::OnTaskCreated(int32_t task_id, | 
| 52 const std::string& package_name, | 60 const std::string& package_name, | 
| 53 const std::string& activity, | 61 const std::string& activity, | 
| 54 const std::string& intent) { | 62 const std::string& intent) { | 
| 55 // Store task id of the app to stop it later when needed. | 63 // Store task id of the app to stop it later when needed. | 
| 56 if (app_info_ && package_name == app_info_->package_name && | 64 if (app_info_ && package_name == app_info_->package_name && | 
| 57 activity == app_info_->activity) { | 65 activity == app_info_->activity) { | 
| 58 task_id_ = task_id; | 66 task_id_ = task_id; | 
| 67 if (delegate_) | |
| 68 delegate_->OnAppStarted(); | |
| 59 } | 69 } | 
| 60 } | 70 } | 
| 61 | 71 | 
| 62 void ArcKioskAppService::OnTaskDestroyed(int32_t task_id) { | 72 void ArcKioskAppService::OnTaskDestroyed(int32_t task_id) { | 
| 63 if (task_id == task_id_) { | 73 if (task_id == task_id_) { | 
| 64 app_launcher_.reset(); | 74 app_launcher_.reset(); | 
| 65 task_id_ = -1; | 75 task_id_ = -1; | 
| 66 // Trying to restart app if it was somehow closed or crashed | 76 // Trying to restart app if it was somehow closed or crashed | 
| 67 // as kiosk app should always be running during the session. | 77 // as kiosk app should always be running during the session. | 
| 68 PreconditionsChanged(); | 78 PreconditionsChanged(); | 
| 69 } | 79 } | 
| 70 } | 80 } | 
| 71 | 81 | 
| 72 void ArcKioskAppService::OnMaintenanceSessionCreated() { | 82 void ArcKioskAppService::OnMaintenanceSessionCreated() { | 
| 73 maintenance_session_running_ = true; | 83 maintenance_session_running_ = true; | 
| 74 PreconditionsChanged(); | 84 PreconditionsChanged(); | 
| 75 } | 85 } | 
| 76 | 86 | 
| 77 void ArcKioskAppService::OnMaintenanceSessionFinished() { | 87 void ArcKioskAppService::OnMaintenanceSessionFinished() { | 
| 78 maintenance_session_running_ = false; | 88 maintenance_session_running_ = false; | 
| 79 PreconditionsChanged(); | 89 PreconditionsChanged(); | 
| 80 } | 90 } | 
| 81 | 91 | 
| 92 void ArcKioskAppService::OnAppWindowLaunched() { | |
| 93 if (delegate_) | |
| 94 delegate_->OnAppWindowLaunched(); | |
| 95 } | |
| 96 | |
| 82 ArcKioskAppService::ArcKioskAppService(Profile* profile) : profile_(profile) { | 97 ArcKioskAppService::ArcKioskAppService(Profile* profile) : profile_(profile) { | 
| 83 ArcAppListPrefs::Get(profile_)->AddObserver(this); | 98 ArcAppListPrefs::Get(profile_)->AddObserver(this); | 
| 84 app_manager_ = ArcKioskAppManager::Get(); | 99 app_manager_ = ArcKioskAppManager::Get(); | 
| 85 DCHECK(app_manager_); | 100 DCHECK(app_manager_); | 
| 86 app_manager_->AddObserver(this); | 101 app_manager_->AddObserver(this); | 
| 87 pref_change_registrar_.reset(new PrefChangeRegistrar()); | 102 pref_change_registrar_.reset(new PrefChangeRegistrar()); | 
| 88 pref_change_registrar_->Init(profile_->GetPrefs()); | 103 pref_change_registrar_->Init(profile_->GetPrefs()); | 
| 89 // Try to start/stop kiosk app on policy compliance state change. | 104 // Try to start/stop kiosk app on policy compliance state change. | 
| 90 pref_change_registrar_->Add( | 105 pref_change_registrar_->Add( | 
| 91 prefs::kArcPolicyCompliant, | 106 prefs::kArcPolicyCompliant, | 
| 92 base::Bind(&ArcKioskAppService::PreconditionsChanged, | 107 base::Bind(&ArcKioskAppService::PreconditionsChanged, | 
| 93 base::Unretained(this))); | 108 base::Unretained(this))); | 
| 94 PreconditionsChanged(); | 109 PreconditionsChanged(); | 
| 95 } | 110 } | 
| 96 | 111 | 
| 97 ArcKioskAppService::~ArcKioskAppService() = default; | 112 ArcKioskAppService::~ArcKioskAppService() = default; | 
| 98 | 113 | 
| 99 void ArcKioskAppService::PreconditionsChanged() { | 114 void ArcKioskAppService::PreconditionsChanged() { | 
| 100 app_id_ = GetAppId(); | 115 app_id_ = GetAppId(); | 
| 101 if (app_id_.empty()) | 116 if (app_id_.empty()) | 
| 102 return; | 117 return; | 
| 103 app_info_ = ArcAppListPrefs::Get(profile_)->GetApp(app_id_); | 118 app_info_ = ArcAppListPrefs::Get(profile_)->GetApp(app_id_); | 
| 104 if (app_info_ && app_info_->ready && | 119 if (app_info_ && app_info_->ready && | 
| 105 profile_->GetPrefs()->GetBoolean(prefs::kArcPolicyCompliant) && | 120 profile_->GetPrefs()->GetBoolean(prefs::kArcPolicyCompliant) && | 
| 106 !maintenance_session_running_) { | 121 !maintenance_session_running_) { | 
| 107 if (!app_launcher_) | 122 if (!app_launcher_) | 
| 108 app_launcher_.reset(new ArcKioskAppLauncher( | 123 app_launcher_.reset(new ArcKioskAppLauncher( | 
| 
 
Luis Héctor Chávez
2017/01/24 18:40:24
nit: app_launcher_ = base::MakeUnique<ArcKioskAppL
 
Sergey Poromov
2017/01/25 14:29:21
Done.
 
 | |
| 109 profile_, ArcAppListPrefs::Get(profile_), app_id_)); | 124 profile_, ArcAppListPrefs::Get(profile_), app_id_, this)); | 
| 110 } else if (task_id_ != -1) { | 125 } else if (task_id_ != -1) { | 
| 111 arc::CloseTask(task_id_); | 126 arc::CloseTask(task_id_); | 
| 112 } | 127 } | 
| 113 } | 128 } | 
| 114 | 129 | 
| 115 std::string ArcKioskAppService::GetAppId() { | 130 std::string ArcKioskAppService::GetAppId() { | 
| 116 AccountId account_id = multi_user_util::GetAccountIdFromProfile(profile_); | 131 AccountId account_id = multi_user_util::GetAccountIdFromProfile(profile_); | 
| 117 const ArcKioskAppManager::ArcKioskApp* app = | 132 const ArcKioskAppManager::ArcKioskApp* app = | 
| 118 app_manager_->GetAppByAccountId(account_id); | 133 app_manager_->GetAppByAccountId(account_id); | 
| 119 if (!app) | 134 if (!app) | 
| 120 return std::string(); | 135 return std::string(); | 
| 121 std::unordered_set<std::string> app_ids = | 136 std::unordered_set<std::string> app_ids = | 
| 122 ArcAppListPrefs::Get(profile_)->GetAppsForPackage( | 137 ArcAppListPrefs::Get(profile_)->GetAppsForPackage( | 
| 123 app->app_info().package_name()); | 138 app->app_info().package_name()); | 
| 124 if (app_ids.empty()) | 139 if (app_ids.empty()) | 
| 125 return std::string(); | 140 return std::string(); | 
| 126 // TODO(poromov@): Choose appropriate app id to launch. See | 141 // TODO(poromov@): Choose appropriate app id to launch. See | 
| 127 // http://crbug.com/665904 | 142 // http://crbug.com/665904 | 
| 128 return std::string(*app_ids.begin()); | 143 return std::string(*app_ids.begin()); | 
| 129 } | 144 } | 
| 130 | 145 | 
| 131 } // namespace chromeos | 146 } // namespace chromeos | 
| OLD | NEW |