OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2011 The Native Client 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 #include "native_client/tests/fake_browser_ppapi/fake_host.h" | |
8 | |
9 #if !NACL_WINDOWS | |
10 # include <dlfcn.h> | |
11 #endif // !NACL_WINDOWS | |
12 | |
13 #include <string.h> | |
14 #include "native_client/src/include/portability.h" | |
15 #include "native_client/src/shared/platform/nacl_check.h" | |
16 | |
17 namespace { | |
18 | |
19 #if NACL_WINDOWS | |
20 | |
21 #define RTLD_NOW 0 | |
22 #define RTLD_LOCAL 0 | |
23 | |
24 void* dlopen(const char* filename, int flag) { | |
25 return reinterpret_cast<void*>(LoadLibrary(filename)); | |
26 } | |
27 | |
28 void* dlsym(void *handle, const char* symbol_name) { | |
29 return GetProcAddress(reinterpret_cast<HMODULE>(handle), symbol_name); | |
30 } | |
31 | |
32 int dlclose(void* handle) { | |
33 return !FreeLibrary(reinterpret_cast<HMODULE>(handle)); | |
34 } | |
35 | |
36 #endif | |
37 | |
38 } // namespace | |
39 | |
40 | |
41 namespace fake_browser_ppapi { | |
42 | |
43 Host::Host(const char* plugin_file) | |
44 : var_deprecated_interface_(NULL), | |
45 last_resource_id_(0), | |
46 last_instance_id_(0) { | |
47 dl_handle_ = dlopen(plugin_file, RTLD_NOW | RTLD_LOCAL); | |
48 CHECK(dl_handle_ != NULL); | |
49 initialize_module_ = | |
50 reinterpret_cast<InitializeModuleFunc>( | |
51 reinterpret_cast<uintptr_t>(dlsym(dl_handle_, "PPP_InitializeModule"))); | |
52 CHECK(initialize_module_ != NULL); | |
53 shutdown_module_ = | |
54 reinterpret_cast<ShutdownModuleFunc>( | |
55 reinterpret_cast<uintptr_t>(dlsym(dl_handle_, "PPP_ShutdownModule"))); | |
56 CHECK(shutdown_module_ != NULL); | |
57 get_interface_ = | |
58 reinterpret_cast<GetInterfaceFunc>( | |
59 reinterpret_cast<uintptr_t>(dlsym(dl_handle_, "PPP_GetInterface"))); | |
60 CHECK(get_interface_ != NULL); | |
61 } | |
62 | |
63 Host::~Host() { | |
64 int rc = dlclose(dl_handle_); | |
65 CHECK(rc == 0); | |
66 | |
67 ResourceMap::iterator ri; | |
68 while ((ri = resource_map_.begin()) != resource_map_.end()) { | |
69 delete(ri->second); | |
70 resource_map_.erase(ri); | |
71 } | |
72 | |
73 InstanceMap::iterator ii; | |
74 while ((ii = instance_map_.begin()) != instance_map_.end()) { | |
75 delete(ii->second); | |
76 instance_map_.erase(ii); | |
77 } | |
78 } | |
79 | |
80 int32_t Host::InitializeModule(PP_Module module, | |
81 PPB_GetInterface get_intf) { | |
82 return (*initialize_module_)(module, get_intf); | |
83 } | |
84 | |
85 void Host::ShutdownModule() { | |
86 return (*shutdown_module_)(); | |
87 } | |
88 | |
89 const void* Host::GetInterface(const char* interface_name) { | |
90 return (*get_interface_)(interface_name); | |
91 } | |
92 | |
93 PP_Resource Host::TrackResource(Resource* resource) { | |
94 PP_Resource resource_id = ++last_resource_id_; | |
95 resource_map_[resource_id] = resource; | |
96 resource->set_resource_id(resource_id); | |
97 return resource_id; | |
98 } | |
99 | |
100 Resource* Host::GetResource(PP_Resource resource_id) { | |
101 ResourceMap::iterator iter = resource_map_.find(resource_id); | |
102 if (iter == resource_map_.end()) | |
103 return Resource::Invalid(); | |
104 return iter->second; | |
105 } | |
106 | |
107 PP_Instance Host::TrackInstance(Instance* instance) { | |
108 PP_Instance instance_id = ++last_instance_id_; | |
109 instance_map_[instance_id] = instance; | |
110 instance->set_instance_id(instance_id); | |
111 return instance_id; | |
112 } | |
113 | |
114 Instance* Host::GetInstance(PP_Instance instance_id) { | |
115 InstanceMap::iterator iter = instance_map_.find(instance_id); | |
116 if (iter == instance_map_.end()) | |
117 return Instance::Invalid(); | |
118 return iter->second; | |
119 } | |
120 | |
121 } // namespace fake_browser_ppapi | |
OLD | NEW |