Closed Bug 487556 Opened 15 years ago Closed 14 years ago

satchel should send a notification when adding/removing/changing an item

Categories

(Toolkit :: Form Manager, enhancement)

enhancement
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla1.9.3a5

People

(Reporter: Dolske, Assigned: MattN)

References

(Blocks 1 open bug)

Details

Attachments

(1 file, 4 obsolete files)

Weave needs to know when data is changed, so for syncing form history it needs a notification to avoid hacky problems as mentioned in bug 487541. We fixed this for password manager over in bug 475634.
Blocks: 487558
Attached patch v.1 with tests (obsolete) — Splinter Review
Assignee: nobody → mnoorenberghe
Status: NEW → ASSIGNED
Attachment #389646 - Flags: review?(dolske)
From a quick look, this patch generally looks fine...

Thunder/Mardak -- can you confirm that the notifications this is sending will be sufficient for Weave's needs?
So... the notifications are..

addEntry: [name, value]
modifyEntry: [name, value]
removeEntry: [name, value]

removeEntriesByTimeframe: [startTime, endTime]
removeEntriesForName: fieldName
removeAllEntries: null
expireOldEntries: expireTime

The notifications with name, value are useful now because we're computing a "guid" based on those. Eventually, if there's a built-in guid, I suppose it could be provided as the 3rd value in the array subject.

Just to double check, none of the bulk remove/expire send individual notifications?
Attachment #389646 - Flags: review?(dolske) → review-
Comment on attachment 389646 [details] [diff] [review]
v.1 with tests


>+  nsCOMPtr<nsIMutableArray> notifData;
>+  rv = CreateNotificationData(aName, aValue, notifData);
>+  NS_ENSURE_SUCCESS(rv, rv);
...
>+    SendNotification(NS_LITERAL_STRING("modifyEntry"), notifData);

It would be cleaner to just make a single call to create + send the notification, instead of doing it in separate steps. [Though the single call might, in turn, call another function, since there are a couple of CreateNotificationData() implementations that can share some code this way.]

s/notifData/notifyData/. We can afford the extra byte. :)


>+nsresult
>+nsFormHistory::SendNotification(const nsAString &aChangeType, nsISupports *aData)
>+{
>+  return mObserverService->NotifyObservers(aData, "satchel-storage-changed", ToNewUnicode(aChangeType));
>+}

ToNewUnicode() allocates, so this leaks.

I think what you want here is PromiseFlatString(aChangeType).get()

https://developer.mozilla.org/en/XPCOM_string_guide

Yes, strings suck. :(

>+  if (!fieldName) {
>+    rv = NS_ERROR_OUT_OF_MEMORY;
>+    return rv;
>+  }

Don't set rv, just return the value directly. You could, in fact, just make this a void function and ignore the return type...
Attached patch v.2 address review comments (obsolete) — Splinter Review
(In reply to comment #3)
> Just to double check, none of the bulk remove/expire send individual
> notifications?

Correct, a single event notification gets sent for bulk removals and does not contain any info about the actual entries that were deleted as there is too much overhead in this.
Attachment #389646 - Attachment is obsolete: true
Attachment #390362 - Flags: review?(dolske)
Comment on attachment 390362 [details] [diff] [review]
v.2 address review comments

>-nsFormHistory::GetExistingEntryID (const nsAString &aName, 
>+nsFormHistory::GetExistingEntryID (const nsAString &aName,

??? Looks like a stray whitespace change.

>-nsFormHistory::EntryExists(const nsAString &aName, 
>+nsFormHistory::EntryExists(const nsAString &aName,

And here too.

>+  nsCOMPtr<nsISupportsString> fieldName = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID);
>+  if (!fieldName)
>+    return NS_ERROR_OUT_OF_MEMORY;
>+
>+  fieldName->SetData(aName);
>+  SendNotification(NS_LITERAL_STRING("removeEntriesForName"), fieldName);

Shouldn't need to create fieldName here. Add a SendNotification() that takes just 1 string.

>-nsFormHistory::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData) 
>+nsFormHistory::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData)

??? this and a number of other whitespace only changes... Please remove.

 
>+  nsCOMPtr<nsISupportsPRInt64> expireTimeNotify = do_CreateInstance(NS_SUPPORTS_PRINT64_CONTRACTID);
>+  if (!expireTimeNotify) {
>+    rv = NS_ERROR_OUT_OF_MEMORY;
>+    return rv;
>+  }
>+  expireTimeNotify->SetData(expireTime);
>+  SendNotification(NS_LITERAL_STRING("expireOldEntries"), expireTimeNotify);

Seems like expireTimeNotify isn't needed here either. Add a SendNotification() that just takes an int.

>+/*
>+ * Send a notification when stored data is changed
>+ */
>+nsresult
>+nsFormHistory::SendNotification(const nsAString &aChangeType, nsISupports *aData)
>+{
>+  return mObserverService->NotifyObservers(aData, "satchel-storage-changed", PromiseFlatString(aChangeType).get());
>+}

Cleanup line wrapping.

>+  nsCOMPtr<nsISupportsString> fieldValue = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID);
>+  if (!fieldValue) {
>+    rv = NS_ERROR_OUT_OF_MEMORY;
>+    return rv;
>+  }

Tsk!

>+  nsCOMPtr<nsISupportsPRInt64> valOne = do_CreateInstance(NS_SUPPORTS_PRINT64_CONTRACTID);
>+  if (!valOne) {
>+    return NS_ERROR_OUT_OF_MEMORY;
>+  }

No braces.

>+  nsCOMPtr<nsISupportsPRInt64> valTwo = do_CreateInstance(NS_SUPPORTS_PRINT64_CONTRACTID);
>+  if (!valTwo) {
>+    return NS_ERROR_OUT_OF_MEMORY;
>+  }

Ditto.

>+   rv = SendNotification(aChangeType, notifyData);
>+  NS_ENSURE_SUCCESS(rv, rv);

Stray space before rv.



>+++ b/toolkit/components/satchel/test/unit/test_notify.js
...
>+const STORAGE_TYPE = "mozStorage";

Don't need this.

>+fh = Cc["@mozilla.org/satchel/form-history;1"].
>+       getService(Ci.nsIFormHistory2);

Cc/getService alignment.

>+/* ========== 9 ========== */
>+testnum++;
>+testdesc = "expireOldEntries";
>+
>+expectedNotification = "expireOldEntries";
>+expectedData = null;

Shouldn't expectedData be a timestamp here? How is this passing as-is?
Attachment #390362 - Flags: review?(dolske) → review-
(In reply to comment #5)
> > Just to double check, none of the bulk remove/expire send individual
> > notifications?
> Correct, a single event notification gets sent for bulk removals and does not
> contain any info about the actual entries that were deleted as there is too
> much overhead in this.
Weave will need a "before-<remove-action>" notification so that we know what things to upload as deleted. Basically when weave gets the before-removeEntriesForName, we'll do a query for all those entries and afterwards let satchel continue with removing.

As long as weave has a way to get at the data before it's actually removed, we'll be good to go.
Blocks: 506402
Attached patch v.3 add "before-" notifications (obsolete) — Splinter Review
Added before-remove notifications per the request in comment #7

(In reply to comment #6)
> >+/* ========== 9 ========== */
> >+testnum++;
> >+testdesc = "expireOldEntries";
> >+
> >+expectedNotification = "expireOldEntries";
> >+expectedData = null;
> 
> Shouldn't expectedData be a timestamp here? How is this passing as-is?

There is a check in the event callback that simply checks that the timestamp > 0
Attachment #390362 - Attachment is obsolete: true
Attachment #391536 - Flags: review?(dolske)
http://hg.mozilla.org/labs/weave/file/ff9b610379ca/source/modules/util.js#l149
Here's some code to generate a 10-character GUID.

For now, it could be okay to just put this inline/part of the file. We'll probably want to split it to a separate jsm/xpcom component (for places?) so that Weave and other parts of Firefox can use shorter guids.
Blocks: 554414
Blocks: 552531
No longer blocks: 506402
Depends on: 506402
Attachment #391536 - Flags: review?(dolske) → review+
Attached patch Patch v.4 (obsolete) — Splinter Review
Updated to trunk. Note that this is on top of the WIP patch for bug 506402.
Attachment #391536 - Attachment is obsolete: true
Blocks: 556361
Attached patch Patch v.5Splinter Review
Updated to trunk again, fixed tests due to GUID format change from bug 506402.
Attachment #436112 - Attachment is obsolete: true
Pushed http://hg.mozilla.org/mozilla-central/rev/aa8d539cf6e1

(zomg, finally :)
Status: ASSIGNED → RESOLVED
Closed: 14 years ago
Flags: in-testsuite+
Resolution: --- → FIXED
Target Milestone: --- → mozilla1.9.3a5
Depends on: 559695
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: