Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: native_client_sdk/src/examples/api/messaging/messaging.cc

Issue 781623002: [NaCl SDK] Messaging example (demonstrates postMessageAndAwaitResponse) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
dmichael (off chromium) 2014/12/03 23:33:33 nit: Prefer no (c) in copyright headers (here and
binji 2014/12/03 23:56:50 Done.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ppapi/cpp/instance.h"
6 #include "ppapi/cpp/instance_handle.h"
7 #include "ppapi/cpp/message_handler.h"
8 #include "ppapi/cpp/module.h"
9 #include "ppapi/cpp/var.h"
10 #include "ppapi/cpp/var_array.h"
11 #include "ppapi/utility/threading/simple_thread.h"
12
13 #ifdef WIN32
14 #undef PostMessage
15 // Allow 'this' in initializer list
16 #pragma warning(disable : 4355)
17 #endif
18
19 class MessageHandler : public pp::MessageHandler {
20 public:
21 virtual void HandleMessage(pp::InstanceHandle instance,
22 const pp::Var& message_data) {
23 if (!message_data.is_array()) {
24 return;
25 }
26
27 pp::VarArray array(message_data);
28 pp::Var response = SumArray(array);
29
30 // Send the response back to JavaScript asynchronously.
31 pp::Instance(instance.pp_instance()).PostMessage(response);
32 }
33
34 virtual pp::Var HandleBlockingMessage(pp::InstanceHandle instance,
35 const pp::Var& message_data) {
36 if (!message_data.is_array()) {
37 // Return an undefined value.
38 return pp::Var();
39 }
40
41 pp::VarArray array(message_data);
42
43 // Send the response back to JavaScript synchronously.
44 return SumArray(array);
45 }
46
47 virtual void WasUnregistered(pp::InstanceHandle instance) {}
48
49 pp::Var SumArray(const pp::VarArray& array) {
50 int32_t result = 0;
51 for (uint32_t i = 0, length = array.GetLength(); i < length; ++i) {
52 if (!array.Get(i).is_int()) {
53 continue;
54 }
55
56 result += array.Get(i).AsInt();
57 }
58
59 return pp::Var(result);
60 }
61 };
62
63 class Instance : public pp::Instance {
64 public:
65 explicit Instance(PP_Instance instance)
66 : pp::Instance(instance), thread_(this) {}
67
68 virtual bool Init(uint32_t /*argc*/,
69 const char* /*argn*/ [],
70 const char* /*argv*/ []) {
71 thread_.Start();
72
73 // The message handler must be registered using a message loop that is not
74 // the main thread's messaging loop. This call will fail otherwise.
75 RegisterMessageHandler(&message_handler_, thread_.message_loop());
76 return true;
77 }
78
79 private:
80 pp::SimpleThread thread_;
81 MessageHandler message_handler_;
82 };
83
84 class Module : public pp::Module {
85 public:
86 Module() : pp::Module() {}
87 virtual ~Module() {}
88
89 virtual pp::Instance* CreateInstance(PP_Instance instance) {
90 return new Instance(instance);
91 }
92 };
93
94 namespace pp {
95 Module* CreateModule() { return new ::Module(); }
96 } // namespace pp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698