~justinmcp/unity-chromium-extension/generic-extension

« back to all changes in this revision

Viewing changes to chromium-extension/browser.js

  • Committer: Tarmac
  • Author(s): Justin McPherson
  • Date: 2014-01-10 14:24:30 UTC
  • mfrom: (236.1.6 remove-tab-proper)
  • Revision ID: tarmac-20140110142430-xfpovvj8wa9yjv1k
Remove tab integration updated to trunk.

Approved by PS Jenkins bot, Robert Bruce Park, Alexandre Abreu.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
   var aWindow = currentWindow;
9
9
   var plugin = null;
10
10
   var service = null;
11
 
   var api = null;
12
 
 
13
 
   var webkitNotificationUnityBackend = {
14
 
     showNotification: function (iconUrl, title, body) {
15
 
       try {
16
 
         var notification = plugin.notification_new(title, body, null/*iconUrl*/);
17
 
         plugin.notification_show(notification, null);
18
 
         plugin.notification_destroy(notification);
19
 
       }
20
 
       catch (err) {
21
 
         // TODO decipher/inject proper & general logging policy for user side of API
22
 
       }
23
 
     }
 
11
   var api = {
 
12
       init: function (options) {
 
13
           options.url = window.location.href;
 
14
           options.hostname = window.location.hostname;
 
15
           options.host = window.location.host;
 
16
           options.protocol = window.location.protocol;
 
17
           chrome.runtime.sendMessage (
 
18
               {method: "init_requested", options: options}
 
19
               , function (response) {});
 
20
       }
24
21
   };
25
22
 
26
23
   /**
40
37
 
41
38
     // 1. inject a piece of javascript proxying all the requests
42
39
     // to the unity webapps api
43
 
     insertScriptIntoWebpage('webkitnotif-wrapper-builder.js');
44
40
     insertScriptIntoWebpage('unity-api-page-proxy-builder-gen.js');
45
41
     insertScriptIntoWebpage('unity-api-page-proxy.js');
46
42
 
114
110
       var reducetarget = api;
115
111
 
116
112
       // a bit hacky
117
 
       if (funcnames[0] == 'webkitNotifications') {
118
 
         funcnames.shift();
119
 
         reducetarget = webkitNotificationUnityBackend;
120
 
       }
121
113
       try {
122
114
         // Assumes that we are calling a 'callable' from a succession of objects
123
115
         funcnames.reduce (
154
146
                               , true);
155
147
   };
156
148
 
157
 
   function initializePluginConnection (logger){
158
 
     plugin = document.getElementById ("unityChromiumExtensionId");
159
 
     if ( ! plugin) {
160
 
       logger ('Could not bind to the scriptable NPAPI plugin');
161
 
     }
162
 
     else {
163
 
       logger ("Got a plugin object: " + plugin);
164
 
       
165
 
       // initialize webkitnotification backend & fallback
166
 
       try {
167
 
         plugin.notification_init("chromium");
168
 
       }
169
 
       catch (err) {
170
 
         logger ("Error while trying to initialize notify OSD NPAPI connection");
171
 
         // decide to bail out
172
 
         return;
173
 
       }
174
 
       UnityWebappsWebkitNotificationApiPageProxyBuilder(webkitNotificationUnityBackend, aWindow);
175
 
 
176
 
       // initialize unity API binding
177
 
       service = plugin.service_new ();
178
 
       logger ("Service object created: " + service);
179
 
 
180
 
       // global plugin initialization
181
 
       // FIXME: there seem to be an issue that started w/ chromium 27+
182
 
       //  where NPobnject v8 bindings are looked up in the wrong
183
 
       //  JS world under some circumstances.
184
 
       api = UnityGlobalPropertyInitializer.makeCallback (
185
 
           function() {
186
 
               return document.getElementById ("unityChromiumExtensionId");
187
 
           },
188
 
           service,
189
 
           logger)(aWindow);
190
 
     }
191
 
   }
192
 
 
193
 
   initializePluginConnection (function (msg) {
194
 
                                 if (consoleService && consoleService.logStringMessage) {
195
 
                                   consoleService.logStringMessage ("Unity Webapps: " + msg);
196
 
                                 }
197
 
                               });
198
 
 
199
149
   // handle the proxy side of the api which is being injected on the
200
150
   //  webpage
201
151
   injectApiProxy();
202
 
   
203
 
   var windowExternal = aWindow.external;
204
 
   function unity() {
205
 
   }
206
 
   unity.prototype = {
207
 
     __proto__: windowExternal,
208
 
     getUnityObject: function (version) {
209
 
       console.log ('getUnityObject called with version ' + version);
210
 
       if (version === 1)
211
 
         return api;
212
 
       throw new Error("incorrect version");
213
 
     }
214
 
   };
215
 
   aWindow.external = new unity();
216
152
 }
217
153
)(window);
218
154