| OLD | NEW |
| (Empty) | |
| 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 |
| 5 #include <chrome/browser/chromeos/app_mode/arc/arc_kiosk_app_service.h> |
| 6 |
| 7 #include "chrome/browser/chromeos/app_mode/arc/arc_kiosk_app_service_factory.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/profiles/profile_manager.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" |
| 12 #include "chrome/common/pref_names.h" |
| 13 #include "components/prefs/pref_service.h" |
| 14 |
| 15 namespace chromeos { |
| 16 |
| 17 // static |
| 18 ArcKioskAppService* ArcKioskAppService::Create(Profile* profile, |
| 19 ArcAppListPrefs* prefs) { |
| 20 return new ArcKioskAppService(profile, prefs); |
| 21 } |
| 22 |
| 23 // static |
| 24 ArcKioskAppService* ArcKioskAppService::Get(content::BrowserContext* context) { |
| 25 return ArcKioskAppServiceFactory::GetForBrowserContext(context); |
| 26 } |
| 27 |
| 28 void ArcKioskAppService::OnAppRegistered( |
| 29 const std::string& app_id, |
| 30 const ArcAppListPrefs::AppInfo& app_info) { |
| 31 if (app_id == app_id_) |
| 32 PreconditionsChanged(); |
| 33 } |
| 34 |
| 35 void ArcKioskAppService::OnAppReadyChanged(const std::string& id, bool ready) { |
| 36 if (id == app_id_) |
| 37 PreconditionsChanged(); |
| 38 } |
| 39 |
| 40 void ArcKioskAppService::OnPackageListInitialRefreshed() { |
| 41 // The app could already be registered. |
| 42 app_id_ = GetAppId(); |
| 43 PreconditionsChanged(); |
| 44 } |
| 45 |
| 46 void ArcKioskAppService::OnArcKioskAppsChanged() { |
| 47 app_id_ = GetAppId(); |
| 48 PreconditionsChanged(); |
| 49 } |
| 50 |
| 51 void ArcKioskAppService::OnTaskCreated(int32_t task_id, |
| 52 const std::string& package_name, |
| 53 const std::string& activity) { |
| 54 // Store task id of the app to stop it later when needed. |
| 55 if (app_info_ && package_name == app_info_->package_name && |
| 56 activity == app_info_->activity) { |
| 57 task_id_ = task_id; |
| 58 } |
| 59 } |
| 60 |
| 61 void ArcKioskAppService::OnTaskDestroyed(int32_t task_id) { |
| 62 if (task_id == task_id_) { |
| 63 app_launcher_.reset(); |
| 64 task_id_ = -1; |
| 65 } |
| 66 } |
| 67 |
| 68 ArcKioskAppService::ArcKioskAppService(Profile* profile, ArcAppListPrefs* prefs) |
| 69 : profile_(profile), prefs_(prefs) { |
| 70 if (prefs_) |
| 71 prefs_->AddObserver(this); |
| 72 app_manager_ = ArcKioskAppManager::Get(); |
| 73 if (app_manager_) { |
| 74 app_manager_->AddObserver(this); |
| 75 app_id_ = GetAppId(); |
| 76 } |
| 77 pref_change_registrar_.reset(new PrefChangeRegistrar()); |
| 78 pref_change_registrar_->Init(profile_->GetPrefs()); |
| 79 // Try to start/stop kiosk app on policy compliance state change. |
| 80 pref_change_registrar_->Add( |
| 81 prefs::kArcPolicyCompliant, |
| 82 base::Bind(&ArcKioskAppService::PreconditionsChanged, |
| 83 base::Unretained(this))); |
| 84 PreconditionsChanged(); |
| 85 } |
| 86 |
| 87 ArcKioskAppService::~ArcKioskAppService() { |
| 88 if (prefs_) |
| 89 prefs_->RemoveObserver(this); |
| 90 if (app_manager_) |
| 91 app_manager_->RemoveObserver(this); |
| 92 } |
| 93 |
| 94 void ArcKioskAppService::PreconditionsChanged() { |
| 95 app_info_ = prefs_->GetApp(app_id_); |
| 96 if (app_info_ && app_info_->ready && |
| 97 profile_->GetPrefs()->GetBoolean(prefs::kArcPolicyCompliant)) { |
| 98 if (!app_launcher_) |
| 99 app_launcher_.reset(new ArcKioskAppLauncher(profile_, prefs_, app_id_)); |
| 100 } else if (task_id_ != -1) { |
| 101 arc::CloseTask(task_id_); |
| 102 } |
| 103 } |
| 104 |
| 105 std::string ArcKioskAppService::GetAppId() { |
| 106 AccountId account_id = multi_user_util::GetAccountIdFromProfile(profile_); |
| 107 const ArcKioskAppManager::ArcKioskApp* app = |
| 108 app_manager_->GetAppByAccountId(account_id); |
| 109 if (!app) |
| 110 return std::string(); |
| 111 std::unordered_set<std::string> app_ids = |
| 112 prefs_->GetAppsForPackage(app->app_info().package_name()); |
| 113 if (app_ids.empty()) |
| 114 return std::string(); |
| 115 // TODO(poromov@): Choose appropriate app id to launch. See |
| 116 // http://crbug.com/665904 |
| 117 return std::string(*app_ids.begin()); |
| 118 } |
| 119 |
| 120 } // namespace chromeos |
| OLD | NEW |