Skip to content

Commit

Permalink
Implements local storage for SharedObject.
Browse files Browse the repository at this point in the history
  • Loading branch information
yurydelendik committed Mar 6, 2015
1 parent b00eb4b commit b383502
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 8 deletions.
7 changes: 7 additions & 0 deletions extension/firefox/chrome/ShumwayCom.jsm
Expand Up @@ -22,6 +22,7 @@ Components.utils.import('resource://gre/modules/Promise.jsm');
Components.utils.import('resource://gre/modules/NetUtil.jsm');

Components.utils.import('chrome://shumway/content/SpecialInflate.jsm');
Components.utils.import('chrome://shumway/content/SpecialStorage.jsm');
Components.utils.import('chrome://shumway/content/RtmpUtils.jsm');

XPCOMUtils.defineLazyModuleGetter(this, 'ShumwayTelemetry',
Expand Down Expand Up @@ -187,6 +188,12 @@ var ShumwayCom = {
}, shumwayComAdapter, {defineAs: 'createRtmpXHR'});
}

Components.utils.exportFunction(function () {
var environment = callbacks.getEnvironment();
return SpecialStorageUtils.createWrappedSpecialStorage(content,
environment.swfUrl, environment.privateBrowsing);
}, shumwayComAdapter, {defineAs: 'createSpecialStorage'});

Components.utils.makeObjectPropsNormal(shumwayComAdapter);

return shumwayComAdapter;
Expand Down
57 changes: 57 additions & 0 deletions extension/firefox/chrome/SpecialStorage.jsm
@@ -0,0 +1,57 @@
/*
* Copyright 2015 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var EXPORTED_SYMBOLS = ['SpecialStorageUtils'];

Components.utils.import('resource://gre/modules/Services.jsm');
Components.utils.import('resource://gre/modules/Services.jsm');

var SpecialStorageUtils = {
createWrappedSpecialStorage: function (sandbox, swfUrl, privateBrowsing) {
// Creating internal localStorage object based on url and privateBrowsing setting.
var uri = Services.io.newURI(swfUrl, null, null);
var principal = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
.getService(Components.interfaces.nsIScriptSecurityManager)
.getNoAppCodebasePrincipal(uri);
var dsm = Components.classes["@mozilla.org/dom/localStorage-manager;1"]
.getService(Components.interfaces.nsIDOMStorageManager);
var storage = dsm.createStorage(null, principal, privateBrowsing);

// We will return object created in the sandbox/content, with some exposed
// properties/methods, so we can send data between wrapped object and
// and sandbox/content.
var wrapper = new sandbox.Object();
var waived = Components.utils.waiveXrays(wrapper);
Object.defineProperties(waived, {
getItem: {
value: function (key) {
return storage.getItem(key);
}
},
setItem: {
value: function (key, value) {
return storage.setItem(key, value);
}
},
removeItem: {
value: function (key) {
return storage.removeItem(key);
}
}
});
return wrapper;
}
};
5 changes: 4 additions & 1 deletion extension/firefox/chrome/content.js
Expand Up @@ -47,13 +47,16 @@ function enableDebug() {
}

addMessageListener('Shumway:init', function (message) {
var environment = message.data;

sendAsyncMessage('Shumway:running', {}, {
externalInterface: externalInterfaceWrapper
});

shumwayComAdapter = ShumwayCom.createAdapter(content, {
sendMessage: sendMessage,
enableDebug: enableDebug
enableDebug: enableDebug,
getEnvironment: function () { return environment; }
});

content.wrappedJSObject.runViewer();
Expand Down
12 changes: 10 additions & 2 deletions extension/firefox/chrome/viewerWrapper.js
Expand Up @@ -38,7 +38,8 @@ function runViewer() {

var shumwayComAdapter = ShumwayCom.createAdapter(childWindow, {
sendMessage: sendMessage,
enableDebug: enableDebug
enableDebug: enableDebug,
getEnvironment: getEnvironment,
});

shumwayActions.onExternalCallback = function (call) {
Expand Down Expand Up @@ -83,7 +84,7 @@ function runViewer() {
messageManager.sendAsyncMessage('Shumway:loadFile', args);
};

messageManager.sendAsyncMessage('Shumway:init', {});
messageManager.sendAsyncMessage('Shumway:init', getEnvironment());
}


Expand Down Expand Up @@ -122,6 +123,13 @@ function runViewer() {
connection.send({action: 'runViewer'}, true);
}

function getEnvironment() {
return {
swfUrl: window.shumwayStartupInfo.url,
privateBrowsing: window.shumwayStartupInfo.privateBrowsing
};
}

function enableDebug() {
DebugUtils.enableDebug(window.shumwayStartupInfo.url);
setTimeout(function () {
Expand Down
1 change: 1 addition & 0 deletions extension/firefox/content/ShumwayStreamConverter.jsm
Expand Up @@ -440,6 +440,7 @@ ShumwayStreamConverterBase.prototype = {
var startupInfo = {};
startupInfo.window = window;
startupInfo.url = url;
startupInfo.privateBrowsing = isContentWindowPrivate(window);
startupInfo.objectParams = objectParams;
startupInfo.movieParams = movieParams;
startupInfo.baseUrl = baseUrl || url;
Expand Down
9 changes: 8 additions & 1 deletion src/base/external.ts
Expand Up @@ -17,7 +17,8 @@
declare var ShumwayCom: {
createSpecialInflate: () => SpecialInflate;
createRtmpSocket: (options) => any;
createRtmpXHR: ()=> XMLHttpRequest;
createRtmpXHR: () => XMLHttpRequest;
createSpecialStorage: () => SpecialStorage;
userInput: () => void;
fallback: () => void;
endActivation: () => void;
Expand All @@ -41,6 +42,12 @@ declare var ShumwayCom: {
onSyncMessage: (data: any) => any;
};

interface SpecialStorage {
getItem(key: string): string;
setItem(key: string, value: string): void;
removeItem(key: string): void;
}

interface SpecialInflate {
onData: (data: Uint8Array) => void;
push(data: Uint8Array);
Expand Down
25 changes: 21 additions & 4 deletions src/flash/net/SharedObject.ts
Expand Up @@ -19,7 +19,24 @@ module Shumway.AVM2.AS.flash.net {
import asCoerceString = Shumway.AVM2.Runtime.asCoerceString;
import somewhatImplemented = Shumway.Debug.somewhatImplemented;

declare var sessionStorage;
interface IStorage {
getItem(key: string): string;
setItem(key: string, value: string): void;
removeItem(key: string): void;
}

var _sharedObjectStorage: IStorage;

function getSharedObjectStorage(): IStorage {
if (!_sharedObjectStorage) {
if (typeof ShumwayCom !== 'undefined' && ShumwayCom.createSpecialStorage) {
_sharedObjectStorage = ShumwayCom.createSpecialStorage();
} else {
_sharedObjectStorage = (<any>window).sessionStorage;
}
}
return _sharedObjectStorage;
}

export class SharedObject extends flash.events.EventDispatcher {

Expand Down Expand Up @@ -80,7 +97,7 @@ module Shumway.AVM2.AS.flash.net {
if (SharedObject._sharedObjects[path]) {
return SharedObject._sharedObjects[path];
}
var data = sessionStorage.getItem(path);
var data = getSharedObjectStorage().getItem(path);
// TODO: JSON here probably needs to convert things into AS3 objects.
var so = SharedObject._create(path, data ? JSON.parse(data) : {});
// so._data[Multiname.getPublicQualifiedName("cookie")] = {};
Expand Down Expand Up @@ -147,11 +164,11 @@ module Shumway.AVM2.AS.flash.net {
break;
case 6: // clear
this._data = {};
sessionStorage.removeItem(this._path);
getSharedObjectStorage().removeItem(this._path);
simulated = true;
break;
case 2: // flush
sessionStorage.setItem(this._path, JSON.stringify(this._data));
getSharedObjectStorage().setItem(this._path, JSON.stringify(this._data));
simulated = true;
result = true;
break;
Expand Down

0 comments on commit b383502

Please sign in to comment.