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/gdata/gdata_wapi_feed_loader.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_wapi_feed_loader.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 } | 132 } |
133 | 133 |
134 bool UseLevelDB() { | 134 bool UseLevelDB() { |
135 return CommandLine::ForCurrentProcess()->HasSwitch( | 135 return CommandLine::ForCurrentProcess()->HasSwitch( |
136 switches::kUseLevelDBForGData); | 136 switches::kUseLevelDBForGData); |
137 } | 137 } |
138 | 138 |
139 } // namespace | 139 } // namespace |
140 | 140 |
141 LoadRootFeedParams::LoadRootFeedParams( | 141 LoadRootFeedParams::LoadRootFeedParams( |
142 FilePath search_file_path, | |
143 bool should_load_from_server, | 142 bool should_load_from_server, |
144 const FindEntryCallback& callback) | 143 const FileOperationCallback& callback) |
145 : search_file_path(search_file_path), | 144 : should_load_from_server(should_load_from_server), |
146 should_load_from_server(should_load_from_server), | |
147 load_error(GDATA_FILE_OK), | 145 load_error(GDATA_FILE_OK), |
148 load_start_time(base::Time::Now()), | 146 load_start_time(base::Time::Now()), |
149 callback(callback) { | 147 callback(callback) { |
150 } | 148 } |
151 | 149 |
152 LoadRootFeedParams::~LoadRootFeedParams() { | 150 LoadRootFeedParams::~LoadRootFeedParams() { |
153 } | 151 } |
154 | 152 |
155 GetDocumentsParams::GetDocumentsParams( | 153 GetDocumentsParams::GetDocumentsParams( |
156 int64 start_changestamp, | 154 int64 start_changestamp, |
157 int64 root_feed_changestamp, | 155 int64 root_feed_changestamp, |
158 std::vector<DocumentFeed*>* feed_list, | 156 std::vector<DocumentFeed*>* feed_list, |
159 bool should_fetch_multiple_feeds, | 157 bool should_fetch_multiple_feeds, |
160 const FilePath& search_file_path, | |
161 const std::string& search_query, | 158 const std::string& search_query, |
162 const std::string& directory_resource_id, | 159 const std::string& directory_resource_id, |
163 const FindEntryCallback& callback, | 160 const FileOperationCallback& callback, |
164 GetDocumentsUiState* ui_state) | 161 GetDocumentsUiState* ui_state) |
165 : start_changestamp(start_changestamp), | 162 : start_changestamp(start_changestamp), |
166 root_feed_changestamp(root_feed_changestamp), | 163 root_feed_changestamp(root_feed_changestamp), |
167 feed_list(feed_list), | 164 feed_list(feed_list), |
168 should_fetch_multiple_feeds(should_fetch_multiple_feeds), | 165 should_fetch_multiple_feeds(should_fetch_multiple_feeds), |
169 search_file_path(search_file_path), | |
170 search_query(search_query), | 166 search_query(search_query), |
171 directory_resource_id(directory_resource_id), | 167 directory_resource_id(directory_resource_id), |
172 callback(callback), | 168 callback(callback), |
173 ui_state(ui_state) { | 169 ui_state(ui_state) { |
174 } | 170 } |
175 | 171 |
176 GetDocumentsParams::~GetDocumentsParams() { | 172 GetDocumentsParams::~GetDocumentsParams() { |
177 STLDeleteElements(feed_list.get()); | 173 STLDeleteElements(feed_list.get()); |
178 } | 174 } |
179 | 175 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 } | 226 } |
231 | 227 |
232 void GDataWapiFeedLoader::RemoveObserver(Observer* observer) { | 228 void GDataWapiFeedLoader::RemoveObserver(Observer* observer) { |
233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
234 observers_.RemoveObserver(observer); | 230 observers_.RemoveObserver(observer); |
235 } | 231 } |
236 | 232 |
237 void GDataWapiFeedLoader::ReloadFromServerIfNeeded( | 233 void GDataWapiFeedLoader::ReloadFromServerIfNeeded( |
238 ContentOrigin initial_origin, | 234 ContentOrigin initial_origin, |
239 int64 local_changestamp, | 235 int64 local_changestamp, |
240 const FilePath& search_file_path, | 236 const FileOperationCallback& callback) { |
241 const FindEntryCallback& callback) { | |
242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 237 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
243 | 238 |
244 DVLOG(1) << "ReloadFeedFromServerIfNeeded local_changestamp=" | 239 DVLOG(1) << "ReloadFeedFromServerIfNeeded local_changestamp=" |
245 << local_changestamp << ", initial_origin=" << initial_origin; | 240 << local_changestamp << ", initial_origin=" << initial_origin; |
246 | 241 |
247 // First fetch the latest changestamp to see if there were any new changes | 242 // First fetch the latest changestamp to see if there were any new changes |
248 // there at all. | 243 // there at all. |
249 if (gdata::util::IsDriveV2ApiEnabled()) { | 244 if (gdata::util::IsDriveV2ApiEnabled()) { |
250 documents_service_->GetAboutResource( | 245 documents_service_->GetAboutResource( |
251 base::Bind(&GDataWapiFeedLoader::OnGetAboutResource, | 246 base::Bind(&GDataWapiFeedLoader::OnGetAboutResource, |
252 weak_ptr_factory_.GetWeakPtr(), | 247 weak_ptr_factory_.GetWeakPtr(), |
253 initial_origin, | 248 initial_origin, |
254 local_changestamp, | 249 local_changestamp, |
255 search_file_path, | |
256 callback)); | 250 callback)); |
257 return; | 251 return; |
258 } | 252 } |
259 | 253 |
260 documents_service_->GetAccountMetadata( | 254 documents_service_->GetAccountMetadata( |
261 base::Bind(&GDataWapiFeedLoader::OnGetAccountMetadata, | 255 base::Bind(&GDataWapiFeedLoader::OnGetAccountMetadata, |
262 weak_ptr_factory_.GetWeakPtr(), | 256 weak_ptr_factory_.GetWeakPtr(), |
263 initial_origin, | 257 initial_origin, |
264 local_changestamp, | 258 local_changestamp, |
265 search_file_path, | |
266 callback)); | 259 callback)); |
267 } | 260 } |
268 | 261 |
269 void GDataWapiFeedLoader::OnGetAccountMetadata( | 262 void GDataWapiFeedLoader::OnGetAccountMetadata( |
270 ContentOrigin initial_origin, | 263 ContentOrigin initial_origin, |
271 int64 local_changestamp, | 264 int64 local_changestamp, |
272 const FilePath& search_file_path, | 265 const FileOperationCallback& callback, |
273 const FindEntryCallback& callback, | |
274 GDataErrorCode status, | 266 GDataErrorCode status, |
275 scoped_ptr<base::Value> feed_data) { | 267 scoped_ptr<base::Value> feed_data) { |
276 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 268 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
277 | 269 |
278 GDataFileError error = util::GDataToGDataFileError(status); | 270 GDataFileError error = util::GDataToGDataFileError(status); |
279 if (error != GDATA_FILE_OK) { | 271 if (error != GDATA_FILE_OK) { |
280 // Get changes starting from the next changestamp from what we have locally. | 272 // Get changes starting from the next changestamp from what we have locally. |
281 LoadFromServer(initial_origin, | 273 LoadFromServer(initial_origin, |
282 local_changestamp + 1, 0, | 274 local_changestamp + 1, 0, |
283 true, /* should_fetch_multiple_feeds */ | 275 true, /* should_fetch_multiple_feeds */ |
284 search_file_path, | |
285 std::string() /* no search query */, | 276 std::string() /* no search query */, |
286 GURL(), /* feed not explicitly set */ | 277 GURL(), /* feed not explicitly set */ |
287 std::string() /* no directory resource ID */, | 278 std::string() /* no directory resource ID */, |
288 callback, | 279 callback, |
289 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded, | 280 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded, |
290 weak_ptr_factory_.GetWeakPtr())); | 281 weak_ptr_factory_.GetWeakPtr())); |
291 return; | 282 return; |
292 } | 283 } |
293 | 284 |
294 scoped_ptr<AccountMetadataFeed> account_metadata; | 285 scoped_ptr<AccountMetadataFeed> account_metadata; |
295 if (feed_data.get()) { | 286 if (feed_data.get()) { |
296 account_metadata = AccountMetadataFeed::CreateFrom(*feed_data); | 287 account_metadata = AccountMetadataFeed::CreateFrom(*feed_data); |
297 #ifndef NDEBUG | 288 #ifndef NDEBUG |
298 // Save account metadata feed for analysis. | 289 // Save account metadata feed for analysis. |
299 const FilePath path = | 290 const FilePath path = |
300 cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_META).Append( | 291 cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_META).Append( |
301 kAccountMetadataFile); | 292 kAccountMetadataFile); |
302 util::PostBlockingPoolSequencedTask( | 293 util::PostBlockingPoolSequencedTask( |
303 FROM_HERE, | 294 FROM_HERE, |
304 blocking_task_runner_, | 295 blocking_task_runner_, |
305 base::Bind(&SaveFeedOnBlockingPoolForDebugging, | 296 base::Bind(&SaveFeedOnBlockingPoolForDebugging, |
306 path, base::Passed(&feed_data))); | 297 path, base::Passed(&feed_data))); |
307 #endif | 298 #endif |
308 } | 299 } |
309 | 300 |
310 if (!account_metadata.get()) { | 301 if (!account_metadata.get()) { |
311 LoadFromServer(initial_origin, | 302 LoadFromServer(initial_origin, |
312 local_changestamp + 1, 0, | 303 local_changestamp + 1, 0, |
313 true, /* should_fetch_multiple_feeds */ | 304 true, /* should_fetch_multiple_feeds */ |
314 search_file_path, | |
315 std::string() /* no search query */, | 305 std::string() /* no search query */, |
316 GURL(), /* feed not explicitly set */ | 306 GURL(), /* feed not explicitly set */ |
317 std::string() /* no directory resource ID */, | 307 std::string() /* no directory resource ID */, |
318 callback, | 308 callback, |
319 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded, | 309 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded, |
320 weak_ptr_factory_.GetWeakPtr())); | 310 weak_ptr_factory_.GetWeakPtr())); |
321 return; | 311 return; |
322 } | 312 } |
323 | 313 |
324 webapps_registry_->UpdateFromFeed(account_metadata.get()); | 314 webapps_registry_->UpdateFromFeed(account_metadata.get()); |
325 | 315 |
326 bool changes_detected = true; | 316 bool changes_detected = true; |
327 if (local_changestamp >= account_metadata->largest_changestamp()) { | 317 if (local_changestamp >= account_metadata->largest_changestamp()) { |
328 if (local_changestamp > account_metadata->largest_changestamp()) { | 318 if (local_changestamp > account_metadata->largest_changestamp()) { |
329 LOG(WARNING) << "Cached client feed is fresher than server, client = " | 319 LOG(WARNING) << "Cached client feed is fresher than server, client = " |
330 << local_changestamp | 320 << local_changestamp |
331 << ", server = " | 321 << ", server = " |
332 << account_metadata->largest_changestamp(); | 322 << account_metadata->largest_changestamp(); |
333 } | 323 } |
334 // If our cache holds the latest state from the server, change the | 324 // If our cache holds the latest state from the server, change the |
335 // state to FROM_SERVER. | 325 // state to FROM_SERVER. |
336 directory_service_->set_origin( | 326 directory_service_->set_origin( |
337 initial_origin == FROM_CACHE ? FROM_SERVER : initial_origin); | 327 initial_origin == FROM_CACHE ? FROM_SERVER : initial_origin); |
338 changes_detected = false; | 328 changes_detected = false; |
339 } | 329 } |
340 | 330 |
341 // No changes detected, continue with search as planned. | 331 // No changes detected, tell the client that the loading was successful. |
342 if (!changes_detected) { | 332 if (!changes_detected) { |
343 if (!callback.is_null()) { | 333 if (!callback.is_null()) |
344 directory_service_->FindEntryByPathAndRunSync(search_file_path, | 334 callback.Run(GDATA_FILE_OK); |
345 callback); | |
346 } | |
347 return; | 335 return; |
348 } | 336 } |
349 | 337 |
350 // Load changes from the server. | 338 // Load changes from the server. |
351 LoadFromServer(initial_origin, | 339 LoadFromServer(initial_origin, |
352 local_changestamp > 0 ? local_changestamp + 1 : 0, | 340 local_changestamp > 0 ? local_changestamp + 1 : 0, |
353 account_metadata->largest_changestamp(), | 341 account_metadata->largest_changestamp(), |
354 true, /* should_fetch_multiple_feeds */ | 342 true, /* should_fetch_multiple_feeds */ |
355 search_file_path, | |
356 std::string() /* no search query */, | 343 std::string() /* no search query */, |
357 GURL(), /* feed not explicitly set */ | 344 GURL(), /* feed not explicitly set */ |
358 std::string() /* no directory resource ID */, | 345 std::string() /* no directory resource ID */, |
359 callback, | 346 callback, |
360 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded, | 347 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded, |
361 weak_ptr_factory_.GetWeakPtr())); | 348 weak_ptr_factory_.GetWeakPtr())); |
362 } | 349 } |
363 | 350 |
364 void GDataWapiFeedLoader::OnGetAboutResource( | 351 void GDataWapiFeedLoader::OnGetAboutResource( |
365 ContentOrigin initial_origin, | 352 ContentOrigin initial_origin, |
366 int64 local_changestamp, | 353 int64 local_changestamp, |
367 const FilePath& search_file_path, | 354 const FileOperationCallback& callback, |
368 const FindEntryCallback& callback, | |
369 GDataErrorCode status, | 355 GDataErrorCode status, |
370 scoped_ptr<base::Value> feed_data) { | 356 scoped_ptr<base::Value> feed_data) { |
371 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 357 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
372 | 358 |
373 GDataFileError error = util::GDataToGDataFileError(status); | 359 GDataFileError error = util::GDataToGDataFileError(status); |
374 if (error != GDATA_FILE_OK) { | 360 if (error != GDATA_FILE_OK) { |
375 // Get changes starting from the next changestamp from what we have locally. | 361 // Get changes starting from the next changestamp from what we have locally. |
376 LoadFromServer(initial_origin, | 362 LoadFromServer(initial_origin, |
377 local_changestamp + 1, 0, | 363 local_changestamp + 1, 0, |
378 true, /* should_fetch_multiple_feeds */ | 364 true, /* should_fetch_multiple_feeds */ |
379 search_file_path, | |
380 std::string() /* no search query */, | 365 std::string() /* no search query */, |
381 GURL(), /* feed not explicitly set */ | 366 GURL(), /* feed not explicitly set */ |
382 std::string() /* no directory resource ID */, | 367 std::string() /* no directory resource ID */, |
383 callback, | 368 callback, |
384 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded, | 369 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded, |
385 weak_ptr_factory_.GetWeakPtr())); | 370 weak_ptr_factory_.GetWeakPtr())); |
386 return; | 371 return; |
387 } | 372 } |
388 | 373 |
389 scoped_ptr<AboutResource> about_resource; | 374 scoped_ptr<AboutResource> about_resource; |
390 if (feed_data.get()) | 375 if (feed_data.get()) |
391 about_resource = AboutResource::CreateFrom(*feed_data); | 376 about_resource = AboutResource::CreateFrom(*feed_data); |
392 | 377 |
393 if (!about_resource.get()) { | 378 if (!about_resource.get()) { |
394 LoadFromServer(initial_origin, | 379 LoadFromServer(initial_origin, |
395 local_changestamp + 1, 0, | 380 local_changestamp + 1, 0, |
396 true, /* should_fetch_multiple_feeds */ | 381 true, /* should_fetch_multiple_feeds */ |
397 search_file_path, | |
398 std::string() /* no search query */, | 382 std::string() /* no search query */, |
399 GURL(), /* feed not explicitly set */ | 383 GURL(), /* feed not explicitly set */ |
400 std::string() /* no directory resource ID */, | 384 std::string() /* no directory resource ID */, |
401 callback, | 385 callback, |
402 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded, | 386 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded, |
403 weak_ptr_factory_.GetWeakPtr())); | 387 weak_ptr_factory_.GetWeakPtr())); |
404 return; | 388 return; |
405 } | 389 } |
406 | 390 |
407 bool changes_detected = true; | 391 bool changes_detected = true; |
408 int64 largest_changestamp = about_resource->largest_change_id(); | 392 int64 largest_changestamp = about_resource->largest_change_id(); |
409 directory_service_->InitializeRootEntry(about_resource->root_folder_id()); | 393 directory_service_->InitializeRootEntry(about_resource->root_folder_id()); |
410 | 394 |
411 if (local_changestamp >= largest_changestamp) { | 395 if (local_changestamp >= largest_changestamp) { |
412 if (local_changestamp > largest_changestamp) { | 396 if (local_changestamp > largest_changestamp) { |
413 LOG(WARNING) << "Cached client feed is fresher than server, client = " | 397 LOG(WARNING) << "Cached client feed is fresher than server, client = " |
414 << local_changestamp | 398 << local_changestamp |
415 << ", server = " | 399 << ", server = " |
416 << largest_changestamp; | 400 << largest_changestamp; |
417 } | 401 } |
418 // If our cache holds the latest state from the server, change the | 402 // If our cache holds the latest state from the server, change the |
419 // state to FROM_SERVER. | 403 // state to FROM_SERVER. |
420 directory_service_->set_origin( | 404 directory_service_->set_origin( |
421 initial_origin == FROM_CACHE ? FROM_SERVER : initial_origin); | 405 initial_origin == FROM_CACHE ? FROM_SERVER : initial_origin); |
422 changes_detected = false; | 406 changes_detected = false; |
423 } | 407 } |
424 | 408 |
425 // No changes detected, continue with search as planned. | 409 // No changes detected, tell the client that the loading was successful. |
426 if (!changes_detected) { | 410 if (!changes_detected) { |
427 if (!callback.is_null()) { | 411 if (!callback.is_null()) |
428 directory_service_->FindEntryByPathAndRunSync(search_file_path, | 412 callback.Run(GDATA_FILE_OK); |
429 callback); | |
430 } | |
431 return; | 413 return; |
432 } | 414 } |
433 | 415 |
434 // Load changes from the server. | 416 // Load changes from the server. |
435 LoadFromServer(initial_origin, | 417 LoadFromServer(initial_origin, |
436 local_changestamp > 0 ? local_changestamp + 1 : 0, | 418 local_changestamp > 0 ? local_changestamp + 1 : 0, |
437 largest_changestamp, | 419 largest_changestamp, |
438 true, /* should_fetch_multiple_feeds */ | 420 true, /* should_fetch_multiple_feeds */ |
439 search_file_path, | |
440 std::string() /* no search query */, | 421 std::string() /* no search query */, |
441 GURL(), /* feed not explicitly set */ | 422 GURL(), /* feed not explicitly set */ |
442 std::string() /* no directory resource ID */, | 423 std::string() /* no directory resource ID */, |
443 callback, | 424 callback, |
444 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded, | 425 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded, |
445 weak_ptr_factory_.GetWeakPtr())); | 426 weak_ptr_factory_.GetWeakPtr())); |
446 } | 427 } |
447 | 428 |
448 // TODO(kochi): Fix too many parameters. http://crbug.com/141359 | 429 // TODO(kochi): Fix too many parameters. http://crbug.com/141359 |
449 void GDataWapiFeedLoader::LoadFromServer( | 430 void GDataWapiFeedLoader::LoadFromServer( |
450 ContentOrigin initial_origin, | 431 ContentOrigin initial_origin, |
451 int64 start_changestamp, | 432 int64 start_changestamp, |
452 int64 root_feed_changestamp, | 433 int64 root_feed_changestamp, |
453 bool should_fetch_multiple_feeds, | 434 bool should_fetch_multiple_feeds, |
454 const FilePath& search_file_path, | |
455 const std::string& search_query, | 435 const std::string& search_query, |
456 const GURL& feed_to_load, | 436 const GURL& feed_to_load, |
457 const std::string& directory_resource_id, | 437 const std::string& directory_resource_id, |
458 const FindEntryCallback& entry_found_callback, | 438 const FileOperationCallback& load_finished_callback, |
459 const LoadDocumentFeedCallback& feed_load_callback) { | 439 const LoadDocumentFeedCallback& feed_load_callback) { |
460 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 440 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 441 DCHECK(!feed_load_callback.is_null()); |
461 | 442 |
462 // |feed_list| will contain the list of all collected feed updates that | 443 // |feed_list| will contain the list of all collected feed updates that |
463 // we will receive through calls of DocumentsService::GetDocuments(). | 444 // we will receive through calls of DocumentsService::GetDocuments(). |
464 scoped_ptr<std::vector<DocumentFeed*> > feed_list( | 445 scoped_ptr<std::vector<DocumentFeed*> > feed_list( |
465 new std::vector<DocumentFeed*>); | 446 new std::vector<DocumentFeed*>); |
466 const base::TimeTicks start_time = base::TimeTicks::Now(); | 447 const base::TimeTicks start_time = base::TimeTicks::Now(); |
467 | 448 |
468 if (gdata::util::IsDriveV2ApiEnabled()) { | 449 if (gdata::util::IsDriveV2ApiEnabled()) { |
469 documents_service_->GetChangelist( | 450 documents_service_->GetChangelist( |
470 feed_to_load, | 451 feed_to_load, |
471 start_changestamp, | 452 start_changestamp, |
472 base::Bind(&GDataWapiFeedLoader::OnGetChangelist, | 453 base::Bind(&GDataWapiFeedLoader::OnGetChangelist, |
473 weak_ptr_factory_.GetWeakPtr(), | 454 weak_ptr_factory_.GetWeakPtr(), |
474 initial_origin, | 455 initial_origin, |
475 feed_load_callback, | 456 feed_load_callback, |
476 base::Owned(new GetDocumentsParams( | 457 base::Owned(new GetDocumentsParams( |
477 start_changestamp, | 458 start_changestamp, |
478 root_feed_changestamp, | 459 root_feed_changestamp, |
479 feed_list.release(), | 460 feed_list.release(), |
480 should_fetch_multiple_feeds, | 461 should_fetch_multiple_feeds, |
481 search_file_path, | |
482 search_query, | 462 search_query, |
483 directory_resource_id, | 463 directory_resource_id, |
484 entry_found_callback, | 464 load_finished_callback, |
485 NULL)), | 465 NULL)), |
486 start_time)); | 466 start_time)); |
487 return; | 467 return; |
488 } | 468 } |
489 | 469 |
490 documents_service_->GetDocuments( | 470 documents_service_->GetDocuments( |
491 feed_to_load, | 471 feed_to_load, |
492 start_changestamp, | 472 start_changestamp, |
493 search_query, | 473 search_query, |
494 directory_resource_id, | 474 directory_resource_id, |
495 base::Bind(&GDataWapiFeedLoader::OnGetDocuments, | 475 base::Bind(&GDataWapiFeedLoader::OnGetDocuments, |
496 weak_ptr_factory_.GetWeakPtr(), | 476 weak_ptr_factory_.GetWeakPtr(), |
497 initial_origin, | 477 initial_origin, |
498 feed_load_callback, | 478 feed_load_callback, |
499 base::Owned(new GetDocumentsParams(start_changestamp, | 479 base::Owned(new GetDocumentsParams(start_changestamp, |
500 root_feed_changestamp, | 480 root_feed_changestamp, |
501 feed_list.release(), | 481 feed_list.release(), |
502 should_fetch_multiple_feeds, | 482 should_fetch_multiple_feeds, |
503 search_file_path, | |
504 search_query, | 483 search_query, |
505 directory_resource_id, | 484 directory_resource_id, |
506 entry_found_callback, | 485 load_finished_callback, |
507 NULL)), | 486 NULL)), |
508 start_time)); | 487 start_time)); |
509 } | 488 } |
510 | 489 |
511 void GDataWapiFeedLoader::OnFeedFromServerLoaded(GetDocumentsParams* params, | 490 void GDataWapiFeedLoader::OnFeedFromServerLoaded(GetDocumentsParams* params, |
512 GDataFileError error) { | 491 GDataFileError error) { |
513 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 492 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
514 | 493 |
515 if (error != GDATA_FILE_OK) { | 494 if (error != GDATA_FILE_OK) { |
516 if (!params->callback.is_null()) | 495 if (!params->callback.is_null()) |
517 params->callback.Run(error, NULL); | 496 params->callback.Run(error); |
518 return; | 497 return; |
519 } | 498 } |
520 | 499 |
521 error = UpdateFromFeed(*params->feed_list, | 500 error = UpdateFromFeed(*params->feed_list, |
522 params->start_changestamp, | 501 params->start_changestamp, |
523 params->root_feed_changestamp); | 502 params->root_feed_changestamp); |
524 | 503 |
525 if (error != GDATA_FILE_OK) { | 504 if (error != GDATA_FILE_OK) { |
526 if (!params->callback.is_null()) | 505 if (!params->callback.is_null()) |
527 params->callback.Run(error, NULL); | 506 params->callback.Run(error); |
528 | 507 |
529 return; | 508 return; |
530 } | 509 } |
531 | 510 |
532 // Save file system metadata to disk. | 511 // Save file system metadata to disk. |
533 SaveFileSystem(); | 512 SaveFileSystem(); |
534 | 513 |
535 // If we had someone to report this too, then this retrieval was done in a | 514 // Tell the client that the loading was successful. |
536 // context of search... so continue search. | |
537 if (!params->callback.is_null()) { | 515 if (!params->callback.is_null()) { |
538 directory_service_->FindEntryByPathAndRunSync(params->search_file_path, | 516 params->callback.Run(GDATA_FILE_OK); |
539 params->callback); | |
540 } | 517 } |
541 | 518 |
542 FOR_EACH_OBSERVER(Observer, observers_, OnFeedFromServerLoaded()); | 519 FOR_EACH_OBSERVER(Observer, observers_, OnFeedFromServerLoaded()); |
543 } | 520 } |
544 | 521 |
545 void GDataWapiFeedLoader::OnGetDocuments( | 522 void GDataWapiFeedLoader::OnGetDocuments( |
546 ContentOrigin initial_origin, | 523 ContentOrigin initial_origin, |
547 const LoadDocumentFeedCallback& callback, | 524 const LoadDocumentFeedCallback& callback, |
548 GetDocumentsParams* params, | 525 GetDocumentsParams* params, |
549 base::TimeTicks start_time, | 526 base::TimeTicks start_time, |
550 GDataErrorCode status, | 527 GDataErrorCode status, |
551 scoped_ptr<base::Value> data) { | 528 scoped_ptr<base::Value> data) { |
552 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 529 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 530 DCHECK(!callback.is_null()); |
553 | 531 |
554 if (params->feed_list->empty()) { | 532 if (params->feed_list->empty()) { |
555 UMA_HISTOGRAM_TIMES("Gdata.InitialFeedLoadTime", | 533 UMA_HISTOGRAM_TIMES("Gdata.InitialFeedLoadTime", |
556 base::TimeTicks::Now() - start_time); | 534 base::TimeTicks::Now() - start_time); |
557 } | 535 } |
558 | 536 |
559 GDataFileError error = util::GDataToGDataFileError(status); | 537 GDataFileError error = util::GDataToGDataFileError(status); |
560 if (error == GDATA_FILE_OK && | 538 if (error == GDATA_FILE_OK && |
561 (!data.get() || data->GetType() != Value::TYPE_DICTIONARY)) { | 539 (!data.get() || data->GetType() != Value::TYPE_DICTIONARY)) { |
562 error = GDATA_FILE_ERROR_FAILED; | 540 error = GDATA_FILE_ERROR_FAILED; |
563 } | 541 } |
564 | 542 |
565 if (error != GDATA_FILE_OK) { | 543 if (error != GDATA_FILE_OK) { |
566 directory_service_->set_origin(initial_origin); | 544 directory_service_->set_origin(initial_origin); |
567 | 545 callback.Run(params, error); |
568 if (!callback.is_null()) | |
569 callback.Run(params, error); | |
570 | |
571 return; | 546 return; |
572 } | 547 } |
573 | 548 |
574 GURL next_feed_url; | 549 GURL next_feed_url; |
575 scoped_ptr<DocumentFeed> current_feed(DocumentFeed::ExtractAndParse(*data)); | 550 scoped_ptr<DocumentFeed> current_feed(DocumentFeed::ExtractAndParse(*data)); |
576 if (!current_feed.get()) { | 551 if (!current_feed.get()) { |
577 if (!callback.is_null()) { | 552 callback.Run(params, GDATA_FILE_ERROR_FAILED); |
578 callback.Run(params, GDATA_FILE_ERROR_FAILED); | |
579 } | |
580 return; | 553 return; |
581 } | 554 } |
582 const bool has_next_feed_url = current_feed->GetNextFeedURL(&next_feed_url); | 555 const bool has_next_feed_url = current_feed->GetNextFeedURL(&next_feed_url); |
583 | 556 |
584 #ifndef NDEBUG | 557 #ifndef NDEBUG |
585 // Save initial root feed for analysis. | 558 // Save initial root feed for analysis. |
586 std::string file_name = | 559 std::string file_name = |
587 base::StringPrintf("DEBUG_feed_%" PRId64 ".json", | 560 base::StringPrintf("DEBUG_feed_%" PRId64 ".json", |
588 params->start_changestamp); | 561 params->start_changestamp); |
589 util::PostBlockingPoolSequencedTask( | 562 util::PostBlockingPoolSequencedTask( |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 base::Bind(&GDataWapiFeedLoader::OnGetDocuments, | 608 base::Bind(&GDataWapiFeedLoader::OnGetDocuments, |
636 weak_ptr_factory_.GetWeakPtr(), | 609 weak_ptr_factory_.GetWeakPtr(), |
637 initial_origin, | 610 initial_origin, |
638 callback, | 611 callback, |
639 base::Owned( | 612 base::Owned( |
640 new GetDocumentsParams( | 613 new GetDocumentsParams( |
641 params->start_changestamp, | 614 params->start_changestamp, |
642 params->root_feed_changestamp, | 615 params->root_feed_changestamp, |
643 params->feed_list.release(), | 616 params->feed_list.release(), |
644 params->should_fetch_multiple_feeds, | 617 params->should_fetch_multiple_feeds, |
645 params->search_file_path, | |
646 params->search_query, | 618 params->search_query, |
647 params->directory_resource_id, | 619 params->directory_resource_id, |
648 params->callback, | 620 params->callback, |
649 params->ui_state.release())), | 621 params->ui_state.release())), |
650 start_time)); | 622 start_time)); |
651 return; | 623 return; |
652 } | 624 } |
653 | 625 |
654 // Notify the observers that a document feed is fetched. | 626 // Notify the observers that all document feeds are fetched. |
655 FOR_EACH_OBSERVER(Observer, observers_, | 627 FOR_EACH_OBSERVER(Observer, observers_, |
656 OnDocumentFeedFetched(num_accumulated_entries)); | 628 OnDocumentFeedFetched(num_accumulated_entries)); |
657 | 629 |
658 UMA_HISTOGRAM_TIMES("Gdata.EntireFeedLoadTime", | 630 UMA_HISTOGRAM_TIMES("Gdata.EntireFeedLoadTime", |
659 base::TimeTicks::Now() - start_time); | 631 base::TimeTicks::Now() - start_time); |
660 | 632 |
661 if (!callback.is_null()) | 633 // Run the callback so the client can process the retrieved feeds. |
662 callback.Run(params, error); | 634 callback.Run(params, error); |
663 } | 635 } |
664 | 636 |
665 void GDataWapiFeedLoader::OnGetChangelist( | 637 void GDataWapiFeedLoader::OnGetChangelist( |
666 ContentOrigin initial_origin, | 638 ContentOrigin initial_origin, |
667 const LoadDocumentFeedCallback& callback, | 639 const LoadDocumentFeedCallback& callback, |
668 GetDocumentsParams* params, | 640 GetDocumentsParams* params, |
669 base::TimeTicks start_time, | 641 base::TimeTicks start_time, |
670 GDataErrorCode status, | 642 GDataErrorCode status, |
671 scoped_ptr<base::Value> data) { | 643 scoped_ptr<base::Value> data) { |
672 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 644 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 645 DCHECK(!callback.is_null()); |
673 | 646 |
674 if (params->feed_list->empty()) { | 647 if (params->feed_list->empty()) { |
675 UMA_HISTOGRAM_TIMES("Drive.InitialFeedLoadTime", | 648 UMA_HISTOGRAM_TIMES("Drive.InitialFeedLoadTime", |
676 base::TimeTicks::Now() - start_time); | 649 base::TimeTicks::Now() - start_time); |
677 } | 650 } |
678 | 651 |
679 GDataFileError error = util::GDataToGDataFileError(status); | 652 GDataFileError error = util::GDataToGDataFileError(status); |
680 if (error == GDATA_FILE_OK && | 653 if (error == GDATA_FILE_OK && |
681 (!data.get() || data->GetType() != Value::TYPE_DICTIONARY)) { | 654 (!data.get() || data->GetType() != Value::TYPE_DICTIONARY)) { |
682 error = GDATA_FILE_ERROR_FAILED; | 655 error = GDATA_FILE_ERROR_FAILED; |
683 } | 656 } |
684 | 657 |
685 if (error != GDATA_FILE_OK) { | 658 if (error != GDATA_FILE_OK) { |
686 directory_service_->set_origin(initial_origin); | 659 directory_service_->set_origin(initial_origin); |
687 | 660 callback.Run(params, error); |
688 if (!callback.is_null()) | |
689 callback.Run(params, error); | |
690 | |
691 return; | 661 return; |
692 } | 662 } |
693 | 663 |
694 GURL next_feed_url; | 664 GURL next_feed_url; |
695 scoped_ptr<ChangeList> current_feed(ChangeList::CreateFrom(*data)); | 665 scoped_ptr<ChangeList> current_feed(ChangeList::CreateFrom(*data)); |
696 if (!current_feed.get()) { | 666 if (!current_feed.get()) { |
697 if (!callback.is_null()) { | 667 callback.Run(params, GDATA_FILE_ERROR_FAILED); |
698 callback.Run(params, GDATA_FILE_ERROR_FAILED); | |
699 } | |
700 return; | 668 return; |
701 } | 669 } |
702 const bool has_next_feed = !current_feed->next_page_token().empty(); | 670 const bool has_next_feed = !current_feed->next_page_token().empty(); |
703 | 671 |
704 #ifndef NDEBUG | 672 #ifndef NDEBUG |
705 // Save initial root feed for analysis. | 673 // Save initial root feed for analysis. |
706 std::string file_name = | 674 std::string file_name = |
707 base::StringPrintf("DEBUG_changelist_%" PRId64 ".json", | 675 base::StringPrintf("DEBUG_changelist_%" PRId64 ".json", |
708 params->start_changestamp); | 676 params->start_changestamp); |
709 util::PostBlockingPoolSequencedTask( | 677 util::PostBlockingPoolSequencedTask( |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
755 base::Bind(&GDataWapiFeedLoader::OnGetChangelist, | 723 base::Bind(&GDataWapiFeedLoader::OnGetChangelist, |
756 weak_ptr_factory_.GetWeakPtr(), | 724 weak_ptr_factory_.GetWeakPtr(), |
757 initial_origin, | 725 initial_origin, |
758 callback, | 726 callback, |
759 base::Owned( | 727 base::Owned( |
760 new GetDocumentsParams( | 728 new GetDocumentsParams( |
761 params->start_changestamp, | 729 params->start_changestamp, |
762 params->root_feed_changestamp, | 730 params->root_feed_changestamp, |
763 params->feed_list.release(), | 731 params->feed_list.release(), |
764 params->should_fetch_multiple_feeds, | 732 params->should_fetch_multiple_feeds, |
765 params->search_file_path, | |
766 params->search_query, | 733 params->search_query, |
767 params->directory_resource_id, | 734 params->directory_resource_id, |
768 params->callback, | 735 params->callback, |
769 NULL)), | 736 NULL)), |
770 start_time)); | 737 start_time)); |
771 return; | 738 return; |
772 } | 739 } |
773 | 740 |
774 // Notify the observers that a document feed is fetched. | 741 // Notify the observers that all document feeds are fetched. |
775 FOR_EACH_OBSERVER(Observer, observers_, | 742 FOR_EACH_OBSERVER(Observer, observers_, |
776 OnDocumentFeedFetched(num_accumulated_entries)); | 743 OnDocumentFeedFetched(num_accumulated_entries)); |
777 | 744 |
778 UMA_HISTOGRAM_TIMES("Drive.EntireFeedLoadTime", | 745 UMA_HISTOGRAM_TIMES("Drive.EntireFeedLoadTime", |
779 base::TimeTicks::Now() - start_time); | 746 base::TimeTicks::Now() - start_time); |
780 | 747 |
781 if (!callback.is_null()) | 748 // Run the callback so the client can process the retrieved feeds. |
782 callback.Run(params, error); | 749 callback.Run(params, error); |
783 } | 750 } |
784 | 751 |
785 void GDataWapiFeedLoader::OnNotifyDocumentFeedFetched( | 752 void GDataWapiFeedLoader::OnNotifyDocumentFeedFetched( |
786 base::WeakPtr<GetDocumentsUiState> ui_state) { | 753 base::WeakPtr<GetDocumentsUiState> ui_state) { |
787 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 754 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
788 | 755 |
789 if (!ui_state) { | 756 if (!ui_state) { |
790 // The ui state instance is already released, which means the fetching | 757 // The ui state instance is already released, which means the fetching |
791 // is done and we don't need to update any more. | 758 // is done and we don't need to update any more. |
792 return; | 759 return; |
(...skipping 21 matching lines...) Expand all Loading... |
814 base::Bind(&GDataWapiFeedLoader::OnNotifyDocumentFeedFetched, | 781 base::Bind(&GDataWapiFeedLoader::OnNotifyDocumentFeedFetched, |
815 weak_ptr_factory_.GetWeakPtr(), | 782 weak_ptr_factory_.GetWeakPtr(), |
816 ui_state->weak_ptr_factory.GetWeakPtr()), | 783 ui_state->weak_ptr_factory.GetWeakPtr()), |
817 remaining_duration / num_remaining_ui_updates); | 784 remaining_duration / num_remaining_ui_updates); |
818 } | 785 } |
819 } | 786 } |
820 } | 787 } |
821 | 788 |
822 void GDataWapiFeedLoader::LoadFromCache( | 789 void GDataWapiFeedLoader::LoadFromCache( |
823 bool should_load_from_server, | 790 bool should_load_from_server, |
824 const FilePath& search_file_path, | 791 const FileOperationCallback& callback) { |
825 const FindEntryCallback& callback) { | |
826 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 792 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
827 | 793 |
828 LoadRootFeedParams* params = new LoadRootFeedParams(search_file_path, | 794 LoadRootFeedParams* params = new LoadRootFeedParams(should_load_from_server, |
829 should_load_from_server, | |
830 callback); | 795 callback); |
831 FilePath path = cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_META); | 796 FilePath path = cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_META); |
832 if (UseLevelDB()) { | 797 if (UseLevelDB()) { |
833 path = path.Append(kResourceMetadataDBFile); | 798 path = path.Append(kResourceMetadataDBFile); |
834 directory_service_->InitFromDB(path, blocking_task_runner_, | 799 directory_service_->InitFromDB(path, blocking_task_runner_, |
835 base::Bind( | 800 base::Bind( |
836 &GDataWapiFeedLoader::ContinueWithInitializedDirectoryService, | 801 &GDataWapiFeedLoader::ContinueWithInitializedDirectoryService, |
837 weak_ptr_factory_.GetWeakPtr(), | 802 weak_ptr_factory_.GetWeakPtr(), |
838 base::Owned(params))); | 803 base::Owned(params))); |
839 } else { | 804 } else { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
871 | 836 |
872 void GDataWapiFeedLoader::ContinueWithInitializedDirectoryService( | 837 void GDataWapiFeedLoader::ContinueWithInitializedDirectoryService( |
873 LoadRootFeedParams* params, | 838 LoadRootFeedParams* params, |
874 GDataFileError error) { | 839 GDataFileError error) { |
875 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 840 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
876 | 841 |
877 DVLOG(1) << "Time elapsed to load directory service from disk=" | 842 DVLOG(1) << "Time elapsed to load directory service from disk=" |
878 << (base::Time::Now() - params->load_start_time).InMilliseconds() | 843 << (base::Time::Now() - params->load_start_time).InMilliseconds() |
879 << " milliseconds"; | 844 << " milliseconds"; |
880 | 845 |
881 FindEntryCallback callback = params->callback; | 846 FileOperationCallback callback = params->callback; |
882 // If we got feed content from cache, try search over it. | 847 // If we got feed content from cache, tell the client that the loading was |
| 848 // successful. |
883 if (error == GDATA_FILE_OK && !callback.is_null()) { | 849 if (error == GDATA_FILE_OK && !callback.is_null()) { |
884 // Continue file content search operation if the delegate hasn't terminated | 850 callback.Run(GDATA_FILE_OK); |
885 // this search branch already. | 851 // Reset the callback so we don't run the same callback once |
886 directory_service_->FindEntryByPathAndRunSync(params->search_file_path, | 852 // ReloadFeedFromServerIfNeeded() is complete. |
887 callback); | |
888 callback.Reset(); | 853 callback.Reset(); |
889 } | 854 } |
890 | 855 |
891 if (!params->should_load_from_server) | 856 if (!params->should_load_from_server) |
892 return; | 857 return; |
893 | 858 |
894 // Decide the |initial_origin| to pass to ReloadFromServerIfNeeded(). | 859 // Decide the |initial_origin| to pass to ReloadFromServerIfNeeded(). |
895 // This is used to restore directory content origin to its initial value when | 860 // This is used to restore directory content origin to its initial value when |
896 // we fail to retrieve the feed from server. | 861 // we fail to retrieve the feed from server. |
897 // By default, if directory content is not yet initialized, restore content | 862 // By default, if directory content is not yet initialized, restore content |
898 // origin to UNINITIALIZED in case of failure. | 863 // origin to UNINITIALIZED in case of failure. |
899 ContentOrigin initial_origin = UNINITIALIZED; | 864 ContentOrigin initial_origin = UNINITIALIZED; |
900 if (directory_service_->origin() != INITIALIZING) { | 865 if (directory_service_->origin() != INITIALIZING) { |
901 // If directory content is already initialized, restore content origin | 866 // If directory content is already initialized, restore content origin |
902 // to FROM_CACHE in case of failure. | 867 // to FROM_CACHE in case of failure. |
903 initial_origin = FROM_CACHE; | 868 initial_origin = FROM_CACHE; |
904 directory_service_->set_origin(REFRESHING); | 869 directory_service_->set_origin(REFRESHING); |
905 } | 870 } |
906 | 871 |
907 // Kick of the retrieval of the feed from server. If we have previously | 872 // Kick of the retrieval of the feed from server. If we have previously |
908 // |reported| to the original callback, then we just need to refresh the | 873 // |reported| to the original callback, then we just need to refresh the |
909 // content without continuing search upon operation completion. | 874 // content without continuing search upon operation completion. |
910 ReloadFromServerIfNeeded(initial_origin, | 875 ReloadFromServerIfNeeded(initial_origin, |
911 directory_service_->largest_changestamp(), | 876 directory_service_->largest_changestamp(), |
912 params->search_file_path, | |
913 callback); | 877 callback); |
914 } | 878 } |
915 | 879 |
916 void GDataWapiFeedLoader::SaveFileSystem() { | 880 void GDataWapiFeedLoader::SaveFileSystem() { |
917 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 881 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
918 | 882 |
919 if (!ShouldSerializeFileSystemNow(directory_service_->serialized_size(), | 883 if (!ShouldSerializeFileSystemNow(directory_service_->serialized_size(), |
920 directory_service_->last_serialized())) { | 884 directory_service_->last_serialized())) { |
921 return; | 885 return; |
922 } | 886 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
963 dir_iter != changed_dirs.end(); ++dir_iter) { | 927 dir_iter != changed_dirs.end(); ++dir_iter) { |
964 FOR_EACH_OBSERVER(Observer, observers_, | 928 FOR_EACH_OBSERVER(Observer, observers_, |
965 OnDirectoryChanged(*dir_iter)); | 929 OnDirectoryChanged(*dir_iter)); |
966 } | 930 } |
967 } | 931 } |
968 | 932 |
969 return error; | 933 return error; |
970 } | 934 } |
971 | 935 |
972 } // namespace gdata | 936 } // namespace gdata |
OLD | NEW |