forked from youtube/cobalt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.h
545 lines (467 loc) · 24 KB
/
system.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// Copyright 2015 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Module Overview: Starboard System module
//
// Defines a broad set of APIs that allow the client application to query
// build and runtime properties of the enclosing system.
#ifndef STARBOARD_SYSTEM_H_
#define STARBOARD_SYSTEM_H_
#include "starboard/configuration.h"
#include "starboard/export.h"
#include "starboard/types.h"
#ifdef __cplusplus
extern "C" {
#endif
// A type that can represent a system error code across all Starboard platforms.
typedef int SbSystemError;
// Enumeration of special paths that the platform can define.
typedef enum SbSystemPathId {
// Path to where the local content files that ship with the binary are
// available.
kSbSystemPathContentDirectory,
// Path to the directory that can be used as a local file cache, if
// available.
kSbSystemPathCacheDirectory,
// Path to the directory where debug output (e.g. logs, trace output,
// screenshots) can be written into.
kSbSystemPathDebugOutputDirectory,
// Path to a directory where system font files can be found. Should only be
// specified on platforms that provide fonts usable by Starboard applications.
kSbSystemPathFontDirectory,
// Path to a directory where system font configuration metadata can be
// found. May be the same directory as |kSbSystemPathFontDirectory|, but not
// necessarily. Should only be specified on platforms that provide fonts
// usable by Starboard applications.
kSbSystemPathFontConfigurationDirectory,
// Path to a directory where temporary files can be written.
kSbSystemPathTempDirectory,
// Full path to the executable file.
kSbSystemPathExecutableFile,
// Path to the directory dedicated for Evergreen Full permanent file storage.
// Both read and write access is required.
// The directory should be used only for storing the updates.
// See starboard/doc/evergreen/cobalt_evergreen_overview.md
kSbSystemPathStorageDirectory,
} SbSystemPathId;
// System properties that can be queried for. Many of these are used in
// User-Agent string generation.
typedef enum SbSystemPropertyId {
// The certification scope that identifies a group of devices.
kSbSystemPropertyCertificationScope,
// The full model number of the main platform chipset, including any
// vendor-specific prefixes.
kSbSystemPropertyChipsetModelNumber,
// The production firmware version number which the device is currently
// running.
kSbSystemPropertyFirmwareVersion,
// A friendly name for this actual device. It may include user-personalization
// like "Upstairs Bedroom." It may be displayed to users as part of some kind
// of device selection (e.g. in-app DIAL).
kSbSystemPropertyFriendlyName,
// A deprecated alias for |kSbSystemPropertyBrandName|.
kSbSystemPropertyManufacturerName,
// The name of the brand under which the device is being sold.
kSbSystemPropertyBrandName = kSbSystemPropertyManufacturerName,
// The final production model number of the device.
kSbSystemPropertyModelName,
// The year the device was launched, e.g. "2016".
kSbSystemPropertyModelYear,
// The corporate entity responsible for submitting the device to YouTube
// certification and for the device maintenance/updates.
kSbSystemPropertySystemIntegratorName,
// The name of the operating system and platform, suitable for inclusion in a
// User-Agent, say.
kSbSystemPropertyPlatformName,
// The Google Speech API key. The platform manufacturer is responsible
// for registering a Google Speech API key for their products. In the API
// Console (http://developers.google.com/console), you can enable the
// Speech APIs and generate a Speech API key.
kSbSystemPropertySpeechApiKey,
// A field that, if available, is appended to the user agent
kSbSystemPropertyUserAgentAuxField,
// Advertising ID or IFA, typically a 128-bit UUID
// Please see https://iabtechlab.com/OTT-IFA for details.
// Corresponds to 'ifa' field. Note: `ifa_type` field is not provided.
kSbSystemPropertyAdvertisingId,
// Limit advertising tracking, treated as boolean. Set to nonzero to indicate
// a true value. Corresponds to 'lmt' field.
kSbSystemPropertyLimitAdTracking,
// Type of the device, e.g. such as "TV", "STB", "OTT"
// Please see Youtube Technical requirements for a full list of allowed values
kSbSystemPropertyDeviceType,
} SbSystemPropertyId;
// Enumeration of device types.
typedef enum SbSystemDeviceType {
// Blue-ray Disc Player (BDP).
kSbSystemDeviceTypeBlueRayDiskPlayer,
// A relatively high-powered TV device used primarily for playing games.
kSbSystemDeviceTypeGameConsole,
// Over the top (OTT) devices stream content via the Internet over another
// type of network, e.g. cable or satellite.
kSbSystemDeviceTypeOverTheTopBox,
// Set top boxes (STBs) stream content primarily over cable or satellite.
// Some STBs can also stream OTT content via the Internet.
kSbSystemDeviceTypeSetTopBox,
// A Smart TV is a TV that can directly run applications that stream OTT
// content via the Internet.
kSbSystemDeviceTypeTV,
// Desktop PC.
kSbSystemDeviceTypeDesktopPC,
// An Android TV Device.
kSbSystemDeviceTypeAndroidTV,
// A wall video projector.
kSbSystemDeviceTypeVideoProjector,
// Unknown device.
kSbSystemDeviceTypeUnknown,
} SbSystemDeviceType;
// Runtime capabilities are boolean properties of a platform that can't be
// determined at compile-time. They may vary from device to device, but they
// will not change over the course of a single execution. They often specify
// particular behavior of other APIs within the bounds of their operating range.
typedef enum SbSystemCapabilityId {
// Whether this system has reversed Enter and Back keys.
kSbSystemCapabilityReversedEnterAndBack,
// Whether this system has the ability to report on GPU memory usage.
// If (and only if) a system has this capability will
// SbSystemGetTotalGPUMemory() and SbSystemGetUsedGPUMemory() be valid to
// call.
kSbSystemCapabilityCanQueryGPUMemoryStats,
// ATTENTION: Do not add more to this enum. Instead add an "IsSupported"
// function in the relevant module.
} SbSystemCapabilityId;
// Enumeration of possible values for the |type| parameter passed to the
// |SbSystemRaisePlatformError| function.
typedef enum SbSystemPlatformErrorType {
// Cobalt received a network connection error, or a network disconnection
// event. If the |response| passed to |SbSystemPlatformErrorCallback| is
// |kSbSystemPlatformErrorResponsePositive| then the request should be
// retried, otherwise the app should be stopped.
kSbSystemPlatformErrorTypeConnectionError,
} SbSystemPlatformErrorType;
// Possible responses for |SbSystemPlatformErrorCallback|.
typedef enum SbSystemPlatformErrorResponse {
kSbSystemPlatformErrorResponsePositive,
kSbSystemPlatformErrorResponseNegative,
kSbSystemPlatformErrorResponseCancel
} SbSystemPlatformErrorResponse;
// Type of callback function that may be called in response to an error
// notification from |SbSystemRaisePlatformError|. |response| is a code to
// indicate the user's response, e.g. if the platform raised a dialog to notify
// the user of the error. |user_data| is the opaque pointer that was passed to
// the call to |SbSystemRaisePlatformError|.
typedef void (*SbSystemPlatformErrorCallback)(
SbSystemPlatformErrorResponse response,
void* user_data);
// Private structure used to represent a raised platform error.
typedef struct SbSystemPlatformErrorPrivate SbSystemPlatformErrorPrivate;
// Cobalt calls this function to notify the platform that an error has occurred
// in the application that the platform may need to handle. The platform is
// expected to then notify the user of the error and to provide a means for
// any required interaction, such as by showing a dialog.
//
// The return value is a boolean. If the platform cannot respond to the error,
// then this function should return |false|, otherwise it should return |true|.
//
// This function may be called from any thread, and it is the platform's
// responsibility to decide how to handle an error received while a previous
// error is still pending. If that platform can only handle one error at a
// time, then it may queue the second error or ignore it by returning
// |kSbSystemPlatformErrorInvalid|.
//
// |type|: An error type, from the SbSystemPlatformErrorType enum,
// that defines the error.
// |callback|: A function that may be called by the platform to let the caller
// know that the user has reacted to the error.
// |user_data|: An opaque pointer that the platform should pass as an argument
// to the callback function, if it is called.
SB_EXPORT bool SbSystemRaisePlatformError(
SbSystemPlatformErrorType type,
SbSystemPlatformErrorCallback callback,
void* user_data);
// Pointer to a function to compare two items. The return value uses standard
// |*cmp| semantics:
// - |< 0| if |a| is less than |b|
// - |0| if the two items are equal
// - |> 1| if |a| is greater than |b|
//
// |a|: The first value to compare.
// |b|: The second value to compare.
typedef int (*SbSystemComparator)(const void* a, const void* b);
// Breaks the current program into the debugger, if a debugger is attached.
// If a debugger is not attached, this function aborts the program.
SB_NORETURN SB_EXPORT void SbSystemBreakIntoDebugger();
// Attempts to determine whether the current program is running inside or
// attached to a debugger. The function returns |false| if neither of those
// cases is true.
SB_EXPORT bool SbSystemIsDebuggerAttached();
// Returns the number of processor cores available to this application. If the
// process is sandboxed to a subset of the physical cores, the function returns
// that sandboxed limit.
SB_EXPORT int SbSystemGetNumberOfProcessors();
// Returns the total CPU memory (in bytes) potentially available to this
// application. If the process is sandboxed to a maximum allowable limit,
// the function returns the lesser of the physical and sandbox limits.
SB_EXPORT int64_t SbSystemGetTotalCPUMemory();
// Returns the total physical CPU memory (in bytes) used by this application.
// This value should always be less than (or, in particularly exciting
// situations, equal to) SbSystemGetTotalCPUMemory().
SB_EXPORT int64_t SbSystemGetUsedCPUMemory();
// Returns the total GPU memory (in bytes) available for use by this
// application. This function may only be called the return value for calls to
// SbSystemHasCapability(kSbSystemCapabilityCanQueryGPUMemoryStats) is |true|.
SB_EXPORT int64_t SbSystemGetTotalGPUMemory();
// Returns the current amount of GPU memory (in bytes) that is currently being
// used by this application. This function may only be called if the return
// value for calls to
// SbSystemHasCapability(kSbSystemCapabilityCanQueryGPUMemoryStats) is |true|.
SB_EXPORT int64_t SbSystemGetUsedGPUMemory();
// Returns if the device is disconnected from network. "Disconnected" is chosen
// over connected because disconnection can be determined with more certainty
// than connection usually.
SB_EXPORT bool SbSystemNetworkIsDisconnected();
// Retrieves the platform-defined system path specified by |path_id| and
// places it as a zero-terminated string into the user-allocated |out_path|
// unless it is longer than |path_length| - 1. This implementation must be
// thread-safe.
//
// This function returns |true| if the path is retrieved successfully. It
// returns |false| under any of the following conditions and, in any such
// case, |out_path| is not changed:
// - |path_id| is invalid for this platform
// - |path_length| is too short for the given result
// - |out_path| is NULL
//
// |path_id|: The system path to be retrieved.
// |out_path|: The platform-defined system path specified by |path_id|.
// |path_length|: The length of the system path.
SB_EXPORT bool SbSystemGetPath(SbSystemPathId path_id,
char* out_path,
int path_length);
// Retrieves the platform-defined system property specified by |property_id| and
// places its value as a zero-terminated string into the user-allocated
// |out_value| unless it is longer than |value_length| - 1. This implementation
// must be thread-safe.
//
// This function returns |true| if the property is retrieved successfully. It
// returns |false| under any of the following conditions and, in any such
// case, |out_value| is not changed:
// - |property_id| is invalid for this platform
// - |value_length| is too short for the given result
// - |out_value| is NULL
//
// |property_id|: The system path to be retrieved.
// |out_value|: The platform-defined system property specified by |property_id|.
// |value_length|: The length of the system property.
SB_EXPORT bool SbSystemGetProperty(SbSystemPropertyId property_id,
char* out_value,
int value_length);
// Returns whether the platform has the runtime capability specified by
// |capability_id|. Returns false for any unknown capabilities. This
// implementation must be thread-safe.
//
// |capability_id|: The runtime capability to check.
SB_EXPORT bool SbSystemHasCapability(SbSystemCapabilityId capability_id);
// Gets the system's current POSIX-style Locale ID. The locale represents the
// location, language, and cultural conventions that the system wants to use,
// which affects which text is displayed to the user as well as how displayed
// numbers, dates, currency, and similar values are formatted.
//
// At its simplest, the locale ID can just be a BCP 47 language code, like
// |en_US|. Currently, POSIX also wants to include the encoding as in
// |en_US.UTF8|. POSIX also allows a couple very bare-bones locales, like "C"
// or "POSIX", but they are not supported here. POSIX also supports different
// locale settings for a few different purposes, but Starboard only exposes one
// locale at a time.
//
// RFC 5646 describes BCP 47 language codes:
// https://tools.ietf.org/html/bcp47
//
// For more information than you probably want about POSIX locales, see:
// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html
SB_EXPORT const char* SbSystemGetLocaleId();
// A cryptographically secure random number generator that gets 64 random bits
// and returns them as an |uint64_t|. This function does not require manual
// seeding.
SB_EXPORT uint64_t SbSystemGetRandomUInt64();
// A cryptographically secure random number generator that produces an
// arbitrary, non-negative number of |buffer_size| random, non-negative bytes.
// The generated number is placed in |out_buffer|. This function does not
// require manual seeding.
//
// |out_buffer|: A pointer for the generated random number. This value must
// not be null.
// |buffer_size|: The size of the random number, in bytes.
SB_EXPORT void SbSystemGetRandomData(void* out_buffer, int buffer_size);
// Gets the last platform-specific error code produced by any Starboard call in
// the current thread for diagnostic purposes. Semantic reactions to Starboard
// function call results should be modeled explicitly.
SB_EXPORT SbSystemError SbSystemGetLastError();
// Clears the last error set by a Starboard call in the current thread.
SB_EXPORT void SbSystemClearLastError();
// Generates a human-readable string for an error. The return value specifies
// the total desired length of the string.
//
// |error|: The error for which a human-readable string is generated.
// |out_string|: The generated string. This value may be null, and it is
// always terminated with a null byte.
// |string_length|: The maximum length of the error string.
SB_EXPORT int SbSystemGetErrorString(SbSystemError error,
char* out_string,
int string_length);
// Places up to |stack_size| instruction pointer addresses of the current
// execution stack into |out_stack|. The return value specifies the number
// of entries added.
//
// The returned stack frames are in "downward" order from the calling frame
// toward the entry point of the thread. So, if all the stack frames do not
// fit, the ones truncated will be the less interesting ones toward the thread
// entry point.
//
// This function is used in crash signal handlers and, therefore, it must
// be async-signal-safe on platforms that support signals. The following
// document discusses what it means to be async-signal-safe on POSIX:
// http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html#tag_02_04_03
//
// |out_stack|: A non-NULL array of |void *| of at least |stack_size| entries.
// |stack_size|: The maximum number of instruction pointer addresses to be
// placed into |out_stack| from the current execution stack.
SB_EXPORT int SbSystemGetStack(void** out_stack, int stack_size);
// Looks up |address| as an instruction pointer and places up to
// (|buffer_size - 1|) characters of the symbol associated with it in
// |out_buffer|, which must not be NULL. |out_buffer| will be NULL-terminated.
//
// The return value indicates whether the function found a reasonable match
// for |address|. If the return value is |false|, then |out_buffer| is not
// modified.
//
// This function is used in crash signal handlers and, therefore, it must
// be async-signal-safe on platforms that support signals.
SB_EXPORT bool SbSystemSymbolize(const void* address,
char* out_buffer,
int buffer_size);
// Requests that the application move into the Blurred state at the next
// convenient point. This should roughly correspond to "unfocused application"
// in a traditional window manager, where the application may be partially
// visible.
//
// This function eventually causes a |kSbEventTypeBlur| event to be dispatched
// to the application. Before the |kSbEventTypeBlur| event is dispatched, some
// work may continue to be done, and unrelated system events may be dispatched.
SB_EXPORT void SbSystemRequestBlur();
// Requests that the application move into the Started state at the next
// convenient point. This should roughly correspond to a "focused application"
// in a traditional window manager, where the application is fully visible and
// the primary receiver of input events.
//
// This function eventually causes a |kSbEventTypeFocus| event to be
// dispatched to the application. Before |kSbEventTypeFocus| is dispatched,
// some work may continue to be done, and unrelated system events may be
// dispatched.
SB_EXPORT void SbSystemRequestFocus();
// Requests that the application move into the Concealed state at the next
// convenient point. This should roughly correspond to "minimization" in a
// traditional window manager, where the application is no longer visible.
// However, the background tasks can still be running.
//
// This function eventually causes a |kSbEventTypeConceal| event to be
// dispatched to the application. Before the |kSbEventTypeConceal| event is
// dispatched, some work may continue to be done, and unrelated system events
// may be dispatched.
//
// In the Concealed state, the application will be invisible, but probably
// still be running background tasks. The expectation is that an external
// system event will bring the application out of the Concealed state.
SB_EXPORT void SbSystemRequestConceal();
// Requests that the application move into the Blurred state at the next
// convenient point. This should roughly correspond to a "focused application"
// in a traditional window manager, where the application is fully visible and
// the primary receiver of input events.
//
// This function eventually causes a |kSbEventTypeReveal| event to be
// dispatched to the application. Before the |kSbEventTypeReveal| event is
// dispatched, some work may continue to be done, and unrelated system events
// may be dispatched.
SB_EXPORT void SbSystemRequestReveal();
// Requests that the application move into the Frozen state at the next
// convenient point.
//
// This function eventually causes a |kSbEventTypeFreeze| event to be
// dispatched to the application. Before the |kSbEventTypeSuspend| event is
// dispatched, some work may continue to be done, and unrelated system events
// may be dispatched.
//
// In the Frozen state, the application will be resident, but probably not
// running. The expectation is that an external system event will bring the
// application out of the Frozen state.
SB_EXPORT void SbSystemRequestFreeze();
// Requests that the application be terminated gracefully at the next
// convenient point. In the meantime, some work may continue to be done, and
// unrelated system events may be dispatched. This function eventually causes
// a |kSbEventTypeStop| event to be dispatched to the application. When the
// process finally terminates, it returns |error_level|, if that has any
// meaning on the current platform.
//
// |error_level|: An integer that serves as the return value for the process
// that is eventually terminated as a result of a call to this function.
SB_EXPORT void SbSystemRequestStop(int error_level);
// Hides the system splash screen on systems that support a splash screen that
// is displayed while the application is loading. This function may be called
// from any thread and must be idempotent.
SB_EXPORT void SbSystemHideSplashScreen();
// Returns false if the platform doesn't need resume after suspend support. In
// such case Cobalt will free up the resource it retains for resume after
// suspend.
// Note that if this function returns false, the Starboard implementation cannot
// send kSbEventTypeResume to the event handler.
// The return value of this function cannot change over the life time of the
// application.
SB_EXPORT bool SbSystemSupportsResume();
// Returns pointer to a constant global struct implementing the extension named
// |name|, if it is implemented. Otherwise return NULL. The |name| string must
// not be NULL.
//
// Extensions are used to implement behavior which is specific to the
// combination of application & platform. An extension relies on a header file
// in the "extension" subdirectory of an app, which is used by both the
// application and the Starboard platform to define an extension API struct.
// Since the header is used both above and below Starboard, it cannot include
// any files from above Starboard. It may depend on Starboard headers. That
// API struct has only 2 required fields which must be first: a const char*
// |name|, storing the extension name, and a uint32_t |version| storing the
// version number of the extension. All other fields may be C types (including
// custom structs) or function pointers. The application will query for the
// function by name using SbSystemGetExtension, and the platform returns a
// pointer to the singleton instance of the extension struct. The singleton
// struct should be constant after initialization, since the application may
// only get the extension once, meaning updated values would be ignored. As
// the version of extensions are incremented, fields may be added to the end
// of the struct, but never removed (only deprecated).
SB_EXPORT const void* SbSystemGetExtension(const char* name);
// Computes a HMAC-SHA256 digest of |message| into |digest| using the
// application's certification secret. The |message| and the |digest|
// pointers must not be NULL.
//
// The output will be written into |digest|. |digest_size_in_bytes| must be 32
// (or greater), since 32-bytes will be written into it.
// Returns false in the case of an error, or if it is not implemented. In this
// case the contents of |digest| will be undefined.
SB_EXPORT bool SbSystemSignWithCertificationSecretKey(
const uint8_t* message,
size_t message_size_in_bytes,
uint8_t* digest,
size_t digest_size_in_bytes);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // STARBOARD_SYSTEM_H_