Chromium Code Reviews| 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..a42509e813d45391d91d1122e4d9fb6ee60a765d |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/lock_screen_apps/app_manager_impl.cc |
| @@ -0,0 +1,224 @@ |
| +// 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/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); |
| + |
| + 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::kIdle; |
| + |
| + 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(AppManager::Observer* observer) { |
| + observer_ = observer; |
| + extensions_observer_.Add( |
| + extensions::ExtensionRegistry::Get(primary_profile_)); |
| + |
| + if (state_ == State::kStarted) |
| + 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::kStarted; |
| +} |
| + |
| +void AppManagerImpl::Stop() { |
| + observer_ = nullptr; |
| + extensions_observer_.RemoveAll(); |
| + |
| + if (state_ == State::kIdle) |
| + return; |
| + |
| + UnloadLockApp(lock_screen_app_id_); |
|
jdufault
2017/05/26 19:15:52
what if state_ == State::kNotInitialized?
tbarzic
2017/05/26 19:34:14
Done.
|
| + lock_screen_app_id_.clear(); |
| + state_ = State::kIdle; |
| +} |
| + |
| +bool AppManagerImpl::IsNoteTakingAppAvailable() const { |
| + return state_ == State::kStarted && !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(); |
| + 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::kStarted) |
| + 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; |
| + |
| + observer_->OnNoteTakingAvailabilityChanged(); |
| +} |
| + |
| +std::string AppManagerImpl::FindLockScreenNoteTakingApp() const { |
| + 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; |
| + |
| + extensions::ExtensionRegistry* lock_screen_registry = |
| + extensions::ExtensionRegistry::Get(lock_screen_profile_); |
| + const extensions::Extension* lock_app = |
| + lock_screen_registry->GetExtensionById( |
| + app_id, extensions::ExtensionRegistry::EVERYTHING); |
| + if (lock_app && !lock_app->runs_on_lock_screen()) { |
| + LOG(ERROR) << "Non lock screen enabled app already installed in profile."; |
| + return false; |
| + } |
| + |
| + ExtensionService* lock_screen_service = |
| + extensions::ExtensionSystem::Get(lock_screen_profile_) |
| + ->extension_service(); |
| + if (lock_app) { |
| + lock_screen_service->UnloadExtension( |
| + app_id, extensions::UnloadedExtensionReason::UNINSTALL); |
| + } |
| + |
| + // TODO(tbarzic): Instead of loading the app from user profile path, consider |
| + // copying and installing the app to the target profile. |
| + std::string error; |
| + scoped_refptr<extensions::Extension> lock_profile_app = |
| + extensions::Extension::Create( |
| + app->path(), app->location(), |
| + *app->manifest()->value()->CreateDeepCopy(), |
| + app->creation_flags() | extensions::Extension::RUNS_ON_LOCK_SCREEN, |
| + app->id(), &error); |
| + |
| + if (!lock_profile_app) |
| + return false; |
| + |
| + lock_screen_service->AddExtension(lock_profile_app.get()); |
| + lock_screen_service->EnableExtension(lock_profile_app->id()); |
| + return true; |
| +} |
| + |
| +void AppManagerImpl::UnloadLockApp(const std::string& app_id) { |
| + if (app_id.empty()) |
| + return; |
| + |
| + extensions::ExtensionRegistry* lock_screen_registry = |
| + extensions::ExtensionRegistry::Get(lock_screen_profile_); |
| + const extensions::Extension* lock_app = |
| + lock_screen_registry->GetExtensionById( |
| + app_id, extensions::ExtensionRegistry::EVERYTHING); |
| + if (!lock_app) |
| + return; |
| + |
| + extensions::ExtensionSystem::Get(lock_screen_profile_) |
| + ->extension_service() |
| + ->UnloadExtension(app_id, extensions::UnloadedExtensionReason::UNINSTALL); |
| +} |
| + |
| +} // namespace lock_screen_apps |