Closed Bug 787273 Opened 12 years ago Closed 12 years ago

War on singletons part 2: electric boogaloo

Categories

(Firefox :: Sync, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla19

People

(Reporter: gps, Assigned: gps)

References

Details

Attachments

(9 files)

+++ This bug was initially created as a clone of Bug #785225 +++

Singletons are like a recurring bad dream. Kill all of the singletons!
See commit message.
Assignee: nobody → gps
Status: NEW → ASSIGNED
Attachment #657082 - Flags: review?(rnewman)
This was a tricky patch. But, I think I got it.

I can't wait to use the unified client library so all these one-offs of Resource go away.
Attachment #657091 - Flags: review?(rnewman)
Gave SyncStorageRequest the same treatment as Resource.
Attachment #657097 - Flags: review?(rnewman)
Another one bites the dust.
Attachment #657155 - Flags: review?(rnewman)
Pretty self-explanatory.
Attachment #659308 - Flags: review?(rnewman)
AFAICT the only singletons we have left now are Status and Service. I think I'm content with this for now.

Please commence review.
Read the commit.
Attachment #659316 - Flags: review?(rnewman)
Not a full elimination of Status. Just funneling more things through Service to work towards our goal of not having Service be dependent on singletons.
Attachment #659355 - Flags: review?(rnewman)
Attachment #657082 - Flags: review?(rnewman) → review+
Comment on attachment 657091 [details] [diff] [review]
Part 2: Remove magic authentication from Resource, v1

Review of attachment 657091 [details] [diff] [review]:
-----------------------------------------------------------------

Two possible comments here, linked.

One is that getResource is looong. I think "Service.resource(url)", or even "Service.res(url)" would suffice.

The second, considering that length:

  let res = Service.getResource(url);
  let res = new Resource(url, Service);

which is to say: why not have a second argument to Resource be an AuthenticatorFactory, and/or have Service act as an Authenticator? This feels a little more general than using the Service as a ResourceFactory, and actually more closely mirrors Android, let alone your own approach to Collection.

Not a formal objection, but think about it.

::: services/sync/modules/record.js
@@ +520,5 @@
>    Resource.call(this, uri);
> +
> +  // This is a bit hacky, but gets the job done.
> +  let res = service.getResource(uri);
> +  this.authenticator = res.authenticator;

See Overview comment, but do you see how s/service/authenticatorFactory can help here?

::: services/sync/modules/resource.js
@@ +21,5 @@
>  
>  /*
>   * AsyncResource represents a remote network resource, identified by a URI.
>   * Create an instance like so:
>   * 

Trailing whitespace from history.
Attachment #657091 - Flags: review?(rnewman) → review+
Comment on attachment 657097 [details] [diff] [review]
Part 3: Remove Identity from SyncStorageRequest, v1

Review of attachment 657097 [details] [diff] [review]:
-----------------------------------------------------------------

r+ modulo naming.

::: services/sync/modules/service.js
@@ +483,5 @@
>  
>    /**
> +   * Obtain a SyncStorageRequest instance with authentication credentials.
> +   */
> +  getRESTRequest: function getRESTRequest(url) {

getStorageRequest.
Attachment #657097 - Flags: review?(rnewman) → review+
(In reply to Richard Newman [:rnewman] from comment #9)
> One is that getResource is looong. I think "Service.resource(url)", or even
> "Service.res(url)" would suffice.
> 
> The second, considering that length:
> 
>   let res = Service.getResource(url);
>   let res = new Resource(url, Service);
> 
> which is to say: why not have a second argument to Resource be an
> AuthenticatorFactory, and/or have Service act as an Authenticator? This
> feels a little more general than using the Service as a ResourceFactory, and
> actually more closely mirrors Android, let alone your own approach to
> Collection.

I agree with you. If I did it again, I'd do it this way. But considering Resource is deprecated, 0fg.
Comment on attachment 657155 [details] [diff] [review]
Part 4: Remove Identity singleton, v1

Review of attachment 657155 [details] [diff] [review]:
-----------------------------------------------------------------

::: services/sync/modules/main.js
@@ +11,5 @@
>    "notifications.js":     ["Notifications", "Notification", "NotificationButton"],
>    "service.js":           ["Service"],
>    "status.js":            ["Status"],
>    "util.js":              ['Utils', 'Svc']
>  };

Yay for this shrinking!

::: services/sync/modules/service.js
@@ +311,5 @@
> +    // Status is instantiated before us and is the first to grab an instance of
> +    // the IdentityManager. We use that instance because IdentityManager really
> +    // needs to be a singleton. Ideally, the longer-lived object would spawn
> +    // this service instance.
> +    this.identity = Status._authManager;

Oh Christ.

Also, thank you for commenting this.

Let's go one step further. If the instantiation order changes, things will screw up, so let's check for falsyness here and do something sane.

::: services/sync/tests/unit/test_identity_manager.js
@@ +9,3 @@
>  function run_test() {
>    initTestLogging("Trace");
> +  Log4Moz.repository.getLogger("Sync.identity").level =

Over-enthusiastic sed…
Attachment #657155 - Flags: review?(rnewman) → review+
Comment on attachment 659308 [details] [diff] [review]
Part 5: Remove CollectionKeys singleton, v1

Review of attachment 659308 [details] [diff] [review]:
-----------------------------------------------------------------

r+ with points addressed.

::: services/sync/modules/engines.js
@@ +826,5 @@
>        handled.push(item.id);
>  
>        try {
>          try {
> +          item.decrypt(self.service.collectionKeys.keyForCollection(self.name));

This is ugly, and worse it relies on a Sufficiently Smart Compiler® to make it efficient.

Let's take this opportunity and cache the key outside of the recordHandler assignment:

    let key = this.service.collectionKeys.keyForCollection(this.name);
    newitems.recordHandler = function(item) {
      ..

@@ +1329,2 @@
>        canDecrypt = true;
> +    }.bind(this);

Similarly. There is no reason for us to be doing four attribute lookups and a method call within a loop.

::: services/sync/modules/service.js
@@ +275,5 @@
>  
>    handleFetchedKeys: function handleFetchedKeys(syncKey, cryptoKeys, skipReset) {
>      // Don't want to wipe if we're just starting up!
>      // This is largely relevant because we don't persist
> +    // collectionKeys yet: Bug 610913.

We just WONTFIXed that bug, and "collectionKeys" here doesn't make sense. Please rewrite this comment.

::: services/sync/tests/unit/test_corrupt_keys.js
@@ +64,1 @@
>                                           Svc.Crypto.generateRandomKey()];

Alignment.
Attachment #659308 - Flags: review?(rnewman) → review+
Comment on attachment 659316 [details] [diff] [review]
Part 6: Remove Weave export from service.js, v1

Review of attachment 659316 [details] [diff] [review]:
-----------------------------------------------------------------

Love it.
Attachment #659316 - Flags: review?(rnewman) → review+
Attachment #659355 - Flags: review?(rnewman) → review+
ZOMG all reviews done!!1!
Landing this busted TPS. The underlying problem is part 6. TPS was relying on Weave being exported from service.js. I refactored TPS to go through main.js.

I hate adding additional Weave. accesses. This is only temporary. I have a patch in my queue that kills Weave.
Attachment #661849 - Flags: review?(rnewman)
Attachment #661849 - Flags: review?(rnewman) → review+
Part 8: https://hg.mozilla.org/services/services-central/rev/1a7e69990b60

I just noticed mochitests are broken too. I guess we're getting a part 9!
This /should/ fix things. I haven't tested locally because I'm waiting on a build to finish. I'll hold off pushing until I can verify locally.
Attachment #662256 - Flags: review?(rnewman)
Attachment #662256 - Flags: review?(rnewman) → review+
Part 9: https://hg.mozilla.org/services/services-central/rev/ec93b64f28bb

I snuck in a slight change from the r+'d patch. But, it was just updating a reference to point to Identity in the proper location.
Depends on: 815412
Component: Firefox Sync: Backend → Sync
Product: Cloud Services → Firefox
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: