Commit 5c8ef348 authored by akuegel@chromium.org's avatar akuegel@chromium.org

Add observers for managed user list data changes.

Update the list of supervised users in the import overlay
when the avatar of a supervised user is changed, or a new supervised user is created.

BUG=339060
TBR=bauerb@chromium.org

Review URL: https://codereview.chromium.org/140663014

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@251393 0039d316-1c4b-4281-b951-d872f2087c98
parent f7b56034
......@@ -64,6 +64,7 @@ class ManagedUserRegistrationUtilityImpl
virtual void OnManagedUserAcknowledged(const std::string& managed_user_id)
OVERRIDE;
virtual void OnManagedUsersSyncingStopped() OVERRIDE;
virtual void OnManagedUsersChanged() OVERRIDE;
private:
// Fetches the managed user token when we have the device name.
......@@ -270,6 +271,8 @@ void ManagedUserRegistrationUtilityImpl::OnManagedUsersSyncingStopped() {
GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED));
}
void ManagedUserRegistrationUtilityImpl::OnManagedUsersChanged() {}
void ManagedUserRegistrationUtilityImpl::FetchToken(
const std::string& client_name) {
token_fetcher_->Start(
......
......@@ -426,6 +426,8 @@ SyncError ManagedUserSyncService::ProcessSyncChanges(
value->SetString(kChromeAvatar, managed_user.chrome_avatar());
value->SetString(kChromeOsAvatar, managed_user.chromeos_avatar());
dict->SetWithoutPathExpansion(managed_user.id(), value);
NotifyManagedUsersChanged();
break;
}
case SyncChange::ACTION_DELETE: {
......@@ -461,6 +463,12 @@ void ManagedUserSyncService::NotifyManagedUsersSyncingStopped() {
OnManagedUsersSyncingStopped());
}
void ManagedUserSyncService::NotifyManagedUsersChanged() {
FOR_EACH_OBSERVER(ManagedUserSyncServiceObserver,
observers_,
OnManagedUsersChanged());
}
void ManagedUserSyncService::DispatchCallbacks() {
const base::DictionaryValue* managed_users =
prefs_->GetDictionary(prefs::kManagedUsers);
......
......@@ -119,6 +119,7 @@ class ManagedUserSyncService : public BrowserContextKeyedService,
void NotifyManagedUserAcknowledged(const std::string& managed_user_id);
void NotifyManagedUsersSyncingStopped();
void NotifyManagedUsersChanged();
void DispatchCallbacks();
......
......@@ -14,6 +14,8 @@ class ManagedUserSyncServiceObserver {
virtual void OnManagedUsersSyncingStopped() = 0;
virtual void OnManagedUsersChanged() = 0;
protected:
virtual ~ManagedUserSyncServiceObserver() {}
};
......
......@@ -76,6 +76,7 @@ cr.define('options', function() {
didShowPage: function() {
options.ManagedUserListData.requestExistingManagedUsers().then(
this.receiveExistingManagedUsers_, this.onSigninError_.bind(this));
options.ManagedUserListData.addObserver(this);
this.updateImportInProgress_(false);
$('managed-user-import-error-bubble').hidden = true;
......@@ -91,6 +92,13 @@ cr.define('options', function() {
loadTimeData.getString('managedUserImportTitle');
},
/**
* @override
*/
didClosePage: function() {
options.ManagedUserListData.removeObserver(this);
},
/**
* Called when the user clicks the "OK" button. In case the managed
* user being imported has no avatar in sync, it shows the avatar
......
......@@ -12,7 +12,9 @@ cr.define('options', function() {
* @constructor
* @class
*/
function ManagedUserListData() {};
function ManagedUserListData() {
this.observers_ = [];
};
cr.addSingletonGetter(ManagedUserListData);
......@@ -31,7 +33,10 @@ cr.define('options', function() {
*/
ManagedUserListData.prototype.receiveExistingManagedUsers_ = function(
managedUsers) {
assert(this.promise_);
if (!this.promise_) {
this.onDataChanged_(managedUsers);
return;
}
this.resolve_(managedUsers);
};
......@@ -86,10 +91,51 @@ cr.define('options', function() {
this.promise_ = null;
};
/**
* Initializes |promise| with the new data and also passes the new data to
* observers.
* @param {Array.<Object>} managedUsers An array of managed user objects.
* For the format of the objects, see receiveExistingManagedUsers_().
* @private
*/
ManagedUserListData.prototype.onDataChanged_ = function(managedUsers) {
this.promise_ = this.createPromise_();
this.resolve_(managedUsers);
for (var i = 0; i < this.observers_.length; ++i)
this.observers_[i].receiveExistingManagedUsers_(managedUsers);
};
/**
* Adds an observer to the list of observers.
* @param {Object} observer The observer to be added.
* @private
*/
ManagedUserListData.prototype.addObserver_ = function(observer) {
for (var i = 0; i < this.observers_.length; ++i)
assert(this.observers_[i] != observer);
this.observers_.push(observer);
};
/**
* Removes an observer from the list of observers.
* @param {Object} observer The observer to be removed.
* @private
*/
ManagedUserListData.prototype.removeObserver_ = function(observer) {
for (var i = 0; i < this.observers_.length; ++i) {
if (this.observers_[i] == observer) {
this.observers_.splice(i, 1);
return;
}
}
};
// Forward public APIs to private implementations.
[
'addObserver',
'onSigninError',
'receiveExistingManagedUsers',
'removeObserver',
'requestExistingManagedUsers',
'resetPromise',
].forEach(function(name) {
......
......@@ -20,6 +20,7 @@
#include "chrome/browser/sync/profile_sync_service_observer.h"
#include "chrome/browser/ui/host_desktop.h"
#include "chrome/browser/ui/webui/options/options_ui.h"
#include "content/public/browser/notification_observer.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "ui/base/models/table_model_observer.h"
#include "ui/shell_dialogs/select_file_dialog.h"
......@@ -52,7 +53,8 @@ class BrowserOptionsHandler
#if defined(OS_CHROMEOS)
public chromeos::system::PointerDeviceObserver::Observer,
#endif
public TemplateURLServiceObserver {
public TemplateURLServiceObserver,
public content::NotificationObserver {
public:
BrowserOptionsHandler();
virtual ~BrowserOptionsHandler();
......
......@@ -404,7 +404,6 @@ void ChangePictureOptionsHandler::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
OptionsPageUIHandler::Observe(type, source, details);
if (type == chrome::NOTIFICATION_PROFILE_IMAGE_UPDATED) {
// User profile image has been updated.
SendProfileImage(*content::Details<const gfx::ImageSkia>(details).ptr(),
......
......@@ -8,6 +8,7 @@
#include "base/memory/weak_ptr.h"
#include "chrome/browser/image_decoder.h"
#include "chrome/browser/ui/webui/options/options_ui.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/native_widget_types.h"
......@@ -27,6 +28,7 @@ namespace options {
// ChromeOS user image options page UI handler.
class ChangePictureOptionsHandler : public ::options::OptionsPageUIHandler,
public ui::SelectFileDialog::Listener,
public content::NotificationObserver,
public ImageDecoder::Delegate {
public:
ChangePictureOptionsHandler();
......
......@@ -1354,7 +1354,6 @@ void InternetOptionsHandler::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
OptionsPageUIHandler::Observe(type, source, details);
if (type == chrome::NOTIFICATION_REQUIRE_PIN_SETTING_CHANGE_ENDED) {
base::FundamentalValue require_pin(*content::Details<bool>(details).ptr());
web_ui()->CallJavascriptFunction(
......
......@@ -12,6 +12,7 @@
#include "chrome/browser/ui/webui/options/options_ui.h"
#include "chromeos/login/login_state.h"
#include "chromeos/network/network_state_handler_observer.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "ui/gfx/native_widget_types.h"
......@@ -38,7 +39,8 @@ namespace options {
class InternetOptionsHandler
: public ::options::OptionsPageUIHandler,
public chromeos::NetworkStateHandlerObserver,
public chromeos::LoginState::Observer {
public chromeos::LoginState::Observer,
public content::NotificationObserver {
public:
InternetOptionsHandler();
virtual ~InternetOptionsHandler();
......@@ -100,7 +102,7 @@ class InternetOptionsHandler
// Updates the logged in user type.
void UpdateLoggedInUserType();
// content::NotificationObserver (from OptionsPageUIHandler)
// content::NotificationObserver
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
......
......@@ -545,9 +545,6 @@ void ContentSettingsHandler::Observe(
UpdateHandlersEnabledRadios();
break;
}
default:
OptionsPageUIHandler::Observe(type, source, details);
}
}
......
......@@ -23,6 +23,7 @@ class ProtocolHandlerRegistry;
namespace options {
class ContentSettingsHandler : public OptionsPageUIHandler,
public content::NotificationObserver,
public PepperFlashSettingsManager::Client {
public:
ContentSettingsHandler();
......
......@@ -8,6 +8,7 @@
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_member.h"
#include "chrome/browser/ui/webui/options/options_ui.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
namespace base {
......@@ -21,7 +22,8 @@ class Extension;
namespace options {
// Font settings overlay page UI handler.
class FontSettingsHandler : public OptionsPageUIHandler {
class FontSettingsHandler : public OptionsPageUIHandler,
public content::NotificationObserver {
public:
FontSettingsHandler();
virtual ~FontSettingsHandler();
......@@ -36,7 +38,7 @@ class FontSettingsHandler : public OptionsPageUIHandler {
virtual void RegisterMessages() OVERRIDE;
private:
// OptionsPageUIHandler implementation.
// content::NotificationObserver implementation.
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
......
......@@ -10,6 +10,7 @@
#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
#include "chrome/browser/ui/webui/options/options_ui.h"
#include "chrome/common/custom_handlers/protocol_handler.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
////////////////////////////////////////////////////////////////////////////////
......@@ -25,7 +26,8 @@ class DictionaryValue;
namespace options {
class HandlerOptionsHandler : public OptionsPageUIHandler {
class HandlerOptionsHandler : public OptionsPageUIHandler,
public content::NotificationObserver {
public:
HandlerOptionsHandler();
virtual ~HandlerOptionsHandler();
......
......@@ -193,8 +193,6 @@ void ManageProfileHandler::Observe(
SendProfileNames();
base::StringValue value(kManageProfileIconGridName);
SendProfileIcons(value);
} else {
OptionsPageUIHandler::Observe(type, source, details);
}
}
......
......@@ -11,6 +11,7 @@
#include "base/prefs/pref_change_registrar.h"
#include "chrome/browser/sync/profile_sync_service_observer.h"
#include "chrome/browser/ui/webui/options/options_ui.h"
#include "content/public/browser/notification_observer.h"
namespace base {
class StringValue;
......@@ -20,6 +21,7 @@ namespace options {
// Chrome personal stuff profiles manage overlay UI handler.
class ManageProfileHandler : public OptionsPageUIHandler,
public content::NotificationObserver,
public ProfileSyncServiceObserver {
public:
ManageProfileHandler();
......
......@@ -48,10 +48,18 @@ scoped_ptr<base::ListValue> GetAvatarIcons() {
namespace options {
ManagedUserImportHandler::ManagedUserImportHandler()
: weak_ptr_factory_(this) {
}
: weak_ptr_factory_(this) {}
ManagedUserImportHandler::~ManagedUserImportHandler() {}
ManagedUserImportHandler::~ManagedUserImportHandler() {
Profile* profile = Profile::FromWebUI(web_ui());
if (!profile->IsManaged()) {
ManagedUserSyncService* service =
ManagedUserSyncServiceFactory::GetForProfile(profile);
if (service)
service->RemoveObserver(this);
subscription_.reset();
}
}
void ManagedUserImportHandler::GetLocalizedValues(
base::DictionaryValue* localized_strings) {
......@@ -76,8 +84,17 @@ void ManagedUserImportHandler::GetLocalizedValues(
}
void ManagedUserImportHandler::InitializeHandler() {
Profile* profile = Profile::FromWebUI(web_ui());
registrar_.Add(this, chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED,
content::Source<Profile>(Profile::FromWebUI(web_ui())));
content::Source<Profile>(profile));
if (!profile->IsManaged()) {
ManagedUserSyncServiceFactory::GetForProfile(profile)->AddObserver(this);
subscription_ =
ManagedUserSharedSettingsServiceFactory::GetForBrowserContext(profile)
->Subscribe(
base::Bind(&ManagedUserImportHandler::OnSharedSettingChanged,
weak_ptr_factory_.GetWeakPtr()));
}
}
void ManagedUserImportHandler::RegisterMessages() {
......@@ -93,14 +110,18 @@ void ManagedUserImportHandler::Observe(
if (type == chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED) {
SigninGlobalError* error =
SigninGlobalError::GetForProfile(Profile::FromWebUI(web_ui()));
if (content::Details<SigninGlobalError>(details).ptr() != error)
return;
RequestManagedUserImportUpdate(NULL);
return;
if (content::Details<SigninGlobalError>(details).ptr() == error)
FetchManagedUsers();
}
}
void ManagedUserImportHandler::OnManagedUsersChanged() {
FetchManagedUsers();
}
OptionsPageUIHandler::Observe(type, source, details);
void ManagedUserImportHandler::FetchManagedUsers() {
web_ui()->CallJavascriptFunction("options.ManagedUserListData.resetPromise");
RequestManagedUserImportUpdate(NULL);
}
void ManagedUserImportHandler::RequestManagedUserImportUpdate(
......@@ -208,4 +229,11 @@ bool ManagedUserImportHandler::HasAuthError() const {
state == GoogleServiceAuthError::ACCOUNT_DISABLED;
}
void ManagedUserImportHandler::OnSharedSettingChanged(
const std::string& managed_user_id,
const std::string& key) {
if (key == managed_users::kChromeAvatarIndex)
FetchManagedUsers();
}
} // namespace options
......@@ -5,7 +5,9 @@
#ifndef CHROME_BROWSER_UI_WEBUI_OPTIONS_MANAGED_USER_IMPORT_HANDLER_H_
#define CHROME_BROWSER_UI_WEBUI_OPTIONS_MANAGED_USER_IMPORT_HANDLER_H_
#include "base/callback_list.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/managed_mode/managed_user_sync_service_observer.h"
#include "chrome/browser/ui/webui/options/options_ui.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
......@@ -18,8 +20,13 @@ class ListValue;
namespace options {
// Handler for the 'import existing managed user' dialog.
class ManagedUserImportHandler : public OptionsPageUIHandler {
class ManagedUserImportHandler : public OptionsPageUIHandler,
public content::NotificationObserver,
public ManagedUserSyncServiceObserver {
public:
typedef base::CallbackList<void(const std::string&, const std::string&)>
CallbackList;
ManagedUserImportHandler();
virtual ~ManagedUserImportHandler();
......@@ -35,7 +42,18 @@ class ManagedUserImportHandler : public OptionsPageUIHandler {
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
// ManagedUserSyncServiceObserver implementation.
virtual void OnManagedUserAcknowledged(const std::string& managed_user_id)
OVERRIDE {}
virtual void OnManagedUsersSyncingStopped() OVERRIDE {}
virtual void OnManagedUsersChanged() OVERRIDE;
private:
// Clears the cached list of managed users and fetches the new list of managed
// users.
void FetchManagedUsers();
// Callback for the "requestManagedUserImportUpdate" message.
// Checks the sign-in status of the custodian and accordingly
// sends an update to the WebUI. The update can be to show/hide
......@@ -61,6 +79,13 @@ class ManagedUserImportHandler : public OptionsPageUIHandler {
bool IsAccountConnected() const;
bool HasAuthError() const;
// Called when a managed user shared setting is changed. If the avatar was
// changed, FetchManagedUsers() is called.
void OnSharedSettingChanged(const std::string& managed_user_id,
const std::string& key);
scoped_ptr<CallbackList::Subscription> subscription_;
base::WeakPtrFactory<ManagedUserImportHandler> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(ManagedUserImportHandler);
......
......@@ -10,9 +10,7 @@
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_ui_controller.h"
#include "content/public/browser/web_ui_message_handler.h"
......@@ -37,8 +35,7 @@ class PointerDeviceObserver;
namespace options {
// The base class handler of Javascript messages of options pages.
class OptionsPageUIHandler : public content::WebUIMessageHandler,
public content::NotificationObserver {
class OptionsPageUIHandler : public content::WebUIMessageHandler {
public:
// Key for identifying the Settings App localized_strings in loadTimeData.
static const char kSettingsAppKey[];
......@@ -70,11 +67,6 @@ class OptionsPageUIHandler : public content::WebUIMessageHandler,
// WebUIMessageHandler implementation.
virtual void RegisterMessages() OVERRIDE {}
// content::NotificationObserver implementation.
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE {}
protected:
struct OptionsStringResource {
// The name of the resource in templateData.
......
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