1// Copyright (c) 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 PPAPI_SIMPLE_PS_INSTANCE_H_
6#define PPAPI_SIMPLE_PS_INSTANCE_H_
7
8#include <stdarg.h>
9
10#include "ppapi/c/pp_instance.h"
11#include "ppapi/c/pp_stdint.h"
12#include "ppapi/c/ppb_core.h"
13#include "ppapi/c/ppb_var.h"
14#include "ppapi/c/ppb_view.h"
15
16#include "ppapi/cpp/fullscreen.h"
17#include "ppapi/cpp/graphics_3d_client.h"
18#include "ppapi/cpp/instance.h"
19#include "ppapi/cpp/message_loop.h"
20#include "ppapi/cpp/mouse_lock.h"
21
22#include "ppapi/utility/completion_callback_factory.h"
23
24#include "ppapi_simple/ps_event.h"
25#include "ppapi_simple/ps_main.h"
26
27#include "sdk_util/thread_safe_queue.h"
28
29
30// The basic instance class which also inherits the MouseLock and
31// Graphics3DClient interfaces.
32class PSInstance : public pp::Instance, pp::MouseLock, pp::Graphics3DClient {
33 public:
34  // Verbosity levels, ecplicitly numbered since we pass these
35  // in from html attributes as numberic values.
36  enum Verbosity {
37    PSV_SILENT = 0,
38    PSV_ERROR = 1,
39    PSV_WARN = 2,
40    PSV_LOG = 3,
41    PSV_TRACE = 4,
42  };
43
44  // Returns a pointer to the global instance
45  static PSInstance* GetInstance();
46
47  PSInstance(PP_Instance inst);
48  virtual ~PSInstance();
49
50  // Set a function which will be called on a new thread once initialized.
51  // NOTE:  This must be called by the Factory, once Init is called, this
52  // function will have no effect.
53  void SetMain(PSMainFunc_t func);
54
55  // Started on Init, a thread which can be safely blocked.
56  virtual int MainThread(int argc, char* argv[]);
57
58  // Logging Functions
59  void SetVerbosity(Verbosity verbosity);
60  void Trace(const char *fmt, ...);
61  void Log(const char *fmt, ...);
62  void Warn(const char *fmt, ...);
63  void Error(const char *fmt, ...);
64
65  // Event Functions
66  void SetEnabledEvents(uint32_t mask);
67  void PostEvent(PSEventType type);
68  void PostEvent(PSEventType type, PP_Bool bool_value);
69  void PostEvent(PSEventType type, PP_Resource resource);
70  void PostEvent(PSEventType type, const PP_Var& var);
71
72  PSEvent* TryAcquireEvent();
73  PSEvent* WaitAcquireEvent();
74  void ReleaseEvent(PSEvent* event);
75
76 protected:
77  // Callback functions triggered by Pepper
78  //
79  // These functions are called on the main pepper thread, so they must
80  // not block.
81  //
82  // Called by the browser when the NaCl module is loaded and all ready to go.
83  // This function will create a new thread which will run the pseudo main.
84  virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]);
85
86  // Output log message to stderr if the current verbosity is set
87  // at or above the given verbosity.
88  void VALog(Verbosity verbosity, const char *fmt, va_list args);
89
90  // Called whenever the in-browser window changes size, it will pass a
91  // context change request to whichever thread is handling rendering.
92  virtual void DidChangeView(const pp::View& view);
93
94  // Called by the browser when the NaCl canvas gets or loses focus.
95  virtual void DidChangeFocus(bool has_focus);
96
97  // Called by the browser to handle the postMessage() call in Javascript.
98  virtual void HandleMessage(const pp::Var& message);
99
100  // Called by the browser to handle incoming input events.  Events are Q'd
101  // and can later be processed on a sperate processing thread.
102  virtual bool HandleInputEvent(const pp::InputEvent& event);
103
104  // Called by the browser when the 3D context is lost.
105  virtual void Graphics3DContextLost();
106
107  // Called by the browser when the mouselock is lost.
108  virtual void MouseLockLost();
109
110  // Called by Init to processes default and embed tag arguments prior to
111  // launching the 'ppapi_main' thread.
112  virtual bool ProcessProperties();
113
114 private:
115  static void* MainThreadThunk(void *start_info);
116
117 protected:
118  pp::MessageLoop* main_loop_;
119
120  sdk_util::ThreadSafeQueue<PSEvent> event_queue_;
121  uint32_t events_enabled_;
122  Verbosity verbosity_;
123  int fd_tty_;
124
125  PSMainFunc_t main_cb_;
126
127  const PPB_Core* ppb_core_;
128  const PPB_Var* ppb_var_;
129  const PPB_View* ppb_view_;
130};
131
132#endif  // PPAPI_MAIN_PS_INSTANCE_H_
133