Ocean
platform/meta/quest/platformsdk/Manager.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_PLATFORM_META_QUEST_PLATFORMSDK_MANAGER_H
9 #define META_OCEAN_PLATFORM_META_QUEST_PLATFORMSDK_MANAGER_H
10 
13 
14 #include "ocean/base/Lock.h"
15 #include "ocean/base/Singleton.h"
16 
17 #include <OVR_Platform.h>
18 
19 namespace Ocean
20 {
21 
22 namespace Platform
23 {
24 
25 namespace Meta
26 {
27 
28 namespace Quest
29 {
30 
31 namespace PlatformSDK
32 {
33 
34 /**
35  * This class implements a manager that handles the central PlatformSDK functionalities.
36  * @ingroup platformmetaquestplatformsdk
37  */
38 class OCEAN_PLATFORM_META_QUEST_PLATFORMSDK_EXPORT Manager :
39  public Singleton<Manager>,
40  protected MessageHandler
41 {
42  friend class Singleton<Manager>;
43  friend class MessageHandler;
44 
45  public:
46 
47  /**
48  * Definition of individual entitlement types.
49  */
50  enum EntitlementType : uint32_t
51  {
52  /// The entitlement state is not yet known.
53  ET_NOT_YET_KNOWN = 0u,
54  /// The user is not entitled.
56  /// The user is entitled.
58  };
59 
60  protected:
61 
62  /**
63  * Definition of an unordered map mapping message requests to response callback functions.
64  */
65  typedef std::unordered_map<ovrRequest, ResponseCallback> RequestMap;
66 
67  /**
68  * Definition of a pair combining a response callback with a subscription id.
69  */
70  typedef std::pair<ResponseCallback, unsigned int> ResponseSubscriptionPair;
71 
72  /**
73  * Definition of an unordered multi-map mapping message types to response subscription pairs.
74  */
75  typedef std::unordered_multimap<ovrMessageType, ResponseSubscriptionPair> ResponseTypeSubscriptionMap;
76 
77  public:
78 
79  /**
80  * Initializes the manager.
81  * The manager must be initialized once at application start.
82  * @param activityObject The native activity object, must be valid
83  * @param jniEnv The JNI environment, must be valid
84  * @param appId The application id which is used (provided when registering a new developer app), must be valid
85  * @return True, if succeeded
86  */
87  bool initialize(jobject activityObject, JNIEnv* jniEnv, const std::string& appId);
88 
89  /**
90  * Updates the manager and the underlying PlatformSDK functionalities.
91  * This function can be called even if the manager is not yet initialized.
92  * @param timestamp The current timestamp, with range (-infinity, infinity)
93  */
94  void update(const double timestamp);
95 
96  /**
97  * Returns whether the manager has been fully initialized.
98  * Call update() before calling this function.
99  * @return True, if so
100  */
101  inline bool isInitialized() const;
102 
103  /**
104  * Returns the id of the currently logged-in user.
105  * @param accessToken Optional resulting access token for the logged-in user, empty if unknown or not yet known
106  * @return The user's id, 0 if unknown or not yet known
107  */
108  inline uint64_t userId(std::string* accessToken = nullptr) const;
109 
110  /**
111  * Returns the entitlement type.
112  * One of the requirements to sell an app in the Meta Quest Store is that you verify the user purchased or obtained your app legitimately.
113  * This check is called the entitlement check. You should make the entitlement check within 10 seconds of the user launching your app.
114  * @return The user's entitelement type
115  * @see https://developer.oculus.com/documentation/native/ps-entitlement-check/
116  */
117  inline EntitlementType entitlementType() const;
118 
119  /**
120  * Invokes a new request.
121  * @param requestId The id of the new request
122  * @param responseCallback The callback function which will be called once the response has arrived
123  * @return True, if succeeded
124  */
125  bool invokeRequest(const ovrRequest requestId, ResponseCallback responseCallback);
126 
127  /**
128  * Subscribes a callback function for response messages with specific type.
129  * @param messageType The type of the message for wich the callback function will be subscibed, ovrMessage_Unknown to subscribe to all messages
130  * @param responseCallback The callback function which will be called whenever a response message with the specified type arrives
131  * @return The subscription object, the subscription is active as long as the subscription object exists
132  * @see unsubscribeForMessageResponse().
133  */
134  MessageScopedSubscription subscribeForMessageResponse(const ovrMessageType& messageType, ResponseCallback responseCallback);
135 
136  protected:
137 
138  /**
139  * Protected default constructor.
140  */
141  Manager() = default;
142 
143  /**
144  * Destructs the manager and releases all resources.
145  */
147 
148  /**
149  * Removes a subscription for response messages with specific message type.
150  * @param subscriptionId The subscription id to unsubscribe, must be valid
151  * @see subscribeForMessageResponse().
152  */
153  void unsubscribeForMessageResponse(const unsigned int subscriptionId);
154 
155  /**
156  * The response function for ovr_PlatformInitializeAndroidAsynchronous().
157  * @param message The response message, must be valid
158  * @param succeeded True, if the request succeeded
159  */
160  void onInitialized(ovrMessage* message, const bool succeeded);
161 
162  /**
163  * The response function for ovr_Entitlement_GetIsViewerEntitled().
164  * @param message The response message, must be valid
165  * @param succeeded True, if the request succeeded
166  */
167  void onGetIsViewerEntitled(ovrMessage* message, const bool succeeded);
168 
169  /**
170  * The response function for ovr_User_GetLoggedInUser().
171  * @param message The response message, must be valid
172  * @param succeeded True, if the request succeeded
173  */
174  void onGetLoggedInUser(ovrMessage* message, const bool succeeded);
175 
176  /**
177  * The response function for ovr_User_GetAccessToken().
178  * @param message The response message, must be valid
179  * @param succeeded True, if the request succeeded
180  */
181  void onGetAccessToken(ovrMessage* message, const bool succeeded);
182 
183  protected:
184 
185  /// True, if the manager's initialization is pending.
186  bool initializationPending_ = false;
187 
188  /// True, if the manager has been initialized.
189  bool isInitialized_ = false;
190 
191  /// The entitlement type of the user.
192  EntitlementType entitlementType_ = ET_NOT_YET_KNOWN;
193 
194  /// The deep link which has been used to launch the app, empty if the app was launched normally.
195  std::string launchDeepLink_;
196 
197  /// The counter for subscription ids.
198  unsigned int subscriptionIdCounter_ = 1u;
199 
200  /// The map mapping request ids to response callback functions.
202 
203  /// The map mapping response message types to subscription pairs.
205 
206  /// The lock for the request map.
208 
209  /// The id of the logged-in user.
210  ovrID userId_ = ovrID(0);
211 
212  /// The access token of the logged-in user.
213  std::string accessToken_;
214 
215  /// The result queue for launchAnotherApplication().
217 
218  /// The manager's lock.
219  mutable Lock lock_;
220 };
221 
222 inline bool Manager::isInitialized() const
223 {
224  const ScopedLock scopedLock(lock_);
225 
226  return isInitialized_;
227 }
228 
229 inline uint64_t Manager::userId(std::string* accessToken) const
230 {
231  static_assert(sizeof(ovrID) == sizeof(uint64_t), "Invalid data type!");
232 
233  const ScopedLock scopedLock(lock_);
234 
235  if (accessToken != nullptr)
236  {
237  *accessToken = accessToken_;
238  }
239 
240  return userId_;
241 }
242 
244 {
245  const ScopedLock scopedLock(lock_);
246 
247  return entitlementType_;
248 }
249 
250 }
251 
252 }
253 
254 }
255 
256 }
257 
258 }
259 
260 #endif // META_OCEAN_PLATFORM_META_QUEST_PLATFORMSDK_MANAGER_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a manager that handles the central PlatformSDK functionalities.
Definition: platform/meta/quest/platformsdk/Manager.h:41
Lock requestMapLock_
The lock for the request map.
Definition: platform/meta/quest/platformsdk/Manager.h:207
void unsubscribeForMessageResponse(const unsigned int subscriptionId)
Removes a subscription for response messages with specific message type.
Manager()=default
Protected default constructor.
std::unordered_map< ovrRequest, ResponseCallback > RequestMap
Definition of an unordered map mapping message requests to response callback functions.
Definition: platform/meta/quest/platformsdk/Manager.h:65
bool isInitialized_
True, if the manager has been initialized.
Definition: platform/meta/quest/platformsdk/Manager.h:189
Lock lock_
The manager's lock.
Definition: platform/meta/quest/platformsdk/Manager.h:219
std::pair< ResponseCallback, unsigned int > ResponseSubscriptionPair
Definition of a pair combining a response callback with a subscription id.
Definition: platform/meta/quest/platformsdk/Manager.h:70
void onInitialized(ovrMessage *message, const bool succeeded)
The response function for ovr_PlatformInitializeAndroidAsynchronous().
EntitlementType
Definition of individual entitlement types.
Definition: platform/meta/quest/platformsdk/Manager.h:51
@ ET_ENTITLED
The user is entitled.
Definition: platform/meta/quest/platformsdk/Manager.h:57
@ ET_NOT_ENTITLED
The user is not entitled.
Definition: platform/meta/quest/platformsdk/Manager.h:55
std::unordered_multimap< ovrMessageType, ResponseSubscriptionPair > ResponseTypeSubscriptionMap
Definition of an unordered multi-map mapping message types to response subscription pairs.
Definition: platform/meta/quest/platformsdk/Manager.h:75
EntitlementType entitlementType() const
Returns the entitlement type.
Definition: platform/meta/quest/platformsdk/Manager.h:243
uint64_t userId(std::string *accessToken=nullptr) const
Returns the id of the currently logged-in user.
Definition: platform/meta/quest/platformsdk/Manager.h:229
void onGetIsViewerEntitled(ovrMessage *message, const bool succeeded)
The response function for ovr_Entitlement_GetIsViewerEntitled().
bool invokeRequest(const ovrRequest requestId, ResponseCallback responseCallback)
Invokes a new request.
bool isInitialized() const
Returns whether the manager has been fully initialized.
Definition: platform/meta/quest/platformsdk/Manager.h:222
std::string launchDeepLink_
The deep link which has been used to launch the app, empty if the app was launched normally.
Definition: platform/meta/quest/platformsdk/Manager.h:195
std::string accessToken_
The access token of the logged-in user.
Definition: platform/meta/quest/platformsdk/Manager.h:213
RequestQueue< bool > setGroupPresenceRequestQueue_
The result queue for launchAnotherApplication().
Definition: platform/meta/quest/platformsdk/Manager.h:216
void onGetAccessToken(ovrMessage *message, const bool succeeded)
The response function for ovr_User_GetAccessToken().
void update(const double timestamp)
Updates the manager and the underlying PlatformSDK functionalities.
ovrID userId_
The id of the logged-in user.
Definition: platform/meta/quest/platformsdk/Manager.h:210
RequestMap requestMap_
The map mapping request ids to response callback functions.
Definition: platform/meta/quest/platformsdk/Manager.h:201
bool initialize(jobject activityObject, JNIEnv *jniEnv, const std::string &appId)
Initializes the manager.
void onGetLoggedInUser(ovrMessage *message, const bool succeeded)
The response function for ovr_User_GetLoggedInUser().
MessageScopedSubscription subscribeForMessageResponse(const ovrMessageType &messageType, ResponseCallback responseCallback)
Subscribes a callback function for response messages with specific type.
ResponseTypeSubscriptionMap responseTypeSubscriptionMap_
The map mapping response message types to subscription pairs.
Definition: platform/meta/quest/platformsdk/Manager.h:204
~Manager()
Destructs the manager and releases all resources.
EntitlementType entitlementType_
The entitlement type of the user.
Definition: platform/meta/quest/platformsdk/Manager.h:192
This class implements a helper object allowing to queue responses of requests.
Definition: MessageHandler.h:162
This class is the base class for all objects needing to handle with messages.
Definition: MessageHandler.h:40
std::function< void(ovrMessage *message, bool succeeded)> ResponseCallback
Definition of a callback function for message responses.
Definition: MessageHandler.h:201
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
This class implements a subscription object which can be used unique subscriptions to e....
Definition: ScopedSubscription.h:28
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15