| 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/search_operation.h" | 5 #include "chrome/browser/chromeos/drive/file_system/search_operation.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/task_runner_util.h" | 13 #include "base/task_runner_util.h" |
| 14 #include "chrome/browser/chromeos/drive/change_list_loader.h" | 14 #include "chrome/browser/chromeos/drive/change_list_loader.h" |
| 15 #include "chrome/browser/chromeos/drive/file_system_util.h" | 15 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 16 #include "chrome/browser/chromeos/drive/job_scheduler.h" | 16 #include "chrome/browser/chromeos/drive/job_scheduler.h" |
| 17 #include "chrome/browser/chromeos/drive/resource_entry_conversion.h" | 17 #include "chrome/browser/chromeos/drive/resource_entry_conversion.h" |
| 18 #include "chrome/browser/chromeos/drive/resource_metadata.h" | 18 #include "chrome/browser/chromeos/drive/resource_metadata.h" |
| 19 #include "chrome/browser/drive/drive_api_util.h" |
| 19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 21 #include "google_apis/drive/drive_api_parser.h" |
| 20 #include "google_apis/drive/gdata_wapi_parser.h" | 22 #include "google_apis/drive/gdata_wapi_parser.h" |
| 21 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 22 | 24 |
| 23 using content::BrowserThread; | 25 using content::BrowserThread; |
| 24 | 26 |
| 25 namespace drive { | 27 namespace drive { |
| 26 namespace file_system { | 28 namespace file_system { |
| 27 namespace { | 29 namespace { |
| 28 | 30 |
| 29 // Computes the path of each item in |resource_list| returned from the server | 31 // Computes the path of each item in |file_list| returned from the server |
| 30 // and stores to |result|, by using |resource_metadata|. If the metadata is not | 32 // and stores to |result|, by using |resource_metadata|. If the metadata is not |
| 31 // up-to-date and did not contain an item, adds the item to "drive/other" for | 33 // up-to-date and did not contain an item, adds the item to "drive/other" for |
| 32 // temporally assigning a path. | 34 // temporally assigning a path. |
| 33 FileError ResolveSearchResultOnBlockingPool( | 35 FileError ResolveSearchResultOnBlockingPool( |
| 34 internal::ResourceMetadata* resource_metadata, | 36 internal::ResourceMetadata* resource_metadata, |
| 35 scoped_ptr<google_apis::ResourceList> resource_list, | 37 scoped_ptr<google_apis::FileList> file_list, |
| 36 std::vector<SearchResultInfo>* result) { | 38 std::vector<SearchResultInfo>* result) { |
| 37 DCHECK(resource_metadata); | 39 DCHECK(resource_metadata); |
| 38 DCHECK(result); | 40 DCHECK(result); |
| 39 | 41 |
| 40 const ScopedVector<google_apis::ResourceEntry>& entries = | 42 const ScopedVector<google_apis::FileResource>& entries = file_list->items(); |
| 41 resource_list->entries(); | |
| 42 result->reserve(entries.size()); | 43 result->reserve(entries.size()); |
| 43 for (size_t i = 0; i < entries.size(); ++i) { | 44 for (size_t i = 0; i < entries.size(); ++i) { |
| 44 std::string local_id; | 45 std::string local_id; |
| 45 FileError error = resource_metadata->GetIdByResourceId( | 46 FileError error = resource_metadata->GetIdByResourceId( |
| 46 entries[i]->resource_id(), &local_id); | 47 entries[i]->file_id(), &local_id); |
| 47 | 48 |
| 48 ResourceEntry entry; | 49 ResourceEntry entry; |
| 49 if (error == FILE_ERROR_OK) | 50 if (error == FILE_ERROR_OK) |
| 50 error = resource_metadata->GetResourceEntryById(local_id, &entry); | 51 error = resource_metadata->GetResourceEntryById(local_id, &entry); |
| 51 | 52 |
| 52 if (error == FILE_ERROR_NOT_FOUND) { | 53 if (error == FILE_ERROR_NOT_FOUND) { |
| 53 std::string original_parent_id; | 54 std::string original_parent_id; |
| 54 if (!ConvertToResourceEntry(*entries[i], &entry, &original_parent_id)) | 55 if (!ConvertToResourceEntry( |
| 56 *util::ConvertFileResourceToResourceEntry(*entries[i]), |
| 57 &entry, &original_parent_id)) |
| 55 continue; // Skip non-file entries. | 58 continue; // Skip non-file entries. |
| 56 | 59 |
| 57 // The result is absent in local resource metadata. This can happen if | 60 // The result is absent in local resource metadata. This can happen if |
| 58 // the metadata is not synced to the latest server state yet. In that | 61 // the metadata is not synced to the latest server state yet. In that |
| 59 // case, we temporarily add the file to the special "drive/other" | 62 // case, we temporarily add the file to the special "drive/other" |
| 60 // directory in order to assign a path, which is needed to access the | 63 // directory in order to assign a path, which is needed to access the |
| 61 // file through FileSystem API. | 64 // file through FileSystem API. |
| 62 // | 65 // |
| 63 // It will be moved to the right place when the metadata gets synced | 66 // It will be moved to the right place when the metadata gets synced |
| 64 // in normal loading process in ChangeListProcessor. | 67 // in normal loading process in ChangeListProcessor. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 void SearchOperation::Search(const std::string& search_query, | 100 void SearchOperation::Search(const std::string& search_query, |
| 98 const GURL& next_link, | 101 const GURL& next_link, |
| 99 const SearchCallback& callback) { | 102 const SearchCallback& callback) { |
| 100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 101 DCHECK(!callback.is_null()); | 104 DCHECK(!callback.is_null()); |
| 102 | 105 |
| 103 if (next_link.is_empty()) { | 106 if (next_link.is_empty()) { |
| 104 // This is first request for the |search_query|. | 107 // This is first request for the |search_query|. |
| 105 scheduler_->Search( | 108 scheduler_->Search( |
| 106 search_query, | 109 search_query, |
| 107 base::Bind(&SearchOperation::SearchAfterGetResourceList, | 110 base::Bind(&SearchOperation::SearchAfterGetFileList, |
| 108 weak_ptr_factory_.GetWeakPtr(), callback)); | 111 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 109 } else { | 112 } else { |
| 110 // There is the remaining result so fetch it. | 113 // There is the remaining result so fetch it. |
| 111 scheduler_->GetRemainingFileList( | 114 scheduler_->GetRemainingFileList( |
| 112 next_link, | 115 next_link, |
| 113 base::Bind(&SearchOperation::SearchAfterGetResourceList, | 116 base::Bind(&SearchOperation::SearchAfterGetFileList, |
| 114 weak_ptr_factory_.GetWeakPtr(), callback)); | 117 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 115 } | 118 } |
| 116 } | 119 } |
| 117 | 120 |
| 118 void SearchOperation::SearchAfterGetResourceList( | 121 void SearchOperation::SearchAfterGetFileList( |
| 119 const SearchCallback& callback, | 122 const SearchCallback& callback, |
| 120 google_apis::GDataErrorCode gdata_error, | 123 google_apis::GDataErrorCode gdata_error, |
| 121 scoped_ptr<google_apis::ResourceList> resource_list) { | 124 scoped_ptr<google_apis::FileList> file_list) { |
| 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 123 DCHECK(!callback.is_null()); | 126 DCHECK(!callback.is_null()); |
| 124 | 127 |
| 125 FileError error = GDataToFileError(gdata_error); | 128 FileError error = GDataToFileError(gdata_error); |
| 126 if (error != FILE_ERROR_OK) { | 129 if (error != FILE_ERROR_OK) { |
| 127 callback.Run(error, GURL(), scoped_ptr<std::vector<SearchResultInfo> >()); | 130 callback.Run(error, GURL(), scoped_ptr<std::vector<SearchResultInfo> >()); |
| 128 return; | 131 return; |
| 129 } | 132 } |
| 130 | 133 |
| 131 DCHECK(resource_list); | 134 DCHECK(file_list); |
| 132 | 135 |
| 133 GURL next_url; | 136 GURL next_url = file_list->next_link(); |
| 134 resource_list->GetNextFeedURL(&next_url); | |
| 135 | 137 |
| 136 scoped_ptr<std::vector<SearchResultInfo> > result( | 138 scoped_ptr<std::vector<SearchResultInfo> > result( |
| 137 new std::vector<SearchResultInfo>); | 139 new std::vector<SearchResultInfo>); |
| 138 if (resource_list->entries().empty()) { | 140 if (file_list->items().empty()) { |
| 139 // Short cut. If the resource entry is empty, we don't need to refresh | 141 // Short cut. If the resource entry is empty, we don't need to refresh |
| 140 // the resource metadata. | 142 // the resource metadata. |
| 141 callback.Run(FILE_ERROR_OK, next_url, result.Pass()); | 143 callback.Run(FILE_ERROR_OK, next_url, result.Pass()); |
| 142 return; | 144 return; |
| 143 } | 145 } |
| 144 | 146 |
| 145 // ResolveSearchResultOnBlockingPool() may add entries newly created on the | 147 // ResolveSearchResultOnBlockingPool() may add entries newly created on the |
| 146 // server to the local metadata. | 148 // server to the local metadata. |
| 147 // This may race with sync tasks so we should ask LoaderController here. | 149 // This may race with sync tasks so we should ask LoaderController here. |
| 148 std::vector<SearchResultInfo>* result_ptr = result.get(); | 150 std::vector<SearchResultInfo>* result_ptr = result.get(); |
| 149 loader_controller_->ScheduleRun(base::Bind( | 151 loader_controller_->ScheduleRun(base::Bind( |
| 150 base::IgnoreResult( | 152 base::IgnoreResult( |
| 151 &base::PostTaskAndReplyWithResult<FileError, FileError>), | 153 &base::PostTaskAndReplyWithResult<FileError, FileError>), |
| 152 blocking_task_runner_, | 154 blocking_task_runner_, |
| 153 FROM_HERE, | 155 FROM_HERE, |
| 154 base::Bind(&ResolveSearchResultOnBlockingPool, | 156 base::Bind(&ResolveSearchResultOnBlockingPool, |
| 155 metadata_, | 157 metadata_, |
| 156 base::Passed(&resource_list), | 158 base::Passed(&file_list), |
| 157 result_ptr), | 159 result_ptr), |
| 158 base::Bind(&SearchOperation::SearchAfterResolveSearchResult, | 160 base::Bind(&SearchOperation::SearchAfterResolveSearchResult, |
| 159 weak_ptr_factory_.GetWeakPtr(), | 161 weak_ptr_factory_.GetWeakPtr(), |
| 160 callback, | 162 callback, |
| 161 next_url, | 163 next_url, |
| 162 base::Passed(&result)))); | 164 base::Passed(&result)))); |
| 163 } | 165 } |
| 164 | 166 |
| 165 void SearchOperation::SearchAfterResolveSearchResult( | 167 void SearchOperation::SearchAfterResolveSearchResult( |
| 166 const SearchCallback& callback, | 168 const SearchCallback& callback, |
| 167 const GURL& next_link, | 169 const GURL& next_link, |
| 168 scoped_ptr<std::vector<SearchResultInfo> > result, | 170 scoped_ptr<std::vector<SearchResultInfo> > result, |
| 169 FileError error) { | 171 FileError error) { |
| 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 171 DCHECK(!callback.is_null()); | 173 DCHECK(!callback.is_null()); |
| 172 DCHECK(result); | 174 DCHECK(result); |
| 173 | 175 |
| 174 if (error != FILE_ERROR_OK) { | 176 if (error != FILE_ERROR_OK) { |
| 175 callback.Run(error, GURL(), scoped_ptr<std::vector<SearchResultInfo> >()); | 177 callback.Run(error, GURL(), scoped_ptr<std::vector<SearchResultInfo> >()); |
| 176 return; | 178 return; |
| 177 } | 179 } |
| 178 | 180 |
| 179 callback.Run(error, next_link, result.Pass()); | 181 callback.Run(error, next_link, result.Pass()); |
| 180 } | 182 } |
| 181 | 183 |
| 182 } // namespace file_system | 184 } // namespace file_system |
| 183 } // namespace drive | 185 } // namespace drive |
| OLD | NEW |