| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/common/file_system/file_system_dispatcher.h" | 5 #include "chrome/common/file_system/file_system_dispatcher.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | |
| 8 #include "chrome/common/child_thread.h" | 7 #include "chrome/common/child_thread.h" |
| 9 #include "chrome/common/render_messages.h" | 8 #include "chrome/common/render_messages.h" |
| 10 #include "chrome/common/render_messages_params.h" | |
| 11 #include "third_party/WebKit/WebKit/chromium/public/WebFileInfo.h" | |
| 12 #include "third_party/WebKit/WebKit/chromium/public/WebFileSystemEntry.h" | |
| 13 #include "third_party/WebKit/WebKit/chromium/public/WebFileSystemCallbacks.h" | 9 #include "third_party/WebKit/WebKit/chromium/public/WebFileSystemCallbacks.h" |
| 14 #include "third_party/WebKit/WebKit/chromium/public/WebVector.h" | |
| 15 #include "webkit/glue/webkit_glue.h" | 10 #include "webkit/glue/webkit_glue.h" |
| 16 | 11 |
| 17 using WebKit::WebFileError; | 12 using WebKit::WebFileError; |
| 18 using WebKit::WebFileInfo; | 13 using WebKit::WebFileInfo; |
| 19 using WebKit::WebFileSystemCallbacks; | 14 using WebKit::WebFileSystemCallbacks; |
| 20 using WebKit::WebFileSystemEntry; | |
| 21 using WebKit::WebVector; | |
| 22 | 15 |
| 23 FileSystemDispatcher::FileSystemDispatcher() { | 16 FileSystemDispatcher::FileSystemDispatcher() { |
| 24 } | 17 } |
| 25 | 18 |
| 26 FileSystemDispatcher::~FileSystemDispatcher() { | 19 FileSystemDispatcher::~FileSystemDispatcher() { |
| 27 // Make sure we fire all the remaining callbacks. | 20 // Make sure we fire all the remaining callbacks. |
| 28 for (IDMap<WebFileSystemCallbacks>::iterator iter(&callbacks_); | 21 for (IDMap<WebFileSystemCallbacks>::iterator iter(&callbacks_); |
| 29 !iter.IsAtEnd(); | 22 !iter.IsAtEnd(); |
| 30 iter.Advance()) { | 23 iter.Advance()) { |
| 31 int callbacks_id = iter.GetCurrentKey(); | 24 int callbacks_id = iter.GetCurrentKey(); |
| 32 WebFileSystemCallbacks* callbacks = iter.GetCurrentValue(); | 25 WebFileSystemCallbacks* callbacks = iter.GetCurrentValue(); |
| 33 DCHECK(callbacks); | 26 DCHECK(callbacks); |
| 34 callbacks_.Remove(callbacks_id); | 27 callbacks_.Remove(callbacks_id); |
| 35 callbacks->didFail(WebKit::WebFileErrorAbort); | 28 callbacks->didFail(WebKit::WebFileErrorAbort); |
| 36 } | 29 } |
| 37 } | 30 } |
| 38 | 31 |
| 39 bool FileSystemDispatcher::OnMessageReceived(const IPC::Message& msg) { | 32 bool FileSystemDispatcher::OnMessageReceived(const IPC::Message& msg) { |
| 40 bool handled = true; | 33 bool handled = true; |
| 41 IPC_BEGIN_MESSAGE_MAP(FileSystemDispatcher, msg) | 34 IPC_BEGIN_MESSAGE_MAP(FileSystemDispatcher, msg) |
| 42 IPC_MESSAGE_HANDLER(ViewMsg_FileSystem_DidSucceed, DidSucceed) | 35 IPC_MESSAGE_HANDLER(ViewMsg_FileSystem_Succeeded, DidSucceed) |
| 43 IPC_MESSAGE_HANDLER(ViewMsg_FileSystem_DidReadDirectory, DidReadDirectory) | 36 IPC_MESSAGE_HANDLER(ViewMsg_FileSystem_Failed, DidFail) |
| 44 IPC_MESSAGE_HANDLER(ViewMsg_FileSystem_DidReadMetadata, DidReadMetadata) | |
| 45 IPC_MESSAGE_HANDLER(ViewMsg_FileSystem_DidFail, DidFail) | |
| 46 IPC_MESSAGE_UNHANDLED(handled = false) | 37 IPC_MESSAGE_UNHANDLED(handled = false) |
| 47 IPC_END_MESSAGE_MAP() | 38 IPC_END_MESSAGE_MAP() |
| 48 return handled; | 39 return handled; |
| 49 } | 40 } |
| 50 | 41 |
| 51 void FileSystemDispatcher::Move( | 42 void FileSystemDispatcher::Move( |
| 52 const string16& src_path, const string16& dest_path, | 43 const string16& src_path, const string16& dest_path, |
| 53 WebFileSystemCallbacks* callbacks) { | 44 WebFileSystemCallbacks* callbacks) { |
| 54 int request_id = callbacks_.Add(callbacks); | 45 int request_id = callbacks_.Add(callbacks); |
| 55 ChildThread::current()->Send( | 46 ChildThread::current()->Send( |
| 56 new ViewHostMsg_FileSystem_Move(request_id, src_path, dest_path)); | 47 new ViewHostMsg_FileSystem_Move(request_id, src_path, dest_path)); |
| 57 } | 48 } |
| 58 | 49 |
| 59 void FileSystemDispatcher::Copy( | |
| 60 const string16& src_path, const string16& dest_path, | |
| 61 WebFileSystemCallbacks* callbacks) { | |
| 62 int request_id = callbacks_.Add(callbacks); | |
| 63 ChildThread::current()->Send( | |
| 64 new ViewHostMsg_FileSystem_Copy(request_id, src_path, dest_path)); | |
| 65 } | |
| 66 | |
| 67 void FileSystemDispatcher::Remove( | |
| 68 const string16& path, WebFileSystemCallbacks* callbacks) { | |
| 69 int request_id = callbacks_.Add(callbacks); | |
| 70 ChildThread::current()->Send( | |
| 71 new ViewHostMsg_FileSystem_Remove(request_id, path)); | |
| 72 } | |
| 73 | |
| 74 void FileSystemDispatcher::ReadMetadata( | |
| 75 const string16& path, WebFileSystemCallbacks* callbacks) { | |
| 76 int request_id = callbacks_.Add(callbacks); | |
| 77 ChildThread::current()->Send( | |
| 78 new ViewHostMsg_FileSystem_ReadMetadata(request_id, path)); | |
| 79 } | |
| 80 | |
| 81 void FileSystemDispatcher::Create( | |
| 82 const string16& path, bool exclusive, bool is_directory, | |
| 83 WebFileSystemCallbacks* callbacks) { | |
| 84 int request_id = callbacks_.Add(callbacks); | |
| 85 ChildThread::current()->Send( | |
| 86 new ViewHostMsg_FileSystem_Create(request_id, path, exclusive, | |
| 87 is_directory)); | |
| 88 } | |
| 89 | |
| 90 void FileSystemDispatcher::Exists( | |
| 91 const string16& path, bool is_directory, | |
| 92 WebFileSystemCallbacks* callbacks) { | |
| 93 int request_id = callbacks_.Add(callbacks); | |
| 94 ChildThread::current()->Send( | |
| 95 new ViewHostMsg_FileSystem_Exists(request_id, path, is_directory)); | |
| 96 } | |
| 97 | |
| 98 void FileSystemDispatcher::ReadDirectory( | |
| 99 const string16& path, WebFileSystemCallbacks* callbacks) { | |
| 100 int request_id = callbacks_.Add(callbacks); | |
| 101 ChildThread::current()->Send( | |
| 102 new ViewHostMsg_FileSystem_ReadDirectory(request_id, path)); | |
| 103 } | |
| 104 | |
| 105 void FileSystemDispatcher::DidSucceed(int request_id) { | 50 void FileSystemDispatcher::DidSucceed(int request_id) { |
| 106 WebFileSystemCallbacks* callbacks = callbacks_.Lookup(request_id); | 51 WebFileSystemCallbacks* callbacks = callbacks_.Lookup(request_id); |
| 107 DCHECK(callbacks); | 52 DCHECK(callbacks); |
| 108 callbacks_.Remove(request_id); | 53 callbacks_.Remove(request_id); |
| 109 callbacks->didSucceed(); | 54 callbacks->didSucceed(); |
| 110 } | 55 } |
| 111 | 56 |
| 112 void FileSystemDispatcher::DidReadMetadata(int request_id, | 57 void FileSystemDispatcher::DidFail(int request_id, int code) { |
| 113 const file_util::FileInfo& file_info) { | |
| 114 WebFileSystemCallbacks* callbacks = callbacks_.Lookup(request_id); | 58 WebFileSystemCallbacks* callbacks = callbacks_.Lookup(request_id); |
| 115 DCHECK(callbacks); | 59 DCHECK(callbacks); |
| 116 callbacks_.Remove(request_id); | 60 callbacks_.Remove(request_id); |
| 117 WebFileInfo web_file_info; | 61 callbacks->didFail(static_cast<WebFileError>(code)); |
| 118 web_file_info.modificationTime = file_info.last_modified.ToDoubleT(); | |
| 119 callbacks->didReadMetadata(web_file_info); | |
| 120 } | 62 } |
| 121 | |
| 122 void FileSystemDispatcher::DidReadDirectory( | |
| 123 const ViewMsg_FileSystem_DidReadDirectory_Params& params) { | |
| 124 WebFileSystemCallbacks* callbacks = callbacks_.Lookup(params.request_id); | |
| 125 DCHECK(callbacks); | |
| 126 if (!params.has_more) | |
| 127 callbacks_.Remove(params.request_id); | |
| 128 WebVector<WebFileSystemEntry> entries(params.entries.size()); | |
| 129 for (size_t i = 0; i < params.entries.size(); ++i) { | |
| 130 entries[i].name = webkit_glue::FilePathToWebString(params.entries[i].name); | |
| 131 entries[i].isDirectory = params.entries[i].is_directory; | |
| 132 } | |
| 133 callbacks->didReadDirectory(entries, params.has_more); | |
| 134 } | |
| 135 | |
| 136 void FileSystemDispatcher::DidFail(int request_id, WebFileError code) { | |
| 137 WebFileSystemCallbacks* callbacks = callbacks_.Lookup(request_id); | |
| 138 DCHECK(callbacks); | |
| 139 callbacks_.Remove(request_id); | |
| 140 callbacks->didFail(code); | |
| 141 } | |
| OLD | NEW |