root/lang/pascal/GeckoComponents/CallbackInterfaces.pas

Revision 4249, 5.5 kB (checked in by plus7, 12 months ago)

initial import

  • Property svn:executable set to *
Line 
1(* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is GeckoComponents for Delphi.
15 *
16 * The Initial Developer of the Original Code is Takanori Ito.
17 * Portions created by the Initial Developer are Copyright (C) 2003
18 * the Initial Developer. All Rights Reserved.
19 *
20 * Contributor(s):
21 *
22 * Alternatively, the contents of this file may be used under the terms of
23 * either the GNU General Public License Version 2 or later (the "GPL"), or
24 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25 * in which case the provisions of the GPL or the LGPL are applicable instead
26 * of those above. If you wish to allow use of your version of this file only
27 * under the terms of either the GPL or the LGPL, and not to allow others to
28 * use your version of this file under the terms of the MPL, indicate your
29 * decision by deleting the provisions above and replace them with the notice
30 * and other provisions required by the GPL or the LGPL. If you do not delete
31 * the provisions above, a recipient may use your version of this file under
32 * the terms of any one of the MPL, the GPL or the LGPL.
33 *
34 * ***** END LICENSE BLOCK ***** *)
35unit CallbackInterfaces;
36
37interface
38
39uses
40  nsXPCOM, nsTypes;
41
42type
43  IGeckoCreateWindowTarget = interface
44  ['{934D4785-B78A-43D0-ABAD-1CC71AC06E24}']
45    function GetWebBrowserChrome: nsIWebBrowserChrome;
46    function DoCreateChromeWindow(chromeFlags: Longword): nsIWebBrowserChrome;
47  end;
48
49procedure AddCreateWindowTarget(target: IGeckoCreateWindowTarget);
50procedure RemoveCreateWindowTarget(target: IGeckoCreateWindowTarget);
51function InitWindowCreator: Boolean;
52
53implementation
54
55uses
56  Classes, SysUtils, nsError, nsXPCOMGlue, GeckoChromeWindow;
57
58type
59  TWindowCreator = class(TInterfacedObject,
60                         nsIWindowCreator)
61    function CreateChromeWindow(parent: nsIWebBrowserChrome;
62                                chromeFlags: PRUint32):
63                                nsIWebBrowserChrome; safecall;
64    function SafeCallException(Obj: TObject; Addr: Pointer): HResult; override;
65  end;
66
67var
68  sTargets: TInterfaceList;
69  sTargetInitialized: Boolean = False;
70
71function GetTargetList: TInterfaceList;
72begin
73  Result := sTargets;
74  if not Assigned(Result) and not sTargetInitialized then
75  try
76    sTargets := TInterfaceList.Create;
77    Result := sTargets;
78  except
79    Result := nil;
80  end;
81end;
82
83procedure AddCreateWindowTarget(target: IGeckoCreateWindowTarget);
84var
85  list: TInterfaceList;
86begin
87  list := GetTargetList;
88  if not Assigned(list) then Exit;
89  list.Add(target);
90end;
91
92procedure RemoveCreateWindowTarget(target: IGeckoCreateWindowTarget);
93var
94  list: TInterfaceList;
95begin
96  list := GetTargetList;
97  if not Assigned(list) then Exit;
98  list.Remove(target);
99end;
100
101function FindTarget(chrome: nsIWebBrowserChrome): IGeckoCreateWindowTarget;
102var
103  list: TInterfaceList;
104  target: IGeckoCreateWindowTarget;
105  i, cnt: Integer;
106begin
107  Result := nil;
108  list := GetTargetList;
109  if not Assigned(list) then Exit;
110  cnt := list.Count;
111  for i:=0 to cnt-1 do
112  begin
113    Supports(list.Items[i], IGeckoCreateWindowTarget, target);
114    if Assigned(target) and (target.GetWebBrowserChrome=chrome) then
115    begin
116      Result := target;
117      Exit;
118    end;
119  end;
120end;
121
122var
123  sWindowCreatorInitialized: Boolean = False;
124  sWindowCreator: TWindowCreator = nil;
125
126function InitWindowCreator: Boolean;
127const
128  NS_WINDOWWATCHER_CONTRACTID = '@mozilla.org/embedcomp/window-watcher;1';
129var
130  creator: TWindowCreator;
131  wwatch: nsIWindowWatcher;
132begin
133  try
134    Result := False;
135    if not sWindowCreatorInitialized then
136    begin
137      sWindowCreatorInitialized := True;
138      creator := TWindowCreator.Create;
139      sWindowCreator := creator;
140    end;
141    if not Assigned(sWindowCreator) then Exit;
142
143    NS_GetService(NS_WINDOWWATCHER_CONTRACTID, nsIWindowWatcher, wwatch);
144
145    wwatch.SetWindowCreator(sWindowCreator);
146
147    Result := True;
148  except
149    Result := False;
150  end;
151end;
152
153function TWindowCreator.CreateChromeWindow(parent: nsIWebBrowserChrome;
154                                           chromeFlags: PRUint32):
155                                           nsIWebBrowserChrome;
156var
157  target: IGeckoCreateWindowTarget;
158  chrome: TGeckoChromeForm;
159begin
160  if (chromeFlags and CHROME_OPENAS_CHROME)<>0 then
161  begin
162    chrome := TGeckoChromeForm.CreateWithChromeFlags(nil, chromeFlags);
163    try
164      target := IGeckoCreateWindowTarget(chrome);
165      Result := target.GetWebBrowserChrome;
166    except
167      chrome.Close;
168      raise;
169    end;
170  end else
171  begin
172    target := FindTarget(parent);
173    if Assigned(target) then
174      Result := target.DoCreateChromeWindow(chromeFlags);
175    if not Assigned(Result) then
176      raise EGeckoError.Create('�V�����u���E�U������Ƃ��o���܂���);
177  end;
178end;
179
180function TWindowCreator.SafeCallException(Obj: TObject; Addr: Pointer): HResult;
181begin
182  Result := HRESULT(NS_ERROR_FAILURE);
183end;
184
185end.
Note: See TracBrowser for help on using the browser.