001: /*
002: * Copyright 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.shell.moz;
017:
018: import com.google.gwt.core.ext.TreeLogger;
019: import com.google.gwt.dev.shell.BrowserWidget;
020: import com.google.gwt.dev.shell.BrowserWidgetHost;
021: import com.google.gwt.dev.shell.LowLevel;
022: import com.google.gwt.dev.shell.ModuleSpace;
023: import com.google.gwt.dev.shell.ModuleSpaceHost;
024: import com.google.gwt.dev.shell.moz.LowLevelMoz.ExternalFactory;
025: import com.google.gwt.dev.shell.moz.LowLevelMoz.ExternalObject;
026:
027: import org.eclipse.swt.events.DisposeEvent;
028: import org.eclipse.swt.events.DisposeListener;
029: import org.eclipse.swt.internal.mozilla.nsIWebBrowser;
030: import org.eclipse.swt.widgets.Shell;
031:
032: /**
033: * Represents an individual browser window and all of its controls.
034: */
035: public class BrowserWidgetMoz extends BrowserWidget {
036:
037: private class ExternalObjectImpl implements ExternalObject {
038:
039: public boolean gwtOnLoad(int scriptObject, String moduleName) {
040: try {
041: if (moduleName == null) {
042: // Indicates one or more modules are being unloaded.
043: //
044: handleUnload(scriptObject);
045: return true;
046: }
047:
048: Object key = new Integer(scriptObject);
049: // Attach a new ModuleSpace to make it programmable.
050: //
051: ModuleSpaceHost msh = getHost().createModuleSpaceHost(
052: BrowserWidgetMoz.this , moduleName);
053: ModuleSpace moduleSpace = new ModuleSpaceMoz(msh,
054: scriptObject, moduleName, key);
055: attachModuleSpace(moduleSpace);
056: return true;
057: } catch (Throwable e) {
058: // We do catch Throwable intentionally because there are a ton of things
059: // that can go wrong trying to load a module, including Error-derived
060: // things like NoClassDefFoundError.
061: //
062: getHost().getLogger().log(TreeLogger.ERROR,
063: "Failure to load module '" + moduleName + "'",
064: e);
065: return false;
066: }
067: }
068:
069: /**
070: * Unload one or more modules.
071: *
072: * @param scriptObject window to unload, 0 if all
073: */
074: protected void handleUnload(int scriptObject) {
075: Integer key = null;
076: if (scriptObject != 0) {
077: key = new Integer(scriptObject);
078: }
079: doUnload(key);
080: }
081: }
082:
083: public BrowserWidgetMoz(Shell shell, BrowserWidgetHost host) {
084: super (shell, host);
085: host.getLogger().log(
086: TreeLogger.DEBUG,
087: "Using Mozilla install at "
088: + MozillaInstall.getLoaded().getPath(), null);
089:
090: // Expose a 'window.external' object factory. The created object's
091: // gwtOnLoad() method will be called when a hosted mode application's
092: // wrapper
093: // HTML is done loading.
094: //
095: final ExternalFactory externalFactory = new ExternalFactory() {
096:
097: private ExternalObject externalObject = null;
098:
099: public ExternalObject createExternalObject() {
100: if (externalObject == null) {
101: externalObject = new ExternalObjectImpl();
102: }
103: return externalObject;
104: }
105:
106: public boolean matchesDOMWindow(int domWindow) {
107: nsIWebBrowser webBrowser = (nsIWebBrowser) LowLevel
108: .snatchFieldObjectValue(browser.getClass(),
109: browser, "webBrowser");
110: int[] aContentDOMWindow = new int[1];
111: webBrowser.GetContentDOMWindow(aContentDOMWindow);
112: if (aContentDOMWindow[0] == domWindow) {
113: return true;
114: }
115: return false;
116: }
117:
118: };
119:
120: LowLevelMoz.registerExternalFactory(externalFactory);
121:
122: addDisposeListener(new DisposeListener() {
123: public void widgetDisposed(DisposeEvent e) {
124: LowLevelMoz.unregisterExternalFactory(externalFactory);
125: }
126: });
127: }
128:
129: @Override
130: public String getUserAgent() {
131: // OLD Mozilla. See UserAgent.gwt.xml
132: return "gecko";
133: }
134: }
|