Bug 549472 - Observer for the disabled list preference. r=jmathies
authorBrian R. Bondy <netzen@gmail.com>
Tue, 06 Sep 2011 15:11:28 -0400
changeset 76613 4ba3fb301ff18e562f08c8d6eef6da1b654da27d
parent 76612 830e9ce147b04a01b221539d3e385ffe054e3435
child 76614 3521d58c2234c4b6b1a1253e47e1b805a04e02d1
push id3
push userfelipc@gmail.com
push dateFri, 30 Sep 2011 20:09:13 +0000
reviewersjmathies
bugs549472
milestone9.0a1
Bug 549472 - Observer for the disabled list preference. r=jmathies
widget/src/windows/JumpListBuilder.cpp
widget/src/windows/JumpListBuilder.h
widget/src/windows/JumpListItem.cpp
widget/src/windows/JumpListItem.h
--- a/widget/src/windows/JumpListBuilder.cpp
+++ b/widget/src/windows/JumpListBuilder.cpp
@@ -45,43 +45,50 @@
 #include "nsCOMPtr.h"
 #include "nsServiceManagerUtils.h"
 #include "nsAutoPtr.h"
 #include "nsString.h"
 #include "nsArrayUtils.h"
 #include "nsIMutableArray.h"
 #include "nsWidgetsCID.h"
 #include "WinTaskbar.h"
+#include "nsDirectoryServiceUtils.h"
+#include "nsISimpleEnumerator.h"
+#include "mozilla/Preferences.h"
 
 namespace mozilla {
 namespace widget {
 
 static NS_DEFINE_CID(kJumpListItemCID,     NS_WIN_JUMPLISTITEM_CID);
 static NS_DEFINE_CID(kJumpListLinkCID,     NS_WIN_JUMPLISTLINK_CID);
 static NS_DEFINE_CID(kJumpListShortcutCID, NS_WIN_JUMPLISTSHORTCUT_CID);
 
 // defined in WinTaskbar.cpp
 extern const wchar_t *gMozillaJumpListIDGeneric;
 
 PRPackedBool JumpListBuilder::sBuildingList = PR_FALSE;
+const char kPrefTaskbarEnabled[] = "browser.taskbar.lists.enabled";
 
-NS_IMPL_ISUPPORTS1(JumpListBuilder, nsIJumpListBuilder)
+NS_IMPL_ISUPPORTS2(JumpListBuilder, nsIJumpListBuilder, nsIObserver)
 
 JumpListBuilder::JumpListBuilder() :
   mMaxItems(0),
   mHasCommit(PR_FALSE)
 {
   ::CoInitialize(NULL);
   
   CoCreateInstance(CLSID_DestinationList, NULL, CLSCTX_INPROC_SERVER,
                    IID_ICustomDestinationList, getter_AddRefs(mJumpListMgr));
+
+  Preferences::AddStrongObserver(this, kPrefTaskbarEnabled);
 }
 
 JumpListBuilder::~JumpListBuilder()
 {
+  Preferences::RemoveObserver(this, kPrefTaskbarEnabled);
   mJumpListMgr = nsnull;
   ::CoUninitialize();
 }
 
 /* readonly attribute short available; */
 NS_IMETHODIMP JumpListBuilder::GetAvailable(PRInt16 *aAvailable)
 {
   *aAvailable = PR_FALSE;
@@ -194,16 +201,60 @@ nsresult JumpListBuilder::RemoveIconCach
           shortcut->SetIconImageUri(nsnull);
         }
       }
     }
 
   } // end for
 }
 
+// Ensures that we have no old ICO files left in the jump list cache
+nsresult JumpListBuilder::RemoveIconCacheForAllItems() 
+{
+  // Construct the path of our jump list cache
+  nsCOMPtr<nsIFile> jumpListCacheDir;
+  nsresult rv = NS_GetSpecialDirectory("ProfLDS", 
+                                       getter_AddRefs(jumpListCacheDir));
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = jumpListCacheDir->AppendNative(nsDependentCString(JumpListItem::kJumpListCacheDir));
+  NS_ENSURE_SUCCESS(rv, rv);
+  nsCOMPtr<nsISimpleEnumerator> entries;
+  rv = jumpListCacheDir->GetDirectoryEntries(getter_AddRefs(entries));
+  NS_ENSURE_SUCCESS(rv, rv);
+  
+  // Loop through each directory entry and remove all ICO files found
+  do {
+    PRBool hasMore = PR_FALSE;
+    if (NS_FAILED(entries->HasMoreElements(&hasMore)) || !hasMore)
+      break;
+
+    nsCOMPtr<nsISupports> supp;
+    if (NS_FAILED(entries->GetNext(getter_AddRefs(supp))))
+      break;
+
+    nsCOMPtr<nsIFile> currFile(do_QueryInterface(supp));
+    nsAutoString path;
+    if (NS_FAILED(currFile->GetPath(path)))
+      continue;
+
+    PRInt32 len = path.Length();
+    if (StringTail(path, 4).LowerCaseEqualsASCII(".ico")) {
+      // Check if the cached ICO file exists
+      PRBool exists;
+      if (NS_FAILED(currFile->Exists(&exists)) || !exists)
+        continue;
+
+      // We found an ICO file that exists, so we should remove it
+      currFile->Remove(PR_FALSE);
+    }
+  } while(true);
+
+  return NS_OK;
+}
+
 /* boolean addListToBuild(in short aCatType, [optional] in nsIArray items, [optional] in AString catName); */
 NS_IMETHODIMP JumpListBuilder::AddListToBuild(PRInt16 aCatType, nsIArray *items, const nsAString &catName, PRBool *_retval)
 {
   nsresult rv;
 
   *_retval = PR_FALSE;
 
   if (!mJumpListMgr)
@@ -452,12 +503,25 @@ nsresult JumpListBuilder::TransferIObjec
 
     if (NS_SUCCEEDED(rv)) {
       removedItems->AppendElement(item, PR_FALSE);
     }
   }
   return NS_OK;
 }
 
+NS_IMETHODIMP JumpListBuilder::Observe(nsISupports* aSubject,
+                                        const char* aTopic,
+                                        const PRUnichar* aData)
+{
+  if (nsDependentString(aData).EqualsASCII(kPrefTaskbarEnabled)) {
+    PRBool enabled = Preferences::GetBool(kPrefTaskbarEnabled, true);
+    if (!enabled) {
+      RemoveIconCacheForAllItems();
+    }
+  }
+  return NS_OK;
+}
+
 } // namespace widget
 } // namespace mozilla
 
 #endif // MOZ_WINSDK_TARGETVER >= MOZ_NTDDI_WIN7
--- a/widget/src/windows/JumpListBuilder.h
+++ b/widget/src/windows/JumpListBuilder.h
@@ -50,40 +50,44 @@
 #include <shobjidl.h>
 
 #include "nsString.h"
 #include "nsIMutableArray.h"
 
 #include "nsIJumpListBuilder.h"
 #include "nsIJumpListItem.h"
 #include "JumpListItem.h"
+#include "nsIObserver.h"
 
 namespace mozilla {
 namespace widget {
 
-class JumpListBuilder : public nsIJumpListBuilder
+class JumpListBuilder : public nsIJumpListBuilder, 
+                        public nsIObserver
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIJUMPLISTBUILDER
+  NS_DECL_NSIOBSERVER
 
   JumpListBuilder();
   virtual ~JumpListBuilder();
 
 protected:
   static PRPackedBool sBuildingList; 
 
 private:
   nsRefPtr<ICustomDestinationList> mJumpListMgr;
   PRUint32 mMaxItems;
   PRBool mHasCommit;
 
   PRBool IsSeparator(nsCOMPtr<nsIJumpListItem>& item);
   nsresult TransferIObjectArrayToIMutableArray(IObjectArray *objArray, nsIMutableArray *removedItems);
   nsresult RemoveIconCacheForItems(nsIMutableArray *removedItems);
+  nsresult RemoveIconCacheForAllItems();
 
   friend class WinTaskbar;
 };
 
 } // namespace widget
 } // namespace mozilla
 
 #endif // MOZ_WINSDK_TARGETVER >= MOZ_NTDDI_WIN7
--- a/widget/src/windows/JumpListItem.cpp
+++ b/widget/src/windows/JumpListItem.cpp
@@ -60,17 +60,17 @@
 
 namespace mozilla {
 namespace widget {
 
 // SHCreateItemFromParsingName is only available on vista and up. We only load this if we
 // need to call it on win7+.
 JumpListLink::SHCreateItemFromParsingNamePtr JumpListLink::createItemFromParsingName = nsnull;
 const PRUnichar JumpListLink::kSehllLibraryName[] =  L"shell32.dll";
-const char kJumpListCacheDir[] = "jumpListCache";
+const char JumpListItem::kJumpListCacheDir[] = "jumpListCache";
 HMODULE JumpListLink::sShellDll = nsnull;
 
 // ISUPPORTS Impl's
 NS_IMPL_ISUPPORTS1(JumpListItem,
                    nsIJumpListItem)
 
 NS_IMPL_ISUPPORTS_INHERITED1(JumpListSeparator,
                              JumpListItem,
@@ -446,17 +446,17 @@ nsresult JumpListShortcut::GetOutputIcon
     if ('/' == *cur) {
       *cur = '_';
     }
   }
 
   // Obtain the local profile directory and construct the output icon file path
   rv = NS_GetSpecialDirectory("ProfLDS", getter_AddRefs(aICOFile));
   NS_ENSURE_SUCCESS(rv, rv);
-  rv = aICOFile->AppendNative(nsDependentCString(kJumpListCacheDir));
+  rv = aICOFile->AppendNative(nsDependentCString(JumpListItem::kJumpListCacheDir));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Try to create the directory if it's not there yet
   rv = aICOFile->Create(nsIFile::DIRECTORY_TYPE, 0777);
   if (NS_FAILED(rv) && rv != NS_ERROR_FILE_ALREADY_EXISTS) {
     return rv;
   }
   
@@ -705,17 +705,17 @@ static nsresult IsPathInOurIconCache(nsC
   NS_ENSURE_ARG_POINTER(aSame);
  
   *aSame = PR_FALSE;
 
   // Construct the path of our jump list cache
   nsCOMPtr<nsIFile> jumpListCache;
   nsresult rv = NS_GetSpecialDirectory("ProfLDS", getter_AddRefs(jumpListCache));
   NS_ENSURE_SUCCESS(rv, rv);
-  rv = jumpListCache->AppendNative(nsDependentCString(kJumpListCacheDir));
+  rv = jumpListCache->AppendNative(nsDependentCString(JumpListItem::kJumpListCacheDir));
   NS_ENSURE_SUCCESS(rv, rv);
   nsAutoString jumpListCachePath;
   rv = jumpListCache->GetPath(jumpListCachePath);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Construct the parent path of the passed in path
   nsCOMPtr<nsILocalFile> passedInFile = do_CreateInstance("@mozilla.org/file/local;1");
   NS_ENSURE_TRUE(passedInFile, NS_ERROR_FAILURE);
--- a/widget/src/windows/JumpListItem.h
+++ b/widget/src/windows/JumpListItem.h
@@ -71,16 +71,18 @@ public:
   {}
 
   virtual ~JumpListItem() 
   {}
 
   NS_DECL_ISUPPORTS
   NS_DECL_NSIJUMPLISTITEM
 
+  static const char kJumpListCacheDir[];
+
 protected:
   short Type() { return mItemType; }
   short mItemType;
 
   static nsresult HashURI(nsCOMPtr<nsICryptoHash> &aCryptoHash,
                           nsIURI *aUri, nsACString& aUriHash);
 };