OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/app_list/arc/arc_app_list_prefs.h" | 5 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <unordered_map> | |
11 | 10 |
12 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
13 #include "base/macros.h" | |
14 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
15 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
16 #include "base/task_runner_util.h" | 14 #include "base/task_runner_util.h" |
17 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/ui/app_list/app_list_service.h" | 16 #include "chrome/browser/ui/app_list/app_list_service.h" |
19 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs_factory.h" | 17 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs_factory.h" |
20 #include "chrome/browser/ui/app_list/arc/arc_app_utils.h" | 18 #include "chrome/browser/ui/app_list/arc/arc_app_utils.h" |
21 #include "chrome/browser/ui/app_list/arc/arc_package_syncable_service.h" | 19 #include "chrome/browser/ui/app_list/arc/arc_package_syncable_service.h" |
22 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
23 #include "components/arc/arc_bridge_service.h" | 21 #include "components/arc/arc_bridge_service.h" |
24 #include "components/crx_file/id_util.h" | 22 #include "components/crx_file/id_util.h" |
25 #include "components/pref_registry/pref_registry_syncable.h" | 23 #include "components/pref_registry/pref_registry_syncable.h" |
26 #include "components/prefs/scoped_user_pref_update.h" | 24 #include "components/prefs/scoped_user_pref_update.h" |
27 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
28 | 26 |
29 namespace { | 27 namespace { |
30 | 28 |
31 const char kName[] = "name"; | |
32 const char kPackageName[] = "package_name"; | |
33 const char kPackageVersion[] = "package_version"; | |
34 const char kActivity[] = "activity"; | 29 const char kActivity[] = "activity"; |
35 const char kShortcut[] = "shortcut"; | 30 const char kIconResourceId[] = "icon_resource_id"; |
| 31 const char kInstallTime[] = "install_time"; |
36 const char kIntentUri[] = "intent_uri"; | 32 const char kIntentUri[] = "intent_uri"; |
37 const char kIconResourceId[] = "icon_resource_id"; | |
38 const char kSticky[] = "sticky"; | |
39 const char kNotificationsEnabled[] = "notifications_enabled"; | |
40 const char kLastBackupAndroidId[] = "last_backup_android_id"; | 33 const char kLastBackupAndroidId[] = "last_backup_android_id"; |
41 const char kLastBackupTime[] = "last_backup_time"; | 34 const char kLastBackupTime[] = "last_backup_time"; |
42 const char kLastLaunchTime[] = "lastlaunchtime"; | 35 const char kLastLaunchTime[] = "lastlaunchtime"; |
43 const char kLaunchable[] = "launchable"; | 36 const char kLaunchable[] = "launchable"; |
| 37 const char kName[] = "name"; |
| 38 const char kNotificationsEnabled[] = "notifications_enabled"; |
| 39 const char kOrientationLock[] = "orientation_lock"; |
| 40 const char kPackageName[] = "package_name"; |
| 41 const char kPackageVersion[] = "package_version"; |
| 42 const char kSticky[] = "sticky"; |
| 43 const char kShortcut[] = "shortcut"; |
44 const char kShouldSync[] = "should_sync"; | 44 const char kShouldSync[] = "should_sync"; |
45 const char kSystem[] = "system"; | 45 const char kSystem[] = "system"; |
46 const char kOrientationLock[] = "orientation_lock"; | 46 const char kUninstalled[] = "uninstalled"; |
47 const char kInstallTime[] = "install_time"; | |
48 | 47 |
49 constexpr int kSetNotificationsEnabledMinVersion = 6; | 48 constexpr int kSetNotificationsEnabledMinVersion = 6; |
50 | 49 |
51 // Provider of write access to a dictionary storing ARC prefs. | 50 // Provider of write access to a dictionary storing ARC prefs. |
52 class ScopedArcPrefUpdate : public DictionaryPrefUpdate { | 51 class ScopedArcPrefUpdate : public DictionaryPrefUpdate { |
53 public: | 52 public: |
54 ScopedArcPrefUpdate(PrefService* service, | 53 ScopedArcPrefUpdate(PrefService* service, |
55 const std::string& id, | 54 const std::string& id, |
56 const std::string& path) | 55 const std::string& path) |
57 : DictionaryPrefUpdate(service, path), id_(id) {} | 56 : DictionaryPrefUpdate(service, path), id_(id) {} |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 | 156 |
158 if (!base::StringToInt64(value_str, value)) { | 157 if (!base::StringToInt64(value_str, value)) { |
159 VLOG(2) << "Can't change string to int64_t. Invalid string value: " | 158 VLOG(2) << "Can't change string to int64_t. Invalid string value: " |
160 << value_str << "."; | 159 << value_str << "."; |
161 return false; | 160 return false; |
162 } | 161 } |
163 | 162 |
164 return true; | 163 return true; |
165 } | 164 } |
166 | 165 |
167 // Add or update local pref for given package. | |
168 void AddOrUpdatePackagePrefs(PrefService* prefs, | |
169 const arc::mojom::ArcPackageInfo& package) { | |
170 DCHECK(IsArcEnabled()); | |
171 const std::string& package_name = package.package_name; | |
172 if (package_name.empty()) { | |
173 VLOG(2) << "Package name cannot be empty."; | |
174 return; | |
175 } | |
176 ScopedArcPrefUpdate update(prefs, package_name, prefs::kArcPackages); | |
177 base::DictionaryValue* package_dict = update.Get(); | |
178 const std::string id_str = | |
179 base::Int64ToString(package.last_backup_android_id); | |
180 const std::string time_str = base::Int64ToString(package.last_backup_time); | |
181 | |
182 package_dict->SetBoolean(kShouldSync, package.sync); | |
183 package_dict->SetInteger(kPackageVersion, package.package_version); | |
184 package_dict->SetString(kLastBackupAndroidId, id_str); | |
185 package_dict->SetString(kLastBackupTime, time_str); | |
186 package_dict->SetBoolean(kSystem, package.system); | |
187 } | |
188 | |
189 // Remove given package from local pref. | |
190 void RemovePackageFromPrefs(PrefService* prefs, | |
191 const std::string& package_name) { | |
192 DCHECK(IsArcEnabled()); | |
193 DictionaryPrefUpdate update(prefs, prefs::kArcPackages); | |
194 base::DictionaryValue* packages = update.Get(); | |
195 const bool removed = packages->RemoveWithoutPathExpansion(package_name, | |
196 nullptr); | |
197 DCHECK(removed); | |
198 } | |
199 | |
200 } // namespace | 166 } // namespace |
201 | 167 |
202 // static | 168 // static |
203 ArcAppListPrefs* ArcAppListPrefs::Create( | 169 ArcAppListPrefs* ArcAppListPrefs::Create( |
204 Profile* profile, | 170 Profile* profile, |
205 arc::InstanceHolder<arc::mojom::AppInstance>* app_instance_holder) { | 171 arc::InstanceHolder<arc::mojom::AppInstance>* app_instance_holder) { |
206 return new ArcAppListPrefs(profile, app_instance_holder); | 172 return new ArcAppListPrefs(profile, app_instance_holder); |
207 } | 173 } |
208 | 174 |
209 // static | 175 // static |
(...skipping 17 matching lines...) Expand all Loading... |
227 } | 193 } |
228 | 194 |
229 ArcAppListPrefs::ArcAppListPrefs( | 195 ArcAppListPrefs::ArcAppListPrefs( |
230 Profile* profile, | 196 Profile* profile, |
231 arc::InstanceHolder<arc::mojom::AppInstance>* app_instance_holder) | 197 arc::InstanceHolder<arc::mojom::AppInstance>* app_instance_holder) |
232 : profile_(profile), | 198 : profile_(profile), |
233 prefs_(profile->GetPrefs()), | 199 prefs_(profile->GetPrefs()), |
234 app_instance_holder_(app_instance_holder), | 200 app_instance_holder_(app_instance_holder), |
235 sync_service_(nullptr), | 201 sync_service_(nullptr), |
236 binding_(this), | 202 binding_(this), |
| 203 default_apps_(this), |
237 weak_ptr_factory_(this) { | 204 weak_ptr_factory_(this) { |
238 DCHECK(profile); | 205 DCHECK(profile); |
239 DCHECK(app_instance_holder); | 206 DCHECK(app_instance_holder); |
| 207 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
240 const base::FilePath& base_path = profile->GetPath(); | 208 const base::FilePath& base_path = profile->GetPath(); |
241 base_path_ = base_path.AppendASCII(prefs::kArcApps); | 209 base_path_ = base_path.AppendASCII(prefs::kArcApps); |
242 | 210 |
243 arc::ArcAuthService* auth_service = arc::ArcAuthService::Get(); | 211 arc::ArcAuthService* auth_service = arc::ArcAuthService::Get(); |
244 if (!auth_service) | 212 if (!auth_service) |
245 return; | 213 return; |
246 | 214 |
247 if (auth_service->state() != arc::ArcAuthService::State::NOT_INITIALIZED) | 215 DCHECK(arc::ArcAuthService::IsAllowedForProfile(profile)); |
248 OnOptInEnabled(auth_service->IsArcEnabled()); | |
249 auth_service->AddObserver(this); | |
250 | 216 |
251 app_instance_holder_->AddObserver(this); | 217 // Once default apps are ready OnDefaultAppsReady is called. |
252 if (!app_instance_holder_->instance()) | |
253 OnInstanceClosed(); | |
254 } | 218 } |
255 | 219 |
256 ArcAppListPrefs::~ArcAppListPrefs() { | 220 ArcAppListPrefs::~ArcAppListPrefs() { |
257 // A reference to ArcBridgeService is kept here so that it would crash the | 221 // A reference to ArcBridgeService is kept here so that it would crash the |
258 // tests where ArcBridgeService and ArcAppListPrefs are not destroyed in right | 222 // tests where ArcBridgeService and ArcAppListPrefs are not destroyed in right |
259 // order. | 223 // order. |
260 arc::ArcBridgeService* bridge_service = arc::ArcBridgeService::Get(); | 224 arc::ArcBridgeService* bridge_service = arc::ArcBridgeService::Get(); |
261 if (bridge_service) | 225 if (bridge_service) |
262 app_instance_holder_->RemoveObserver(this); | 226 app_instance_holder_->RemoveObserver(this); |
263 | 227 |
264 arc::ArcAuthService* auth_service = arc::ArcAuthService::Get(); | 228 arc::ArcAuthService* auth_service = arc::ArcAuthService::Get(); |
265 if (auth_service) | 229 if (auth_service) |
266 auth_service->RemoveObserver(this); | 230 auth_service->RemoveObserver(this); |
267 } | 231 } |
268 | 232 |
| 233 void ArcAppListPrefs::StartPrefs() { |
| 234 arc::ArcAuthService* auth_service = arc::ArcAuthService::Get(); |
| 235 CHECK(auth_service); |
| 236 |
| 237 if (auth_service->state() != arc::ArcAuthService::State::NOT_INITIALIZED) |
| 238 OnOptInEnabled(auth_service->IsArcEnabled()); |
| 239 auth_service->AddObserver(this); |
| 240 |
| 241 app_instance_holder_->AddObserver(this); |
| 242 if (!app_instance_holder_->instance()) |
| 243 OnInstanceClosed(); |
| 244 } |
| 245 |
269 base::FilePath ArcAppListPrefs::GetAppPath(const std::string& app_id) const { | 246 base::FilePath ArcAppListPrefs::GetAppPath(const std::string& app_id) const { |
| 247 const ArcDefaultAppList::AppInfo* default_app = default_apps_.GetApp(app_id); |
| 248 if (default_app) { |
| 249 const base::DictionaryValue* app = nullptr; |
| 250 const base::DictionaryValue* apps = prefs_->GetDictionary(prefs::kArcApps); |
| 251 // If app is not ready yet, use resources from pre-build. |
| 252 if (!apps || !apps->GetDictionaryWithoutPathExpansion(app_id, &app)) |
| 253 return default_app->app_path; |
| 254 } |
| 255 |
270 return base_path_.AppendASCII(app_id); | 256 return base_path_.AppendASCII(app_id); |
271 } | 257 } |
272 | 258 |
273 base::FilePath ArcAppListPrefs::GetIconPath( | 259 base::FilePath ArcAppListPrefs::GetIconPath( |
274 const std::string& app_id, | 260 const std::string& app_id, |
275 ui::ScaleFactor scale_factor) const { | 261 ui::ScaleFactor scale_factor) const { |
276 const base::FilePath app_path = GetAppPath(app_id); | 262 const base::FilePath app_path = GetAppPath(app_id); |
277 switch (scale_factor) { | 263 switch (scale_factor) { |
278 case ui::SCALE_FACTOR_100P: | 264 case ui::SCALE_FACTOR_100P: |
279 return app_path.AppendASCII("icon_100p.png"); | 265 return app_path.AppendASCII("icon_100p.png"); |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 package->GetInteger(kPackageVersion, &package_version); | 399 package->GetInteger(kPackageVersion, &package_version); |
414 package->GetBoolean(kShouldSync, &should_sync); | 400 package->GetBoolean(kShouldSync, &should_sync); |
415 package->GetBoolean(kSystem, &system); | 401 package->GetBoolean(kSystem, &system); |
416 | 402 |
417 return base::MakeUnique<PackageInfo>(package_name, package_version, | 403 return base::MakeUnique<PackageInfo>(package_name, package_version, |
418 last_backup_android_id, last_backup_time, | 404 last_backup_android_id, last_backup_time, |
419 should_sync, system); | 405 should_sync, system); |
420 } | 406 } |
421 | 407 |
422 std::vector<std::string> ArcAppListPrefs::GetAppIds() const { | 408 std::vector<std::string> ArcAppListPrefs::GetAppIds() const { |
423 if (!IsArcEnabled()) | 409 if (!IsArcEnabled()) { |
424 return std::vector<std::string>(); | 410 // Default Arc apps available before OptIn. |
| 411 std::vector<std::string> ids; |
| 412 for (const auto& default_app : default_apps_.app_map()) { |
| 413 if (default_apps_.HasApp(default_app.first)) |
| 414 ids.push_back(default_app.first); |
| 415 } |
| 416 return ids; |
| 417 } |
425 return GetAppIdsNoArcEnabledCheck(); | 418 return GetAppIdsNoArcEnabledCheck(); |
426 } | 419 } |
427 | 420 |
428 std::vector<std::string> ArcAppListPrefs::GetAppIdsNoArcEnabledCheck() const { | 421 std::vector<std::string> ArcAppListPrefs::GetAppIdsNoArcEnabledCheck() const { |
429 std::vector<std::string> ids; | 422 std::vector<std::string> ids; |
430 | 423 |
| 424 const base::DictionaryValue* apps = prefs_->GetDictionary(prefs::kArcApps); |
| 425 DCHECK(apps); |
| 426 |
| 427 // Include default app ids into the list. In order not to list app twice, |
| 428 // at first step include default apps that do not exist yet. |
| 429 for (const auto& default_app : default_apps_.app_map()) { |
| 430 const std::string& app_id = default_app.first; |
| 431 const base::DictionaryValue* app = nullptr; |
| 432 if (default_apps_.HasApp(default_app.first) && |
| 433 !apps->GetDictionaryWithoutPathExpansion(app_id, &app)) { |
| 434 ids.push_back(app_id); |
| 435 } |
| 436 } |
| 437 |
431 // crx_file::id_util is de-facto utility for id generation. | 438 // crx_file::id_util is de-facto utility for id generation. |
432 const base::DictionaryValue* apps = prefs_->GetDictionary(prefs::kArcApps); | |
433 for (base::DictionaryValue::Iterator app_id(*apps); !app_id.IsAtEnd(); | 439 for (base::DictionaryValue::Iterator app_id(*apps); !app_id.IsAtEnd(); |
434 app_id.Advance()) { | 440 app_id.Advance()) { |
435 if (!crx_file::id_util::IdIsValid(app_id.key())) | 441 if (!crx_file::id_util::IdIsValid(app_id.key())) |
436 continue; | 442 continue; |
437 | 443 |
438 ids.push_back(app_id.key()); | 444 ids.push_back(app_id.key()); |
439 } | 445 } |
440 | 446 |
441 return ids; | 447 return ids; |
442 } | 448 } |
443 | 449 |
444 std::unique_ptr<ArcAppListPrefs::AppInfo> ArcAppListPrefs::GetApp( | 450 std::unique_ptr<ArcAppListPrefs::AppInfo> ArcAppListPrefs::GetApp( |
445 const std::string& app_id) const { | 451 const std::string& app_id) const { |
446 if (!IsArcEnabled()) | 452 // Information for default app is available before Arc enabled. |
| 453 const ArcDefaultAppList::AppInfo* default_app = default_apps_.GetApp(app_id); |
| 454 if (!IsArcEnabled() && !default_app) |
447 return std::unique_ptr<AppInfo>(); | 455 return std::unique_ptr<AppInfo>(); |
448 | 456 |
449 const base::DictionaryValue* app = nullptr; | 457 const base::DictionaryValue* app = nullptr; |
450 const base::DictionaryValue* apps = prefs_->GetDictionary(prefs::kArcApps); | 458 const base::DictionaryValue* apps = prefs_->GetDictionary(prefs::kArcApps); |
451 if (!apps || !apps->GetDictionaryWithoutPathExpansion(app_id, &app)) | 459 if (!apps || !apps->GetDictionaryWithoutPathExpansion(app_id, &app)) { |
| 460 // Default app may not be available at this point so create app info based |
| 461 // on its forward declaration. |
| 462 if (default_app) { |
| 463 return base::MakeUnique<AppInfo>( |
| 464 default_app->name, default_app->package_name, default_app->activity, |
| 465 std::string() /* intent_uri */, std::string() /* icon_resource_id */, |
| 466 base::Time() /* last_launch_time */, base::Time() /* install_time */, |
| 467 false /* sticky */, false /* notifications_enabled */, |
| 468 false /* ready */, true /* showInLauncher */, false /* shortcut */, |
| 469 true /* launchable */, arc::mojom::OrientationLock::NONE); |
| 470 } |
452 return std::unique_ptr<AppInfo>(); | 471 return std::unique_ptr<AppInfo>(); |
| 472 } |
453 | 473 |
454 std::string name; | 474 std::string name; |
455 std::string package_name; | 475 std::string package_name; |
456 std::string activity; | 476 std::string activity; |
457 std::string intent_uri; | 477 std::string intent_uri; |
458 std::string icon_resource_id; | 478 std::string icon_resource_id; |
459 bool sticky = false; | 479 bool sticky = false; |
460 bool notifications_enabled = true; | 480 bool notifications_enabled = true; |
461 bool shortcut = false; | 481 bool shortcut = false; |
462 bool launchable = true; | 482 bool launchable = true; |
(...skipping 28 matching lines...) Expand all Loading... |
491 | 511 |
492 return base::MakeUnique<AppInfo>( | 512 return base::MakeUnique<AppInfo>( |
493 name, package_name, activity, intent_uri, icon_resource_id, | 513 name, package_name, activity, intent_uri, icon_resource_id, |
494 last_launch_time, GetInstallTime(app_id), sticky, notifications_enabled, | 514 last_launch_time, GetInstallTime(app_id), sticky, notifications_enabled, |
495 ready_apps_.count(app_id) > 0, | 515 ready_apps_.count(app_id) > 0, |
496 launchable && arc::ShouldShowInLauncher(app_id), shortcut, launchable, | 516 launchable && arc::ShouldShowInLauncher(app_id), shortcut, launchable, |
497 orientation_lock); | 517 orientation_lock); |
498 } | 518 } |
499 | 519 |
500 bool ArcAppListPrefs::IsRegistered(const std::string& app_id) const { | 520 bool ArcAppListPrefs::IsRegistered(const std::string& app_id) const { |
| 521 if (default_apps_.HasApp(app_id)) |
| 522 return true; |
| 523 |
501 if (!IsArcEnabled()) | 524 if (!IsArcEnabled()) |
502 return false; | 525 return false; |
503 | 526 |
504 const base::DictionaryValue* app = nullptr; | 527 const base::DictionaryValue* app = nullptr; |
505 const base::DictionaryValue* apps = prefs_->GetDictionary(prefs::kArcApps); | 528 const base::DictionaryValue* apps = prefs_->GetDictionary(prefs::kArcApps); |
506 return apps && apps->GetDictionaryWithoutPathExpansion(app_id, &app); | 529 return apps && apps->GetDictionaryWithoutPathExpansion(app_id, &app); |
507 } | 530 } |
508 | 531 |
| 532 bool ArcAppListPrefs::IsDefault(const std::string& app_id) const { |
| 533 return default_apps_.HasApp(app_id); |
| 534 } |
| 535 |
| 536 bool ArcAppListPrefs::IsOem(const std::string& app_id) const { |
| 537 const ArcDefaultAppList::AppInfo* app_info = default_apps_.GetApp(app_id); |
| 538 return app_info && app_info->oem; |
| 539 } |
| 540 |
509 bool ArcAppListPrefs::IsShortcut(const std::string& app_id) const { | 541 bool ArcAppListPrefs::IsShortcut(const std::string& app_id) const { |
510 std::unique_ptr<ArcAppListPrefs::AppInfo> app_info = GetApp(app_id); | 542 std::unique_ptr<ArcAppListPrefs::AppInfo> app_info = GetApp(app_id); |
511 return app_info && app_info->shortcut; | 543 return app_info && app_info->shortcut; |
512 } | 544 } |
513 | 545 |
514 void ArcAppListPrefs::SetLastLaunchTime(const std::string& app_id, | 546 void ArcAppListPrefs::SetLastLaunchTime(const std::string& app_id, |
515 const base::Time& time) { | 547 const base::Time& time) { |
516 if (!IsRegistered(app_id)) { | 548 if (!IsRegistered(app_id)) { |
517 NOTREACHED(); | 549 NOTREACHED(); |
518 return; | 550 return; |
(...skipping 23 matching lines...) Expand all Loading... |
542 return; | 574 return; |
543 | 575 |
544 DCHECK(ready_apps_.empty()); | 576 DCHECK(ready_apps_.empty()); |
545 std::vector<std::string> app_ids = GetAppIdsNoArcEnabledCheck(); | 577 std::vector<std::string> app_ids = GetAppIdsNoArcEnabledCheck(); |
546 for (const auto& app_id : app_ids) { | 578 for (const auto& app_id : app_ids) { |
547 std::unique_ptr<AppInfo> app_info = GetApp(app_id); | 579 std::unique_ptr<AppInfo> app_info = GetApp(app_id); |
548 if (!app_info) { | 580 if (!app_info) { |
549 NOTREACHED(); | 581 NOTREACHED(); |
550 continue; | 582 continue; |
551 } | 583 } |
| 584 // Default apps are reported earlier. |
| 585 if (default_apps_.HasApp(app_id)) |
| 586 continue; |
552 FOR_EACH_OBSERVER(Observer, observer_list_, | 587 FOR_EACH_OBSERVER(Observer, observer_list_, |
553 OnAppRegistered(app_id, *app_info)); | 588 OnAppRegistered(app_id, *app_info)); |
554 } | 589 } |
555 | 590 |
556 apps_restored_ = true; | 591 apps_restored_ = true; |
557 } | 592 } |
558 | 593 |
559 void ArcAppListPrefs::RemoveAllApps() { | 594 void ArcAppListPrefs::RemoveAllApps() { |
560 std::vector<std::string> app_ids = GetAppIdsNoArcEnabledCheck(); | 595 std::vector<std::string> app_ids = GetAppIdsNoArcEnabledCheck(); |
561 for (const auto& app_id : app_ids) | 596 for (const auto& app_id : app_ids) { |
562 RemoveApp(app_id); | 597 if (!default_apps_.HasApp(app_id)) |
| 598 RemoveApp(app_id); |
| 599 } |
563 } | 600 } |
564 | 601 |
565 void ArcAppListPrefs::OnOptInEnabled(bool enabled) { | 602 void ArcAppListPrefs::OnOptInEnabled(bool enabled) { |
566 if (enabled) | 603 if (enabled) |
567 NotifyRegisteredApps(); | 604 NotifyRegisteredApps(); |
568 else | 605 else |
569 RemoveAllApps(); | 606 RemoveAllApps(); |
570 } | 607 } |
571 | 608 |
| 609 void ArcAppListPrefs::OnDefaultAppsReady() { |
| 610 // Apply uninstalled packages now. |
| 611 const std::vector<std::string> uninstalled_package_names = |
| 612 GetPackagesFromPrefs(false); |
| 613 for (const auto& uninstalled_package_name : uninstalled_package_names) |
| 614 default_apps_.MaybeMarkPackageUninstalled(uninstalled_package_name, true); |
| 615 |
| 616 // Report default apps first. |
| 617 for (const auto& default_app : default_apps_.app_map()) { |
| 618 const std::string& app_id = default_app.first; |
| 619 std::unique_ptr<AppInfo> app_info = GetApp(app_id); |
| 620 if (app_info) { |
| 621 FOR_EACH_OBSERVER(Observer, observer_list_, |
| 622 OnAppRegistered(app_id, *app_info)); |
| 623 } |
| 624 } |
| 625 |
| 626 default_apps_ready_ = true; |
| 627 if (!default_apps_ready_callback_.is_null()) |
| 628 default_apps_ready_callback_.Run(); |
| 629 |
| 630 StartPrefs(); |
| 631 } |
| 632 |
| 633 void ArcAppListPrefs::SetDefaltAppsReadyCallback(base::Closure callback) { |
| 634 DCHECK(!callback.is_null()); |
| 635 DCHECK(default_apps_ready_callback_.is_null()); |
| 636 default_apps_ready_callback_ = callback; |
| 637 if (default_apps_ready_) |
| 638 default_apps_ready_callback_.Run(); |
| 639 } |
| 640 |
572 void ArcAppListPrefs::OnInstanceReady() { | 641 void ArcAppListPrefs::OnInstanceReady() { |
573 arc::mojom::AppInstance* app_instance = app_instance_holder_->instance(); | 642 arc::mojom::AppInstance* app_instance = app_instance_holder_->instance(); |
574 | 643 |
575 sync_service_ = arc::ArcPackageSyncableService::Get(profile_); | 644 sync_service_ = arc::ArcPackageSyncableService::Get(profile_); |
576 DCHECK(sync_service_); | 645 DCHECK(sync_service_); |
577 | 646 |
578 // In some tests app_instance may not be set. | 647 // In some tests app_instance may not be set. |
579 if (!app_instance) { | 648 if (!app_instance) { |
580 VLOG(2) << "Request to refresh app list when bridge service is not ready."; | 649 VLOG(2) << "Request to refresh app list when bridge service is not ready."; |
581 return; | 650 return; |
(...skipping 10 matching lines...) Expand all Loading... |
592 binding_.Close(); | 661 binding_.Close(); |
593 | 662 |
594 if (sync_service_) { | 663 if (sync_service_) { |
595 sync_service_->StopSyncing(syncer::ARC_PACKAGE); | 664 sync_service_->StopSyncing(syncer::ARC_PACKAGE); |
596 sync_service_ = nullptr; | 665 sync_service_ = nullptr; |
597 } | 666 } |
598 | 667 |
599 is_initialized_ = false; | 668 is_initialized_ = false; |
600 } | 669 } |
601 | 670 |
602 void ArcAppListPrefs::MayAddNonLaunchableApp(const std::string& name, | 671 void ArcAppListPrefs::MaybeAddNonLaunchableApp(const std::string& name, |
603 const std::string& package_name, | 672 const std::string& package_name, |
604 const std::string& activity) { | 673 const std::string& activity) { |
605 DCHECK(IsArcEnabled()); | 674 DCHECK(IsArcEnabled()); |
606 if (IsRegistered(GetAppId(package_name, activity))) | 675 if (IsRegistered(GetAppId(package_name, activity))) |
607 return; | 676 return; |
608 | 677 |
609 AddAppAndShortcut(name, package_name, activity, | 678 AddAppAndShortcut(name, package_name, activity, |
610 std::string() /* intent_uri */, | 679 std::string() /* intent_uri */, |
611 std::string() /* icon_resource_id */, false /* sticky */, | 680 std::string() /* icon_resource_id */, false /* sticky */, |
612 false /* notifications_enabled */, false /* shortcut */, | 681 false /* notifications_enabled */, false /* shortcut */, |
613 false /* launchable */, arc::mojom::OrientationLock::NONE); | 682 false /* launchable */, arc::mojom::OrientationLock::NONE); |
614 } | 683 } |
615 | 684 |
616 void ArcAppListPrefs::AddAppAndShortcut( | 685 void ArcAppListPrefs::AddAppAndShortcut( |
617 const std::string& name, | 686 const std::string& name, |
618 const std::string& package_name, | 687 const std::string& package_name, |
619 const std::string& activity, | 688 const std::string& activity, |
620 const std::string& intent_uri, | 689 const std::string& intent_uri, |
621 const std::string& icon_resource_id, | 690 const std::string& icon_resource_id, |
622 const bool sticky, | 691 const bool sticky, |
623 const bool notifications_enabled, | 692 const bool notifications_enabled, |
624 const bool shortcut, | 693 const bool shortcut, |
625 const bool launchable, | 694 const bool launchable, |
626 const arc::mojom::OrientationLock orientation_lock) { | 695 const arc::mojom::OrientationLock orientation_lock) { |
627 std::string app_id = shortcut ? GetAppId(package_name, intent_uri) | 696 const std::string app_id = shortcut ? GetAppId(package_name, intent_uri) |
628 : GetAppId(package_name, activity); | 697 : GetAppId(package_name, activity); |
629 const bool was_registered = IsRegistered(app_id); | 698 const bool was_registered = IsRegistered(app_id); |
630 if (was_registered) { | 699 if (was_registered) { |
631 std::unique_ptr<ArcAppListPrefs::AppInfo> app_old_info = GetApp(app_id); | 700 std::unique_ptr<ArcAppListPrefs::AppInfo> app_old_info = GetApp(app_id); |
632 DCHECK(app_old_info); | 701 DCHECK(app_old_info); |
633 DCHECK(launchable); | 702 DCHECK(launchable); |
634 if (name != app_old_info->name) { | 703 if (name != app_old_info->name) { |
635 FOR_EACH_OBSERVER(Observer, observer_list_, | 704 FOR_EACH_OBSERVER(Observer, observer_list_, |
636 OnAppNameUpdated(app_id, name)); | 705 OnAppNameUpdated(app_id, name)); |
637 } | 706 } |
638 } | 707 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
699 void ArcAppListPrefs::RemoveApp(const std::string& app_id) { | 768 void ArcAppListPrefs::RemoveApp(const std::string& app_id) { |
700 // Delete cached icon if there is any. | 769 // Delete cached icon if there is any. |
701 std::unique_ptr<ArcAppListPrefs::AppInfo> app_info = GetApp(app_id); | 770 std::unique_ptr<ArcAppListPrefs::AppInfo> app_info = GetApp(app_id); |
702 if (app_info && !app_info->icon_resource_id.empty()) { | 771 if (app_info && !app_info->icon_resource_id.empty()) { |
703 arc::RemoveCachedIcon(app_info->icon_resource_id); | 772 arc::RemoveCachedIcon(app_info->icon_resource_id); |
704 } | 773 } |
705 | 774 |
706 // From now, app is not available. | 775 // From now, app is not available. |
707 ready_apps_.erase(app_id); | 776 ready_apps_.erase(app_id); |
708 | 777 |
| 778 // app_id may be released by observers, get the path first. It should be done |
| 779 // before removing prefs entry in order not to mix with pre-build default apps |
| 780 // files. |
| 781 const base::FilePath app_path = GetAppPath(app_id); |
| 782 |
709 // Remove from prefs. | 783 // Remove from prefs. |
710 DictionaryPrefUpdate update(prefs_, prefs::kArcApps); | 784 DictionaryPrefUpdate update(prefs_, prefs::kArcApps); |
711 base::DictionaryValue* apps = update.Get(); | 785 base::DictionaryValue* apps = update.Get(); |
712 const bool removed = apps->Remove(app_id, nullptr); | 786 const bool removed = apps->Remove(app_id, nullptr); |
713 DCHECK(removed); | 787 DCHECK(removed); |
714 // app_id may be released by observers, get the path first. | |
715 const base::FilePath app_path = GetAppPath(app_id); | |
716 | 788 |
717 FOR_EACH_OBSERVER(Observer, observer_list_, OnAppRemoved(app_id)); | 789 FOR_EACH_OBSERVER(Observer, observer_list_, OnAppRemoved(app_id)); |
718 | 790 |
719 // Remove local data on file system. | 791 // Remove local data on file system. |
720 content::BrowserThread::GetBlockingPool()->PostTask( | 792 content::BrowserThread::GetBlockingPool()->PostTask( |
721 FROM_HERE, base::Bind(&DeleteAppFolderFromFileThread, app_path)); | 793 FROM_HERE, base::Bind(&DeleteAppFolderFromFileThread, app_path)); |
722 } | 794 } |
723 | 795 |
| 796 void ArcAppListPrefs::AddOrUpdatePackagePrefs( |
| 797 PrefService* prefs, const arc::mojom::ArcPackageInfo& package) { |
| 798 DCHECK(IsArcEnabled()); |
| 799 const std::string& package_name = package.package_name; |
| 800 default_apps_.MaybeMarkPackageUninstalled(package_name, false); |
| 801 if (package_name.empty()) { |
| 802 VLOG(2) << "Package name cannot be empty."; |
| 803 return; |
| 804 } |
| 805 ScopedArcPrefUpdate update(prefs, package_name, prefs::kArcPackages); |
| 806 base::DictionaryValue* package_dict = update.Get(); |
| 807 const std::string id_str = |
| 808 base::Int64ToString(package.last_backup_android_id); |
| 809 const std::string time_str = base::Int64ToString(package.last_backup_time); |
| 810 |
| 811 package_dict->SetBoolean(kShouldSync, package.sync); |
| 812 package_dict->SetInteger(kPackageVersion, package.package_version); |
| 813 package_dict->SetString(kLastBackupAndroidId, id_str); |
| 814 package_dict->SetString(kLastBackupTime, time_str); |
| 815 package_dict->SetBoolean(kSystem, package.system); |
| 816 } |
| 817 |
| 818 void ArcAppListPrefs::RemovePackageFromPrefs(PrefService* prefs, |
| 819 const std::string& package_name) { |
| 820 DCHECK(IsArcEnabled()); |
| 821 default_apps_.MaybeMarkPackageUninstalled(package_name, true); |
| 822 if (!default_apps_.HasPackage(package_name)) { |
| 823 DictionaryPrefUpdate update(prefs, prefs::kArcPackages); |
| 824 base::DictionaryValue* packages = update.Get(); |
| 825 const bool removed = packages->RemoveWithoutPathExpansion(package_name, |
| 826 nullptr); |
| 827 DCHECK(removed); |
| 828 } else { |
| 829 ScopedArcPrefUpdate update(prefs, package_name, prefs::kArcPackages); |
| 830 base::DictionaryValue* package_dict = update.Get(); |
| 831 package_dict->SetBoolean(kUninstalled, true); |
| 832 } |
| 833 } |
| 834 |
724 void ArcAppListPrefs::OnAppListRefreshed( | 835 void ArcAppListPrefs::OnAppListRefreshed( |
725 mojo::Array<arc::mojom::AppInfoPtr> apps) { | 836 mojo::Array<arc::mojom::AppInfoPtr> apps) { |
726 DCHECK(IsArcEnabled()); | 837 DCHECK(IsArcEnabled()); |
727 std::vector<std::string> old_apps = GetAppIds(); | 838 std::vector<std::string> old_apps = GetAppIds(); |
728 | 839 |
729 ready_apps_.clear(); | 840 ready_apps_.clear(); |
730 for (const auto& app : apps) { | 841 for (const auto& app : apps) { |
731 // TODO(oshima): Do we have to update orientation? | 842 // TODO(oshima): Do we have to update orientation? |
732 AddAppAndShortcut(app->name, app->package_name, app->activity, | 843 AddAppAndShortcut(app->name, app->package_name, app->activity, |
733 std::string() /* intent_uri */, | 844 std::string() /* intent_uri */, |
734 std::string() /* icon_resource_id */, app->sticky, | 845 std::string() /* icon_resource_id */, app->sticky, |
735 app->notifications_enabled, false /* shortcut */, | 846 app->notifications_enabled, false /* shortcut */, |
736 true /* launchable */, app->orientation_lock); | 847 true /* launchable */, app->orientation_lock); |
737 } | 848 } |
738 | 849 |
739 // Detect removed ARC apps after current refresh. | 850 // Detect removed ARC apps after current refresh. |
740 for (const auto& app_id : old_apps) { | 851 for (const auto& app_id : old_apps) { |
741 if (ready_apps_.count(app_id)) | 852 if (ready_apps_.count(app_id)) |
742 continue; | 853 continue; |
743 | 854 |
744 if (IsShortcut(app_id)) { | 855 if (IsShortcut(app_id)) { |
745 // If this is a shortcut, we just mark it as ready. | 856 // If this is a shortcut, we just mark it as ready. |
746 ready_apps_.insert(app_id); | 857 ready_apps_.insert(app_id); |
747 } else { | 858 } else { |
748 RemoveApp(app_id); | 859 // Default apps may not be installed yet at this moment. |
| 860 if (!default_apps_.HasApp(app_id)) |
| 861 RemoveApp(app_id); |
749 } | 862 } |
750 } | 863 } |
751 | 864 |
752 if (!is_initialized_) { | 865 if (!is_initialized_) { |
753 is_initialized_ = true; | 866 is_initialized_ = true; |
754 UMA_HISTOGRAM_COUNTS_1000("Arc.AppsInstalledAtStartup", ready_apps_.size()); | 867 UMA_HISTOGRAM_COUNTS_1000("Arc.AppsInstalledAtStartup", ready_apps_.size()); |
755 } | 868 } |
756 } | 869 } |
757 | 870 |
758 void ArcAppListPrefs::OnTaskOrientationLockRequested( | 871 void ArcAppListPrefs::OnTaskOrientationLockRequested( |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
879 } | 992 } |
880 | 993 |
881 InstallIcon(app_id, static_cast<ui::ScaleFactor>(scale_factor), | 994 InstallIcon(app_id, static_cast<ui::ScaleFactor>(scale_factor), |
882 icon_png_data.To<std::vector<uint8_t>>()); | 995 icon_png_data.To<std::vector<uint8_t>>()); |
883 } | 996 } |
884 | 997 |
885 void ArcAppListPrefs::OnTaskCreated(int32_t task_id, | 998 void ArcAppListPrefs::OnTaskCreated(int32_t task_id, |
886 const mojo::String& package_name, | 999 const mojo::String& package_name, |
887 const mojo::String& activity, | 1000 const mojo::String& activity, |
888 const mojo::String& name) { | 1001 const mojo::String& name) { |
889 MayAddNonLaunchableApp(name, package_name, activity); | 1002 MaybeAddNonLaunchableApp(name, package_name, activity); |
890 FOR_EACH_OBSERVER(Observer, observer_list_, | 1003 FOR_EACH_OBSERVER(Observer, observer_list_, |
891 OnTaskCreated(task_id, package_name, activity)); | 1004 OnTaskCreated(task_id, package_name, activity)); |
892 } | 1005 } |
893 | 1006 |
894 void ArcAppListPrefs::OnTaskDestroyed(int32_t task_id) { | 1007 void ArcAppListPrefs::OnTaskDestroyed(int32_t task_id) { |
895 FOR_EACH_OBSERVER(Observer, observer_list_, OnTaskDestroyed(task_id)); | 1008 FOR_EACH_OBSERVER(Observer, observer_list_, OnTaskDestroyed(task_id)); |
896 } | 1009 } |
897 | 1010 |
898 void ArcAppListPrefs::OnTaskSetActive(int32_t task_id) { | 1011 void ArcAppListPrefs::OnTaskSetActive(int32_t task_id) { |
899 FOR_EACH_OBSERVER(Observer, observer_list_, OnTaskSetActive(task_id)); | 1012 FOR_EACH_OBSERVER(Observer, observer_list_, OnTaskSetActive(task_id)); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
989 | 1102 |
990 // Start ArcPackageSyncService ASAP. This is the call_back of | 1103 // Start ArcPackageSyncService ASAP. This is the call_back of |
991 // app_instance->RefreshAppList() in OnInstanceReady(). SyncStarted() should | 1104 // app_instance->RefreshAppList() in OnInstanceReady(). SyncStarted() should |
992 // only be called after packagelist refresh is completed. SyncStarted() is | 1105 // only be called after packagelist refresh is completed. SyncStarted() is |
993 // no-op after first time setup or if sync is disabled. | 1106 // no-op after first time setup or if sync is disabled. |
994 DCHECK(sync_service_); | 1107 DCHECK(sync_service_); |
995 sync_service_->SyncStarted(); | 1108 sync_service_->SyncStarted(); |
996 } | 1109 } |
997 | 1110 |
998 std::vector<std::string> ArcAppListPrefs::GetPackagesFromPrefs() const { | 1111 std::vector<std::string> ArcAppListPrefs::GetPackagesFromPrefs() const { |
| 1112 return GetPackagesFromPrefs(true); |
| 1113 } |
| 1114 |
| 1115 std::vector<std::string> ArcAppListPrefs::GetPackagesFromPrefs( |
| 1116 bool installed) const { |
999 std::vector<std::string> packages; | 1117 std::vector<std::string> packages; |
1000 if (!IsArcEnabled()) | 1118 if (!IsArcEnabled() && installed) |
1001 return packages; | 1119 return packages; |
1002 | 1120 |
1003 const base::DictionaryValue* package_prefs = | 1121 const base::DictionaryValue* package_prefs = |
1004 prefs_->GetDictionary(prefs::kArcPackages); | 1122 prefs_->GetDictionary(prefs::kArcPackages); |
1005 for (base::DictionaryValue::Iterator package(*package_prefs); | 1123 for (base::DictionaryValue::Iterator package(*package_prefs); |
1006 !package.IsAtEnd(); package.Advance()) { | 1124 !package.IsAtEnd(); package.Advance()) { |
| 1125 |
| 1126 bool uninstalled = false; |
| 1127 package_prefs->GetBoolean(kSystem, &uninstalled); |
| 1128 if (installed != !uninstalled) |
| 1129 continue; |
| 1130 |
1007 packages.push_back(package.key()); | 1131 packages.push_back(package.key()); |
1008 } | 1132 } |
1009 | 1133 |
1010 return packages; | 1134 return packages; |
1011 } | 1135 } |
1012 | 1136 |
1013 base::Time ArcAppListPrefs::GetInstallTime(const std::string& app_id) const { | 1137 base::Time ArcAppListPrefs::GetInstallTime(const std::string& app_id) const { |
1014 const base::DictionaryValue* app = nullptr; | 1138 const base::DictionaryValue* app = nullptr; |
1015 const base::DictionaryValue* apps = prefs_->GetDictionary(prefs::kArcApps); | 1139 const base::DictionaryValue* apps = prefs_->GetDictionary(prefs::kArcApps); |
1016 if (!apps || !apps->GetDictionaryWithoutPathExpansion(app_id, &app)) | 1140 if (!apps || !apps->GetDictionaryWithoutPathExpansion(app_id, &app)) |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1087 int64_t last_backup_android_id, | 1211 int64_t last_backup_android_id, |
1088 int64_t last_backup_time, | 1212 int64_t last_backup_time, |
1089 bool should_sync, | 1213 bool should_sync, |
1090 bool system) | 1214 bool system) |
1091 : package_name(package_name), | 1215 : package_name(package_name), |
1092 package_version(package_version), | 1216 package_version(package_version), |
1093 last_backup_android_id(last_backup_android_id), | 1217 last_backup_android_id(last_backup_android_id), |
1094 last_backup_time(last_backup_time), | 1218 last_backup_time(last_backup_time), |
1095 should_sync(should_sync), | 1219 should_sync(should_sync), |
1096 system(system) {} | 1220 system(system) {} |
OLD | NEW |