Move Windows safe file creation code into WebCore from WebPreferences
authoraroben <aroben@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 24 Oct 2007 17:27:53 +0000 (17:27 +0000)
committeraroben <aroben@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 24 Oct 2007 17:27:53 +0000 (17:27 +0000)
 WebCore:

         Move Windows safe file creation code into WebCore from WebPreferences

         Reviewed by Brady.

         * platform/FileSystem.h:
         * platform/win/FileSystemWin.cpp:
         (WebCore::safeCreateFile): Moved here from WebKit/win/WebPreferences.cpp.

 WebKit/win:

         Move safe file creation code to WebCore

         Reviewed by Brady.

         * WebPreferences.cpp:
         (preferencesPath): Made into a static helper function.
         (WebPreferences::save): Now calls WebCore's safeCreateFile function.
         (WebPreferences::load): Uses String/CString to handle the UTF-8
         conversion.
         * WebPreferences.h: Removed preferencesPath and
         safeCreateFileWithData.

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

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

index 1988a56c4d0d6007e290e0ef0b2b3e18518d8c43..444d1ef9f4e55ba18358a865ebd5a39a9b6b6c20 100644 (file)
@@ -1,5 +1,15 @@
 2007-10-24  Adam Roben  <aroben@apple.com>
 
+        Move Windows safe file creation code into WebCore from WebPreferences
+
+        Reviewed by Brady.
+
+        * platform/FileSystem.h:
+        * platform/win/FileSystemWin.cpp:
+        (WebCore::safeCreateFile): Moved here from WebKit/win/WebPreferences.cpp.
+
+2007-10-23  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,
index c3adedeb66d9818d100d4e908a1c66ccf76bf1bd..461795167b4eac0a8cb5962651034ee2ef0c7500 100644 (file)
@@ -32,6 +32,8 @@
 #include <wtf/Platform.h>
 #include <wtf/Vector.h>
 
+typedef const struct __CFData* CFDataRef;
+
 namespace WebCore {
 
 class CString;
@@ -49,6 +51,8 @@ CString fileSystemRepresentation(const String&);
 #if PLATFORM(WIN)
 String localUserSpecificStorageDirectory();
 String roamingUserSpecificStorageDirectory();
+
+bool safeCreateFile(const String&, CFDataRef);
 #endif
 
 } // namespace WebCore
index 25d3e4a82540ea8dc48fe204ba96d42c5ee88e92..cfba4ae96ac22fa9719b895d90eb5677f210546e 100644 (file)
@@ -163,5 +163,35 @@ String roamingUserSpecificStorageDirectory()
 {
     return cachedStorageDirectory(CSIDL_APPDATA);
 }
+
+bool safeCreateFile(const String& path, CFDataRef data)
+{
+    // Create a temporary file.
+    WCHAR tempDirPath[MAX_PATH];
+    if (!GetTempPathW(ARRAYSIZE(tempDirPath), tempDirPath))
+        return false;
+
+    WCHAR tempPath[MAX_PATH];
+    if (!GetTempFileNameW(tempDirPath, L"WEBKIT", 0, tempPath))
+        return false;
+
+    HANDLE tempFileHandle = CreateFileW(tempPath, GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
+    if (tempFileHandle == INVALID_HANDLE_VALUE)
+        return false;
+
+    // Write the data to this temp file.
+    DWORD written;
+    if (!WriteFile(tempFileHandle, CFDataGetBytePtr(data), static_cast<DWORD>(CFDataGetLength(data)), &written, 0))
+        return false;
+
+    CloseHandle(tempFileHandle);
+
+    // Copy the temp file to the destination file.
+    String destination = path;
+    if (!MoveFileExW(tempPath, destination.charactersWithNullTermination(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
+        return false;
+
+    return true;
+}
 \r
 } // namespace WebCore\r
index 00db09cf39220669949b3da25105837b8066ad5b..28f21f95d75e55e90b2121731b7684220093807b 100644 (file)
@@ -1,3 +1,17 @@
+2007-10-23  Adam Roben  <aroben@apple.com>
+
+        Move safe file creation code to WebCore
+
+        Reviewed by Brady.
+
+        * WebPreferences.cpp:
+        (preferencesPath): Made into a static helper function.
+        (WebPreferences::save): Now calls WebCore's safeCreateFile function.
+        (WebPreferences::load): Uses String/CString to handle the UTF-8
+        conversion.
+        * WebPreferences.h: Removed preferencesPath and
+        safeCreateFileWithData.
+
 2007-10-23  Adam Roben  <aroben@apple.com>
 
         Reduce code duplication by using WebCore's FileSystem functions
index 85faa697f315f38bf43927c108f015f2cce97b35..43d1f51f18135e06b1b3470ba45ded6608b85be9 100644 (file)
@@ -32,6 +32,7 @@
 #include "WebPreferenceKeysPrivate.h"
 
 #pragma warning( push, 0 )
+#include <WebCore/CString.h>
 #include <WebCore/FileSystem.h>
 #include <WebCore/Font.h>
 #include <WebCore/PlatformString.h>
@@ -60,6 +61,12 @@ static unsigned long long WebSystemMainMemory()
     return statex.ullTotalPhys;
 }
 
+static String preferencesPath()
+{
+    static String path = pathByAppendingComponent(roamingUserSpecificStorageDirectory(), "WebKitPreferences.plist");
+    return path;
+}
+
 // WebPreferences ----------------------------------------------------------------
 
 CFDictionaryRef WebPreferences::s_defaultSettings = 0;
@@ -361,10 +368,6 @@ void WebPreferences::setBoolValue(CFStringRef key, BOOL value)
 
 void WebPreferences::save()
 {
-    TCHAR appDataPath[MAX_PATH];
-    if (FAILED(preferencesPath(appDataPath, MAX_PATH)))
-        return;
-
     RetainPtr<CFWriteStreamRef> stream(AdoptCF,
         CFWriteStreamCreateWithAllocatedBuffers(kCFAllocatorDefault, kCFAllocatorDefault)); 
     if (!stream)
@@ -384,26 +387,16 @@ void WebPreferences::save()
     if (!dataRef)
         return;
 
-    void* bytes = (void*)CFDataGetBytePtr(dataRef.get());
-    size_t length = CFDataGetLength(dataRef.get());
-    safeCreateFileWithData(appDataPath, bytes, length);
+    safeCreateFile(preferencesPath(), dataRef.get());
 }
 
 void WebPreferences::load()
 {
     initializeDefaultSettings();
 
-    TCHAR appDataPath[MAX_PATH];
-    if (FAILED(preferencesPath(appDataPath, MAX_PATH)))
-        return;
+    CString path = preferencesPath().utf8();
 
-    DWORD appDataPathLength = (DWORD) _tcslen(appDataPath)+1;
-    int result = WideCharToMultiByte(CP_UTF8, 0, appDataPath, appDataPathLength, 0, 0, 0, 0);
-    Vector<UInt8> utf8Path(result);
-    if (!WideCharToMultiByte(CP_UTF8, 0, appDataPath, appDataPathLength, (LPSTR)utf8Path.data(), result, 0, 0))
-        return;
-
-    RetainPtr<CFURLRef> urlRef(AdoptCF, CFURLCreateFromFileSystemRepresentation(0, utf8Path.data(), result-1, false));
+    RetainPtr<CFURLRef> urlRef(AdoptCF, CFURLCreateFromFileSystemRepresentation(0, reinterpret_cast<const UInt8*>(path.data()), path.length(), false));
     if (!urlRef)
         return;
 
@@ -472,62 +465,6 @@ void WebPreferences::removeValuesMatchingDefaultSettings()
     }
 }
 
-HRESULT WebPreferences::preferencesPath(LPTSTR path, size_t cchPath)
-{
-    static const String filename = "WebKitPreferences.plist";
-
-    String prefs = pathByAppendingComponent(roamingUserSpecificStorageDirectory(), filename);
-    if (int err = _tcscpy_s(path, cchPath, prefs.charactersWithNullTermination()))
-        return HRESULT_FROM_WIN32(err);
-
-    return S_OK;
-}
-
-HRESULT WebPreferences::safeCreateFileWithData(LPCTSTR path, void* data, size_t length)
-{
-    TCHAR tempDirPath[MAX_PATH];
-    TCHAR tempPath[MAX_PATH];
-    HANDLE tempFileHandle = INVALID_HANDLE_VALUE;
-    HRESULT hr = S_OK;
-    tempPath[0] = 0;
-
-    // create a temporary file
-    if (!GetTempPath(sizeof(tempDirPath)/sizeof(tempDirPath[0]), tempDirPath)) {
-        hr = HRESULT_FROM_WIN32(GetLastError());
-        goto exit;
-    }
-    if (!GetTempFileName(tempDirPath, TEXT("WEBKIT"), 0, tempPath))
-        return HRESULT_FROM_WIN32(GetLastError());
-    tempFileHandle = CreateFile(tempPath, GENERIC_READ|GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
-    if (tempFileHandle == INVALID_HANDLE_VALUE) {
-        hr = HRESULT_FROM_WIN32(GetLastError());
-        goto exit;
-    }
-
-    // write the data to this temp file
-    DWORD written;
-    if (!WriteFile(tempFileHandle, data, (DWORD)length, &written, 0)) {
-        hr = HRESULT_FROM_WIN32(GetLastError());
-        goto exit;
-    }
-
-    // copy the temp file to the destination file
-    CloseHandle(tempFileHandle);
-    tempFileHandle = INVALID_HANDLE_VALUE;
-    if (!MoveFileEx(tempPath, path, MOVEFILE_REPLACE_EXISTING)) {
-        hr = HRESULT_FROM_WIN32(GetLastError());
-        goto exit;
-    }
-
-exit:
-    if (tempFileHandle != INVALID_HANDLE_VALUE)
-        CloseHandle(tempFileHandle);
-    if (tempPath[0])
-        DeleteFile(tempPath);
-
-    return hr;
-}
-
 // IUnknown -------------------------------------------------------------------
 
 HRESULT STDMETHODCALLTYPE WebPreferences::QueryInterface(REFIID riid, void** ppvObject)
index 3966c2ed1ba734e12ce94578f62aa23a3d228ad3..34309ae1fdb4f4299c8ddf60f21d7ed88b39c3da 100644 (file)
@@ -313,8 +313,6 @@ protected:
     void load();
     void migrateDefaultSettingsFromSafari3Beta();
     void removeValuesMatchingDefaultSettings();
-    static HRESULT preferencesPath(LPTSTR path, size_t cchPath);
-    static HRESULT safeCreateFileWithData(LPCTSTR path, void* data, size_t length);
 
 protected:
     ULONG m_refCount;