Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1432)

Unified Diff: chrome/browser/chromeos/lock_screen_apps/app_manager_impl.cc

Issue 2902293002: Introduce lock screen app manager (Closed)
Patch Set: . Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/lock_screen_apps/app_manager_impl.cc
diff --git a/chrome/browser/chromeos/lock_screen_apps/app_manager_impl.cc b/chrome/browser/chromeos/lock_screen_apps/app_manager_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8746ca2c26d3d83ce0b7b147e9fd1b8074c18e62
--- /dev/null
+++ b/chrome/browser/chromeos/lock_screen_apps/app_manager_impl.cc
@@ -0,0 +1,228 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/chromeos/lock_screen_apps/app_manager_impl.h"
+
+#include <memory>
+#include <utility>
+#include <vector>
+
+#include "apps/launcher.h"
+#include "base/bind.h"
+#include "base/files/file_path.h"
+#include "base/memory/ptr_util.h"
+#include "base/memory/ref_counted.h"
+#include "base/strings/string16.h"
+#include "chrome/browser/chromeos/note_taking_helper.h"
+#include "chrome/browser/chromeos/profiles/profile_helper.h"
+#include "chrome/browser/extensions/extension_service.h"
+#include "chrome/common/pref_names.h"
+#include "extensions/browser/extension_registry.h"
+#include "extensions/browser/extension_system.h"
+#include "extensions/browser/install_flag.h"
+#include "extensions/common/api/app_runtime.h"
+#include "extensions/common/extension.h"
+#include "extensions/common/extension_set.h"
+
+namespace lock_screen_apps {
+
+AppManagerImpl::AppManagerImpl() : extensions_observer_(this) {}
+
+AppManagerImpl::~AppManagerImpl() = default;
+
+void AppManagerImpl::Initialize(Profile* primary_profile,
+ Profile* lock_screen_profile) {
+ DCHECK_EQ(State::kNotInitialized, state_);
+ DCHECK(primary_profile);
+ DCHECK(lock_screen_profile);
+ DCHECK_NE(primary_profile, lock_screen_profile);
Devlin 2017/06/05 16:09:35 Is there any defined relation between primary and
tbarzic 2017/06/06 01:54:55 Not really. GetOriginalProfile would return the si
+
+ CHECK(!chromeos::ProfileHelper::Get()->GetUserByProfile(lock_screen_profile))
+ << "Lock screen profile should not be associated with any users.";
+
+ primary_profile_ = primary_profile;
+ lock_screen_profile_ = lock_screen_profile;
+ state_ = State::kInactive;
+
+ pref_change_registrar_.Init(primary_profile->GetPrefs());
+ pref_change_registrar_.Add(
+ prefs::kNoteTakingAppId,
+ base::Bind(&AppManagerImpl::OnNoteTakingExtensionChanged,
+ base::Unretained(this)));
+ pref_change_registrar_.Add(
+ prefs::kNoteTakingAppEnabledOnLockScreen,
+ base::Bind(&AppManagerImpl::OnNoteTakingExtensionChanged,
+ base::Unretained(this)));
+}
+
+void AppManagerImpl::Start(const base::Closure& note_taking_changed_callback) {
+ DCHECK_NE(State::kNotInitialized, state_);
+
+ note_taking_changed_callback_ = note_taking_changed_callback;
+ extensions_observer_.Add(
+ extensions::ExtensionRegistry::Get(primary_profile_));
+
+ if (state_ == State::kActive)
+ return;
+
+ lock_screen_app_id_.clear();
+ std::string app_id = FindLockScreenNoteTakingApp();
+ if (!app_id.empty() && AddAppToLockScreenProfile(app_id))
+ lock_screen_app_id_ = app_id;
+
+ state_ = State::kActive;
+}
+
+void AppManagerImpl::Stop() {
+ DCHECK_NE(State::kNotInitialized, state_);
+
+ note_taking_changed_callback_.Reset();
+ extensions_observer_.RemoveAll();
Devlin 2017/06/05 16:09:35 Is the ExtensionRegistry guaranteed to outlive thi
tbarzic 2017/06/06 01:54:55 yes, this is owned by StateController, which is de
+
+ if (state_ == State::kInactive)
+ return;
+
+ UnloadLockApp(lock_screen_app_id_);
+ lock_screen_app_id_.clear();
+ state_ = State::kInactive;
+}
+
+bool AppManagerImpl::IsNoteTakingAppAvailable() const {
+ return state_ == State::kActive && !lock_screen_app_id_.empty();
+}
+
+std::string AppManagerImpl::GetNoteTakingAppId() const {
+ if (!IsNoteTakingAppAvailable())
+ return std::string();
+ return lock_screen_app_id_;
+}
+
+bool AppManagerImpl::LaunchNoteTaking() {
+ if (!IsNoteTakingAppAvailable())
+ return false;
+
+ Profile* target_profile = lock_screen_profile_->GetOriginalProfile();
Devlin 2017/06/05 16:09:35 can we somewhere explain target, lockscreen, and p
tbarzic 2017/06/06 01:54:55 Documentation for AppManager::Initialize has some
+ const extensions::ExtensionRegistry* extension_registry =
+ extensions::ExtensionRegistry::Get(target_profile);
+ const extensions::Extension* app = extension_registry->GetExtensionById(
+ lock_screen_app_id_, extensions::ExtensionRegistry::ENABLED);
+ if (!app)
+ return false;
+
+ auto action_data =
+ base::MakeUnique<extensions::api::app_runtime::ActionData>();
+ action_data->action_type =
+ extensions::api::app_runtime::ActionType::ACTION_TYPE_NEW_NOTE;
+ apps::LaunchPlatformAppWithAction(target_profile, app, std::move(action_data),
+ base::FilePath());
+ return true;
+}
+
+void AppManagerImpl::OnExtensionLoaded(content::BrowserContext* browser_context,
+ const extensions::Extension* extension) {
+ if (extension->id() ==
+ primary_profile_->GetPrefs()->GetString(prefs::kNoteTakingAppId)) {
+ OnNoteTakingExtensionChanged();
+ }
+}
+
+void AppManagerImpl::OnExtensionUnloaded(
+ content::BrowserContext* browser_context,
+ const extensions::Extension* extension,
+ extensions::UnloadedExtensionReason reason) {
+ if (extension->id() == lock_screen_app_id_)
+ OnNoteTakingExtensionChanged();
+}
+
+void AppManagerImpl::OnNoteTakingExtensionChanged() {
+ if (state_ != State::kActive)
+ return;
+ std::string app_id = FindLockScreenNoteTakingApp();
+ if (app_id == lock_screen_app_id_)
+ return;
+
+ UnloadLockApp(lock_screen_app_id_);
+ lock_screen_app_id_.clear();
+
+ if (AddAppToLockScreenProfile(app_id))
+ lock_screen_app_id_ = app_id;
+
+ if (!note_taking_changed_callback_.is_null())
+ note_taking_changed_callback_.Run();
+}
+
+std::string AppManagerImpl::FindLockScreenNoteTakingApp() const {
+ // Note that lock screen does not currently support Android apps, so
+ // it's enough to only check the state of the preferred Chrome app.
+ std::unique_ptr<chromeos::NoteTakingAppInfo> note_taking_app =
+ chromeos::NoteTakingHelper::Get()->GetPreferredChromeAppInfo(
+ primary_profile_);
+
+ if (!note_taking_app ||
+ note_taking_app->lock_screen_support !=
+ chromeos::NoteTakingLockScreenSupport::kSelected) {
+ return std::string();
+ }
+
+ return note_taking_app->app_id;
+}
+
+bool AppManagerImpl::AddAppToLockScreenProfile(const std::string& app_id) {
+ extensions::ExtensionRegistry* primary_registry =
+ extensions::ExtensionRegistry::Get(primary_profile_);
+ const extensions::Extension* app =
+ primary_registry->enabled_extensions().GetByID(app_id);
+ if (!app)
+ return false;
+
+ ExtensionService* lock_screen_service =
+ extensions::ExtensionSystem::Get(lock_screen_profile_)
+ ->extension_service();
+
+ // If the app is already installed, remove it.
+ extensions::ExtensionRegistry* lock_screen_registry =
+ extensions::ExtensionRegistry::Get(lock_screen_profile_);
+ if (lock_screen_registry->GetExtensionById(
+ app_id, extensions::ExtensionRegistry::EVERYTHING)) {
+ ExtensionService::UninstallExtensionHelper(
Devlin 2017/06/05 16:09:35 nit: prefer just using ExtensionService::Uninstall
tbarzic 2017/06/06 01:54:55 Done.
+ lock_screen_service, app_id,
+ extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT);
+ }
+
+ // TODO(tbarzic): Instead of loading the app from user profile path, consider
+ // copying and installing the app to the target profile.
Devlin 2017/06/05 16:09:35 Yeah, this strikes me as very strange. What happe
tbarzic 2017/06/06 01:54:55 Yeah, I have not yet completely figured out the be
Devlin 2017/06/09 18:18:40 Out of curiosity, is there a reason to not write t
tbarzic 2017/06/09 19:01:45 One reason is time - I do plan to look more closel
Devlin 2017/06/12 18:31:45 I talked with kerrnel@ about this, and we both agr
rkc 2017/06/13 21:18:17 We've had this discussion in many different forms
+ std::string error;
+ scoped_refptr<extensions::Extension> lock_profile_app =
+ extensions::Extension::Create(app->path(), app->location(),
+ *app->manifest()->value()->CreateDeepCopy(),
+ app->creation_flags(), app->id(), &error);
+
+ if (!lock_profile_app)
+ return false;
+
+ lock_screen_service->OnExtensionInstalled(
+ lock_profile_app.get(), syncer::StringOrdinal(),
+ extensions::kInstallFlagInstallImmediately);
+ lock_screen_service->EnableExtension(lock_profile_app->id());
+ return true;
+}
+
+void AppManagerImpl::UnloadLockApp(const std::string& app_id) {
Devlin 2017/06/05 16:09:35 s/Lock/LockScreen?
tbarzic 2017/06/06 01:54:55 Done.
+ if (app_id.empty())
+ return;
+
+ extensions::ExtensionRegistry* lock_screen_registry =
+ extensions::ExtensionRegistry::Get(lock_screen_profile_);
+ if (!lock_screen_registry->GetExtensionById(
+ app_id, extensions::ExtensionRegistry::EVERYTHING)) {
+ return;
+ }
+
+ ExtensionService::UninstallExtensionHelper(
Devlin 2017/06/05 16:09:34 This is pretty strange in combination with using t
Devlin 2017/06/05 16:09:34 Do we unload it, or uninstall it? The two are not
tbarzic 2017/06/06 01:54:55 Closer to uninstall (though, the initial version o
+ extensions::ExtensionSystem::Get(lock_screen_profile_)
+ ->extension_service(),
+ app_id, extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT);
+}
+
+} // namespace lock_screen_apps

Powered by Google App Engine
This is Rietveld 408576698