| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/change_list_loader.h" | 5 #include "chrome/browser/chromeos/drive/change_list_loader.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 class FullFeedFetcher : public ChangeListLoader::FeedFetcher { | 41 class FullFeedFetcher : public ChangeListLoader::FeedFetcher { |
| 42 public: | 42 public: |
| 43 explicit FullFeedFetcher(JobScheduler* scheduler) | 43 explicit FullFeedFetcher(JobScheduler* scheduler) |
| 44 : scheduler_(scheduler), | 44 : scheduler_(scheduler), |
| 45 weak_ptr_factory_(this) { | 45 weak_ptr_factory_(this) { |
| 46 } | 46 } |
| 47 | 47 |
| 48 ~FullFeedFetcher() override {} | 48 ~FullFeedFetcher() override {} |
| 49 | 49 |
| 50 void Run(const FeedFetcherCallback& callback) override { | 50 void Run(const FeedFetcherCallback& callback) override { |
| 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 51 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 52 DCHECK(!callback.is_null()); | 52 DCHECK(!callback.is_null()); |
| 53 | 53 |
| 54 // Remember the time stamp for usage stats. | 54 // Remember the time stamp for usage stats. |
| 55 start_time_ = base::TimeTicks::Now(); | 55 start_time_ = base::TimeTicks::Now(); |
| 56 | 56 |
| 57 // This is full resource list fetch. | 57 // This is full resource list fetch. |
| 58 scheduler_->GetAllFileList( | 58 scheduler_->GetAllFileList( |
| 59 base::Bind(&FullFeedFetcher::OnFileListFetched, | 59 base::Bind(&FullFeedFetcher::OnFileListFetched, |
| 60 weak_ptr_factory_.GetWeakPtr(), callback)); | 60 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 61 } | 61 } |
| 62 | 62 |
| 63 private: | 63 private: |
| 64 void OnFileListFetched(const FeedFetcherCallback& callback, | 64 void OnFileListFetched(const FeedFetcherCallback& callback, |
| 65 google_apis::DriveApiErrorCode status, | 65 google_apis::DriveApiErrorCode status, |
| 66 scoped_ptr<google_apis::FileList> file_list) { | 66 scoped_ptr<google_apis::FileList> file_list) { |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 67 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 68 DCHECK(!callback.is_null()); | 68 DCHECK(!callback.is_null()); |
| 69 | 69 |
| 70 FileError error = GDataToFileError(status); | 70 FileError error = GDataToFileError(status); |
| 71 if (error != FILE_ERROR_OK) { | 71 if (error != FILE_ERROR_OK) { |
| 72 callback.Run(error, ScopedVector<ChangeList>()); | 72 callback.Run(error, ScopedVector<ChangeList>()); |
| 73 return; | 73 return; |
| 74 } | 74 } |
| 75 | 75 |
| 76 DCHECK(file_list); | 76 DCHECK(file_list); |
| 77 change_lists_.push_back(new ChangeList(*file_list)); | 77 change_lists_.push_back(new ChangeList(*file_list)); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 106 public: | 106 public: |
| 107 DeltaFeedFetcher(JobScheduler* scheduler, int64 start_change_id) | 107 DeltaFeedFetcher(JobScheduler* scheduler, int64 start_change_id) |
| 108 : scheduler_(scheduler), | 108 : scheduler_(scheduler), |
| 109 start_change_id_(start_change_id), | 109 start_change_id_(start_change_id), |
| 110 weak_ptr_factory_(this) { | 110 weak_ptr_factory_(this) { |
| 111 } | 111 } |
| 112 | 112 |
| 113 ~DeltaFeedFetcher() override {} | 113 ~DeltaFeedFetcher() override {} |
| 114 | 114 |
| 115 void Run(const FeedFetcherCallback& callback) override { | 115 void Run(const FeedFetcherCallback& callback) override { |
| 116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 116 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 117 DCHECK(!callback.is_null()); | 117 DCHECK(!callback.is_null()); |
| 118 | 118 |
| 119 scheduler_->GetChangeList( | 119 scheduler_->GetChangeList( |
| 120 start_change_id_, | 120 start_change_id_, |
| 121 base::Bind(&DeltaFeedFetcher::OnChangeListFetched, | 121 base::Bind(&DeltaFeedFetcher::OnChangeListFetched, |
| 122 weak_ptr_factory_.GetWeakPtr(), callback)); | 122 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 123 } | 123 } |
| 124 | 124 |
| 125 private: | 125 private: |
| 126 void OnChangeListFetched(const FeedFetcherCallback& callback, | 126 void OnChangeListFetched(const FeedFetcherCallback& callback, |
| 127 google_apis::DriveApiErrorCode status, | 127 google_apis::DriveApiErrorCode status, |
| 128 scoped_ptr<google_apis::ChangeList> change_list) { | 128 scoped_ptr<google_apis::ChangeList> change_list) { |
| 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 129 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 130 DCHECK(!callback.is_null()); | 130 DCHECK(!callback.is_null()); |
| 131 | 131 |
| 132 FileError error = GDataToFileError(status); | 132 FileError error = GDataToFileError(status); |
| 133 if (error != FILE_ERROR_OK) { | 133 if (error != FILE_ERROR_OK) { |
| 134 callback.Run(error, ScopedVector<ChangeList>()); | 134 callback.Run(error, ScopedVector<ChangeList>()); |
| 135 return; | 135 return; |
| 136 } | 136 } |
| 137 | 137 |
| 138 DCHECK(change_list); | 138 DCHECK(change_list); |
| 139 change_lists_.push_back(new ChangeList(*change_list)); | 139 change_lists_.push_back(new ChangeList(*change_list)); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 158 ScopedVector<ChangeList> change_lists_; | 158 ScopedVector<ChangeList> change_lists_; |
| 159 base::WeakPtrFactory<DeltaFeedFetcher> weak_ptr_factory_; | 159 base::WeakPtrFactory<DeltaFeedFetcher> weak_ptr_factory_; |
| 160 DISALLOW_COPY_AND_ASSIGN(DeltaFeedFetcher); | 160 DISALLOW_COPY_AND_ASSIGN(DeltaFeedFetcher); |
| 161 }; | 161 }; |
| 162 | 162 |
| 163 } // namespace | 163 } // namespace |
| 164 | 164 |
| 165 LoaderController::LoaderController() | 165 LoaderController::LoaderController() |
| 166 : lock_count_(0), | 166 : lock_count_(0), |
| 167 weak_ptr_factory_(this) { | 167 weak_ptr_factory_(this) { |
| 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 168 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 169 } | 169 } |
| 170 | 170 |
| 171 LoaderController::~LoaderController() { | 171 LoaderController::~LoaderController() { |
| 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 172 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 173 } | 173 } |
| 174 | 174 |
| 175 scoped_ptr<base::ScopedClosureRunner> LoaderController::GetLock() { | 175 scoped_ptr<base::ScopedClosureRunner> LoaderController::GetLock() { |
| 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 176 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 177 | 177 |
| 178 ++lock_count_; | 178 ++lock_count_; |
| 179 return make_scoped_ptr(new base::ScopedClosureRunner( | 179 return make_scoped_ptr(new base::ScopedClosureRunner( |
| 180 base::Bind(&LoaderController::Unlock, | 180 base::Bind(&LoaderController::Unlock, |
| 181 weak_ptr_factory_.GetWeakPtr()))); | 181 weak_ptr_factory_.GetWeakPtr()))); |
| 182 } | 182 } |
| 183 | 183 |
| 184 void LoaderController::ScheduleRun(const base::Closure& task) { | 184 void LoaderController::ScheduleRun(const base::Closure& task) { |
| 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 185 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 186 DCHECK(!task.is_null()); | 186 DCHECK(!task.is_null()); |
| 187 | 187 |
| 188 if (lock_count_ > 0) { | 188 if (lock_count_ > 0) { |
| 189 pending_tasks_.push_back(task); | 189 pending_tasks_.push_back(task); |
| 190 } else { | 190 } else { |
| 191 task.Run(); | 191 task.Run(); |
| 192 } | 192 } |
| 193 } | 193 } |
| 194 | 194 |
| 195 void LoaderController::Unlock() { | 195 void LoaderController::Unlock() { |
| 196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 196 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 197 DCHECK_LT(0, lock_count_); | 197 DCHECK_LT(0, lock_count_); |
| 198 | 198 |
| 199 if (--lock_count_ > 0) | 199 if (--lock_count_ > 0) |
| 200 return; | 200 return; |
| 201 | 201 |
| 202 std::vector<base::Closure> tasks; | 202 std::vector<base::Closure> tasks; |
| 203 tasks.swap(pending_tasks_); | 203 tasks.swap(pending_tasks_); |
| 204 for (size_t i = 0; i < tasks.size(); ++i) | 204 for (size_t i = 0; i < tasks.size(); ++i) |
| 205 tasks[i].Run(); | 205 tasks[i].Run(); |
| 206 } | 206 } |
| 207 | 207 |
| 208 AboutResourceLoader::AboutResourceLoader(JobScheduler* scheduler) | 208 AboutResourceLoader::AboutResourceLoader(JobScheduler* scheduler) |
| 209 : scheduler_(scheduler), | 209 : scheduler_(scheduler), |
| 210 current_update_task_id_(-1), | 210 current_update_task_id_(-1), |
| 211 weak_ptr_factory_(this) { | 211 weak_ptr_factory_(this) { |
| 212 } | 212 } |
| 213 | 213 |
| 214 AboutResourceLoader::~AboutResourceLoader() {} | 214 AboutResourceLoader::~AboutResourceLoader() {} |
| 215 | 215 |
| 216 void AboutResourceLoader::GetAboutResource( | 216 void AboutResourceLoader::GetAboutResource( |
| 217 const google_apis::AboutResourceCallback& callback) { | 217 const google_apis::AboutResourceCallback& callback) { |
| 218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 218 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 219 DCHECK(!callback.is_null()); | 219 DCHECK(!callback.is_null()); |
| 220 | 220 |
| 221 // If the latest UpdateAboutResource task is still running. Wait for it, | 221 // If the latest UpdateAboutResource task is still running. Wait for it, |
| 222 if (pending_callbacks_.count(current_update_task_id_)) { | 222 if (pending_callbacks_.count(current_update_task_id_)) { |
| 223 pending_callbacks_[current_update_task_id_].push_back(callback); | 223 pending_callbacks_[current_update_task_id_].push_back(callback); |
| 224 return; | 224 return; |
| 225 } | 225 } |
| 226 | 226 |
| 227 if (cached_about_resource_) { | 227 if (cached_about_resource_) { |
| 228 base::MessageLoopProxy::current()->PostTask( | 228 base::MessageLoopProxy::current()->PostTask( |
| 229 FROM_HERE, | 229 FROM_HERE, |
| 230 base::Bind( | 230 base::Bind( |
| 231 callback, | 231 callback, |
| 232 google_apis::HTTP_NO_CONTENT, | 232 google_apis::HTTP_NO_CONTENT, |
| 233 base::Passed(scoped_ptr<google_apis::AboutResource>( | 233 base::Passed(scoped_ptr<google_apis::AboutResource>( |
| 234 new google_apis::AboutResource(*cached_about_resource_))))); | 234 new google_apis::AboutResource(*cached_about_resource_))))); |
| 235 } else { | 235 } else { |
| 236 UpdateAboutResource(callback); | 236 UpdateAboutResource(callback); |
| 237 } | 237 } |
| 238 } | 238 } |
| 239 | 239 |
| 240 void AboutResourceLoader::UpdateAboutResource( | 240 void AboutResourceLoader::UpdateAboutResource( |
| 241 const google_apis::AboutResourceCallback& callback) { | 241 const google_apis::AboutResourceCallback& callback) { |
| 242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 242 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 243 DCHECK(!callback.is_null()); | 243 DCHECK(!callback.is_null()); |
| 244 | 244 |
| 245 ++current_update_task_id_; | 245 ++current_update_task_id_; |
| 246 pending_callbacks_[current_update_task_id_].push_back(callback); | 246 pending_callbacks_[current_update_task_id_].push_back(callback); |
| 247 | 247 |
| 248 scheduler_->GetAboutResource( | 248 scheduler_->GetAboutResource( |
| 249 base::Bind(&AboutResourceLoader::UpdateAboutResourceAfterGetAbout, | 249 base::Bind(&AboutResourceLoader::UpdateAboutResourceAfterGetAbout, |
| 250 weak_ptr_factory_.GetWeakPtr(), | 250 weak_ptr_factory_.GetWeakPtr(), |
| 251 current_update_task_id_)); | 251 current_update_task_id_)); |
| 252 } | 252 } |
| 253 | 253 |
| 254 void AboutResourceLoader::UpdateAboutResourceAfterGetAbout( | 254 void AboutResourceLoader::UpdateAboutResourceAfterGetAbout( |
| 255 int task_id, | 255 int task_id, |
| 256 google_apis::DriveApiErrorCode status, | 256 google_apis::DriveApiErrorCode status, |
| 257 scoped_ptr<google_apis::AboutResource> about_resource) { | 257 scoped_ptr<google_apis::AboutResource> about_resource) { |
| 258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 258 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 259 FileError error = GDataToFileError(status); | 259 FileError error = GDataToFileError(status); |
| 260 | 260 |
| 261 const std::vector<google_apis::AboutResourceCallback> callbacks = | 261 const std::vector<google_apis::AboutResourceCallback> callbacks = |
| 262 pending_callbacks_[task_id]; | 262 pending_callbacks_[task_id]; |
| 263 pending_callbacks_.erase(task_id); | 263 pending_callbacks_.erase(task_id); |
| 264 | 264 |
| 265 if (error != FILE_ERROR_OK) { | 265 if (error != FILE_ERROR_OK) { |
| 266 for (size_t i = 0; i < callbacks.size(); ++i) | 266 for (size_t i = 0; i < callbacks.size(); ++i) |
| 267 callbacks[i].Run(status, scoped_ptr<google_apis::AboutResource>()); | 267 callbacks[i].Run(status, scoped_ptr<google_apis::AboutResource>()); |
| 268 return; | 268 return; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 ChangeListLoader::~ChangeListLoader() { | 305 ChangeListLoader::~ChangeListLoader() { |
| 306 } | 306 } |
| 307 | 307 |
| 308 bool ChangeListLoader::IsRefreshing() const { | 308 bool ChangeListLoader::IsRefreshing() const { |
| 309 // Callback for change list loading is stored in pending_load_callback_. | 309 // Callback for change list loading is stored in pending_load_callback_. |
| 310 // It is non-empty if and only if there is an in-flight loading operation. | 310 // It is non-empty if and only if there is an in-flight loading operation. |
| 311 return !pending_load_callback_.empty(); | 311 return !pending_load_callback_.empty(); |
| 312 } | 312 } |
| 313 | 313 |
| 314 void ChangeListLoader::AddObserver(ChangeListLoaderObserver* observer) { | 314 void ChangeListLoader::AddObserver(ChangeListLoaderObserver* observer) { |
| 315 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 315 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 316 observers_.AddObserver(observer); | 316 observers_.AddObserver(observer); |
| 317 } | 317 } |
| 318 | 318 |
| 319 void ChangeListLoader::RemoveObserver(ChangeListLoaderObserver* observer) { | 319 void ChangeListLoader::RemoveObserver(ChangeListLoaderObserver* observer) { |
| 320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 320 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 321 observers_.RemoveObserver(observer); | 321 observers_.RemoveObserver(observer); |
| 322 } | 322 } |
| 323 | 323 |
| 324 void ChangeListLoader::CheckForUpdates(const FileOperationCallback& callback) { | 324 void ChangeListLoader::CheckForUpdates(const FileOperationCallback& callback) { |
| 325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 325 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 326 DCHECK(!callback.is_null()); | 326 DCHECK(!callback.is_null()); |
| 327 | 327 |
| 328 // We only start to check for updates iff the load is done. | 328 // We only start to check for updates iff the load is done. |
| 329 // I.e., we ignore checking updates if not loaded to avoid starting the | 329 // I.e., we ignore checking updates if not loaded to avoid starting the |
| 330 // load without user's explicit interaction (such as opening Drive). | 330 // load without user's explicit interaction (such as opening Drive). |
| 331 if (!loaded_ && !IsRefreshing()) | 331 if (!loaded_ && !IsRefreshing()) |
| 332 return; | 332 return; |
| 333 | 333 |
| 334 // For each CheckForUpdates() request, always refresh the changestamp info. | 334 // For each CheckForUpdates() request, always refresh the changestamp info. |
| 335 about_resource_loader_->UpdateAboutResource( | 335 about_resource_loader_->UpdateAboutResource( |
| 336 base::Bind(&ChangeListLoader::OnAboutResourceUpdated, | 336 base::Bind(&ChangeListLoader::OnAboutResourceUpdated, |
| 337 weak_ptr_factory_.GetWeakPtr())); | 337 weak_ptr_factory_.GetWeakPtr())); |
| 338 | 338 |
| 339 if (IsRefreshing()) { | 339 if (IsRefreshing()) { |
| 340 // There is in-flight loading. So keep the callback here, and check for | 340 // There is in-flight loading. So keep the callback here, and check for |
| 341 // updates when the in-flight loading is completed. | 341 // updates when the in-flight loading is completed. |
| 342 pending_update_check_callback_ = callback; | 342 pending_update_check_callback_ = callback; |
| 343 return; | 343 return; |
| 344 } | 344 } |
| 345 | 345 |
| 346 DCHECK(loaded_); | 346 DCHECK(loaded_); |
| 347 logger_->Log(logging::LOG_INFO, "Checking for updates"); | 347 logger_->Log(logging::LOG_INFO, "Checking for updates"); |
| 348 Load(callback); | 348 Load(callback); |
| 349 } | 349 } |
| 350 | 350 |
| 351 void ChangeListLoader::LoadIfNeeded(const FileOperationCallback& callback) { | 351 void ChangeListLoader::LoadIfNeeded(const FileOperationCallback& callback) { |
| 352 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 352 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 353 DCHECK(!callback.is_null()); | 353 DCHECK(!callback.is_null()); |
| 354 | 354 |
| 355 // If the metadata is not yet loaded, start loading. | 355 // If the metadata is not yet loaded, start loading. |
| 356 if (!loaded_ && !IsRefreshing()) | 356 if (!loaded_ && !IsRefreshing()) |
| 357 Load(callback); | 357 Load(callback); |
| 358 } | 358 } |
| 359 | 359 |
| 360 void ChangeListLoader::Load(const FileOperationCallback& callback) { | 360 void ChangeListLoader::Load(const FileOperationCallback& callback) { |
| 361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 361 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 362 DCHECK(!callback.is_null()); | 362 DCHECK(!callback.is_null()); |
| 363 | 363 |
| 364 // Check if this is the first time this ChangeListLoader do loading. | 364 // Check if this is the first time this ChangeListLoader do loading. |
| 365 // Note: IsRefreshing() depends on pending_load_callback_ so check in advance. | 365 // Note: IsRefreshing() depends on pending_load_callback_ so check in advance. |
| 366 const bool is_initial_load = (!loaded_ && !IsRefreshing()); | 366 const bool is_initial_load = (!loaded_ && !IsRefreshing()); |
| 367 | 367 |
| 368 // Register the callback function to be called when it is loaded. | 368 // Register the callback function to be called when it is loaded. |
| 369 pending_load_callback_.push_back(callback); | 369 pending_load_callback_.push_back(callback); |
| 370 | 370 |
| 371 // If loading task is already running, do nothing. | 371 // If loading task is already running, do nothing. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 383 base::Bind(&ChangeListLoader::LoadAfterGetLargestChangestamp, | 383 base::Bind(&ChangeListLoader::LoadAfterGetLargestChangestamp, |
| 384 weak_ptr_factory_.GetWeakPtr(), | 384 weak_ptr_factory_.GetWeakPtr(), |
| 385 is_initial_load, | 385 is_initial_load, |
| 386 base::Owned(local_changestamp))); | 386 base::Owned(local_changestamp))); |
| 387 } | 387 } |
| 388 | 388 |
| 389 void ChangeListLoader::LoadAfterGetLargestChangestamp( | 389 void ChangeListLoader::LoadAfterGetLargestChangestamp( |
| 390 bool is_initial_load, | 390 bool is_initial_load, |
| 391 const int64* local_changestamp, | 391 const int64* local_changestamp, |
| 392 FileError error) { | 392 FileError error) { |
| 393 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 393 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 394 | 394 |
| 395 if (error != FILE_ERROR_OK) { | 395 if (error != FILE_ERROR_OK) { |
| 396 OnChangeListLoadComplete(error); | 396 OnChangeListLoadComplete(error); |
| 397 return; | 397 return; |
| 398 } | 398 } |
| 399 | 399 |
| 400 if (is_initial_load && *local_changestamp > 0) { | 400 if (is_initial_load && *local_changestamp > 0) { |
| 401 // The local data is usable. Flush callbacks to tell loading was successful. | 401 // The local data is usable. Flush callbacks to tell loading was successful. |
| 402 OnChangeListLoadComplete(FILE_ERROR_OK); | 402 OnChangeListLoadComplete(FILE_ERROR_OK); |
| 403 | 403 |
| 404 // Continues to load from server in background. | 404 // Continues to load from server in background. |
| 405 // Put dummy callbacks to indicate that fetching is still continuing. | 405 // Put dummy callbacks to indicate that fetching is still continuing. |
| 406 pending_load_callback_.push_back( | 406 pending_load_callback_.push_back( |
| 407 base::Bind(&util::EmptyFileOperationCallback)); | 407 base::Bind(&util::EmptyFileOperationCallback)); |
| 408 } | 408 } |
| 409 | 409 |
| 410 about_resource_loader_->GetAboutResource( | 410 about_resource_loader_->GetAboutResource( |
| 411 base::Bind(&ChangeListLoader::LoadAfterGetAboutResource, | 411 base::Bind(&ChangeListLoader::LoadAfterGetAboutResource, |
| 412 weak_ptr_factory_.GetWeakPtr(), | 412 weak_ptr_factory_.GetWeakPtr(), |
| 413 *local_changestamp)); | 413 *local_changestamp)); |
| 414 } | 414 } |
| 415 | 415 |
| 416 void ChangeListLoader::LoadAfterGetAboutResource( | 416 void ChangeListLoader::LoadAfterGetAboutResource( |
| 417 int64 local_changestamp, | 417 int64 local_changestamp, |
| 418 google_apis::DriveApiErrorCode status, | 418 google_apis::DriveApiErrorCode status, |
| 419 scoped_ptr<google_apis::AboutResource> about_resource) { | 419 scoped_ptr<google_apis::AboutResource> about_resource) { |
| 420 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 420 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 421 | 421 |
| 422 FileError error = GDataToFileError(status); | 422 FileError error = GDataToFileError(status); |
| 423 if (error != FILE_ERROR_OK) { | 423 if (error != FILE_ERROR_OK) { |
| 424 OnChangeListLoadComplete(error); | 424 OnChangeListLoadComplete(error); |
| 425 return; | 425 return; |
| 426 } | 426 } |
| 427 | 427 |
| 428 DCHECK(about_resource); | 428 DCHECK(about_resource); |
| 429 | 429 |
| 430 int64 remote_changestamp = about_resource->largest_change_id(); | 430 int64 remote_changestamp = about_resource->largest_change_id(); |
| 431 int64 start_changestamp = local_changestamp > 0 ? local_changestamp + 1 : 0; | 431 int64 start_changestamp = local_changestamp > 0 ? local_changestamp + 1 : 0; |
| 432 if (local_changestamp >= remote_changestamp) { | 432 if (local_changestamp >= remote_changestamp) { |
| 433 if (local_changestamp > remote_changestamp) { | 433 if (local_changestamp > remote_changestamp) { |
| 434 LOG(WARNING) << "Local resource metadata is fresher than server, " | 434 LOG(WARNING) << "Local resource metadata is fresher than server, " |
| 435 << "local = " << local_changestamp | 435 << "local = " << local_changestamp |
| 436 << ", server = " << remote_changestamp; | 436 << ", server = " << remote_changestamp; |
| 437 } | 437 } |
| 438 | 438 |
| 439 // No changes detected, tell the client that the loading was successful. | 439 // No changes detected, tell the client that the loading was successful. |
| 440 OnChangeListLoadComplete(FILE_ERROR_OK); | 440 OnChangeListLoadComplete(FILE_ERROR_OK); |
| 441 } else { | 441 } else { |
| 442 // Start loading the change list. | 442 // Start loading the change list. |
| 443 LoadChangeListFromServer(start_changestamp); | 443 LoadChangeListFromServer(start_changestamp); |
| 444 } | 444 } |
| 445 } | 445 } |
| 446 | 446 |
| 447 void ChangeListLoader::OnChangeListLoadComplete(FileError error) { | 447 void ChangeListLoader::OnChangeListLoadComplete(FileError error) { |
| 448 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 448 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 449 | 449 |
| 450 if (!loaded_ && error == FILE_ERROR_OK) { | 450 if (!loaded_ && error == FILE_ERROR_OK) { |
| 451 loaded_ = true; | 451 loaded_ = true; |
| 452 FOR_EACH_OBSERVER(ChangeListLoaderObserver, | 452 FOR_EACH_OBSERVER(ChangeListLoaderObserver, |
| 453 observers_, | 453 observers_, |
| 454 OnInitialLoadComplete()); | 454 OnInitialLoadComplete()); |
| 455 } | 455 } |
| 456 | 456 |
| 457 for (size_t i = 0; i < pending_load_callback_.size(); ++i) { | 457 for (size_t i = 0; i < pending_load_callback_.size(); ++i) { |
| 458 base::MessageLoopProxy::current()->PostTask( | 458 base::MessageLoopProxy::current()->PostTask( |
| 459 FROM_HERE, | 459 FROM_HERE, |
| 460 base::Bind(pending_load_callback_[i], error)); | 460 base::Bind(pending_load_callback_[i], error)); |
| 461 } | 461 } |
| 462 pending_load_callback_.clear(); | 462 pending_load_callback_.clear(); |
| 463 | 463 |
| 464 // If there is pending update check, try to load the change from the server | 464 // If there is pending update check, try to load the change from the server |
| 465 // again, because there may exist an update during the completed loading. | 465 // again, because there may exist an update during the completed loading. |
| 466 if (!pending_update_check_callback_.is_null()) { | 466 if (!pending_update_check_callback_.is_null()) { |
| 467 Load(base::ResetAndReturn(&pending_update_check_callback_)); | 467 Load(base::ResetAndReturn(&pending_update_check_callback_)); |
| 468 } | 468 } |
| 469 } | 469 } |
| 470 | 470 |
| 471 void ChangeListLoader::OnAboutResourceUpdated( | 471 void ChangeListLoader::OnAboutResourceUpdated( |
| 472 google_apis::DriveApiErrorCode error, | 472 google_apis::DriveApiErrorCode error, |
| 473 scoped_ptr<google_apis::AboutResource> resource) { | 473 scoped_ptr<google_apis::AboutResource> resource) { |
| 474 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 474 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 475 | 475 |
| 476 if (drive::GDataToFileError(error) != drive::FILE_ERROR_OK) { | 476 if (drive::GDataToFileError(error) != drive::FILE_ERROR_OK) { |
| 477 logger_->Log(logging::LOG_ERROR, | 477 logger_->Log(logging::LOG_ERROR, |
| 478 "Failed to update the about resource: %s", | 478 "Failed to update the about resource: %s", |
| 479 google_apis::DriveApiErrorCodeToString(error).c_str()); | 479 google_apis::DriveApiErrorCodeToString(error).c_str()); |
| 480 return; | 480 return; |
| 481 } | 481 } |
| 482 logger_->Log(logging::LOG_INFO, | 482 logger_->Log(logging::LOG_INFO, |
| 483 "About resource updated to: %s", | 483 "About resource updated to: %s", |
| 484 base::Int64ToString(resource->largest_change_id()).c_str()); | 484 base::Int64ToString(resource->largest_change_id()).c_str()); |
| 485 } | 485 } |
| 486 | 486 |
| 487 void ChangeListLoader::LoadChangeListFromServer(int64 start_changestamp) { | 487 void ChangeListLoader::LoadChangeListFromServer(int64 start_changestamp) { |
| 488 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 488 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 489 DCHECK(!change_feed_fetcher_); | 489 DCHECK(!change_feed_fetcher_); |
| 490 DCHECK(about_resource_loader_->cached_about_resource()); | 490 DCHECK(about_resource_loader_->cached_about_resource()); |
| 491 | 491 |
| 492 bool is_delta_update = start_changestamp != 0; | 492 bool is_delta_update = start_changestamp != 0; |
| 493 | 493 |
| 494 // Set up feed fetcher. | 494 // Set up feed fetcher. |
| 495 if (is_delta_update) { | 495 if (is_delta_update) { |
| 496 change_feed_fetcher_.reset( | 496 change_feed_fetcher_.reset( |
| 497 new DeltaFeedFetcher(scheduler_, start_changestamp)); | 497 new DeltaFeedFetcher(scheduler_, start_changestamp)); |
| 498 } else { | 498 } else { |
| 499 change_feed_fetcher_.reset(new FullFeedFetcher(scheduler_)); | 499 change_feed_fetcher_.reset(new FullFeedFetcher(scheduler_)); |
| 500 } | 500 } |
| 501 | 501 |
| 502 // Make a copy of cached_about_resource_ to remember at which changestamp we | 502 // Make a copy of cached_about_resource_ to remember at which changestamp we |
| 503 // are fetching change list. | 503 // are fetching change list. |
| 504 change_feed_fetcher_->Run( | 504 change_feed_fetcher_->Run( |
| 505 base::Bind(&ChangeListLoader::LoadChangeListFromServerAfterLoadChangeList, | 505 base::Bind(&ChangeListLoader::LoadChangeListFromServerAfterLoadChangeList, |
| 506 weak_ptr_factory_.GetWeakPtr(), | 506 weak_ptr_factory_.GetWeakPtr(), |
| 507 base::Passed(make_scoped_ptr(new google_apis::AboutResource( | 507 base::Passed(make_scoped_ptr(new google_apis::AboutResource( |
| 508 *about_resource_loader_->cached_about_resource()))), | 508 *about_resource_loader_->cached_about_resource()))), |
| 509 is_delta_update)); | 509 is_delta_update)); |
| 510 } | 510 } |
| 511 | 511 |
| 512 void ChangeListLoader::LoadChangeListFromServerAfterLoadChangeList( | 512 void ChangeListLoader::LoadChangeListFromServerAfterLoadChangeList( |
| 513 scoped_ptr<google_apis::AboutResource> about_resource, | 513 scoped_ptr<google_apis::AboutResource> about_resource, |
| 514 bool is_delta_update, | 514 bool is_delta_update, |
| 515 FileError error, | 515 FileError error, |
| 516 ScopedVector<ChangeList> change_lists) { | 516 ScopedVector<ChangeList> change_lists) { |
| 517 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 517 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 518 DCHECK(about_resource); | 518 DCHECK(about_resource); |
| 519 | 519 |
| 520 // Delete the fetcher first. | 520 // Delete the fetcher first. |
| 521 change_feed_fetcher_.reset(); | 521 change_feed_fetcher_.reset(); |
| 522 | 522 |
| 523 if (error != FILE_ERROR_OK) { | 523 if (error != FILE_ERROR_OK) { |
| 524 OnChangeListLoadComplete(error); | 524 OnChangeListLoadComplete(error); |
| 525 return; | 525 return; |
| 526 } | 526 } |
| 527 | 527 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 549 base::Owned(change_list_processor), | 549 base::Owned(change_list_processor), |
| 550 should_notify_changed_directories, | 550 should_notify_changed_directories, |
| 551 base::Time::Now()))); | 551 base::Time::Now()))); |
| 552 } | 552 } |
| 553 | 553 |
| 554 void ChangeListLoader::LoadChangeListFromServerAfterUpdate( | 554 void ChangeListLoader::LoadChangeListFromServerAfterUpdate( |
| 555 ChangeListProcessor* change_list_processor, | 555 ChangeListProcessor* change_list_processor, |
| 556 bool should_notify_changed_directories, | 556 bool should_notify_changed_directories, |
| 557 const base::Time& start_time, | 557 const base::Time& start_time, |
| 558 FileError error) { | 558 FileError error) { |
| 559 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 559 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 560 | 560 |
| 561 const base::TimeDelta elapsed = base::Time::Now() - start_time; | 561 const base::TimeDelta elapsed = base::Time::Now() - start_time; |
| 562 logger_->Log(logging::LOG_INFO, | 562 logger_->Log(logging::LOG_INFO, |
| 563 "Change lists applied (elapsed time: %sms)", | 563 "Change lists applied (elapsed time: %sms)", |
| 564 base::Int64ToString(elapsed.InMilliseconds()).c_str()); | 564 base::Int64ToString(elapsed.InMilliseconds()).c_str()); |
| 565 | 565 |
| 566 if (should_notify_changed_directories) { | 566 if (should_notify_changed_directories) { |
| 567 FOR_EACH_OBSERVER(ChangeListLoaderObserver, | 567 FOR_EACH_OBSERVER(ChangeListLoaderObserver, |
| 568 observers_, | 568 observers_, |
| 569 OnFileChanged(change_list_processor->changed_files())); | 569 OnFileChanged(change_list_processor->changed_files())); |
| 570 } | 570 } |
| 571 | 571 |
| 572 OnChangeListLoadComplete(error); | 572 OnChangeListLoadComplete(error); |
| 573 | 573 |
| 574 FOR_EACH_OBSERVER(ChangeListLoaderObserver, | 574 FOR_EACH_OBSERVER(ChangeListLoaderObserver, |
| 575 observers_, | 575 observers_, |
| 576 OnLoadFromServerComplete()); | 576 OnLoadFromServerComplete()); |
| 577 } | 577 } |
| 578 | 578 |
| 579 } // namespace internal | 579 } // namespace internal |
| 580 } // namespace drive | 580 } // namespace drive |
| OLD | NEW |