1 | /* Copyright (C) 2013 Red Hat
|
---|
2 |
|
---|
3 | This file is part of IcedTea.
|
---|
4 |
|
---|
5 | IcedTea is free software; you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation; either version 2, or (at your option)
|
---|
8 | any later version.
|
---|
9 |
|
---|
10 | IcedTea is distributed in the hope that it will be useful, but
|
---|
11 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License
|
---|
16 | along with IcedTea; see the file COPYING. If not, write to the
|
---|
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
---|
18 | 02110-1301 USA.
|
---|
19 |
|
---|
20 | Linking this library statically or dynamically with other modules is
|
---|
21 | making a combined work based on this library. Thus, the terms and
|
---|
22 | conditions of the GNU General Public License cover the whole
|
---|
23 | combination.
|
---|
24 |
|
---|
25 | As a special exception, the copyright holders of this library give you
|
---|
26 | permission to link this library with independent modules to produce an
|
---|
27 | executable, regardless of the license terms of these independent
|
---|
28 | modules, and to copy and distribute the resulting executable under
|
---|
29 | terms of your choice, provided that you also meet, for each linked
|
---|
30 | independent module, the terms and conditions of the license of that
|
---|
31 | module. An independent module is a module which is not derived from
|
---|
32 | or based on this library. If you modify this library, you may extend
|
---|
33 | this exception to your version of the library, but you are not
|
---|
34 | obligated to do so. If you do not wish to do so, delete this
|
---|
35 | exception statement from your version. */
|
---|
36 |
|
---|
37 | #include <cstdlib>
|
---|
38 |
|
---|
39 | #include <map>
|
---|
40 |
|
---|
41 | #include "IcedTeaNPPlugin.h"
|
---|
42 | #include "checked_allocations.h"
|
---|
43 | #include "browser_mock_npidentifier.h"
|
---|
44 |
|
---|
45 | struct MockedNPIdentifier_t; // foward declare
|
---|
46 | typedef std::basic_string<char, std::char_traits<char>, SafeAllocator> SafeString;
|
---|
47 | typedef std::map<int, MockedNPIdentifier_t*, std::less<int>, SafeAllocator> SafeIntToIDMap;
|
---|
48 | typedef std::map<SafeString, MockedNPIdentifier_t*, std::less<SafeString>, SafeAllocator> SafeStringToIDMap;
|
---|
49 |
|
---|
50 |
|
---|
51 | // Handles creation of NPIdentifier
|
---|
52 |
|
---|
53 | // Carefully avoids operator new so as not to interfere with leak detection.
|
---|
54 | // This mimics browser internal state.
|
---|
55 | struct MockedNPIdentifier_t {
|
---|
56 | SafeString string;
|
---|
57 | int integer;
|
---|
58 | bool is_integer; // If false, it is a string
|
---|
59 |
|
---|
60 | // Carefully avoids operator new so as not to interfere with leak detection
|
---|
61 | static MockedNPIdentifier_t* safe_allocate(SafeString str) {
|
---|
62 | MockedNPIdentifier_t* mem = (MockedNPIdentifier_t*)malloc(sizeof(MockedNPIdentifier_t));
|
---|
63 | new (&mem->string) SafeString(str);
|
---|
64 | mem->integer = -1;
|
---|
65 | mem->is_integer = false;
|
---|
66 | return mem;
|
---|
67 | }
|
---|
68 |
|
---|
69 | // Carefully avoids operator new so as not to interfere with leak detection
|
---|
70 | static MockedNPIdentifier_t* safe_allocate(int i) {
|
---|
71 | MockedNPIdentifier_t* mem = (MockedNPIdentifier_t*) malloc(
|
---|
72 | sizeof(MockedNPIdentifier_t));
|
---|
73 | new (&mem->string) SafeString();
|
---|
74 | mem->integer = i;
|
---|
75 | mem->is_integer = true;
|
---|
76 | return mem;
|
---|
77 | }
|
---|
78 | };
|
---|
79 |
|
---|
80 | // Mimics global browser data. OK if not cleared in-between tests, does not change semantics.
|
---|
81 | // Used to ensure NPIdentifiers are unique. Never freed.
|
---|
82 | static SafeIntToIDMap __np_int_identifiers;
|
---|
83 | static SafeStringToIDMap __np_string_identifiers;
|
---|
84 |
|
---|
85 | // Carefully avoids operator new so as not to interfere with leak detection
|
---|
86 | NPIdentifier browsermock_getstringidentifier(const NPUTF8* name) {
|
---|
87 | SafeString safe_copy(name);
|
---|
88 | if (__np_string_identifiers.find(safe_copy) == __np_string_identifiers.end()) {
|
---|
89 | __np_string_identifiers[safe_copy] = MockedNPIdentifier_t::safe_allocate(safe_copy);
|
---|
90 | }
|
---|
91 | return __np_string_identifiers[safe_copy];
|
---|
92 | }
|
---|
93 |
|
---|
94 | // Carefully avoids operator new so as not to interfere with leak detection
|
---|
95 | NPIdentifier browsermock_getintidentifier(int i) {
|
---|
96 | if (__np_int_identifiers.find(i) == __np_int_identifiers.end()) {
|
---|
97 | __np_int_identifiers[i] = MockedNPIdentifier_t::safe_allocate(i);
|
---|
98 | }
|
---|
99 | return __np_int_identifiers[i];
|
---|
100 | }
|
---|
101 |
|
---|
102 | bool browsermock_identifierisstring(NPIdentifier identifier) {
|
---|
103 | MockedNPIdentifier_t* contents = (MockedNPIdentifier_t*)identifier;
|
---|
104 | return !contents->is_integer;
|
---|
105 | }
|
---|
106 |
|
---|
107 | NPUTF8* browsermock_utf8fromidentifier(NPIdentifier identifier) {
|
---|
108 | MockedNPIdentifier_t* contents = (MockedNPIdentifier_t*)identifier;
|
---|
109 | if (contents->is_integer) {
|
---|
110 | return NULL;
|
---|
111 | }
|
---|
112 |
|
---|
113 | // We expect this string to be freed with 'memfree'
|
---|
114 | NPUTF8* copy = (NPUTF8*) browser_functions.memalloc(contents->string.size() + 1);
|
---|
115 | memcpy(copy, contents->string.c_str(), contents->string.size() + 1);
|
---|
116 | return copy;
|
---|
117 | }
|
---|