Commit 4774e3a7 authored by munjal@chromium.org's avatar munjal@chromium.org

Add ability to add/remove observers for login ui shown/closed

to LoginUIService. This will be used by the extension API to show
login UI in a popup and then obseve when it is closed and respond back
to the extension accordingly.

Review URL: https://chromiumcodereview.appspot.com/10699013

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@146143 0039d316-1c4b-4281-b951-d872f2087c98
parent 87e4416d
......@@ -13,14 +13,26 @@ LoginUIService::LoginUIService() : ui_(NULL) {
LoginUIService::~LoginUIService() {}
void LoginUIService::AddObserver(LoginUIService::Observer* observer) {
observer_list_.AddObserver(observer);
}
void LoginUIService::RemoveObserver(LoginUIService::Observer* observer) {
observer_list_.RemoveObserver(observer);
}
void LoginUIService::SetLoginUI(LoginUI* ui) {
DCHECK(!current_login_ui() || current_login_ui() == ui);
ui_ = ui;
FOR_EACH_OBSERVER(Observer, observer_list_, OnLoginUIShown(ui_));
}
void LoginUIService::LoginUIClosed(LoginUI* ui) {
if (current_login_ui() == ui)
ui_ = NULL;
if (current_login_ui() != ui)
return;
ui_ = NULL;
FOR_EACH_OBSERVER(Observer, observer_list_, OnLoginUIClosed(ui));
}
void LoginUIService::ShowLoginUI(Browser* browser) {
......
......@@ -7,6 +7,7 @@
#pragma once
#include "base/basictypes.h"
#include "base/observer_list.h"
#include "chrome/browser/profiles/profile_keyed_service.h"
class Browser;
......@@ -29,6 +30,21 @@ class LoginUIService : public ProfileKeyedService {
virtual ~LoginUI() {}
};
// Interface for obervers of LoginUIService.
class Observer {
public:
// Called when a new login UI is shown.
// |ui| The login UI that was just shown. Will never be null.
virtual void OnLoginUIShown(LoginUI* ui) = 0;
// Called when a login UI is closed.
// |ui| The login UI that was just closed; will never be null.
virtual void OnLoginUIClosed(LoginUI* ui) = 0;
protected:
virtual ~Observer() {}
};
LoginUIService();
virtual ~LoginUIService();
......@@ -37,6 +53,10 @@ class LoginUIService : public ProfileKeyedService {
return ui_;
}
// |observer| The observer to add or remove; cannot be NULL.
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
// Sets the currently active login UI. It is illegal to call this if there is
// already login UI visible.
void SetLoginUI(LoginUI* ui);
......@@ -54,6 +74,9 @@ class LoginUIService : public ProfileKeyedService {
// Weak pointer to the currently active login UI, or null if none.
LoginUI* ui_;
// List of observers.
ObserverList<Observer> observer_list_;
DISALLOW_COPY_AND_ASSIGN(LoginUIService);
};
......
// Copyright (c) 2012 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/ui/webui/signin/login_ui_service.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "testing/gtest/include/gtest/gtest.h"
class TestLoginUI : public LoginUIService::LoginUI {
public:
TestLoginUI() { }
virtual ~TestLoginUI() { }
virtual void FocusUI() OVERRIDE { }
virtual void CloseUI() OVERRIDE { }
private:
DISALLOW_COPY_AND_ASSIGN(TestLoginUI);
};
class TestObserver : public LoginUIService::Observer {
public:
TestObserver() : ui_shown_count_(0),
ui_closed_count_(0) {
}
int ui_shown_count() const { return ui_shown_count_; }
int ui_closed_count() const { return ui_closed_count_; }
private:
virtual void OnLoginUIShown(LoginUIService::LoginUI* ui) OVERRIDE {
++ui_shown_count_;
}
virtual void OnLoginUIClosed(LoginUIService::LoginUI* ui) OVERRIDE {
++ui_closed_count_;
}
int ui_shown_count_;
int ui_closed_count_;
DISALLOW_COPY_AND_ASSIGN(TestObserver);
};
class LoginUIServiceTest : public testing::Test {
public:
LoginUIServiceTest() { }
virtual ~LoginUIServiceTest() { }
protected:
LoginUIService service_;
TestObserver observer_;
DISALLOW_COPY_AND_ASSIGN(LoginUIServiceTest);
};
// Test that the observer is called back when login UI is shown
// or closed.
TEST_F(LoginUIServiceTest, LoginUIServiceObserver) {
service_.AddObserver(&observer_);
EXPECT_EQ(NULL, service_.current_login_ui());
TestLoginUI ui;
service_.SetLoginUI(&ui);
EXPECT_EQ(1, observer_.ui_shown_count());
// If a different UI is closed than the one that was shown, no
// notification should be fired.
TestLoginUI other_ui;
service_.LoginUIClosed(&other_ui);
EXPECT_EQ(1, observer_.ui_shown_count());
// If the right UI is closed, notification should be fired.
service_.LoginUIClosed(&ui);
EXPECT_EQ(1, observer_.ui_closed_count());
service_.RemoveObserver(&observer_);
}
......@@ -1852,6 +1852,7 @@
'browser/ui/webui/policy_ui_unittest.cc',
'browser/ui/webui/print_preview/print_preview_handler_unittest.cc',
'browser/ui/webui/print_preview/print_preview_ui_unittest.cc',
'browser/ui/webui/signin/login_ui_service_unittest.cc',
'browser/ui/webui/sync_internals_ui_unittest.cc',
'browser/ui/webui/sync_setup_handler_unittest.cc',
'browser/ui/webui/theme_source_unittest.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment