OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "chrome/browser/local_discovery/storage/privet_filesystem_operations.h" |
| 6 |
| 7 namespace local_discovery { |
| 8 |
| 9 namespace { |
| 10 const char kPrivetListEntries[] = "entries"; |
| 11 const char kPrivetListKeyName[] = "name"; |
| 12 const char kPrivetListKeySize[] = "size"; |
| 13 const char kPrivetListKeyType[] = "type"; |
| 14 const char kPrivetListTypeDir[] = "dir"; |
| 15 } |
| 16 |
| 17 PrivetFileSystemAsyncOperationUtil::PrivetFileSystemAsyncOperationUtil( |
| 18 const base::FilePath& full_path, |
| 19 net::URLRequestContextGetter* request_context, |
| 20 Delegate* delegate) |
| 21 : parsed_path_(full_path), request_context_(request_context), |
| 22 delegate_(delegate) { |
| 23 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 24 } |
| 25 |
| 26 PrivetFileSystemAsyncOperationUtil::~PrivetFileSystemAsyncOperationUtil() { |
| 27 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 28 } |
| 29 |
| 30 void PrivetFileSystemAsyncOperationUtil::Start() { |
| 31 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 32 service_discovery_client_ = ServiceDiscoverySharedClient::GetInstance(); |
| 33 privet_device_resolver_.reset(new PrivetDeviceResolver( |
| 34 service_discovery_client_.get(), |
| 35 parsed_path_.service_name, |
| 36 base::Bind( |
| 37 &PrivetFileSystemAsyncOperationUtil::OnGotDeviceDescription, |
| 38 base::Unretained(this)))); |
| 39 privet_device_resolver_->Start(); |
| 40 } |
| 41 |
| 42 void PrivetFileSystemAsyncOperationUtil::OnGotDeviceDescription( |
| 43 bool success, const DeviceDescription& device_description) { |
| 44 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 45 if (!success) { |
| 46 delegate_->PrivetFileSystemResolved(NULL, |
| 47 parsed_path_.path); |
| 48 return; |
| 49 } |
| 50 |
| 51 privet_async_factory_ = PrivetHTTPAsynchronousFactory::CreateInstance( |
| 52 service_discovery_client_.get(), |
| 53 request_context_.get()); |
| 54 privet_http_resolution_ = privet_async_factory_->CreatePrivetHTTP( |
| 55 parsed_path_.service_name, |
| 56 device_description.address, |
| 57 base::Bind(&PrivetFileSystemAsyncOperationUtil::OnGotPrivetHTTP, |
| 58 base::Unretained(this))); |
| 59 privet_http_resolution_->Start(); |
| 60 } |
| 61 |
| 62 void PrivetFileSystemAsyncOperationUtil::OnGotPrivetHTTP( |
| 63 scoped_ptr<PrivetHTTPClient> privet_http_client) { |
| 64 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 65 privet_client_ = privet_http_client.Pass(); |
| 66 delegate_->PrivetFileSystemResolved(privet_client_.get(), |
| 67 parsed_path_.path); |
| 68 } |
| 69 |
| 70 |
| 71 PrivetFileSystemListOperation::PrivetFileSystemListOperation( |
| 72 const base::FilePath& full_path, |
| 73 content::BrowserContext* browser_context, |
| 74 PrivetFileSystemAsyncOperationContainer* async_file_util, |
| 75 const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback) |
| 76 : full_path_(full_path), browser_context_(browser_context), |
| 77 async_file_util_(async_file_util), |
| 78 callback_(callback), weak_factory_(this) { |
| 79 } |
| 80 |
| 81 PrivetFileSystemListOperation::~PrivetFileSystemListOperation() { |
| 82 } |
| 83 |
| 84 void PrivetFileSystemListOperation::Start() { |
| 85 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 86 content::BrowserThread::PostTask( |
| 87 content::BrowserThread::UI, |
| 88 FROM_HERE, |
| 89 base::Bind( |
| 90 &PrivetFileSystemListOperation::StartOnUIThread, |
| 91 weak_factory_.GetWeakPtr())); |
| 92 } |
| 93 |
| 94 void PrivetFileSystemListOperation::StartOnUIThread() { |
| 95 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 96 core_.reset(new PrivetFileSystemAsyncOperationUtil( |
| 97 full_path_, |
| 98 browser_context_->GetRequestContext(), |
| 99 this)); |
| 100 core_->Start(); |
| 101 } |
| 102 |
| 103 void PrivetFileSystemListOperation::PrivetFileSystemResolved( |
| 104 PrivetHTTPClient* http_client, |
| 105 const std::string& path) { |
| 106 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 107 if (!http_client) { |
| 108 TriggerCallback(base::File::FILE_ERROR_FAILED, |
| 109 fileapi::AsyncFileUtil::EntryList(), false); |
| 110 return; |
| 111 } |
| 112 |
| 113 list_operation_ = http_client->CreateStorageListOperation( |
| 114 path, |
| 115 base::Bind(&PrivetFileSystemListOperation::OnStorageListResult, |
| 116 base::Unretained(this))); |
| 117 list_operation_->Start(); |
| 118 } |
| 119 |
| 120 void PrivetFileSystemListOperation::TriggerCallback( |
| 121 base::File::Error result, |
| 122 const fileapi::AsyncFileUtil::EntryList& file_list, |
| 123 bool has_more) { |
| 124 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 125 list_operation_.reset(); |
| 126 core_.reset(); |
| 127 content::BrowserThread::PostTask( |
| 128 content::BrowserThread::IO, |
| 129 FROM_HERE, |
| 130 base::Bind( |
| 131 &PrivetFileSystemListOperation::TriggerCallbackOnIOThread, |
| 132 weak_factory_.GetWeakPtr(), |
| 133 result, file_list, has_more)); |
| 134 } |
| 135 |
| 136 void PrivetFileSystemListOperation::TriggerCallbackOnIOThread( |
| 137 base::File::Error result, |
| 138 fileapi::AsyncFileUtil::EntryList file_list, |
| 139 bool has_more) { |
| 140 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 141 fileapi::AsyncFileUtil::ReadDirectoryCallback callback; |
| 142 callback = callback_; |
| 143 async_file_util_->RemoveOperation(this); |
| 144 callback.Run(result, file_list, has_more); |
| 145 } |
| 146 |
| 147 void PrivetFileSystemListOperation::OnStorageListResult( |
| 148 const base::DictionaryValue* value) { |
| 149 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 150 fileapi::AsyncFileUtil::EntryList entry_list; |
| 151 |
| 152 if (!value) { |
| 153 TriggerCallback(base::File::FILE_ERROR_FAILED, |
| 154 fileapi::AsyncFileUtil::EntryList(), false); |
| 155 return; |
| 156 } |
| 157 |
| 158 const base::ListValue* entries; |
| 159 if (!value->GetList(kPrivetListEntries, &entries)) { |
| 160 TriggerCallback(base::File::FILE_ERROR_FAILED, |
| 161 fileapi::AsyncFileUtil::EntryList(), false); |
| 162 return; |
| 163 } |
| 164 |
| 165 for (size_t i = 0; i < entries->GetSize(); i++) { |
| 166 const base::DictionaryValue* entry_value; |
| 167 if (!entries->GetDictionary(i, &entry_value)) { |
| 168 TriggerCallback(base::File::FILE_ERROR_FAILED, |
| 169 fileapi::AsyncFileUtil::EntryList(), false); |
| 170 return; |
| 171 } |
| 172 |
| 173 std::string name; |
| 174 std::string type; |
| 175 int size = 0; |
| 176 |
| 177 entry_value->GetString(kPrivetListKeyName, &name); |
| 178 entry_value->GetString(kPrivetListKeyType, &type); |
| 179 entry_value->GetInteger(kPrivetListKeySize, &size); |
| 180 |
| 181 fileapi::DirectoryEntry entry( |
| 182 name, |
| 183 (type == kPrivetListTypeDir) ? |
| 184 fileapi::DirectoryEntry::DIRECTORY : fileapi::DirectoryEntry::FILE, |
| 185 size, |
| 186 base::Time() /* TODO(noamsml) */); |
| 187 |
| 188 entry_list.push_back(entry); |
| 189 } |
| 190 |
| 191 TriggerCallback(base::File::FILE_OK, entry_list, false); |
| 192 } |
| 193 |
| 194 |
| 195 } // namespace local_discovery |
OLD | NEW |