OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/disk_cache/cache_util.h" | 5 #include "net/disk_cache/cache_util.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" |
10 #include "base/scoped_handle.h" | 11 #include "base/scoped_handle.h" |
11 #include "base/file_util.h" | 12 #include "base/file_util.h" |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 // Deletes all the files on path that match search_name pattern. | 16 // Deletes all the files on path that match search_name pattern. |
16 void DeleteFiles(const wchar_t* path, const wchar_t* search_name) { | 17 void DeleteFiles(const wchar_t* path, const wchar_t* search_name) { |
17 std::wstring name(path); | 18 std::wstring name(path); |
18 file_util::AppendToPath(&name, search_name); | 19 file_util::AppendToPath(&name, search_name); |
19 | 20 |
(...skipping 11 matching lines...) Expand all Loading... |
31 std::wstring current(adjusted_path); | 32 std::wstring current(adjusted_path); |
32 current += data.cFileName; | 33 current += data.cFileName; |
33 DeleteFile(current.c_str()); | 34 DeleteFile(current.c_str()); |
34 } while (FindNextFile(handle, &data)); | 35 } while (FindNextFile(handle, &data)); |
35 } | 36 } |
36 | 37 |
37 } // namespace | 38 } // namespace |
38 | 39 |
39 namespace disk_cache { | 40 namespace disk_cache { |
40 | 41 |
| 42 // Implemented in file_win.cc. |
| 43 MessageLoopForIO::IOHandler* GetFileIOHandler(); |
| 44 |
41 bool MoveCache(const std::wstring& from_path, const std::wstring& to_path) { | 45 bool MoveCache(const std::wstring& from_path, const std::wstring& to_path) { |
42 // I don't want to use the shell version of move because if something goes | 46 // I don't want to use the shell version of move because if something goes |
43 // wrong, that version will attempt to move file by file and fail at the end. | 47 // wrong, that version will attempt to move file by file and fail at the end. |
44 if (!MoveFileEx(from_path.c_str(), to_path.c_str(), 0)) { | 48 if (!MoveFileEx(from_path.c_str(), to_path.c_str(), 0)) { |
45 LOG(ERROR) << "Unable to move the cache: " << GetLastError(); | 49 LOG(ERROR) << "Unable to move the cache: " << GetLastError(); |
46 return false; | 50 return false; |
47 } | 51 } |
48 return true; | 52 return true; |
49 } | 53 } |
50 | 54 |
51 void DeleteCache(const std::wstring& path, bool remove_folder) { | 55 void DeleteCache(const std::wstring& path, bool remove_folder) { |
52 DeleteFiles(path.c_str(), L"*"); | 56 DeleteFiles(path.c_str(), L"*"); |
53 if (remove_folder) | 57 if (remove_folder) |
54 RemoveDirectory(path.c_str()); | 58 RemoveDirectory(path.c_str()); |
55 } | 59 } |
56 | 60 |
57 bool DeleteCacheFile(const std::wstring& name) { | 61 bool DeleteCacheFile(const std::wstring& name) { |
58 // We do a simple delete, without ever falling back to SHFileOperation, as the | 62 // We do a simple delete, without ever falling back to SHFileOperation, as the |
59 // version from base does. | 63 // version from base does. |
60 return DeleteFile(name.c_str()) ? true : false; | 64 return DeleteFile(name.c_str()) ? true : false; |
61 } | 65 } |
62 | 66 |
63 void WaitForPendingIO(int* num_pending_io) { | 67 void WaitForPendingIO(int* num_pending_io) { |
64 while (*num_pending_io) { | 68 while (*num_pending_io) { |
65 // Asynchronous IO operations may be in flight and the completion may end | 69 // Asynchronous IO operations may be in flight and the completion may end |
66 // up calling us back so let's wait for them (we need an alertable wait). | 70 // up calling us back so let's wait for them. |
67 // The idea is to let other threads do usefull work and at the same time | 71 MessageLoopForIO::current()->WaitForIOCompletion(100, GetFileIOHandler()); |
68 // allow more than one IO to finish... 20 mS later, we process all queued | |
69 // APCs and see if we have to repeat the wait. | |
70 Sleep(20); | |
71 SleepEx(0, TRUE); | |
72 } | 72 } |
73 } | 73 } |
74 | 74 |
75 } // namespace disk_cache | 75 } // namespace disk_cache |
76 | 76 |
OLD | NEW |