OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webkit/fileapi/file_system_usage_tracker.h" |
| 6 |
| 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/message_loop_proxy.h" |
| 11 #include "base/scoped_ptr.h" |
| 12 #include "base/task.h" |
| 13 #include "googleurl/src/gurl.h" |
| 14 #include "webkit/fileapi/file_system_path_manager.h" |
| 15 |
| 16 namespace fileapi { |
| 17 |
| 18 class FileSystemUsageTracker::GetUsageTask |
| 19 : public base::RefCountedThreadSafe<GetUsageTask> { |
| 20 public: |
| 21 GetUsageTask( |
| 22 FileSystemUsageTracker* tracker, |
| 23 scoped_refptr<base::MessageLoopProxy> file_message_loop, |
| 24 std::string fs_name, |
| 25 const FilePath& origin_base_path) |
| 26 : tracker_(tracker), |
| 27 file_message_loop_(file_message_loop), |
| 28 origin_message_loop_( |
| 29 base::MessageLoopProxy::CreateForCurrentThread()), |
| 30 fs_name_(fs_name), |
| 31 fs_usage_(0), |
| 32 origin_base_path_(origin_base_path) { |
| 33 } |
| 34 |
| 35 virtual ~GetUsageTask() {} |
| 36 |
| 37 void Start() { |
| 38 if (tracker_) |
| 39 tracker_->RegisterUsageTask(this); |
| 40 file_message_loop_->PostTask( |
| 41 FROM_HERE, NewRunnableMethod(this, &GetUsageTask::RunOnFileThread)); |
| 42 } |
| 43 |
| 44 void RunOnFileThread() { |
| 45 DCHECK(file_message_loop_->BelongsToCurrentThread()); |
| 46 |
| 47 // TODO(dmikurube): add the code that retrieves the origin usage here. |
| 48 |
| 49 origin_message_loop_->PostTask( |
| 50 FROM_HERE, NewRunnableMethod(this, &GetUsageTask::Completed)); |
| 51 } |
| 52 |
| 53 void Cancel() { |
| 54 DCHECK(origin_message_loop_->BelongsToCurrentThread()); |
| 55 tracker_ = NULL; |
| 56 } |
| 57 |
| 58 private: |
| 59 void Completed() { |
| 60 DCHECK(origin_message_loop_->BelongsToCurrentThread()); |
| 61 if (tracker_) { |
| 62 tracker_->UnregisterUsageTask(this); |
| 63 tracker_->DidGetOriginUsage(fs_name_, fs_usage_); |
| 64 } |
| 65 } |
| 66 |
| 67 FileSystemUsageTracker* tracker_; |
| 68 scoped_refptr<base::MessageLoopProxy> file_message_loop_; |
| 69 scoped_refptr<base::MessageLoopProxy> origin_message_loop_; |
| 70 std::string fs_name_; |
| 71 int64 fs_usage_; |
| 72 FilePath origin_base_path_; |
| 73 }; |
| 74 |
| 75 FileSystemUsageTracker::FileSystemUsageTracker( |
| 76 scoped_refptr<base::MessageLoopProxy> file_message_loop, |
| 77 const FilePath& profile_path, |
| 78 bool is_incognito) |
| 79 : file_message_loop_(file_message_loop), |
| 80 base_path_(profile_path.Append( |
| 81 FileSystemPathManager::kFileSystemDirectory)), |
| 82 is_incognito_(is_incognito) { |
| 83 DCHECK(file_message_loop); |
| 84 } |
| 85 |
| 86 FileSystemUsageTracker::~FileSystemUsageTracker() { |
| 87 std::for_each(running_usage_tasks_.begin(), |
| 88 running_usage_tasks_.end(), |
| 89 std::mem_fun(&GetUsageTask::Cancel)); |
| 90 } |
| 91 |
| 92 void FileSystemUsageTracker::GetOriginUsage( |
| 93 const GURL& origin_url, |
| 94 fileapi::FileSystemType type, |
| 95 GetUsageCallback* callback_ptr) { |
| 96 DCHECK(callback_ptr); |
| 97 scoped_ptr<GetUsageCallback> callback(callback_ptr); |
| 98 |
| 99 if (is_incognito_) { |
| 100 // We don't support FileSystem in incognito mode yet. |
| 101 callback->Run(0); |
| 102 return; |
| 103 } |
| 104 |
| 105 std::string fs_name = FileSystemPathManager::GetFileSystemName( |
| 106 origin_url, type); |
| 107 if (pending_usage_callbacks_.find(fs_name) != |
| 108 pending_usage_callbacks_.end()) { |
| 109 // Another get usage task is running. Add the callback to |
| 110 // the pending queue and return. |
| 111 pending_usage_callbacks_[fs_name].push_back(callback.release()); |
| 112 return; |
| 113 } |
| 114 |
| 115 // Get the filesystem base path (i.e. "FileSystem/<origin>/<type>", |
| 116 // without unique part). |
| 117 FilePath origin_base_path = |
| 118 FileSystemPathManager::GetFileSystemBaseDirectoryForOriginAndType( |
| 119 base_path_, origin_url, type); |
| 120 if (origin_base_path.empty()) { |
| 121 // The request seems to be invalid. |
| 122 callback->Run(0); |
| 123 return; |
| 124 } |
| 125 |
| 126 pending_usage_callbacks_[fs_name].push_back(callback.release()); |
| 127 scoped_refptr<GetUsageTask> task( |
| 128 new GetUsageTask(this, file_message_loop_, fs_name, origin_base_path)); |
| 129 task->Start(); |
| 130 } |
| 131 |
| 132 void FileSystemUsageTracker::RegisterUsageTask(GetUsageTask* task) { |
| 133 running_usage_tasks_.push_back(task); |
| 134 } |
| 135 |
| 136 void FileSystemUsageTracker::UnregisterUsageTask(GetUsageTask* task) { |
| 137 DCHECK(running_usage_tasks_.front() == task); |
| 138 running_usage_tasks_.pop_front(); |
| 139 } |
| 140 |
| 141 void FileSystemUsageTracker::DidGetOriginUsage( |
| 142 const std::string& fs_name, int64 usage) { |
| 143 PendingUsageCallbackMap::iterator cb_list_iter = |
| 144 pending_usage_callbacks_.find(fs_name); |
| 145 DCHECK(cb_list_iter != pending_usage_callbacks_.end()); |
| 146 PendingCallbackList cb_list = cb_list_iter->second; |
| 147 for (PendingCallbackList::iterator cb_iter = cb_list.begin(); |
| 148 cb_iter != cb_list.end(); |
| 149 ++cb_iter) { |
| 150 scoped_ptr<GetUsageCallback> callback(*cb_iter); |
| 151 callback->Run(usage); |
| 152 } |
| 153 pending_usage_callbacks_.erase(cb_list_iter); |
| 154 } |
| 155 |
| 156 } // namespace fileapi |
OLD | NEW |