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.
5 #ifndef CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_OAUTH2_TOKEN_SERVICE_FACTORY_H_
6 #define CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_OAUTH2_TOKEN_SERVICE_FACTORY_H_
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "base/memory/weak_ptr.h"
17 class DeviceOAuth2TokenService;
19 class DeviceOAuth2TokenServiceFactory {
21 // Callback type used for Get() function.
22 typedef base::Callback<void(DeviceOAuth2TokenService*)> GetCallback;
24 // Returns the instance of the DeviceOAuth2TokenService singleton via the
25 // given callback. This function is asynchronous as initializing
26 // DeviceOAuth2TokenService involves asynchronous D-Bus method calls.
28 // May return NULL during browser startup and shutdown. May also return
29 // NULL if Initialize() is not called beforehand, which can happen in unit
32 // Do not hold the pointer returned by this method; call this method every
33 // time and check for NULL to handle the case where this instance is
34 // destroyed during shutdown.
35 static void Get(const GetCallback& callback);
37 // Called by ChromeBrowserMainPartsChromeOS in order to bootstrap the
38 // DeviceOAuth2TokenService instance after the required global data is
39 // available (local state and request context getter).
40 static void Initialize();
42 // Called by ChromeBrowserMainPartsChromeOS in order to shutdown the
43 // DeviceOAuth2TokenService instance and cancel all in-flight requests
44 // before the required global data is destroyed (local state and request
46 static void Shutdown();
49 DeviceOAuth2TokenServiceFactory();
50 ~DeviceOAuth2TokenServiceFactory();
52 // Creates the token service asynchronously in the following steps:
53 // 1) Get the system salt from cryptohomed asynchronously
54 // 2) Create CryptohomeTokenEncryptor using the system salt
55 // 3) Create DeviceOAuth2TokenServiceFactory using the token encryptor
56 void CreateTokenService();
58 // Continuation of CreateTokenService(). Called when GetSystemSalt() is
60 void DidGetSystemSalt(const std::string& system_salt);
62 // Runs the callback asynchronously. If |token_service_| is ready, the
63 // callback will be simply run via MessageLoop. Otherwise, the callback
64 // will be queued in |pending_callbacks_| and run when |token_service_| is
66 void RunAsync(const GetCallback& callback);
68 // True if the factory is initialized (i.e. system salt retrieval is done
69 // regardless of whether it succeeded or failed).
71 DeviceOAuth2TokenService* token_service_;
72 std::queue<GetCallback> pending_callbacks_;
73 base::WeakPtrFactory<DeviceOAuth2TokenServiceFactory> weak_ptr_factory_;
75 DISALLOW_COPY_AND_ASSIGN(DeviceOAuth2TokenServiceFactory);
78 } // namespace chromeos
80 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_OAUTH2_TOKEN_SERVICE_FACTORY_H_