| 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/browser/renderer_host/database_dispatcher_host.h" | 5 #include "chrome/browser/renderer_host/database_dispatcher_host.h" |
| 6 | 6 |
| 7 #if defined(OS_POSIX) | 7 #if defined(OS_POSIX) |
| 8 #include "base/file_descriptor_posix.h" | 8 #include "base/file_descriptor_posix.h" |
| 9 #endif | 9 #endif |
| 10 | 10 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 | 83 |
| 84 db_tracker_->RemoveObserver(this); | 84 db_tracker_->RemoveObserver(this); |
| 85 } | 85 } |
| 86 | 86 |
| 87 bool DatabaseDispatcherHost::OnMessageReceived( | 87 bool DatabaseDispatcherHost::OnMessageReceived( |
| 88 const IPC::Message& message, bool* message_was_ok) { | 88 const IPC::Message& message, bool* message_was_ok) { |
| 89 DCHECK(!shutdown_); | 89 DCHECK(!shutdown_); |
| 90 *message_was_ok = true; | 90 *message_was_ok = true; |
| 91 bool handled = true; | 91 bool handled = true; |
| 92 IPC_BEGIN_MESSAGE_MAP_EX(DatabaseDispatcherHost, message, *message_was_ok) | 92 IPC_BEGIN_MESSAGE_MAP_EX(DatabaseDispatcherHost, message, *message_was_ok) |
| 93 IPC_MESSAGE_HANDLER(ViewHostMsg_DatabaseOpenFile, OnDatabaseOpenFile) | 93 IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_DatabaseOpenFile, |
| 94 IPC_MESSAGE_HANDLER(ViewHostMsg_DatabaseDeleteFile, OnDatabaseDeleteFile) | 94 OnDatabaseOpenFile) |
| 95 IPC_MESSAGE_HANDLER(ViewHostMsg_DatabaseGetFileAttributes, | 95 IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_DatabaseDeleteFile, |
| 96 OnDatabaseGetFileAttributes) | 96 OnDatabaseDeleteFile) |
| 97 IPC_MESSAGE_HANDLER(ViewHostMsg_DatabaseGetFileSize, | 97 IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_DatabaseGetFileAttributes, |
| 98 OnDatabaseGetFileSize) | 98 OnDatabaseGetFileAttributes) |
| 99 IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_DatabaseGetFileSize, |
| 100 OnDatabaseGetFileSize) |
| 99 IPC_MESSAGE_HANDLER(ViewHostMsg_DatabaseOpened, OnDatabaseOpened) | 101 IPC_MESSAGE_HANDLER(ViewHostMsg_DatabaseOpened, OnDatabaseOpened) |
| 100 IPC_MESSAGE_HANDLER(ViewHostMsg_DatabaseModified, OnDatabaseModified) | 102 IPC_MESSAGE_HANDLER(ViewHostMsg_DatabaseModified, OnDatabaseModified) |
| 101 IPC_MESSAGE_HANDLER(ViewHostMsg_DatabaseClosed, OnDatabaseClosed) | 103 IPC_MESSAGE_HANDLER(ViewHostMsg_DatabaseClosed, OnDatabaseClosed) |
| 102 IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_AllowDatabase, OnAllowDatabase) | 104 IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_AllowDatabase, OnAllowDatabase) |
| 103 IPC_MESSAGE_UNHANDLED(handled = false) | 105 IPC_MESSAGE_UNHANDLED(handled = false) |
| 104 IPC_END_MESSAGE_MAP_EX() | 106 IPC_END_MESSAGE_MAP_EX() |
| 105 return handled; | 107 return handled; |
| 106 } | 108 } |
| 107 | 109 |
| 108 void DatabaseDispatcherHost::ReceivedBadMessage(uint32 msg_type) { | 110 void DatabaseDispatcherHost::ReceivedBadMessage(uint32 msg_type) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 122 } | 124 } |
| 123 | 125 |
| 124 if (!shutdown_ && message_sender_) | 126 if (!shutdown_ && message_sender_) |
| 125 message_sender_->Send(message); | 127 message_sender_->Send(message); |
| 126 else | 128 else |
| 127 delete message; | 129 delete message; |
| 128 } | 130 } |
| 129 | 131 |
| 130 void DatabaseDispatcherHost::OnDatabaseOpenFile(const string16& vfs_file_name, | 132 void DatabaseDispatcherHost::OnDatabaseOpenFile(const string16& vfs_file_name, |
| 131 int desired_flags, | 133 int desired_flags, |
| 132 int32 message_id) { | 134 IPC::Message* reply_msg) { |
| 133 if (!observer_added_) { | 135 if (!observer_added_) { |
| 134 observer_added_ = true; | 136 observer_added_ = true; |
| 135 ChromeThread::PostTask( | 137 ChromeThread::PostTask( |
| 136 ChromeThread::FILE, FROM_HERE, | 138 ChromeThread::FILE, FROM_HERE, |
| 137 NewRunnableMethod(this, &DatabaseDispatcherHost::AddObserver)); | 139 NewRunnableMethod(this, &DatabaseDispatcherHost::AddObserver)); |
| 138 } | 140 } |
| 139 | 141 |
| 140 ChromeThread::PostTask( | 142 ChromeThread::PostTask( |
| 141 ChromeThread::FILE, FROM_HERE, | 143 ChromeThread::FILE, FROM_HERE, |
| 142 NewRunnableMethod(this, | 144 NewRunnableMethod(this, |
| 143 &DatabaseDispatcherHost::DatabaseOpenFile, | 145 &DatabaseDispatcherHost::DatabaseOpenFile, |
| 144 vfs_file_name, | 146 vfs_file_name, |
| 145 desired_flags, | 147 desired_flags, |
| 146 message_id)); | 148 reply_msg)); |
| 147 } | |
| 148 | |
| 149 static void SetOpenFileResponseParams( | |
| 150 ViewMsg_DatabaseOpenFileResponse_Params* params, | |
| 151 base::PlatformFile file_handle, | |
| 152 base::PlatformFile dir_handle) { | |
| 153 #if defined(OS_WIN) | |
| 154 params->file_handle = file_handle; | |
| 155 #elif defined(OS_POSIX) | |
| 156 params->file_handle = base::FileDescriptor(file_handle, true); | |
| 157 params->dir_handle = base::FileDescriptor(dir_handle, true); | |
| 158 #endif | |
| 159 } | 149 } |
| 160 | 150 |
| 161 // Scheduled by the IO thread on the file thread. | 151 // Scheduled by the IO thread on the file thread. |
| 162 // Opens the given database file, then schedules | 152 // Opens the given database file, then schedules |
| 163 // a task on the IO thread's message loop to send an IPC back to | 153 // a task on the IO thread's message loop to send an IPC back to |
| 164 // corresponding renderer process with the file handle. | 154 // corresponding renderer process with the file handle. |
| 165 void DatabaseDispatcherHost::DatabaseOpenFile(const string16& vfs_file_name, | 155 void DatabaseDispatcherHost::DatabaseOpenFile(const string16& vfs_file_name, |
| 166 int desired_flags, | 156 int desired_flags, |
| 167 int32 message_id) { | 157 IPC::Message* reply_msg) { |
| 168 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); | 158 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 169 base::PlatformFile target_handle = base::kInvalidPlatformFileValue; | 159 base::PlatformFile target_handle = base::kInvalidPlatformFileValue; |
| 170 base::PlatformFile target_dir_handle = base::kInvalidPlatformFileValue; | 160 base::PlatformFile target_dir_handle = base::kInvalidPlatformFileValue; |
| 171 string16 origin_identifier; | 161 string16 origin_identifier; |
| 172 string16 database_name; | 162 string16 database_name; |
| 173 if (vfs_file_name.empty()) { | 163 if (vfs_file_name.empty()) { |
| 174 VfsBackend::OpenTempFileInDirectory(db_tracker_->DatabaseDirectory(), | 164 VfsBackend::OpenTempFileInDirectory(db_tracker_->DatabaseDirectory(), |
| 175 desired_flags, process_handle_, | 165 desired_flags, process_handle_, |
| 176 &target_handle, &target_dir_handle); | 166 &target_handle, &target_dir_handle); |
| 177 } else if (DatabaseUtil::CrackVfsFileName(vfs_file_name, &origin_identifier, | 167 } else if (DatabaseUtil::CrackVfsFileName(vfs_file_name, &origin_identifier, |
| 178 &database_name, NULL) && | 168 &database_name, NULL) && |
| 179 !db_tracker_->IsDatabaseScheduledForDeletion(origin_identifier, | 169 !db_tracker_->IsDatabaseScheduledForDeletion(origin_identifier, |
| 180 database_name)) { | 170 database_name)) { |
| 181 FilePath db_file = | 171 FilePath db_file = |
| 182 DatabaseUtil::GetFullFilePathForVfsFile(db_tracker_, vfs_file_name); | 172 DatabaseUtil::GetFullFilePathForVfsFile(db_tracker_, vfs_file_name); |
| 183 if (!db_file.empty()) { | 173 if (!db_file.empty()) { |
| 184 VfsBackend::OpenFile(db_file, desired_flags, process_handle_, | 174 VfsBackend::OpenFile(db_file, desired_flags, process_handle_, |
| 185 &target_handle, &target_dir_handle); | 175 &target_handle, &target_dir_handle); |
| 186 } | 176 } |
| 187 } | 177 } |
| 188 | 178 |
| 189 ViewMsg_DatabaseOpenFileResponse_Params response_params; | 179 ViewHostMsg_DatabaseOpenFile::WriteReplyParams( |
| 190 SetOpenFileResponseParams(&response_params, target_handle, target_dir_handle); | 180 reply_msg, |
| 191 Send(new ViewMsg_DatabaseOpenFileResponse(message_id, response_params)); | 181 #if defined(OS_WIN) |
| 182 target_handle |
| 183 #elif defined(OS_POSIX) |
| 184 base::FileDescriptor(target_handle, true), |
| 185 base::FileDescriptor(target_dir_handle, true) |
| 186 #endif |
| 187 ); |
| 188 Send(reply_msg); |
| 192 } | 189 } |
| 193 | 190 |
| 194 void DatabaseDispatcherHost::OnDatabaseDeleteFile(const string16& vfs_file_name, | 191 void DatabaseDispatcherHost::OnDatabaseDeleteFile(const string16& vfs_file_name, |
| 195 const bool& sync_dir, | 192 const bool& sync_dir, |
| 196 int32 message_id) { | 193 IPC::Message* reply_msg) { |
| 197 ChromeThread::PostTask( | 194 ChromeThread::PostTask( |
| 198 ChromeThread::FILE, FROM_HERE, | 195 ChromeThread::FILE, FROM_HERE, |
| 199 NewRunnableMethod(this, | 196 NewRunnableMethod(this, |
| 200 &DatabaseDispatcherHost::DatabaseDeleteFile, | 197 &DatabaseDispatcherHost::DatabaseDeleteFile, |
| 201 vfs_file_name, | 198 vfs_file_name, |
| 202 sync_dir, | 199 sync_dir, |
| 203 message_id, | 200 reply_msg, |
| 204 kNumDeleteRetries)); | 201 kNumDeleteRetries)); |
| 205 } | 202 } |
| 206 | 203 |
| 207 // Scheduled by the IO thread on the file thread. | 204 // Scheduled by the IO thread on the file thread. |
| 208 // Deletes the given database file, then schedules | 205 // Deletes the given database file, then schedules |
| 209 // a task on the IO thread's message loop to send an IPC back to | 206 // a task on the IO thread's message loop to send an IPC back to |
| 210 // corresponding renderer process with the error code. | 207 // corresponding renderer process with the error code. |
| 211 void DatabaseDispatcherHost::DatabaseDeleteFile(const string16& vfs_file_name, | 208 void DatabaseDispatcherHost::DatabaseDeleteFile(const string16& vfs_file_name, |
| 212 bool sync_dir, | 209 bool sync_dir, |
| 213 int32 message_id, | 210 IPC::Message* reply_msg, |
| 214 int reschedule_count) { | 211 int reschedule_count) { |
| 215 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); | 212 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 216 | 213 |
| 217 // Return an error if the file name is invalid or if the file could not | 214 // Return an error if the file name is invalid or if the file could not |
| 218 // be deleted after kNumDeleteRetries attempts. | 215 // be deleted after kNumDeleteRetries attempts. |
| 219 int error_code = SQLITE_IOERR_DELETE; | 216 int error_code = SQLITE_IOERR_DELETE; |
| 220 FilePath db_file = | 217 FilePath db_file = |
| 221 DatabaseUtil::GetFullFilePathForVfsFile(db_tracker_, vfs_file_name); | 218 DatabaseUtil::GetFullFilePathForVfsFile(db_tracker_, vfs_file_name); |
| 222 if (!db_file.empty()) { | 219 if (!db_file.empty()) { |
| 223 error_code = VfsBackend::DeleteFile(db_file, sync_dir); | 220 error_code = VfsBackend::DeleteFile(db_file, sync_dir); |
| 224 if ((error_code == SQLITE_IOERR_DELETE) && reschedule_count) { | 221 if ((error_code == SQLITE_IOERR_DELETE) && reschedule_count) { |
| 225 // If the file could not be deleted, try again. | 222 // If the file could not be deleted, try again. |
| 226 ChromeThread::PostDelayedTask( | 223 ChromeThread::PostDelayedTask( |
| 227 ChromeThread::FILE, FROM_HERE, | 224 ChromeThread::FILE, FROM_HERE, |
| 228 NewRunnableMethod(this, | 225 NewRunnableMethod(this, |
| 229 &DatabaseDispatcherHost::DatabaseDeleteFile, | 226 &DatabaseDispatcherHost::DatabaseDeleteFile, |
| 230 vfs_file_name, | 227 vfs_file_name, |
| 231 sync_dir, | 228 sync_dir, |
| 232 message_id, | 229 reply_msg, |
| 233 reschedule_count - 1), | 230 reschedule_count - 1), |
| 234 kDelayDeleteRetryMs); | 231 kDelayDeleteRetryMs); |
| 235 return; | 232 return; |
| 236 } | 233 } |
| 237 } | 234 } |
| 238 | 235 |
| 239 Send(new ViewMsg_DatabaseDeleteFileResponse(message_id, error_code)); | 236 ViewHostMsg_DatabaseDeleteFile::WriteReplyParams(reply_msg, error_code); |
| 237 Send(reply_msg); |
| 240 } | 238 } |
| 241 | 239 |
| 242 void DatabaseDispatcherHost::OnDatabaseGetFileAttributes( | 240 void DatabaseDispatcherHost::OnDatabaseGetFileAttributes( |
| 243 const string16& vfs_file_name, | 241 const string16& vfs_file_name, |
| 244 int32 message_id) { | 242 IPC::Message* reply_msg) { |
| 245 ChromeThread::PostTask( | 243 ChromeThread::PostTask( |
| 246 ChromeThread::FILE, FROM_HERE, | 244 ChromeThread::FILE, FROM_HERE, |
| 247 NewRunnableMethod(this, | 245 NewRunnableMethod(this, |
| 248 &DatabaseDispatcherHost::DatabaseGetFileAttributes, | 246 &DatabaseDispatcherHost::DatabaseGetFileAttributes, |
| 249 vfs_file_name, | 247 vfs_file_name, |
| 250 message_id)); | 248 reply_msg)); |
| 251 } | 249 } |
| 252 | 250 |
| 253 // Scheduled by the IO thread on the file thread. | 251 // Scheduled by the IO thread on the file thread. |
| 254 // Gets the attributes of the given database file, then schedules | 252 // Gets the attributes of the given database file, then schedules |
| 255 // a task on the IO thread's message loop to send an IPC back to | 253 // a task on the IO thread's message loop to send an IPC back to |
| 256 // corresponding renderer process. | 254 // corresponding renderer process. |
| 257 void DatabaseDispatcherHost::DatabaseGetFileAttributes( | 255 void DatabaseDispatcherHost::DatabaseGetFileAttributes( |
| 258 const string16& vfs_file_name, | 256 const string16& vfs_file_name, |
| 259 int32 message_id) { | 257 IPC::Message* reply_msg) { |
| 260 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); | 258 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 261 int32 attributes = -1; | 259 int32 attributes = -1; |
| 262 FilePath db_file = | 260 FilePath db_file = |
| 263 DatabaseUtil::GetFullFilePathForVfsFile(db_tracker_, vfs_file_name); | 261 DatabaseUtil::GetFullFilePathForVfsFile(db_tracker_, vfs_file_name); |
| 264 if (!db_file.empty()) | 262 if (!db_file.empty()) |
| 265 attributes = VfsBackend::GetFileAttributes(db_file); | 263 attributes = VfsBackend::GetFileAttributes(db_file); |
| 266 Send(new ViewMsg_DatabaseGetFileAttributesResponse(message_id, attributes)); | 264 |
| 265 ViewHostMsg_DatabaseGetFileAttributes::WriteReplyParams( |
| 266 reply_msg, attributes); |
| 267 Send(reply_msg); |
| 267 } | 268 } |
| 268 | 269 |
| 269 void DatabaseDispatcherHost::OnDatabaseGetFileSize( | 270 void DatabaseDispatcherHost::OnDatabaseGetFileSize( |
| 270 const string16& vfs_file_name, int32 message_id) { | 271 const string16& vfs_file_name, IPC::Message* reply_msg) { |
| 271 ChromeThread::PostTask( | 272 ChromeThread::PostTask( |
| 272 ChromeThread::FILE, FROM_HERE, | 273 ChromeThread::FILE, FROM_HERE, |
| 273 NewRunnableMethod(this, | 274 NewRunnableMethod(this, |
| 274 &DatabaseDispatcherHost::DatabaseGetFileSize, | 275 &DatabaseDispatcherHost::DatabaseGetFileSize, |
| 275 vfs_file_name, | 276 vfs_file_name, |
| 276 message_id)); | 277 reply_msg)); |
| 277 } | 278 } |
| 278 | 279 |
| 279 // Scheduled by the IO thread on the file thread. | 280 // Scheduled by the IO thread on the file thread. |
| 280 // Gets the size of the given file, then schedules a task | 281 // Gets the size of the given file, then schedules a task |
| 281 // on the IO thread's message loop to send an IPC back to | 282 // on the IO thread's message loop to send an IPC back to |
| 282 // the corresponding renderer process. | 283 // the corresponding renderer process. |
| 283 void DatabaseDispatcherHost::DatabaseGetFileSize(const string16& vfs_file_name, | 284 void DatabaseDispatcherHost::DatabaseGetFileSize(const string16& vfs_file_name, |
| 284 int32 message_id) { | 285 IPC::Message* reply_msg) { |
| 285 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); | 286 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 286 int64 size = 0; | 287 int64 size = 0; |
| 287 FilePath db_file = | 288 FilePath db_file = |
| 288 DatabaseUtil::GetFullFilePathForVfsFile(db_tracker_, vfs_file_name); | 289 DatabaseUtil::GetFullFilePathForVfsFile(db_tracker_, vfs_file_name); |
| 289 if (!db_file.empty()) | 290 if (!db_file.empty()) |
| 290 size = VfsBackend::GetFileSize(db_file); | 291 size = VfsBackend::GetFileSize(db_file); |
| 291 Send(new ViewMsg_DatabaseGetFileSizeResponse(message_id, size)); | 292 |
| 293 ViewHostMsg_DatabaseGetFileSize::WriteReplyParams(reply_msg, size); |
| 294 Send(reply_msg); |
| 292 } | 295 } |
| 293 | 296 |
| 294 void DatabaseDispatcherHost::OnDatabaseOpened(const string16& origin_identifier, | 297 void DatabaseDispatcherHost::OnDatabaseOpened(const string16& origin_identifier, |
| 295 const string16& database_name, | 298 const string16& database_name, |
| 296 const string16& description, | 299 const string16& description, |
| 297 int64 estimated_size) { | 300 int64 estimated_size) { |
| 298 ChromeThread::PostTask( | 301 ChromeThread::PostTask( |
| 299 ChromeThread::FILE, FROM_HERE, | 302 ChromeThread::FILE, FROM_HERE, |
| 300 NewRunnableMethod(this, | 303 NewRunnableMethod(this, |
| 301 &DatabaseDispatcherHost::DatabaseOpened, | 304 &DatabaseDispatcherHost::DatabaseOpened, |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 database_size, space_available)); | 420 database_size, space_available)); |
| 418 } | 421 } |
| 419 } | 422 } |
| 420 | 423 |
| 421 void DatabaseDispatcherHost::OnDatabaseScheduledForDeletion( | 424 void DatabaseDispatcherHost::OnDatabaseScheduledForDeletion( |
| 422 const string16& origin_identifier, | 425 const string16& origin_identifier, |
| 423 const string16& database_name) { | 426 const string16& database_name) { |
| 424 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); | 427 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 425 Send(new ViewMsg_DatabaseCloseImmediately(origin_identifier, database_name)); | 428 Send(new ViewMsg_DatabaseCloseImmediately(origin_identifier, database_name)); |
| 426 } | 429 } |
| OLD | NEW |