- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / settings / device_oauth2_token_service_factory.h
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_CHROMEOS_SETTINGS_DEVICE_OAUTH2_TOKEN_SERVICE_FACTORY_H_
6 #define CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_OAUTH2_TOKEN_SERVICE_FACTORY_H_
7
8 #include <queue>
9 #include <string>
10
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "base/memory/weak_ptr.h"
14
15 namespace chromeos {
16
17 class DeviceOAuth2TokenService;
18
19 class DeviceOAuth2TokenServiceFactory {
20  public:
21   // Callback type used for Get() function.
22   typedef base::Callback<void(DeviceOAuth2TokenService*)> GetCallback;
23
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.
27   //
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
30   // tests.
31   //
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);
36
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();
41
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
45   // context getter).
46   static void Shutdown();
47
48  private:
49   DeviceOAuth2TokenServiceFactory();
50   ~DeviceOAuth2TokenServiceFactory();
51
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();
57
58   // Continuation of CreateTokenService(). Called when GetSystemSalt() is
59   // complete.
60   void DidGetSystemSalt(const std::string& system_salt);
61
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
65   // ready.
66   void RunAsync(const GetCallback& callback);
67
68   // True if the factory is initialized (i.e. system salt retrieval is done
69   // regardless of whether it succeeded or failed).
70   bool initialized_;
71   DeviceOAuth2TokenService* token_service_;
72   std::queue<GetCallback> pending_callbacks_;
73   base::WeakPtrFactory<DeviceOAuth2TokenServiceFactory> weak_ptr_factory_;
74
75   DISALLOW_COPY_AND_ASSIGN(DeviceOAuth2TokenServiceFactory);
76 };
77
78 }  // namespace chromeos
79
80 #endif  // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_OAUTH2_TOKEN_SERVICE_FACTORY_H_