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

Side by Side Diff: native_client_sdk/src/examples/tutorial/dlopen/dlopen.cc

Issue 14607005: [NaCl SDK] Cleanup examples. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /// @file 5 /// @file
noelallen1 2013/05/07 19:07:29 @file? Didn't you get rid of these?
binji 2013/05/07 20:37:35 Done.
6 /// This example demonstrates building a dynamic library which is loaded by the 6 /// This example demonstrates building a dynamic library which is loaded by the
7 /// NaCl module. To load the NaCl module, the browser first looks for the 7 /// NaCl module. To load the NaCl module, the browser first looks for the
8 /// CreateModule() factory method (at the end of this file). It calls 8 /// CreateModule() factory method (at the end of this file). It calls
9 /// CreateModule() once to load the module code from your .nexe. After the 9 /// CreateModule() once to load the module code from your .nexe. After the
10 /// .nexe code is loaded, CreateModule() is not called again. 10 /// .nexe code is loaded, CreateModule() is not called again.
11 /// 11 ///
12 /// Once the .nexe code is loaded, the browser then calls the CreateInstance() 12 /// Once the .nexe code is loaded, the browser then calls the CreateInstance()
13 /// method on the object returned by CreateModule(). If the CreateInstance 13 /// method on the object returned by CreateModule(). If the CreateInstance
14 /// returns successfully, then Init function is called, which will load the 14 /// returns successfully, then Init function is called, which will load the
15 /// shared object on a worker thread. We use a worker because dlopen is 15 /// shared object on a worker thread. We use a worker because dlopen is
(...skipping 17 matching lines...) Expand all
33 #if defined(NACL_SDK_DEBUG) 33 #if defined(NACL_SDK_DEBUG)
34 #define CONFIG_NAME "Debug" 34 #define CONFIG_NAME "Debug"
35 #else 35 #else
36 #define CONFIG_NAME "Release" 36 #define CONFIG_NAME "Release"
37 #endif 37 #endif
38 38
39 #define XSTRINGIFY(x) STRINGIFY(x) 39 #define XSTRINGIFY(x) STRINGIFY(x)
40 #define STRINGIFY(x) #x 40 #define STRINGIFY(x) #x
41 #define NACL_ARCH_STRING XSTRINGIFY(NACL_ARCH) 41 #define NACL_ARCH_STRING XSTRINGIFY(NACL_ARCH)
42 42
43 class DlopenInstance : public pp::Instance { 43 class DlOpenInstance : public pp::Instance {
44 public: 44 public:
45 explicit DlopenInstance(PP_Instance instance) 45 explicit DlOpenInstance(PP_Instance instance)
46 : pp::Instance(instance), 46 : pp::Instance(instance),
47 eightball_so_(NULL), 47 eightball_so_(NULL),
48 reverse_so_(NULL), 48 reverse_so_(NULL),
49 eightball_(NULL), 49 eightball_(NULL),
50 reverse_(NULL), 50 reverse_(NULL),
51 tid_(NULL) {} 51 tid_(NULL) {}
52 52
53 virtual ~DlopenInstance() {} 53 virtual ~DlOpenInstance() {}
54 ;
55 54
56 // Helper function to post a message back to the JS and stdout functions. 55 // Helper function to post a message back to the JS and stdout functions.
57 void logmsg(const char* pStr) { 56 void logmsg(const char* pStr) {
58 PostMessage(pp::Var(std::string("log:") + pStr)); 57 PostMessage(pp::Var(std::string("log:") + pStr));
59 fprintf(stdout, pStr); 58 fprintf(stdout, pStr);
60 } 59 }
61 60
62 // Initialize the module, staring a worker thread to load the shared object. 61 // Initialize the module, staring a worker thread to load the shared object.
63 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { 62 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
64 nacl_io_init_ppapi(pp_instance(), 63 nacl_io_init_ppapi(pp_instance(),
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 159
161 logmsg(message.c_str()); 160 logmsg(message.c_str());
162 } else { 161 } else {
163 std::string errormsg = "Unexpected message: "; 162 std::string errormsg = "Unexpected message: ";
164 errormsg += message + "\n"; 163 errormsg += message + "\n";
165 logmsg(errormsg.c_str()); 164 logmsg(errormsg.c_str());
166 } 165 }
167 } 166 }
168 167
169 static void* LoadLibrariesOnWorker(void* pInst) { 168 static void* LoadLibrariesOnWorker(void* pInst) {
170 DlopenInstance* inst = static_cast<DlopenInstance*>(pInst); 169 DlOpenInstance* inst = static_cast<DlOpenInstance*>(pInst);
171 inst->LoadLibrary(); 170 inst->LoadLibrary();
172 return NULL; 171 return NULL;
173 } 172 }
174 173
175 static void LoadDoneCB(void* pInst, int32_t result) { 174 static void LoadDoneCB(void* pInst, int32_t result) {
176 DlopenInstance* inst = static_cast<DlopenInstance*>(pInst); 175 DlOpenInstance* inst = static_cast<DlOpenInstance*>(pInst);
177 inst->UseLibrary(); 176 inst->UseLibrary();
178 } 177 }
179 178
180 private: 179 private:
181 void* eightball_so_; 180 void* eightball_so_;
182 void* reverse_so_; 181 void* reverse_so_;
183 TYPE_eightball eightball_; 182 TYPE_eightball eightball_;
184 TYPE_reverse reverse_; 183 TYPE_reverse reverse_;
185 pthread_t tid_; 184 pthread_t tid_;
186 }; 185 };
187 186
188 // The Module class. The browser calls the CreateInstance() method to create 187 class DlOpenModule : public pp::Module {
189 // an instance of your NaCl module on the web page. The browser creates a new
190 // instance for each <embed> tag with type="application/x-nacl".
191 class dlOpenModule : public pp::Module {
192 public: 188 public:
193 dlOpenModule() : pp::Module() {} 189 DlOpenModule() : pp::Module() {}
194 virtual ~dlOpenModule() {} 190 virtual ~DlOpenModule() {}
195 191
196 // Create and return a DlopenInstance object. 192 // Create and return a DlOpenInstance object.
197 virtual pp::Instance* CreateInstance(PP_Instance instance) { 193 virtual pp::Instance* CreateInstance(PP_Instance instance) {
198 return new DlopenInstance(instance); 194 return new DlOpenInstance(instance);
199 } 195 }
200 }; 196 };
201 197
202 // Factory function called by the browser when the module is first loaded.
203 // The browser keeps a singleton of this module. It calls the
204 // CreateInstance() method on the object you return to make instances. There
205 // is one instance per <embed> tag on the page. This is the main binding
206 // point for your NaCl module with the browser.
207 namespace pp { 198 namespace pp {
208 Module* CreateModule() { return new dlOpenModule(); } 199 Module* CreateModule() { return new DlOpenModule(); }
209 } // namespace pp 200 } // namespace pp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698