| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/chromeos/drive/file_system/open_file_operation.h" | 5 #include "chrome/browser/chromeos/drive/file_system/open_file_operation.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 weak_ptr_factory_(this) { | 43 weak_ptr_factory_(this) { |
| 44 } | 44 } |
| 45 | 45 |
| 46 OpenFileOperation::~OpenFileOperation() { | 46 OpenFileOperation::~OpenFileOperation() { |
| 47 } | 47 } |
| 48 | 48 |
| 49 void OpenFileOperation::OpenFile(const base::FilePath& file_path, | 49 void OpenFileOperation::OpenFile(const base::FilePath& file_path, |
| 50 OpenMode open_mode, | 50 OpenMode open_mode, |
| 51 const std::string& mime_type, | 51 const std::string& mime_type, |
| 52 const OpenFileCallback& callback) { | 52 const OpenFileCallback& callback) { |
| 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 53 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 54 DCHECK(!callback.is_null()); | 54 DCHECK(!callback.is_null()); |
| 55 | 55 |
| 56 switch (open_mode) { | 56 switch (open_mode) { |
| 57 case OPEN_FILE: | 57 case OPEN_FILE: |
| 58 // It is not necessary to create a new file even if not exists. | 58 // It is not necessary to create a new file even if not exists. |
| 59 // So call OpenFileAfterCreateFile directly with FILE_ERROR_OK | 59 // So call OpenFileAfterCreateFile directly with FILE_ERROR_OK |
| 60 // to skip file creation. | 60 // to skip file creation. |
| 61 OpenFileAfterCreateFile(file_path, callback, FILE_ERROR_OK); | 61 OpenFileAfterCreateFile(file_path, callback, FILE_ERROR_OK); |
| 62 break; | 62 break; |
| 63 case CREATE_FILE: | 63 case CREATE_FILE: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 76 base::Bind(&OpenFileOperation::OpenFileAfterCreateFile, | 76 base::Bind(&OpenFileOperation::OpenFileAfterCreateFile, |
| 77 weak_ptr_factory_.GetWeakPtr(), file_path, callback)); | 77 weak_ptr_factory_.GetWeakPtr(), file_path, callback)); |
| 78 break; | 78 break; |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 | 81 |
| 82 void OpenFileOperation::OpenFileAfterCreateFile( | 82 void OpenFileOperation::OpenFileAfterCreateFile( |
| 83 const base::FilePath& file_path, | 83 const base::FilePath& file_path, |
| 84 const OpenFileCallback& callback, | 84 const OpenFileCallback& callback, |
| 85 FileError error) { | 85 FileError error) { |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 86 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 87 DCHECK(!callback.is_null()); | 87 DCHECK(!callback.is_null()); |
| 88 | 88 |
| 89 if (error != FILE_ERROR_OK) { | 89 if (error != FILE_ERROR_OK) { |
| 90 callback.Run(error, base::FilePath(), base::Closure()); | 90 callback.Run(error, base::FilePath(), base::Closure()); |
| 91 return; | 91 return; |
| 92 } | 92 } |
| 93 | 93 |
| 94 download_operation_->EnsureFileDownloadedByPath( | 94 download_operation_->EnsureFileDownloadedByPath( |
| 95 file_path, | 95 file_path, |
| 96 ClientContext(USER_INITIATED), | 96 ClientContext(USER_INITIATED), |
| 97 GetFileContentInitializedCallback(), | 97 GetFileContentInitializedCallback(), |
| 98 google_apis::GetContentCallback(), | 98 google_apis::GetContentCallback(), |
| 99 base::Bind( | 99 base::Bind( |
| 100 &OpenFileOperation::OpenFileAfterFileDownloaded, | 100 &OpenFileOperation::OpenFileAfterFileDownloaded, |
| 101 weak_ptr_factory_.GetWeakPtr(), callback)); | 101 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 102 } | 102 } |
| 103 | 103 |
| 104 void OpenFileOperation::OpenFileAfterFileDownloaded( | 104 void OpenFileOperation::OpenFileAfterFileDownloaded( |
| 105 const OpenFileCallback& callback, | 105 const OpenFileCallback& callback, |
| 106 FileError error, | 106 FileError error, |
| 107 const base::FilePath& local_file_path, | 107 const base::FilePath& local_file_path, |
| 108 scoped_ptr<ResourceEntry> entry) { | 108 scoped_ptr<ResourceEntry> entry) { |
| 109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 109 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 110 DCHECK(!callback.is_null()); | 110 DCHECK(!callback.is_null()); |
| 111 | 111 |
| 112 if (error == FILE_ERROR_OK) { | 112 if (error == FILE_ERROR_OK) { |
| 113 DCHECK(entry); | 113 DCHECK(entry); |
| 114 DCHECK(entry->has_file_specific_info()); | 114 DCHECK(entry->has_file_specific_info()); |
| 115 if (entry->file_specific_info().is_hosted_document()) | 115 if (entry->file_specific_info().is_hosted_document()) |
| 116 // No support for opening a hosted document. | 116 // No support for opening a hosted document. |
| 117 error = FILE_ERROR_INVALID_OPERATION; | 117 error = FILE_ERROR_INVALID_OPERATION; |
| 118 } | 118 } |
| 119 | 119 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 138 callback, | 138 callback, |
| 139 base::Owned(file_closer))); | 139 base::Owned(file_closer))); |
| 140 } | 140 } |
| 141 | 141 |
| 142 void OpenFileOperation::OpenFileAfterOpenForWrite( | 142 void OpenFileOperation::OpenFileAfterOpenForWrite( |
| 143 const base::FilePath& local_file_path, | 143 const base::FilePath& local_file_path, |
| 144 const std::string& local_id, | 144 const std::string& local_id, |
| 145 const OpenFileCallback& callback, | 145 const OpenFileCallback& callback, |
| 146 scoped_ptr<base::ScopedClosureRunner>* file_closer, | 146 scoped_ptr<base::ScopedClosureRunner>* file_closer, |
| 147 FileError error) { | 147 FileError error) { |
| 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 148 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 149 DCHECK(!callback.is_null()); | 149 DCHECK(!callback.is_null()); |
| 150 | 150 |
| 151 if (error != FILE_ERROR_OK) { | 151 if (error != FILE_ERROR_OK) { |
| 152 callback.Run(error, base::FilePath(), base::Closure()); | 152 callback.Run(error, base::FilePath(), base::Closure()); |
| 153 return; | 153 return; |
| 154 } | 154 } |
| 155 | 155 |
| 156 ++open_files_[local_id]; | 156 ++open_files_[local_id]; |
| 157 callback.Run(error, local_file_path, | 157 callback.Run(error, local_file_path, |
| 158 base::Bind(&OpenFileOperation::CloseFile, | 158 base::Bind(&OpenFileOperation::CloseFile, |
| 159 weak_ptr_factory_.GetWeakPtr(), | 159 weak_ptr_factory_.GetWeakPtr(), |
| 160 local_id, | 160 local_id, |
| 161 base::Passed(file_closer))); | 161 base::Passed(file_closer))); |
| 162 } | 162 } |
| 163 | 163 |
| 164 void OpenFileOperation::CloseFile( | 164 void OpenFileOperation::CloseFile( |
| 165 const std::string& local_id, | 165 const std::string& local_id, |
| 166 scoped_ptr<base::ScopedClosureRunner> file_closer) { | 166 scoped_ptr<base::ScopedClosureRunner> file_closer) { |
| 167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 167 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 168 DCHECK_GT(open_files_[local_id], 0); | 168 DCHECK_GT(open_files_[local_id], 0); |
| 169 | 169 |
| 170 if (--open_files_[local_id] == 0) { | 170 if (--open_files_[local_id] == 0) { |
| 171 // All clients closes this file, so notify to upload the file. | 171 // All clients closes this file, so notify to upload the file. |
| 172 open_files_.erase(local_id); | 172 open_files_.erase(local_id); |
| 173 delegate_->OnEntryUpdatedByOperation(ClientContext(USER_INITIATED), | 173 delegate_->OnEntryUpdatedByOperation(ClientContext(USER_INITIATED), |
| 174 local_id); | 174 local_id); |
| 175 | 175 |
| 176 // Clients may have enlarged the file. By FreeDiskpSpaceIfNeededFor(0), | 176 // Clients may have enlarged the file. By FreeDiskpSpaceIfNeededFor(0), |
| 177 // we try to ensure (0 + the-minimum-safe-margin = 512MB as of now) space. | 177 // we try to ensure (0 + the-minimum-safe-margin = 512MB as of now) space. |
| 178 blocking_task_runner_->PostTask( | 178 blocking_task_runner_->PostTask( |
| 179 FROM_HERE, | 179 FROM_HERE, |
| 180 base::Bind(base::IgnoreResult( | 180 base::Bind(base::IgnoreResult( |
| 181 base::Bind(&internal::FileCache::FreeDiskSpaceIfNeededFor, | 181 base::Bind(&internal::FileCache::FreeDiskSpaceIfNeededFor, |
| 182 base::Unretained(cache_), | 182 base::Unretained(cache_), |
| 183 0)))); | 183 0)))); |
| 184 } | 184 } |
| 185 } | 185 } |
| 186 | 186 |
| 187 } // namespace file_system | 187 } // namespace file_system |
| 188 } // namespace drive | 188 } // namespace drive |
| OLD | NEW |