OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2008 The Native Client Authors. All rights reserved. | |
3 * Use of this source code is governed by a BSD-style license that can | |
4 * be found in the LICENSE file. | |
5 */ | |
6 | |
7 | |
8 // NPAPI Scriptable handle implementation. | |
9 | |
10 #include "native_client/src/trusted/plugin/srpc/scriptable_handle.h" | |
11 | |
12 #include <stdio.h> | |
13 #include <string.h> | |
14 | |
15 #include <set> | |
16 | |
17 #include "native_client/src/include/checked_cast.h" | |
18 #include "native_client/src/include/portability.h" | |
19 #include "native_client/src/trusted/plugin/srpc/browser_interface.h" | |
20 #include "native_client/src/trusted/plugin/srpc/socket_address.h" | |
21 #include "native_client/src/trusted/plugin/srpc/utility.h" | |
22 #include "native_client/src/shared/srpc/nacl_srpc.h" | |
23 | |
24 | |
25 namespace { | |
26 | |
27 // For security we keep track of the set of scriptable handles that were | |
28 // created. | |
29 | |
30 std::set<const plugin::ScriptableHandle*>* g_ValidHandles = 0; | |
31 | |
32 } // namespace | |
33 | |
34 namespace plugin { | |
35 | |
36 ScriptableHandle::ScriptableHandle(PortableHandle* handle) : handle_(handle) { | |
37 PLUGIN_PRINTF(("ScriptableHandle::ScriptableHandle(%p, %p)\n", | |
38 static_cast<void*>(this), | |
39 static_cast<void*>(handle))); | |
40 // Initialize the set. | |
41 // BUG: this is not thread safe. We may leak sets, or worse, may not | |
42 // correctly insert valid handles into the set. | |
43 // TODO(sehr): use pthread_once or similar initialization. | |
44 if (NULL == g_ValidHandles) { | |
45 g_ValidHandles = new(std::nothrow) std::set<const ScriptableHandle*>; | |
46 if (NULL == g_ValidHandles) { | |
47 return; | |
48 } | |
49 } | |
50 // Remember the scriptable handle. | |
51 g_ValidHandles->insert(this); | |
52 } | |
53 | |
54 ScriptableHandle::~ScriptableHandle() { | |
55 PLUGIN_PRINTF(("ScriptableHandle::~ScriptableHandle(%p)\n", | |
56 static_cast<void*>(this))); | |
57 // If the set was empty, just return. | |
58 if (NULL == g_ValidHandles) { | |
59 return; | |
60 } | |
61 // Remove the scriptable handle from the set of valid handles. | |
62 g_ValidHandles->erase(this); | |
63 // Free the portable handle. | |
64 handle_->Delete(); | |
65 // Avoid a possible source of ref-after-delete issues. | |
66 handle_ = NULL; | |
67 } | |
68 | |
69 // Check that an object is a validly created ScriptableHandle. | |
70 bool ScriptableHandle::is_valid(const ScriptableHandle* handle) { | |
71 PLUGIN_PRINTF(("ScriptableHandle::is_valid(%p)\n", | |
72 static_cast<void*>(const_cast<ScriptableHandle*>(handle)))); | |
73 if (NULL == g_ValidHandles) { | |
74 PLUGIN_PRINTF(("ScriptableHandle::is_valid -- no set\n")); | |
75 return false; | |
76 } | |
77 size_t count = | |
78 g_ValidHandles->count(static_cast<const ScriptableHandle*>(handle)); | |
79 PLUGIN_PRINTF(("ScriptableHandle::is_valid(%p, count %"NACL_PRIuS")\n", | |
80 static_cast<void*>(const_cast<ScriptableHandle*>(handle)), | |
81 count)); | |
82 return 0 != count; | |
83 } | |
84 | |
85 } // namespace plugin | |
OLD | NEW |