Bug 1074604 - Handle file "modified" event in MozMtpDatabase and MozMtpServer. r=dhylands, a=bajaj
authoreden-chuang <echuang@mozilla.com>
Wed, 08 Oct 2014 00:12:00 +0200
changeset 225519 fb2c26b10c0be9d48f2ea8b2f9fbce491e0c8825
parent 225518 187ca75356bbb4981eb75938a838859ac93cafd5
child 225520 0fe683c5cabc8b68ea9a13f28b4c05b9175cc101
push id3979
push userraliiev@mozilla.com
push dateMon, 13 Oct 2014 16:35:44 +0000
treeherdermozilla-beta@30f2cc610691 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdhylands, bajaj
bugs1074604
milestone34.0a2
Bug 1074604 - Handle file "modified" event in MozMtpDatabase and MozMtpServer. r=dhylands, a=bajaj
dom/system/gonk/MozMtpDatabase.cpp
dom/system/gonk/MozMtpServer.cpp
--- a/dom/system/gonk/MozMtpDatabase.cpp
+++ b/dom/system/gonk/MozMtpDatabase.cpp
@@ -262,42 +262,41 @@ MozMtpDatabase::FileWatcherUpdate(RefCou
   aFile->mFile->GetPath(wideFilePath);
   NS_ConvertUTF16toUTF8 filePath(wideFilePath);
 
   nsCString evtType(aEventType);
   MTP_LOG("file %s %s", filePath.get(), evtType.get());
 
   MtpObjectHandle entryHandle = FindEntryByPath(filePath);
 
-  if (aEventType.EqualsLiteral("created")) {
+  if (aEventType.EqualsLiteral("modified")) {
+    // To update the file information to the newest, we remove the entry for
+    // the existing file, then re-add the entry for the file.
     if (entryHandle != 0) {
-      // The entry already exists. This means that we're being notified
-      // about a file added by MTP. So we can ignore it.
-
-      return;
+      MTP_LOG("About to call sendObjectRemoved Handle 0x%08x file %s", entryHandle, filePath.get());
+      aMtpServer->sendObjectRemoved(entryHandle);
+      RemoveEntry(entryHandle);
     }
     entryHandle = CreateEntryForFile(filePath, aFile);
     if (entryHandle == 0) {
-      // CreateEntryForFile didn't create a new entry. We can't tell MTP.
+      // creating entry for the file failed, don't tell MTP
       return;
     }
     MTP_LOG("About to call sendObjectAdded Handle 0x%08x file %s", entryHandle, filePath.get());
     aMtpServer->sendObjectAdded(entryHandle);
     return;
   }
 
   if (aEventType.EqualsLiteral("deleted")) {
     if (entryHandle == 0) {
       // The entry has already been removed. We can't tell MTP.
       return;
     }
-
     MTP_LOG("About to call sendObjectRemoved Handle 0x%08x file %s", entryHandle, filePath.get());
     aMtpServer->sendObjectRemoved(entryHandle);
-
     RemoveEntry(entryHandle);
     return;
   }
 }
 
 nsCString
 MozMtpDatabase::BaseName(const nsCString& path)
 {
@@ -611,21 +610,17 @@ MozMtpDatabase::endSendObject(const char
                               MtpObjectFormat aFormat,
                               bool aSucceeded)
 {
   MTP_LOG("Handle: 0x%08x Path: '%s'", aHandle, aPath);
 
   if (aSucceeded) {
     RefPtr<DbEntry> entry = GetEntry(aHandle);
     if (entry) {
-      if (mBeginSendObjectCalled) {
-        FileWatcherNotify(entry, "created");
-      } else {
-        FileWatcherNotify(entry, "modified");
-      }
+      FileWatcherNotify(entry, "modified");
     }
   } else {
     RemoveEntry(aHandle);
   }
   mBeginSendObjectCalled = false;
 }
 
 //virtual
--- a/dom/system/gonk/MozMtpServer.cpp
+++ b/dom/system/gonk/MozMtpServer.cpp
@@ -103,18 +103,19 @@ public:
     MOZ_ASSERT(NS_IsMainThread());
 
     if (strcmp(aTopic, "file-watcher-update")) {
       // We're only interested in file-watcher-update events
       return NS_OK;
     }
 
     NS_ConvertUTF16toUTF8 eventType(aData);
-    if (!eventType.EqualsLiteral("created") && !eventType.EqualsLiteral("deleted")) {
-      // MTP doesn't have a modified notification.
+    if (!eventType.EqualsLiteral("modified") && !eventType.EqualsLiteral("deleted")) {
+      // Bug 1074604: Needn't handle "created" event, once file operation
+      // finished, it would trigger "modified" event.
       return NS_OK;
     }
 
     DeviceStorageFile* file = static_cast<DeviceStorageFile*>(aSubject);
     file->Dump("file-watcher-update");
     MTP_LOG("file-watcher-update: file %s %s",
             NS_LossyConvertUTF16toASCII(file->mPath).get(),
             eventType.get());