Reduce code duplication by using WebCore's FileSystem functions
authoraroben <aroben@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 24 Oct 2007 17:27:02 +0000 (17:27 +0000)
committeraroben <aroben@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 24 Oct 2007 17:27:02 +0000 (17:27 +0000)
WebCore:

        Add methods to FileSystemWin to get some user profile directories

        These directories are used to hold things like preferences, caches,
        etc.

        Reviewed by Brady.

        * platform/FileSystem.h: Added new method declarations for Windows
        only.
        * platform/win/FileSystemWin.cpp:
        (WebCore::bundleName): Added.
        (WebCore::storageDirectory): Added.
        (WebCore::cachedStorageDirectory): Added.
        (WebCore::localUserSpecificStorageDirectory): Added. Returns the
        directory where WebKit should store any user-specific data that should
        stay local to the current machine (i.e., shouldn't be stored in a
        roaming profile).
        (WebCore::roamingUserSpecificStorageDirectory): Added. Returns the
        directory where WebKit should store any user-specific data that should
        move with the user from machine to machine (i.e., should be stored in
        a roaming profile).

WebKit/win:

        Reduce code duplication by using WebCore's FileSystem functions

        Reviewed by Brady.

        * WebIconDatabase.cpp: Removed a now-unused function and a fixed
        FIXME.
        (WebIconDatabase::init): Changed to use FileSystem functions.
        * WebPreferences.cpp:
        (WebPreferences::preferencesPath): Ditto.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@26990 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog
WebCore/platform/FileSystem.h
WebCore/platform/win/FileSystemWin.cpp
WebKit/win/ChangeLog
WebKit/win/WebIconDatabase.cpp
WebKit/win/WebPreferences.cpp

index 909e67faf713db032dc2c012fdaed3eac796d849..1988a56c4d0d6007e290e0ef0b2b3e18518d8c43 100644 (file)
@@ -1,3 +1,27 @@
+2007-10-24  Adam Roben  <aroben@apple.com>
+
+        Add methods to FileSystemWin to get some user profile directories
+
+        These directories are used to hold things like preferences, caches,
+        etc.
+
+        Reviewed by Brady.
+
+        * platform/FileSystem.h: Added new method declarations for Windows
+        only.
+        * platform/win/FileSystemWin.cpp:
+        (WebCore::bundleName): Added.
+        (WebCore::storageDirectory): Added.
+        (WebCore::cachedStorageDirectory): Added.
+        (WebCore::localUserSpecificStorageDirectory): Added. Returns the
+        directory where WebKit should store any user-specific data that should
+        stay local to the current machine (i.e., shouldn't be stored in a
+        roaming profile).
+        (WebCore::roamingUserSpecificStorageDirectory): Added. Returns the
+        directory where WebKit should store any user-specific data that should
+        move with the user from machine to machine (i.e., should be stored in
+        a roaming profile).
+
 2007-10-24  Alp Toker  <alp@atoker.com>
 
         Reviewed by Mark Rowe.
index 582726cf27f5dc0dadb5442f8effc0b4abc67898..c3adedeb66d9818d100d4e908a1c66ccf76bf1bd 100644 (file)
@@ -29,6 +29,7 @@
 #ifndef FileSystem_h
 #define FileSystem_h
 
+#include <wtf/Platform.h>
 #include <wtf/Vector.h>
 
 namespace WebCore {
@@ -45,6 +46,11 @@ String homeDirectoryPath();
 
 CString fileSystemRepresentation(const String&);
 
+#if PLATFORM(WIN)
+String localUserSpecificStorageDirectory();
+String roamingUserSpecificStorageDirectory();
+#endif
+
 } // namespace WebCore
 
 #endif // FileSystem_h
index ac47ebf53ad854aed56ac2d9869fcd9093e02cce..25d3e4a82540ea8dc48fe204ba96d42c5ee88e92 100644 (file)
@@ -107,4 +107,61 @@ String homeDirectoryPath()
     return "";\r
 }\r
 \r
+static String bundleName()\r
+{\r
+    static bool initialized;\r
+    static String name = "WebKit";\r
+\r
+    if (!initialized) {\r
+        initialized = true;\r
+\r
+        if (CFBundleRef bundle = CFBundleGetMainBundle())
+            if (CFTypeRef bundleExecutable = CFBundleGetValueForInfoDictionaryKey(bundle, kCFBundleExecutableKey))
+                if (CFGetTypeID(bundleExecutable) == CFStringGetTypeID())
+                    name = reinterpret_cast<CFStringRef>(bundleExecutable);\r
+    }\r
+\r
+    return name;\r
+}\r
+\r
+static String storageDirectory(DWORD pathIdentifier)
+{
+    Vector<UChar> buffer(MAX_PATH);
+    if (FAILED(SHGetFolderPathW(0, pathIdentifier | CSIDL_FLAG_CREATE, 0, 0, buffer.data())))
+        return String();
+    buffer.resize(wcslen(buffer.data()));
+    String directory = String::adopt(buffer);
+
+    static const String companyNameDirectory = "Apple Computer\\";
+    directory = pathByAppendingComponent(directory, companyNameDirectory + bundleName());
+    if (!makeAllDirectories(directory))
+        return String();
+
+    return directory;
+}\r
+\r
+static String cachedStorageDirectory(DWORD pathIdentifier)\r
+{\r
+    static HashMap<DWORD, String> directories;\r
+\r
+    HashMap<DWORD, String>::iterator it = directories.find(pathIdentifier);\r
+    if (it != directories.end())\r
+        return it->second;\r
+\r
+    String directory = storageDirectory(pathIdentifier);\r
+    directories.add(pathIdentifier, directory);\r
+\r
+    return directory;\r
+}\r
+\r
+String localUserSpecificStorageDirectory()
+{
+    return cachedStorageDirectory(CSIDL_LOCAL_APPDATA);
+}
+
+String roamingUserSpecificStorageDirectory()
+{
+    return cachedStorageDirectory(CSIDL_APPDATA);
+}
+\r
 } // namespace WebCore\r
index 5b2cd5ab4e7b7d6ff172ab6b262af63bde91afb8..00db09cf39220669949b3da25105837b8066ad5b 100644 (file)
@@ -1,3 +1,15 @@
+2007-10-23  Adam Roben  <aroben@apple.com>
+
+        Reduce code duplication by using WebCore's FileSystem functions
+
+        Reviewed by Brady.
+
+        * WebIconDatabase.cpp: Removed a now-unused function and a fixed
+        FIXME.
+        (WebIconDatabase::init): Changed to use FileSystem functions.
+        * WebPreferences.cpp:
+        (WebPreferences::preferencesPath): Ditto.
+
 2007-10-23  Sam Weinig  <sam@webkit.org>
 
         Make the WebNotificationCenter work with null (wildcard) and specific
index 7ff05f986818e49189734b0ea9411f2e81add362..0e2acbe945fe81ef1dc03c2d7ae1179ff888b3b6 100644 (file)
@@ -33,6 +33,7 @@
 #include "WebNotificationCenter.h"
 #pragma warning(push, 0)
 #include <WebCore/BString.h>
+#include <WebCore/FileSystem.h>
 #include <WebCore/IconDatabase.h>
 #include <WebCore/Image.h>
 #include <WebCore/PlatformString.h>
@@ -58,39 +59,6 @@ WebIconDatabase::~WebIconDatabase()
     gClassCount--;
 }
 
-// FIXME - <rdar://problem/4721579>
-// This is code ripped directly from FileUtilities.cpp - it may be extremely useful
-// to have it in a centralized location in WebKit.  But also, getting the icon database
-// path should use the WebPreferences system before it falls back to some reasonable default
-HRESULT userIconDatabasePath(String& path)
-{
-    // get the path to the user's non-roaming application data folder (Example: C:\Documents and Settings\{username}\Local Settings\Application Data\)
-    TCHAR appDataPath[MAX_PATH];
-    HRESULT hr = SHGetFolderPath(0, CSIDL_LOCAL_APPDATA | CSIDL_FLAG_CREATE, 0, 0, appDataPath);
-    if (FAILED(hr))
-        return hr;
-
-    // make the Apple Computer and WebKit subfolder
-    path = String(appDataPath) + "\\Apple Computer\\";
-
-    WebCore::String appName = "WebKit";
-    CFBundleRef bundle = CFBundleGetMainBundle();
-    if (bundle) {
-        CFStringRef bundleExecutable = (CFStringRef)CFBundleGetValueForInfoDictionaryKey(bundle, kCFBundleExecutableKey);
-        if (bundleExecutable)
-            appName = bundleExecutable;
-    }
-    path += appName;
-
-    if (!CreateDirectory(path.charactersWithNullTermination(), 0)) {
-        DWORD err = GetLastError();
-        if (err != ERROR_ALREADY_EXISTS)
-            return (HRESULT_FROM_WIN32(err));
-    }
-
-    return S_OK;
-}
-
 void WebIconDatabase::init()
 {
     WebPreferences* standardPrefs = WebPreferences::sharedStandardPreferences();
@@ -109,9 +77,12 @@ void WebIconDatabase::init()
 
     String databasePath(prefDatabasePath, SysStringLen(prefDatabasePath));
     SysFreeString(prefDatabasePath);
-    if (databasePath.isEmpty())
-        if (FAILED(userIconDatabasePath(databasePath)))
+
+    if (databasePath.isEmpty()) {
+        databasePath = localUserSpecificStorageDirectory();
+        if (databasePath.isEmpty())
             LOG_ERROR("Failed to construct default icon database path");
+    }
 
     if (!iconDatabase()->open(databasePath))
             LOG_ERROR("Failed to open icon database path");
index e4c4642a4a99cc970e3b2b4eb995e2685ec12ea6..85faa697f315f38bf43927c108f015f2cce97b35 100644 (file)
@@ -32,6 +32,7 @@
 #include "WebPreferenceKeysPrivate.h"
 
 #pragma warning( push, 0 )
+#include <WebCore/FileSystem.h>
 #include <WebCore/Font.h>
 #include <WebCore/PlatformString.h>
 #include <WebCore/StringHash.h>
@@ -48,6 +49,8 @@
 #include <wtf/OwnPtr.h>
 #include <wtf/Vector.h>
 
+using namespace WebCore;
+
 static unsigned long long WebSystemMainMemory()
 {
     MEMORYSTATUSEX statex;
@@ -471,30 +474,11 @@ void WebPreferences::removeValuesMatchingDefaultSettings()
 
 HRESULT WebPreferences::preferencesPath(LPTSTR path, size_t cchPath)
 {
-    // get the path to the user's Application Data folder (Example: C:\Documents and Settings\{username}\Application Data\Apple Computer\WebKit)
-    HRESULT hr = SHGetFolderPath(0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, 0, 0, path);
-    if (FAILED(hr))
-        return hr;
-
-    if (_tcscat_s(path, cchPath, TEXT("\\Apple Computer\\")))
-        return E_FAIL;
-
-    WebCore::String appName = "WebKit";
-    CFBundleRef bundle = CFBundleGetMainBundle();
-    if (bundle) {
-        CFStringRef bundleExecutable = (CFStringRef)CFBundleGetValueForInfoDictionaryKey(bundle, kCFBundleExecutableKey);
-        if (bundleExecutable)
-            appName = bundleExecutable;
-    }
-    if (_tcscat_s(path, cchPath, appName.charactersWithNullTermination()))
-        return E_FAIL;
-
-    int err = SHCreateDirectoryEx(0, path, 0);
-    if (err != ERROR_SUCCESS && err != ERROR_ALREADY_EXISTS)
-        return E_FAIL;
+    static const String filename = "WebKitPreferences.plist";
 
-    if (_tcscat_s(path, cchPath, TEXT("\\WebKitPreferences.plist")))
-        return E_FAIL;
+    String prefs = pathByAppendingComponent(roamingUserSpecificStorageDirectory(), filename);
+    if (int err = _tcscpy_s(path, cchPath, prefs.charactersWithNullTermination()))
+        return HRESULT_FROM_WIN32(err);
 
     return S_OK;
 }