forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding some tests for easy_unlock_service
Extracted parts that deal with extension system from EasyUnlockService to a separate class (mainly to make setting up unittests easier). Added tests for EasyUnlockAuthAttempt Converted existing browser tests to unit tests and removed official build ifdefs. BUG=414829 Review URL: https://codereview.chromium.org/679463003 Cr-Commit-Position: refs/heads/master@{#315826}
- Loading branch information
tbarzic
authored and
Commit bot
committed
Feb 11, 2015
1 parent
6a030f5
commit 5242236
Showing
21 changed files
with
1,956 additions
and
479 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
// Copyright 2015 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/signin/easy_unlock_app_manager.h" | ||
|
||
#include "base/command_line.h" | ||
#include "chrome/browser/extensions/component_loader.h" | ||
#include "chrome/browser/extensions/extension_service.h" | ||
#include "chrome/browser/ui/extensions/application_launch.h" | ||
#include "chrome/common/extensions/api/easy_unlock_private.h" | ||
#include "chrome/common/extensions/api/screenlock_private.h" | ||
#include "chrome/common/extensions/extension_constants.h" | ||
#include "components/proximity_auth/switches.h" | ||
#include "extensions/browser/event_router.h" | ||
#include "extensions/browser/extension_system.h" | ||
#include "extensions/common/one_shot_event.h" | ||
|
||
#if defined(OS_CHROMEOS) | ||
#include "base/sys_info.h" | ||
#endif | ||
|
||
namespace { | ||
|
||
class EasyUnlockAppManagerImpl : public EasyUnlockAppManager { | ||
public: | ||
EasyUnlockAppManagerImpl(extensions::ExtensionSystem* extension_system, | ||
int manifest_id, | ||
const base::FilePath& app_path); | ||
~EasyUnlockAppManagerImpl() override; | ||
|
||
// EasyUnlockAppManager overrides. | ||
void EnsureReady(const base::Closure& ready_callback) override; | ||
void LaunchSetup() override; | ||
void LoadApp() override; | ||
void DisableAppIfLoaded() override; | ||
void ReloadApp() override; | ||
bool SendUserUpdatedEvent(const std::string& user_id, | ||
bool is_logged_in, | ||
bool data_ready) override; | ||
bool SendAuthAttemptEvent() override; | ||
|
||
private: | ||
extensions::ExtensionSystem* extension_system_; | ||
|
||
// The Easy Unlock app id. | ||
std::string app_id_; | ||
int manifest_id_; | ||
|
||
// The path from which Easy Unlock app should be loaded. | ||
base::FilePath app_path_; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(EasyUnlockAppManagerImpl); | ||
}; | ||
|
||
EasyUnlockAppManagerImpl::EasyUnlockAppManagerImpl( | ||
extensions::ExtensionSystem* extension_system, | ||
int manifest_id, | ||
const base::FilePath& app_path) | ||
: extension_system_(extension_system), | ||
app_id_(extension_misc::kEasyUnlockAppId), | ||
manifest_id_(manifest_id), | ||
app_path_(app_path) { | ||
} | ||
|
||
EasyUnlockAppManagerImpl::~EasyUnlockAppManagerImpl() { | ||
} | ||
|
||
void EasyUnlockAppManagerImpl::EnsureReady( | ||
const base::Closure& ready_callback) { | ||
extension_system_->ready().Post(FROM_HERE, ready_callback); | ||
} | ||
|
||
void EasyUnlockAppManagerImpl::LaunchSetup() { | ||
ExtensionService* extension_service = extension_system_->extension_service(); | ||
if (!extension_service) | ||
return; | ||
|
||
const extensions::Extension* extension = | ||
extension_service->GetExtensionById(app_id_, false); | ||
if (!extension) | ||
return; | ||
|
||
OpenApplication(AppLaunchParams(extension_service->profile(), extension, | ||
extensions::LAUNCH_CONTAINER_WINDOW, | ||
NEW_WINDOW, | ||
extensions::SOURCE_CHROME_INTERNAL)); | ||
} | ||
|
||
void EasyUnlockAppManagerImpl::LoadApp() { | ||
ExtensionService* extension_service = extension_system_->extension_service(); | ||
if (!extension_service) | ||
return; | ||
|
||
#if defined(OS_CHROMEOS) | ||
// TODO(xiyuan): Remove this when the app is bundled with chrome. | ||
if (!base::SysInfo::IsRunningOnChromeOS() && | ||
!base::CommandLine::ForCurrentProcess()->HasSwitch( | ||
proximity_auth::switches::kForceLoadEasyUnlockAppInTests)) { | ||
return; | ||
} | ||
#endif | ||
|
||
if (app_path_.empty()) | ||
return; | ||
|
||
extensions::ComponentLoader* loader = extension_service->component_loader(); | ||
if (!loader->Exists(app_id_)) | ||
app_id_ = loader->Add(manifest_id_, app_path_); | ||
|
||
extension_service->EnableExtension(app_id_); | ||
} | ||
|
||
void EasyUnlockAppManagerImpl::DisableAppIfLoaded() { | ||
ExtensionService* extension_service = extension_system_->extension_service(); | ||
if (!extension_service) | ||
return; | ||
|
||
if (!extension_service->component_loader()->Exists(app_id_)) | ||
return; | ||
|
||
extension_service->DisableExtension(app_id_, | ||
extensions::Extension::DISABLE_RELOAD); | ||
} | ||
|
||
void EasyUnlockAppManagerImpl::ReloadApp() { | ||
ExtensionService* extension_service = extension_system_->extension_service(); | ||
if (!extension_service) | ||
return; | ||
|
||
if (!extension_service->component_loader()->Exists(app_id_)) | ||
return; | ||
|
||
extension_service->ReloadExtension(app_id_); | ||
} | ||
|
||
bool EasyUnlockAppManagerImpl::SendUserUpdatedEvent(const std::string& user_id, | ||
bool is_logged_in, | ||
bool data_ready) { | ||
extensions::EventRouter* event_router = extension_system_->event_router(); | ||
if (!event_router) | ||
return false; | ||
|
||
std::string event_name = | ||
extensions::api::easy_unlock_private::OnUserInfoUpdated::kEventName; | ||
|
||
if (!event_router->ExtensionHasEventListener(app_id_, event_name)) | ||
return false; | ||
|
||
extensions::api::easy_unlock_private::UserInfo info; | ||
info.user_id = user_id; | ||
info.logged_in = is_logged_in; | ||
info.data_ready = data_ready; | ||
|
||
scoped_ptr<base::ListValue> args(new base::ListValue()); | ||
args->Append(info.ToValue().release()); | ||
|
||
scoped_ptr<extensions::Event> event( | ||
new extensions::Event(event_name, args.Pass())); | ||
|
||
event_router->DispatchEventToExtension(app_id_, event.Pass()); | ||
return true; | ||
} | ||
|
||
bool EasyUnlockAppManagerImpl::SendAuthAttemptEvent() { | ||
extensions::EventRouter* event_router = extension_system_->event_router(); | ||
if (!event_router) | ||
return false; | ||
|
||
std::string event_name = | ||
extensions::api::screenlock_private::OnAuthAttempted::kEventName; | ||
|
||
if (!event_router->HasEventListener(event_name)) | ||
return false; | ||
|
||
scoped_ptr<base::ListValue> args(new base::ListValue()); | ||
args->AppendString(extensions::api::screenlock_private::ToString( | ||
extensions::api::screenlock_private::AUTH_TYPE_USERCLICK)); | ||
args->AppendString(std::string()); | ||
|
||
scoped_ptr<extensions::Event> event( | ||
new extensions::Event(event_name, args.Pass())); | ||
|
||
// TODO(tbarzic): Restrict this to EasyUnlock app. | ||
event_router->BroadcastEvent(event.Pass()); | ||
return true; | ||
} | ||
|
||
} // namespace | ||
|
||
EasyUnlockAppManager::~EasyUnlockAppManager() { | ||
} | ||
|
||
// static | ||
scoped_ptr<EasyUnlockAppManager> EasyUnlockAppManager::Create( | ||
extensions::ExtensionSystem* extension_system, | ||
int manifest_id, | ||
const base::FilePath& app_path) { | ||
return scoped_ptr<EasyUnlockAppManager>( | ||
new EasyUnlockAppManagerImpl(extension_system, manifest_id, app_path)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2015 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. | ||
|
||
#ifndef CHROME_BROWSER_SIGNIN_EASY_UNLOCK_APP_MANAGER_H_ | ||
#define CHROME_BROWSER_SIGNIN_EASY_UNLOCK_APP_MANAGER_H_ | ||
|
||
#include <string> | ||
|
||
#include "base/callback.h" | ||
#include "base/files/file_path.h" | ||
#include "base/macros.h" | ||
|
||
namespace extensions { | ||
class ExtensionSystem; | ||
} | ||
|
||
// Used to manage Easy Unlock app's lifetime and to dispatch events to the app. | ||
// It's main purpose is to abstract extension system from the rest of easy | ||
// unlock code. | ||
class EasyUnlockAppManager { | ||
public: | ||
virtual ~EasyUnlockAppManager(); | ||
|
||
// Creates EasyUnlockAppManager object that should be used in production. | ||
static scoped_ptr<EasyUnlockAppManager> Create( | ||
extensions::ExtensionSystem* extension_system, | ||
int manifest_id, | ||
const base::FilePath& app_path); | ||
|
||
// Wait for the extension system to get ready and invokes |ready_callback| | ||
// when that happens. | ||
// Note that the callback may be triggered after |this| is deleted. | ||
virtual void EnsureReady(const base::Closure& ready_callback) = 0; | ||
|
||
// Launches Easy Unlock setup app, if the setup app is loaded. | ||
virtual void LaunchSetup() = 0; | ||
|
||
// Loads Easy Unlock app. | ||
virtual void LoadApp() = 0; | ||
|
||
// Disables Easy Unlock app. | ||
virtual void DisableAppIfLoaded() = 0; | ||
|
||
// Reloads Easy Unlock app. | ||
virtual void ReloadApp() = 0; | ||
|
||
// Sends easyUnlockPrivate.onUserInfoUpdate event to Easy Unlock app. | ||
virtual bool SendUserUpdatedEvent(const std::string& user_id, | ||
bool is_logged_in, | ||
bool data_ready) = 0; | ||
|
||
// Sends screenlockPrivate.onAuthAttempted event to Easy Unlock app. | ||
virtual bool SendAuthAttemptEvent() = 0; | ||
}; | ||
|
||
#endif // CHROME_BROWSER_SIGNIN_EASY_UNLOCK_APP_MANAGER_H_ |
Oops, something went wrong.