Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.sync.middleware; |
michael@0 | 6 | |
michael@0 | 7 | import org.mozilla.gecko.sync.crypto.KeyBundle; |
michael@0 | 8 | import org.mozilla.gecko.sync.repositories.IdentityRecordFactory; |
michael@0 | 9 | import org.mozilla.gecko.sync.repositories.RecordFactory; |
michael@0 | 10 | import org.mozilla.gecko.sync.repositories.Repository; |
michael@0 | 11 | import org.mozilla.gecko.sync.repositories.RepositorySession; |
michael@0 | 12 | import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionCleanDelegate; |
michael@0 | 13 | import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionCreationDelegate; |
michael@0 | 14 | |
michael@0 | 15 | import android.content.Context; |
michael@0 | 16 | |
michael@0 | 17 | /** |
michael@0 | 18 | * Wrap an existing repository in middleware that encrypts and decrypts records |
michael@0 | 19 | * passing through. |
michael@0 | 20 | * |
michael@0 | 21 | * @author rnewman |
michael@0 | 22 | * |
michael@0 | 23 | */ |
michael@0 | 24 | public class Crypto5MiddlewareRepository extends MiddlewareRepository { |
michael@0 | 25 | |
michael@0 | 26 | public RecordFactory recordFactory = new IdentityRecordFactory(); |
michael@0 | 27 | |
michael@0 | 28 | public class Crypto5MiddlewareRepositorySessionCreationDelegate extends MiddlewareRepository.SessionCreationDelegate { |
michael@0 | 29 | private Crypto5MiddlewareRepository repository; |
michael@0 | 30 | private RepositorySessionCreationDelegate outerDelegate; |
michael@0 | 31 | |
michael@0 | 32 | public Crypto5MiddlewareRepositorySessionCreationDelegate(Crypto5MiddlewareRepository repository, RepositorySessionCreationDelegate outerDelegate) { |
michael@0 | 33 | this.repository = repository; |
michael@0 | 34 | this.outerDelegate = outerDelegate; |
michael@0 | 35 | } |
michael@0 | 36 | public void onSessionCreateFailed(Exception ex) { |
michael@0 | 37 | this.outerDelegate.onSessionCreateFailed(ex); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | @Override |
michael@0 | 41 | public void onSessionCreated(RepositorySession session) { |
michael@0 | 42 | // Do some work, then report success with the wrapping session. |
michael@0 | 43 | Crypto5MiddlewareRepositorySession cryptoSession; |
michael@0 | 44 | try { |
michael@0 | 45 | // Synchronous, baby. |
michael@0 | 46 | cryptoSession = new Crypto5MiddlewareRepositorySession(session, this.repository, recordFactory); |
michael@0 | 47 | } catch (Exception ex) { |
michael@0 | 48 | this.outerDelegate.onSessionCreateFailed(ex); |
michael@0 | 49 | return; |
michael@0 | 50 | } |
michael@0 | 51 | this.outerDelegate.onSessionCreated(cryptoSession); |
michael@0 | 52 | } |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | public KeyBundle keyBundle; |
michael@0 | 56 | private Repository inner; |
michael@0 | 57 | |
michael@0 | 58 | public Crypto5MiddlewareRepository(Repository inner, KeyBundle keys) { |
michael@0 | 59 | super(); |
michael@0 | 60 | this.inner = inner; |
michael@0 | 61 | this.keyBundle = keys; |
michael@0 | 62 | } |
michael@0 | 63 | @Override |
michael@0 | 64 | public void createSession(RepositorySessionCreationDelegate delegate, Context context) { |
michael@0 | 65 | Crypto5MiddlewareRepositorySessionCreationDelegate delegateWrapper = new Crypto5MiddlewareRepositorySessionCreationDelegate(this, delegate); |
michael@0 | 66 | inner.createSession(delegateWrapper, context); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | @Override |
michael@0 | 70 | public void clean(boolean success, RepositorySessionCleanDelegate delegate, |
michael@0 | 71 | Context context) { |
michael@0 | 72 | this.inner.clean(success, delegate, context); |
michael@0 | 73 | } |
michael@0 | 74 | } |