1/*
2 * Copyright (c) 2011 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7// A collection of debugging related interfaces.
8
9#ifndef NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_UTILITY_H_
10#define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_UTILITY_H_
11
12#include "native_client/src/include/nacl_macros.h"
13#include "native_client/src/include/portability.h"
14#include "native_client/src/shared/platform/nacl_threads.h"
15#include "native_client/src/shared/platform/nacl_time.h"
16#include "ppapi/c/private/pp_file_handle.h"
17#include "ppapi/c/private/ppb_nacl_private.h"
18
19#define SRPC_PLUGIN_DEBUG 1
20
21namespace plugin {
22
23// Tests that a string is a valid JavaScript identifier.  According to the
24// ECMAScript spec, this should be done in terms of unicode character
25// categories.  For now, we are simply limiting identifiers to the ASCII
26// subset of that spec.  If successful, it returns the length of the
27// identifier in the location pointed to by length (if it is not NULL).
28// TODO(sehr): add Unicode identifier support.
29bool IsValidIdentifierString(const char* strval, uint32_t* length);
30
31const PPB_NaCl_Private* GetNaClInterface();
32void SetNaClInterface(const PPB_NaCl_Private* nacl_interface);
33
34void CloseFileHandle(PP_FileHandle file_handle);
35
36// Converts a PP_FileHandle to a POSIX file descriptor.
37int32_t ConvertFileDescriptor(PP_FileHandle handle, bool read_only);
38
39// Debugging print utility
40extern int gNaClPluginDebugPrintEnabled;
41extern int NaClPluginPrintLog(const char *format, ...);
42extern int NaClPluginDebugPrintCheckEnv();
43#if SRPC_PLUGIN_DEBUG
44#define INIT_PLUGIN_LOGGING() do {                                    \
45    if (-1 == ::plugin::gNaClPluginDebugPrintEnabled) {               \
46      ::plugin::gNaClPluginDebugPrintEnabled =                        \
47          ::plugin::NaClPluginDebugPrintCheckEnv();                   \
48    }                                                                 \
49} while (0)
50
51#define PLUGIN_PRINTF(args) do {                                      \
52    INIT_PLUGIN_LOGGING();                                            \
53    if (0 != ::plugin::gNaClPluginDebugPrintEnabled) {                \
54      ::plugin::NaClPluginPrintLog("PLUGIN %" NACL_PRIu64 ": ",       \
55                                   NaClGetTimeOfDayMicroseconds());   \
56      ::plugin::NaClPluginPrintLog args;                              \
57    }                                                                 \
58  } while (0)
59
60// MODULE_PRINTF is used in the module because PLUGIN_PRINTF uses a
61// a timer that may not yet be initialized.
62#define MODULE_PRINTF(args) do {                                      \
63    INIT_PLUGIN_LOGGING();                                            \
64    if (0 != ::plugin::gNaClPluginDebugPrintEnabled) {                \
65      ::plugin::NaClPluginPrintLog("MODULE: ");                       \
66      ::plugin::NaClPluginPrintLog args;                              \
67    }                                                                 \
68  } while (0)
69#else
70#  define PLUGIN_PRINTF(args) do { if (0) { printf args; } } while (0)
71#  define MODULE_PRINTF(args) do { if (0) { printf args; } } while (0)
72/* allows DCE but compiler can still do format string checks */
73#endif
74
75}  // namespace plugin
76
77#endif  // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_UTILITY_H_
78