jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 1 | #ifndef H_LocalObjectTable |
| 2 | #define H_LocalObjectTable |
| 3 | /* |
| 4 | * Copyright 2008 Google Inc. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 7 | * use this file except in compliance with the License. You may obtain a copy of |
| 8 | * the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | * License for the specific language governing permissions and limitations under |
| 16 | * the License. |
| 17 | */ |
| 18 | |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 19 | #include <map> |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 20 | |
| 21 | #include "Debug.h" |
| 22 | |
| 23 | #include "mozincludes.h" |
codefu@google.com | f722fe2 | 2011-07-11 13:15:51 +0000 | [diff] [blame] | 24 | #include "NPVariantUtil.h" |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 25 | |
| 26 | class LocalObjectTable { |
| 27 | private: |
codefu@google.com | f722fe2 | 2011-07-11 13:15:51 +0000 | [diff] [blame] | 28 | /* The host expects Value objects to have int's for JSO id's, hence the |
| 29 | * dual mapping. ObjectMap is for host communication (Value.getJsObjectId) |
| 30 | * and the IdMap is for browser communication (NPObject to ID). |
| 31 | */ |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 32 | typedef std::map<int, NPObject*> ObjectMap; |
codefu@google.com | f722fe2 | 2011-07-11 13:15:51 +0000 | [diff] [blame] | 33 | typedef std::map<NPObject*,int> IdMap; |
| 34 | |
| 35 | NPP npp; |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 36 | |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 37 | int nextId; |
| 38 | ObjectMap objects; |
codefu@google.com | f722fe2 | 2011-07-11 13:15:51 +0000 | [diff] [blame] | 39 | IdMap ids; |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 40 | bool dontFree; |
| 41 | |
codefu@google.com | f722fe2 | 2011-07-11 13:15:51 +0000 | [diff] [blame] | 42 | bool jsIdentitySafe; |
| 43 | |
| 44 | const NPIdentifier gwtId; |
| 45 | |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 46 | void setFree(int id) { |
codefu@google.com | f722fe2 | 2011-07-11 13:15:51 +0000 | [diff] [blame] | 47 | NPObject *obj = getById(id); |
| 48 | if(!obj) { |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 49 | Debug::log(Debug::Error) << "setFree(id=" << id << "): object not in table" |
| 50 | << Debug::flush; |
codefu@google.com | f722fe2 | 2011-07-11 13:15:51 +0000 | [diff] [blame] | 51 | return; |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 52 | } |
codefu@google.com | f722fe2 | 2011-07-11 13:15:51 +0000 | [diff] [blame] | 53 | ids.erase(obj); |
| 54 | objects.erase(id); |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 55 | } |
jat@google.com | 97dcbe0 | 2009-09-08 23:09:51 +0000 | [diff] [blame] | 56 | |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 57 | public: |
codefu@google.com | f722fe2 | 2011-07-11 13:15:51 +0000 | [diff] [blame] | 58 | LocalObjectTable(NPP npp, bool jsIdentitySafe): |
| 59 | nextId(0), dontFree(false), jsIdentitySafe(jsIdentitySafe), |
| 60 | gwtId(NPN_GetStringIdentifier("__gwt_ObjectId")) { |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | virtual ~LocalObjectTable(); |
| 64 | |
jat@google.com | f78175f | 2009-09-09 21:57:53 +0000 | [diff] [blame] | 65 | /** |
| 66 | * Add a new object, which must not be in the table, and return a new id for it. |
| 67 | */ |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 68 | int add(NPObject* obj) { |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 69 | int id = nextId++; |
| 70 | set(id, obj); |
codefu@google.com | f722fe2 | 2011-07-11 13:15:51 +0000 | [diff] [blame] | 71 | |
| 72 | if (!jsIdentitySafe) { |
| 73 | NPVariant idVariant; |
| 74 | Debug::log(Debug::Debugging) << "LocalObjectTable::set(): setting expando(" |
| 75 | << id << ")" << Debug::flush; |
| 76 | INT32_TO_NPVARIANT(id,idVariant); |
| 77 | if (!NPN_SetProperty(npp, obj, gwtId, &idVariant)) { |
| 78 | Debug::log(Debug::Error) << "Setting GWT id on object failed" << Debug::flush; |
| 79 | } |
| 80 | } |
| 81 | |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 82 | return id; |
| 83 | } |
| 84 | |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 85 | void set(int id, NPObject* obj) { |
codefu@google.com | f722fe2 | 2011-07-11 13:15:51 +0000 | [diff] [blame] | 86 | Debug::log(Debug::Debugging) << "LocalObjectTable::set(id=" << id << ",obj=" << (void*)obj |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 87 | << ")" << Debug::flush; |
codefu@google.com | f722fe2 | 2011-07-11 13:15:51 +0000 | [diff] [blame] | 88 | if (!jsIdentitySafe) { |
| 89 | ObjectMap::iterator it; |
| 90 | it = objects.find(id); |
| 91 | if( it != objects.end() ) { |
| 92 | if (it->second != obj) { |
| 93 | //The JSO has changed and we need to update the map, releasing |
| 94 | //the old and remembering the new object. |
| 95 | ids.erase(it->second); |
| 96 | NPN_ReleaseObject(it->second); |
| 97 | NPN_RetainObject(obj); |
| 98 | } else { |
| 99 | //do nothing; object exists and is already mapped |
| 100 | return; |
| 101 | } |
| 102 | } else { |
| 103 | //New insertion, retain the object in the table |
| 104 | NPN_RetainObject(obj); |
| 105 | } |
| 106 | } else { |
| 107 | //Not dealing with identity hack, retain |
| 108 | NPN_RetainObject(obj); |
| 109 | } |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 110 | objects[id] = obj; |
codefu@google.com | f722fe2 | 2011-07-11 13:15:51 +0000 | [diff] [blame] | 111 | ids[obj] = id; |
| 112 | |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 113 | // keep track that we hold a reference in the table |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 114 | } |
| 115 | |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 116 | void free(int id) { |
codefu@google.com | f722fe2 | 2011-07-11 13:15:51 +0000 | [diff] [blame] | 117 | Debug::log(Debug::Debugging) << "LocalObjectTable::free(id=" << id << ")" << Debug::flush; |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 118 | ObjectMap::iterator it = objects.find(id); |
| 119 | if (it == objects.end()) { |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 120 | Debug::log(Debug::Error) << "Freeing freed object slot " << id << Debug::flush; |
| 121 | return; |
| 122 | } |
codefu@google.com | f722fe2 | 2011-07-11 13:15:51 +0000 | [diff] [blame] | 123 | if (!jsIdentitySafe) { |
| 124 | Debug::log(Debug::Debugging) << "removing expando!" << Debug::flush; |
| 125 | NPN_RemoveProperty(npp, it->second, gwtId); |
| 126 | } |
| 127 | setFree(id); |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 128 | if (!dontFree) { |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 129 | NPObject* obj = it->second; |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 130 | NPN_ReleaseObject(obj); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void freeAll() { |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 135 | Debug::log(Debug::Info) << "LocalObjectTable::freeAll()" << Debug::flush; |
| 136 | for (ObjectMap::const_iterator it = objects.begin(); it != objects.end(); ++it) { |
| 137 | NPObject* obj = it->second; |
| 138 | if (!dontFree) { |
| 139 | NPN_ReleaseObject(obj); |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 142 | objects.clear(); |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 143 | } |
| 144 | |
codefu@google.com | f722fe2 | 2011-07-11 13:15:51 +0000 | [diff] [blame] | 145 | NPObject* getById(int id) { |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 146 | ObjectMap::iterator it = objects.find(id); |
| 147 | if (it == objects.end()) { |
| 148 | Debug::log(Debug::Error) << "LocalObjectTable::get(id=" << id |
| 149 | << "): no object found" << Debug::flush; |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 150 | } |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 151 | return it->second; |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 152 | } |
| 153 | |
codefu@google.com | f722fe2 | 2011-07-11 13:15:51 +0000 | [diff] [blame] | 154 | int getObjectId(NPObject* jsObject) { |
| 155 | int id = -1; |
| 156 | if(!jsIdentitySafe) { |
| 157 | NPVariant idVariant; |
| 158 | VOID_TO_NPVARIANT(idVariant); |
| 159 | Debug::log(Debug::Debugging) << "LocalObjectTable::get(): expando test" |
| 160 | << Debug::flush; |
| 161 | if (NPN_GetProperty(npp, jsObject, gwtId, &idVariant) && |
| 162 | NPVariantUtil::isInt(idVariant)) { |
| 163 | id = NPVariantUtil::getAsInt(idVariant); |
| 164 | Debug::log(Debug::Debugging) << "LocalObjectTable::get(): expando: " |
| 165 | << id << Debug::flush; |
| 166 | set(id, jsObject); |
| 167 | } |
| 168 | NPN_ReleaseVariantValue(&idVariant); |
| 169 | } else { |
| 170 | IdMap::iterator it = ids.find(jsObject); |
| 171 | if (it != ids.end()) { |
| 172 | id = it->second; |
| 173 | } |
| 174 | } |
| 175 | return id; |
| 176 | } |
| 177 | |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 178 | void setDontFree(bool dontFree) { |
| 179 | this->dontFree = dontFree; |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | #endif |