]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/components/forceexternal.js
Add component to *force* external browser use
[Evergreen.git] / Open-ILS / xul / staff_client / components / forceexternal.js
1 const nsISupports           = Components.interfaces.nsISupports;
2 const nsICategoryManager    = Components.interfaces.nsICategoryManager;
3 const nsIComponentRegistrar = Components.interfaces.nsIComponentRegistrar;
4 const nsIContentPolicy      = Components.interfaces.nsIContentPolicy;
5 const nsIFactory            = Components.interfaces.nsIFactory;
6 const nsIModule             = Components.interfaces.nsIModule;
7 const nsIWindowWatcher      = Components.interfaces.nsIWindowWatcher;
8
9 const WINDOW_MAIN = "eg_main"
10
11 const fe_contractID = "@mozilla.org/content-policy;1?type=egfe";
12 const fe_CID = Components.ID("{D969ED61-DF4C-FA12-A2A6-70AA94C222FB}");
13 // category names are sorted alphabetically. Typical command-line handlers use a
14 // category that begins with the letter "m".
15 const fe_category = "m-egfe";
16
17 const myAppHandler = {
18
19    shouldLoad: function(contentType, contentLocation, requestOrigin, node, mimeTypeGuess, extra)
20    {
21       if (contentType == nsIContentPolicy.TYPE_DOCUMENT) {
22           var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
23             getService(Components.interfaces.nsIWindowMediator);
24           var targetWindow = wm.getMostRecentWindow("eg_main");
25           if (targetWindow != null) {
26             var host = targetWindow.G.data.server_unadorned;
27             if(host && (contentLocation.scheme == 'http' || contentLocation.scheme == 'https') && contentLocation.host != host) {
28                 // first construct an nsIURI object using the ioservice
29                 var ioservice = Components.classes["@mozilla.org/network/io-service;1"]
30                                 .getService(Components.interfaces.nsIIOService);
31
32                 var uriToOpen = ioservice.newURI(contentLocation.spec, null, null);
33
34                 var extps = Components.classes["@mozilla.org/uriloader/external-protocol-service;1"]
35                                 .getService(Components.interfaces.nsIExternalProtocolService);
36
37                 // now, open it!
38                 extps.loadURI(uriToOpen, null);
39
40                 return nsIContentPolicy.REJECT_REQUEST;
41             }
42           }
43       }
44       return nsIContentPolicy.ACCEPT;
45    },
46
47    shouldProcess: function(contentType, contentLocation, requestOrigin, insecNode, mimeType, extra)
48    {
49       return nsIContentPolicy.ACCEPT;
50    },
51
52   /* nsISupports */
53   QueryInterface : function fe_QI(iid)
54   {
55     if (iid.equals(nsIContentPolicy) ||
56         iid.equals(nsIFactory) ||
57         iid.equals(nsISupports))
58       return this;
59
60     throw Components.results.NS_ERROR_NO_INTERFACE;
61   },
62
63   /* nsIFactory */
64
65   createInstance : function fe_CI(outer, iid)
66   {
67     if (outer != null)
68       throw Components.results.NS_ERROR_NO_AGGREGATION;
69
70     return this.QueryInterface(iid);
71   },
72
73   lockFactory : function fe_lock(lock)
74   {
75     /* no-op */
76   }
77 };
78
79 /**
80  * The XPCOM glue that implements nsIModule
81  */
82 const myAppHandlerModule = {
83   /* nsISupports */
84   QueryInterface : function mod_QI(iid)
85   {
86     if (iid.equals(nsIModule) ||
87         iid.equals(nsISupports))
88       return this;
89
90     throw Components.results.NS_ERROR_NO_INTERFACE;
91   },
92
93   /* nsIModule */
94   getClassObject : function mod_gch(compMgr, cid, iid)
95   {
96     if (cid.equals(fe_CID))
97       return myAppHandler.QueryInterface(iid);
98
99     throw Components.results.NS_ERROR_NOT_REGISTERED;
100   },
101
102   registerSelf : function mod_regself(compMgr, fileSpec, location, type)
103   {
104     compMgr.QueryInterface(nsIComponentRegistrar);
105
106     compMgr.registerFactoryLocation(fe_CID,
107                                     "myAppHandler",
108                                     fe_contractID,
109                                     fileSpec,
110                                     location,
111                                     type);
112
113     var catMan = Components.classes["@mozilla.org/categorymanager;1"].
114       getService(nsICategoryManager);
115     catMan.addCategoryEntry("content-policy",
116                             fe_category,
117                             fe_contractID, true, true);
118   },
119
120   unregisterSelf : function mod_unreg(compMgr, location, type)
121   {
122     compMgr.QueryInterface(nsIComponentRegistrar);
123     compMgr.unregisterFactoryLocation(fe_CID, location);
124
125     var catMan = Components.classes["@mozilla.org/categorymanager;1"].
126       getService(nsICategoryManager);
127     catMan.deleteCategoryEntry("content-policy", fe_category);
128   },
129
130   canUnload : function (compMgr)
131   {
132     return true;
133   }
134 };
135
136 /* The NSGetModule function is the magic entry point that XPCOM uses to find what XPCOM objects
137  * this component provides
138  */
139 function NSGetModule(comMgr, fileSpec)
140 {
141   return myAppHandlerModule;
142 }
143