| 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" | 
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   44 // static |   44 // static | 
|   45 ArcKioskAppService* ArcKioskAppService::Create(Profile* profile) { |   45 ArcKioskAppService* ArcKioskAppService::Create(Profile* profile) { | 
|   46   return new ArcKioskAppService(profile); |   46   return new ArcKioskAppService(profile); | 
|   47 } |   47 } | 
|   48  |   48  | 
|   49 // static |   49 // static | 
|   50 ArcKioskAppService* ArcKioskAppService::Get(content::BrowserContext* context) { |   50 ArcKioskAppService* ArcKioskAppService::Get(content::BrowserContext* context) { | 
|   51   return ArcKioskAppServiceFactory::GetForBrowserContext(context); |   51   return ArcKioskAppServiceFactory::GetForBrowserContext(context); | 
|   52 } |   52 } | 
|   53  |   53  | 
 |   54 void ArcKioskAppService::SetDelegate(Delegate* delegate) { | 
 |   55   delegate_ = delegate; | 
 |   56 } | 
 |   57  | 
|   54 void ArcKioskAppService::Shutdown() { |   58 void ArcKioskAppService::Shutdown() { | 
|   55   ArcAppListPrefs::Get(profile_)->RemoveObserver(this); |   59   ArcAppListPrefs::Get(profile_)->RemoveObserver(this); | 
|   56   app_manager_->RemoveObserver(this); |   60   app_manager_->RemoveObserver(this); | 
|   57 } |   61 } | 
|   58  |   62  | 
|   59 void ArcKioskAppService::OnAppRegistered( |   63 void ArcKioskAppService::OnAppRegistered( | 
|   60     const std::string& app_id, |   64     const std::string& app_id, | 
|   61     const ArcAppListPrefs::AppInfo& app_info) { |   65     const ArcAppListPrefs::AppInfo& app_info) { | 
 |   66   if (!app_id_.empty() && app_id != app_id_) | 
 |   67     return; | 
|   62   PreconditionsChanged(); |   68   PreconditionsChanged(); | 
|   63 } |   69 } | 
|   64  |   70  | 
|   65 void ArcKioskAppService::OnAppReadyChanged(const std::string& id, bool ready) { |   71 void ArcKioskAppService::OnAppReadyChanged(const std::string& id, bool ready) { | 
 |   72   if (!app_id_.empty() && id != app_id_) | 
 |   73     return; | 
|   66   PreconditionsChanged(); |   74   PreconditionsChanged(); | 
|   67 } |   75 } | 
|   68  |   76  | 
|   69 void ArcKioskAppService::OnPackageListInitialRefreshed() { |   77 void ArcKioskAppService::OnPackageListInitialRefreshed() { | 
|   70   // The app could already be registered. |   78   // The app could already be registered. | 
|   71   PreconditionsChanged(); |   79   PreconditionsChanged(); | 
|   72 } |   80 } | 
|   73  |   81  | 
|   74 void ArcKioskAppService::OnArcKioskAppsChanged() { |   82 void ArcKioskAppService::OnArcKioskAppsChanged() { | 
|   75   PreconditionsChanged(); |   83   PreconditionsChanged(); | 
|   76 } |   84 } | 
|   77  |   85  | 
|   78 void ArcKioskAppService::OnTaskCreated(int32_t task_id, |   86 void ArcKioskAppService::OnTaskCreated(int32_t task_id, | 
|   79                                        const std::string& package_name, |   87                                        const std::string& package_name, | 
|   80                                        const std::string& activity, |   88                                        const std::string& activity, | 
|   81                                        const std::string& intent) { |   89                                        const std::string& intent) { | 
|   82   // Store task id of the app to stop it later when needed. |   90   // Store task id of the app to stop it later when needed. | 
|   83   if (app_info_ && package_name == app_info_->package_name && |   91   if (app_info_ && package_name == app_info_->package_name && | 
|   84       activity == app_info_->activity) { |   92       activity == app_info_->activity) { | 
|   85     task_id_ = task_id; |   93     task_id_ = task_id; | 
 |   94     if (delegate_) | 
 |   95       delegate_->OnAppStarted(); | 
|   86   } |   96   } | 
|   87 } |   97 } | 
|   88  |   98  | 
|   89 void ArcKioskAppService::OnTaskDestroyed(int32_t task_id) { |   99 void ArcKioskAppService::OnTaskDestroyed(int32_t task_id) { | 
|   90   if (task_id == task_id_) { |  100   if (task_id == task_id_) { | 
|   91     app_launcher_.reset(); |  101     app_launcher_.reset(); | 
|   92     task_id_ = -1; |  102     task_id_ = -1; | 
|   93     // Trying to restart app if it was somehow closed or crashed |  103     // Trying to restart app if it was somehow closed or crashed | 
|   94     // as kiosk app should always be running during the session. |  104     // as kiosk app should always be running during the session. | 
|   95     PreconditionsChanged(); |  105     PreconditionsChanged(); | 
|   96   } |  106   } | 
|   97 } |  107 } | 
|   98  |  108  | 
|   99 void ArcKioskAppService::OnMaintenanceSessionCreated() { |  109 void ArcKioskAppService::OnMaintenanceSessionCreated() { | 
|  100   maintenance_session_running_ = true; |  110   maintenance_session_running_ = true; | 
|  101   PreconditionsChanged(); |  111   PreconditionsChanged(); | 
|  102 } |  112 } | 
|  103  |  113  | 
|  104 void ArcKioskAppService::OnMaintenanceSessionFinished() { |  114 void ArcKioskAppService::OnMaintenanceSessionFinished() { | 
|  105   maintenance_session_running_ = false; |  115   maintenance_session_running_ = false; | 
|  106   PreconditionsChanged(); |  116   PreconditionsChanged(); | 
|  107 } |  117 } | 
|  108  |  118  | 
 |  119 void ArcKioskAppService::OnAppWindowLaunched() { | 
 |  120   if (delegate_) | 
 |  121     delegate_->OnAppWindowLaunched(); | 
 |  122 } | 
 |  123  | 
|  109 ArcKioskAppService::ArcKioskAppService(Profile* profile) : profile_(profile) { |  124 ArcKioskAppService::ArcKioskAppService(Profile* profile) : profile_(profile) { | 
|  110   ArcAppListPrefs::Get(profile_)->AddObserver(this); |  125   ArcAppListPrefs::Get(profile_)->AddObserver(this); | 
|  111   app_manager_ = ArcKioskAppManager::Get(); |  126   app_manager_ = ArcKioskAppManager::Get(); | 
|  112   DCHECK(app_manager_); |  127   DCHECK(app_manager_); | 
|  113   app_manager_->AddObserver(this); |  128   app_manager_->AddObserver(this); | 
|  114   pref_change_registrar_.reset(new PrefChangeRegistrar()); |  129   pref_change_registrar_.reset(new PrefChangeRegistrar()); | 
|  115   pref_change_registrar_->Init(profile_->GetPrefs()); |  130   pref_change_registrar_->Init(profile_->GetPrefs()); | 
|  116   // Try to start/stop kiosk app on policy compliance state change. |  131   // Try to start/stop kiosk app on policy compliance state change. | 
|  117   pref_change_registrar_->Add( |  132   pref_change_registrar_->Add( | 
|  118       prefs::kArcPolicyCompliant, |  133       prefs::kArcPolicyCompliant, | 
|  119       base::Bind(&ArcKioskAppService::PreconditionsChanged, |  134       base::Bind(&ArcKioskAppService::PreconditionsChanged, | 
|  120                  base::Unretained(this))); |  135                  base::Unretained(this))); | 
|  121   notification_blocker_.reset(new ArcKioskNotificationBlocker()); |  136   notification_blocker_.reset(new ArcKioskNotificationBlocker()); | 
|  122   PreconditionsChanged(); |  137   PreconditionsChanged(); | 
|  123 } |  138 } | 
|  124  |  139  | 
|  125 ArcKioskAppService::~ArcKioskAppService() = default; |  140 ArcKioskAppService::~ArcKioskAppService() = default; | 
|  126  |  141  | 
|  127 void ArcKioskAppService::PreconditionsChanged() { |  142 void ArcKioskAppService::PreconditionsChanged() { | 
|  128   app_id_ = GetAppId(); |  143   app_id_ = GetAppId(); | 
|  129   if (app_id_.empty()) |  144   if (app_id_.empty()) | 
|  130     return; |  145     return; | 
|  131   app_info_ = ArcAppListPrefs::Get(profile_)->GetApp(app_id_); |  146   app_info_ = ArcAppListPrefs::Get(profile_)->GetApp(app_id_); | 
|  132   if (app_info_ && app_info_->ready && |  147   if (app_info_ && app_info_->ready && | 
|  133       profile_->GetPrefs()->GetBoolean(prefs::kArcPolicyCompliant) && |  148       profile_->GetPrefs()->GetBoolean(prefs::kArcPolicyCompliant) && | 
|  134       !maintenance_session_running_) { |  149       !maintenance_session_running_) { | 
|  135     if (!app_launcher_) |  150     if (!app_launcher_) | 
|  136       app_launcher_.reset(new ArcKioskAppLauncher( |  151       app_launcher_ = base::MakeUnique<ArcKioskAppLauncher>( | 
|  137           profile_, ArcAppListPrefs::Get(profile_), app_id_)); |  152           profile_, ArcAppListPrefs::Get(profile_), app_id_, this); | 
|  138   } else if (task_id_ != -1) { |  153   } else if (task_id_ != -1) { | 
|  139     arc::CloseTask(task_id_); |  154     arc::CloseTask(task_id_); | 
|  140   } |  155   } | 
|  141 } |  156 } | 
|  142  |  157  | 
|  143 std::string ArcKioskAppService::GetAppId() { |  158 std::string ArcKioskAppService::GetAppId() { | 
|  144   AccountId account_id = multi_user_util::GetAccountIdFromProfile(profile_); |  159   AccountId account_id = multi_user_util::GetAccountIdFromProfile(profile_); | 
|  145   const ArcKioskAppManager::ArcKioskApp* app = |  160   const ArcKioskAppManager::ArcKioskApp* app = | 
|  146       app_manager_->GetAppByAccountId(account_id); |  161       app_manager_->GetAppByAccountId(account_id); | 
|  147   if (!app) |  162   if (!app) | 
|  148     return std::string(); |  163     return std::string(); | 
|  149   std::unordered_set<std::string> app_ids = |  164   std::unordered_set<std::string> app_ids = | 
|  150       ArcAppListPrefs::Get(profile_)->GetAppsForPackage( |  165       ArcAppListPrefs::Get(profile_)->GetAppsForPackage( | 
|  151           app->app_info().package_name()); |  166           app->app_info().package_name()); | 
|  152   if (app_ids.empty()) |  167   if (app_ids.empty()) | 
|  153     return std::string(); |  168     return std::string(); | 
|  154   // TODO(poromov@): Choose appropriate app id to launch. See |  169   // TODO(poromov@): Choose appropriate app id to launch. See | 
|  155   // http://crbug.com/665904 |  170   // http://crbug.com/665904 | 
|  156   return std::string(*app_ids.begin()); |  171   return std::string(*app_ids.begin()); | 
|  157 } |  172 } | 
|  158  |  173  | 
|  159 }  // namespace chromeos |  174 }  // namespace chromeos | 
| OLD | NEW |