OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Native Client 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 NATIVE_CLIENT_SRC_SHARED_PPAPI_PROXY_PROXY_VAR_CACHE_H_ |
| 6 #define NATIVE_CLIENT_SRC_SHARED_PPAPI_PROXY_PROXY_VAR_CACHE_H_ |
| 7 |
| 8 #include <map> |
| 9 |
| 10 #include "native_client/src/include/nacl_memory.h" |
| 11 #include "native_client/src/shared/ppapi_proxy/proxy_var.h" |
| 12 #include "ppapi/c/pp_var.h" |
| 13 |
| 14 namespace ppapi_proxy { |
| 15 |
| 16 // This class manages the proxy-local cache of ProxyVars. The base factory |
| 17 // method generates a unique id that gets used as the PP_Var's id, this id is |
| 18 // associated with the variant's content. The factory also inserts the new |
| 19 // instance into the proxy-local cache. |
| 20 // Note: There is one proxy var cache per NaCl module instance. |
| 21 class ProxyVarCache { |
| 22 public: |
| 23 // Return the global proxy var cache. Always returns a valid (possibly |
| 24 // empty) cache. |
| 25 static ProxyVarCache& GetInstance(); |
| 26 |
| 27 // If |proxy_var| already exists in the cache, then increment its reference |
| 28 // count. Otherwise, add it to the cache. This method does not check the |
| 29 // validity of |proxy_var|, assuming that it's not possible to make a |
| 30 // SharedProxyVar unless you use one of the cacheable subclasses. |
| 31 // TODO(dspringer): Should the contents of the proxy_var in the cache be |
| 32 // replaced with |proxy_var|? Should there be some kind of assert if an |
| 33 // object with the same id exists in the cache but the contents are different? |
| 34 void RetainSharedProxyVar(const SharedProxyVar& proxy_var); |
| 35 |
| 36 // Find the proxy var associated with |id| and increment its ref count. Does |
| 37 // nothing if no such object exists. This only operates on vars that are |
| 38 // cached (that is, IsCachedType() returns |true|). Any other var type is |
| 39 // not cached, and this function does nothing. |
| 40 void RetainProxyVar(const PP_Var& var); |
| 41 |
| 42 // Release the cached object associated with |id|. If the reference count |
| 43 // of the object falls to 0, it gets removed from the cache. This only |
| 44 // operates on vars that are cached (that is, IsCachedType() returns |true|). |
| 45 // Any other var type is ignored, and this function does nothing. |
| 46 void ReleaseProxyVar(const PP_Var& var); |
| 47 |
| 48 // Find the object in the cache associated with |pp_var|. |
| 49 SharedProxyVar SharedProxyVarForVar(PP_Var pp_var) const; |
| 50 |
| 51 private: |
| 52 // Return whether or not a var type is cached. |
| 53 // Note to implementers: be sure to add to this function when adding new |
| 54 // cached types. |
| 55 // TODO(dspringer): When all the complex var types are handled, this |
| 56 // test can turn into something like var.type >= PP_VARTYPE_STRING. |
| 57 bool IsCachedType(const PP_Var& var) { |
| 58 return var.type == PP_VARTYPE_STRING || var.type == PP_VARTYPE_OBJECT; |
| 59 } |
| 60 |
| 61 // The cache of these objects. The value is a shared pointer so that |
| 62 // instances of subclasses can be inserted. |
| 63 typedef std::map<int64_t, SharedProxyVar> ProxyVarDictionary; |
| 64 ProxyVarDictionary proxy_var_cache_; |
| 65 |
| 66 static ProxyVarCache* cache_singleton; |
| 67 |
| 68 ProxyVarCache() {} |
| 69 }; |
| 70 |
| 71 } // namespace ppapi_proxy |
| 72 |
| 73 #endif // NATIVE_CLIENT_SRC_SHARED_PPAPI_PROXY_PROXY_VAR_CACHE_H_ |
OLD | NEW |