1// Copyright 2013 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_REGISTRATION_UTILITY_H_ 6#define CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_REGISTRATION_UTILITY_H_ 7 8#include <map> 9#include <string> 10 11#include "base/callback.h" 12#include "base/gtest_prod_util.h" 13#include "base/memory/weak_ptr.h" 14#include "base/prefs/pref_change_registrar.h" 15#include "base/strings/string16.h" 16#include "chrome/browser/managed_mode/managed_user_sync_service.h" 17#include "chrome/browser/managed_mode/managed_user_sync_service_observer.h" 18#include "chrome/browser/managed_mode/managed_users.h" 19#include "chrome/browser/profiles/profile_manager.h" 20#include "components/browser_context_keyed_service/browser_context_keyed_service.h" 21 22class GoogleServiceAuthError; 23class ManagedUserRefreshTokenFetcher; 24class ManagedUserRegistrationUtilityTest; 25class PrefService; 26 27namespace browser_sync { 28class DeviceInfo; 29} 30 31// Structure to store registration information. 32struct ManagedUserRegistrationInfo { 33 explicit ManagedUserRegistrationInfo(const string16& name); 34 string16 name; 35 std::string master_key; 36}; 37 38// Holds the state necessary for registering a new managed user with the 39// management server and associating it with its custodian. Each instance 40// of this class handles registering a single managed user and should not 41// be used afterwards. 42class ManagedUserRegistrationUtility 43 : public ManagedUserSyncServiceObserver { 44 public: 45 // Callback for Register() below. If registration is successful, |token| will 46 // contain an OAuth2 refresh token for the newly registered managed user, 47 // otherwise |token| will be empty and |error| will contain the authentication 48 // error for the custodian. 49 typedef base::Callback<void(const GoogleServiceAuthError& /* error */, 50 const std::string& /* token */)> 51 RegistrationCallback; 52 53 virtual ~ManagedUserRegistrationUtility(); 54 55 static scoped_ptr<ManagedUserRegistrationUtility> Create(Profile* profile); 56 57 static std::string GenerateNewManagedUserId(); 58 59 // Registers a new managed user with the server. |managed_user_id| is a new 60 // unique ID for the new managed user. If its value is the same as that of 61 // of one of the existing managed users, then the same user will be created 62 // on this machine. |info| contains necessary information like the display 63 // name of the the user. |callback| is called with the result of the 64 // registration. We use the info here and not the profile, because on 65 // Chrome OS the profile of the managed user does not yet exist. 66 void Register(const std::string& managed_user_id, 67 const ManagedUserRegistrationInfo& info, 68 const RegistrationCallback& callback); 69 70 // ManagedUserSyncServiceObserver: 71 virtual void OnManagedUserAcknowledged(const std::string& managed_user_id) 72 OVERRIDE; 73 virtual void OnManagedUsersSyncingStopped() OVERRIDE; 74 75 private: 76 FRIEND_TEST_ALL_PREFIXES(ManagedUserRegistrationUtilityTest, Register); 77 FRIEND_TEST_ALL_PREFIXES(ManagedUserRegistrationUtilityTest, 78 RegisterBeforeInitialSync); 79 FRIEND_TEST_ALL_PREFIXES(ManagedUserRegistrationUtilityTest, 80 SyncServiceShutdownBeforeRegFinish); 81 FRIEND_TEST_ALL_PREFIXES(ManagedUserRegistrationUtilityTest, 82 StopSyncingBeforeRegFinish); 83 84 // Use the |Create(...)| method to get instances of this class. 85 ManagedUserRegistrationUtility( 86 PrefService* prefs, 87 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher, 88 ManagedUserSyncService* service); 89 // Fetches the managed user token when we have the device name. 90 void FetchToken(const std::string& client_name); 91 92 // Called when we have received a token for the managed user. 93 void OnReceivedToken(const GoogleServiceAuthError& error, 94 const std::string& token); 95 96 // Dispatches the callback and cleans up if all the conditions have been met. 97 void CompleteRegistrationIfReady(); 98 99 // Aborts any registration currently in progress. If |run_callback| is true, 100 // calls the callback specified in Register() with the given |error|. 101 void AbortPendingRegistration(bool run_callback, 102 const GoogleServiceAuthError& error); 103 104 // If |run_callback| is true, dispatches the callback with the saved token 105 // (which may be empty) and the given |error|. In any case, resets internal 106 // variables to be ready for the next registration. 107 void CompleteRegistration(bool run_callback, 108 const GoogleServiceAuthError& error); 109 110 // Cancels any registration currently in progress, without calling the 111 // callback or reporting an error. 112 void CancelPendingRegistration(); 113 114 base::WeakPtrFactory<ManagedUserRegistrationUtility> weak_ptr_factory_; 115 PrefService* prefs_; 116 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher_; 117 118 // A |BrowserContextKeyedService| owned by the custodian profile. 119 ManagedUserSyncService* managed_user_sync_service_; 120 121 std::string pending_managed_user_id_; 122 std::string pending_managed_user_token_; 123 bool pending_managed_user_acknowledged_; 124 bool is_existing_managed_user_; 125 RegistrationCallback callback_; 126 127 DISALLOW_COPY_AND_ASSIGN(ManagedUserRegistrationUtility); 128}; 129 130#endif // CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_REGISTRATION_UTILITY_H_ 131