KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > shell > ie > COMObjectProxy


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.shell.ie;
17
18 import com.google.gwt.dev.shell.LowLevel;
19
20 import org.eclipse.swt.internal.ole.win32.COMObject;
21
22 import java.util.Map JavaDoc;
23
24 /**
25  * A proxy object that allows you to override behavior in an existing COM
26  * object. This is used primarily for fixing up the
27  * {@link org.eclipse.swt.browser.Browser} object's 'window.external' handling.
28  */

29 class COMObjectProxy extends COMObject {
30
31   private static final int MAX_METHODS_WRAPPED = 23;
32
33   private COMObject target;
34
35   /**
36    * Construct a proxy object.
37    *
38    * @param argCounts must be the same array of argCounts used to contruct the
39    * wrapped object.
40    */

41   public COMObjectProxy(int[] argCounts) {
42     // Construct myself as a COMObject, even though my vtbl will never
43
// actually be used, I need to castable to a COMObject when I injected
44
// myself into the ObjectMap.
45
super(argCounts);
46
47     // Because I will never be called through my vtbl, I can free my OS
48
// memory created in the superclass ctor and remove myself from the
49
// ObjectMap. If this didn't work, we'd be leaking memory when
50
// the last release is called (unless we assume that method index 2 was
51
// Release(), which actually is likely a safe assumption.)
52
dispose();
53
54     // Make sure the interface isn't too big.
55
if (argCounts != null && argCounts.length >= MAX_METHODS_WRAPPED) {
56       throw new IllegalArgumentException JavaDoc("No more than " + MAX_METHODS_WRAPPED
57           + " methods can be wrapped right now.");
58     }
59   }
60
61   /**
62    * Interpose this object in front of an existing object.
63    */

64   public void interpose(COMObject victim) {
65     if (this.target != null) {
66       throw new IllegalStateException JavaDoc("interpose() can only be called once");
67     }
68
69     // Hang onto the object we're wrapping so that we can delegate later.
70
this.target = victim;
71
72     // Get the COMObject ObjectMap so that we can hijack the target's slot.
73
Map JavaDoc objectMap = (Map JavaDoc) LowLevel.snatchFieldObjectValue(COMObject.class,
74         null, "ObjectMap");
75     Integer JavaDoc ppVtableTarget = new Integer JavaDoc(target.getAddress());
76
77     // First, make sure that the target is still actually in the map.
78
// If it isn't still in there, then the caller is using me incorrectly.
79
Object JavaDoc currValue = objectMap.get(ppVtableTarget);
80     if (currValue != target) {
81       throw new IllegalStateException JavaDoc("target object is not currently mapped");
82     }
83
84     // Replace target's entry in COMObject's (vtbl -> instance) map with
85
// a reference to this object instead. Calls still come in on the
86
// target's vtbl, but COMObject will route them to me instead,
87
// so that I can hook/delegate them.
88
objectMap.put(ppVtableTarget, this);
89   }
90
91   public int method0(int[] args) {
92     return target.method0(args);
93   }
94
95   public int method1(int[] args) {
96     return target.method1(args);
97   }
98
99   public int method10(int[] args) {
100     return target.method10(args);
101   }
102
103   public int method11(int[] args) {
104     return target.method11(args);
105   }
106
107   public int method12(int[] args) {
108     return target.method12(args);
109   }
110
111   public int method13(int[] args) {
112     return target.method13(args);
113   }
114
115   public int method14(int[] args) {
116     return target.method14(args);
117   }
118
119   public int method15(int[] args) {
120     return target.method15(args);
121   }
122
123   public int method16(int[] args) {
124     return target.method16(args);
125   }
126
127   public int method17(int[] args) {
128     return target.method17(args);
129   }
130
131   public int method18(int[] args) {
132     return target.method18(args);
133   }
134
135   public int method19(int[] args) {
136     return target.method19(args);
137   }
138
139   public int method2(int[] args) {
140     return target.method2(args);
141   }
142
143   public int method20(int[] args) {
144     return target.method20(args);
145   }
146
147   public int method21(int[] args) {
148     return target.method21(args);
149   }
150
151   public int method22(int[] args) {
152     return target.method22(args);
153   }
154 }
Popular Tags