michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.sync.middleware; michael@0: michael@0: import org.mozilla.gecko.sync.crypto.KeyBundle; michael@0: import org.mozilla.gecko.sync.repositories.IdentityRecordFactory; michael@0: import org.mozilla.gecko.sync.repositories.RecordFactory; michael@0: import org.mozilla.gecko.sync.repositories.Repository; michael@0: import org.mozilla.gecko.sync.repositories.RepositorySession; michael@0: import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionCleanDelegate; michael@0: import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionCreationDelegate; michael@0: michael@0: import android.content.Context; michael@0: michael@0: /** michael@0: * Wrap an existing repository in middleware that encrypts and decrypts records michael@0: * passing through. michael@0: * michael@0: * @author rnewman michael@0: * michael@0: */ michael@0: public class Crypto5MiddlewareRepository extends MiddlewareRepository { michael@0: michael@0: public RecordFactory recordFactory = new IdentityRecordFactory(); michael@0: michael@0: public class Crypto5MiddlewareRepositorySessionCreationDelegate extends MiddlewareRepository.SessionCreationDelegate { michael@0: private Crypto5MiddlewareRepository repository; michael@0: private RepositorySessionCreationDelegate outerDelegate; michael@0: michael@0: public Crypto5MiddlewareRepositorySessionCreationDelegate(Crypto5MiddlewareRepository repository, RepositorySessionCreationDelegate outerDelegate) { michael@0: this.repository = repository; michael@0: this.outerDelegate = outerDelegate; michael@0: } michael@0: public void onSessionCreateFailed(Exception ex) { michael@0: this.outerDelegate.onSessionCreateFailed(ex); michael@0: } michael@0: michael@0: @Override michael@0: public void onSessionCreated(RepositorySession session) { michael@0: // Do some work, then report success with the wrapping session. michael@0: Crypto5MiddlewareRepositorySession cryptoSession; michael@0: try { michael@0: // Synchronous, baby. michael@0: cryptoSession = new Crypto5MiddlewareRepositorySession(session, this.repository, recordFactory); michael@0: } catch (Exception ex) { michael@0: this.outerDelegate.onSessionCreateFailed(ex); michael@0: return; michael@0: } michael@0: this.outerDelegate.onSessionCreated(cryptoSession); michael@0: } michael@0: } michael@0: michael@0: public KeyBundle keyBundle; michael@0: private Repository inner; michael@0: michael@0: public Crypto5MiddlewareRepository(Repository inner, KeyBundle keys) { michael@0: super(); michael@0: this.inner = inner; michael@0: this.keyBundle = keys; michael@0: } michael@0: @Override michael@0: public void createSession(RepositorySessionCreationDelegate delegate, Context context) { michael@0: Crypto5MiddlewareRepositorySessionCreationDelegate delegateWrapper = new Crypto5MiddlewareRepositorySessionCreationDelegate(this, delegate); michael@0: inner.createSession(delegateWrapper, context); michael@0: } michael@0: michael@0: @Override michael@0: public void clean(boolean success, RepositorySessionCleanDelegate delegate, michael@0: Context context) { michael@0: this.inner.clean(success, delegate, context); michael@0: } michael@0: }