Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Side by Side Diff: chrome/browser/chromeos/extensions/file_manager/event_router.cc

Issue 507293002: Enrich fileBrowserPrivate.onFileTransfersUpdated event to support displaying total number of jobs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/extensions/file_manager/event_router.h" 5 #include "chrome/browser/chromeos/extensions/file_manager/event_router.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/prefs/pref_change_registrar.h" 10 #include "base/prefs/pref_change_registrar.h"
11 #include "base/prefs/pref_service.h" 11 #include "base/prefs/pref_service.h"
12 #include "base/stl_util.h" 12 #include "base/stl_util.h"
13 #include "base/thread_task_runner_handle.h"
13 #include "base/threading/sequenced_worker_pool.h" 14 #include "base/threading/sequenced_worker_pool.h"
14 #include "base/values.h" 15 #include "base/values.h"
15 #include "chrome/browser/app_mode/app_mode_utils.h" 16 #include "chrome/browser/app_mode/app_mode_utils.h"
16 #include "chrome/browser/chrome_notification_types.h" 17 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/chromeos/drive/drive_integration_service.h" 18 #include "chrome/browser/chromeos/drive/drive_integration_service.h"
18 #include "chrome/browser/chromeos/drive/file_change.h" 19 #include "chrome/browser/chromeos/drive/file_change.h"
19 #include "chrome/browser/chromeos/drive/file_system_interface.h" 20 #include "chrome/browser/chromeos/drive/file_system_interface.h"
20 #include "chrome/browser/chromeos/drive/file_system_util.h" 21 #include "chrome/browser/chromeos/drive/file_system_util.h"
21 #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h" 22 #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h"
22 #include "chrome/browser/chromeos/file_manager/app_id.h" 23 #include "chrome/browser/chromeos/file_manager/app_id.h"
(...skipping 29 matching lines...) Expand all
52 using drive::DriveIntegrationService; 53 using drive::DriveIntegrationService;
53 using drive::DriveIntegrationServiceFactory; 54 using drive::DriveIntegrationServiceFactory;
54 using file_manager::util::EntryDefinition; 55 using file_manager::util::EntryDefinition;
55 using file_manager::util::FileDefinition; 56 using file_manager::util::FileDefinition;
56 57
57 namespace file_browser_private = extensions::api::file_browser_private; 58 namespace file_browser_private = extensions::api::file_browser_private;
58 59
59 namespace file_manager { 60 namespace file_manager {
60 namespace { 61 namespace {
61 // Constants for the "transferState" field of onFileTransferUpdated event. 62 // Constants for the "transferState" field of onFileTransferUpdated event.
63 const char kFileTransferStateAdded[] = "added";
62 const char kFileTransferStateStarted[] = "started"; 64 const char kFileTransferStateStarted[] = "started";
63 const char kFileTransferStateInProgress[] = "in_progress"; 65 const char kFileTransferStateInProgress[] = "in_progress";
64 const char kFileTransferStateCompleted[] = "completed"; 66 const char kFileTransferStateCompleted[] = "completed";
65 const char kFileTransferStateFailed[] = "failed"; 67 const char kFileTransferStateFailed[] = "failed";
66 68
67 // Frequency of sending onFileTransferUpdated. 69 // Frequency of sending onFileTransferUpdated.
68 const int64 kProgressEventFrequencyInMilliseconds = 1000; 70 const int64 kProgressEventFrequencyInMilliseconds = 1000;
69 71
70 // Maximim size of detailed change info on directory change event. If the size 72 // Maximim size of detailed change info on directory change event. If the size
71 // exceeds the maximum size, the detailed info is omitted and the force refresh 73 // exceeds the maximum size, the detailed info is omitted and the force refresh
(...skipping 17 matching lines...) Expand all
89 91
90 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue); 92 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
91 GURL url = util::ConvertDrivePathToFileSystemUrl( 93 GURL url = util::ConvertDrivePathToFileSystemUrl(
92 profile, job_info.file_path, extension_id); 94 profile, job_info.file_path, extension_id);
93 status->file_url = url.spec(); 95 status->file_url = url.spec();
94 status->transfer_state = file_browser_private::ParseTransferState(job_status); 96 status->transfer_state = file_browser_private::ParseTransferState(job_status);
95 status->transfer_type = 97 status->transfer_type =
96 IsUploadJob(job_info.job_type) ? 98 IsUploadJob(job_info.job_type) ?
97 file_browser_private::TRANSFER_TYPE_UPLOAD : 99 file_browser_private::TRANSFER_TYPE_UPLOAD :
98 file_browser_private::TRANSFER_TYPE_DOWNLOAD; 100 file_browser_private::TRANSFER_TYPE_DOWNLOAD;
101 status->num_total_jobs.reset(
102 new double(static_cast<double>(job_info.num_total_jobs)));
99 // JavaScript does not have 64-bit integers. Instead we use double, which 103 // JavaScript does not have 64-bit integers. Instead we use double, which
100 // is in IEEE 754 formant and accurate up to 52-bits in JS, and in practice 104 // is in IEEE 754 formant and accurate up to 52-bits in JS, and in practice
101 // in C++. Larger values are rounded. 105 // in C++. Larger values are rounded.
102 status->processed.reset( 106 status->processed.reset(
103 new double(static_cast<double>(job_info.num_completed_bytes))); 107 new double(static_cast<double>(job_info.num_completed_bytes)));
104 status->total.reset( 108 status->total.reset(
105 new double(static_cast<double>(job_info.num_total_bytes))); 109 new double(static_cast<double>(job_info.num_total_bytes)));
106 } 110 }
107 111
108 // Checks if the Recovery Tool is running. This is a temporary solution. 112 // Checks if the Recovery Tool is running. This is a temporary solution.
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 EventRouter::DriveJobInfoWithStatus::DriveJobInfoWithStatus() 337 EventRouter::DriveJobInfoWithStatus::DriveJobInfoWithStatus()
334 : job_info(drive::TYPE_DOWNLOAD_FILE) { 338 : job_info(drive::TYPE_DOWNLOAD_FILE) {
335 } 339 }
336 340
337 EventRouter::DriveJobInfoWithStatus::DriveJobInfoWithStatus( 341 EventRouter::DriveJobInfoWithStatus::DriveJobInfoWithStatus(
338 const drive::JobInfo& info, const std::string& status) 342 const drive::JobInfo& info, const std::string& status)
339 : job_info(info), status(status) { 343 : job_info(info), status(status) {
340 } 344 }
341 345
342 EventRouter::EventRouter(Profile* profile) 346 EventRouter::EventRouter(Profile* profile)
343 : pref_change_registrar_(new PrefChangeRegistrar), 347 : startup_time_delta_(base::TimeDelta::FromSeconds(1)),
348 pref_change_registrar_(new PrefChangeRegistrar),
344 profile_(profile), 349 profile_(profile),
345 multi_user_window_manager_observer_registered_(false), 350 multi_user_window_manager_observer_registered_(false),
346 weak_factory_(this) { 351 weak_factory_(this) {
347 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 352 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
348 } 353 }
349 354
350 EventRouter::~EventRouter() { 355 EventRouter::~EventRouter() {
351 } 356 }
352 357
353 void EventRouter::Shutdown() { 358 void EventRouter::Shutdown() {
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 } 577 }
573 578
574 BroadcastEvent( 579 BroadcastEvent(
575 profile_, 580 profile_,
576 file_browser_private::OnPreferencesChanged::kEventName, 581 file_browser_private::OnPreferencesChanged::kEventName,
577 file_browser_private::OnPreferencesChanged::Create()); 582 file_browser_private::OnPreferencesChanged::Create());
578 } 583 }
579 584
580 void EventRouter::OnJobAdded(const drive::JobInfo& job_info) { 585 void EventRouter::OnJobAdded(const drive::JobInfo& job_info) {
581 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 586 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
582 OnJobUpdated(job_info); 587 if (!drive::IsActiveFileTransferJobInfo(job_info))
588 return;
589 DriveJobInfoWithStatus job_info_with_status =
590 DriveJobInfoWithStatus(job_info, kFileTransferStateAdded);
591 SendDriveFileTransferEventDelay(
592 job_info_with_status, base::Time::Now(), false /* is_delayed */);
583 } 593 }
584 594
585 void EventRouter::OnJobUpdated(const drive::JobInfo& job_info) { 595 void EventRouter::OnJobUpdated(const drive::JobInfo& job_info) {
586 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 596 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
587 if (!drive::IsActiveFileTransferJobInfo(job_info)) 597 if (!drive::IsActiveFileTransferJobInfo(job_info))
588 return; 598 return;
589 599
590 bool is_new_job = (drive_jobs_.find(job_info.job_id) == drive_jobs_.end()); 600 bool is_new_job = (drive_jobs_.find(job_info.job_id) == drive_jobs_.end());
591 601
592 // Replace with the latest job info. 602 // Replace with the latest job info.
(...skipping 19 matching lines...) Expand all
612 : kFileTransferStateFailed); 622 : kFileTransferStateFailed);
613 623
614 // Fire event if needed. 624 // Fire event if needed.
615 bool always = true; 625 bool always = true;
616 SendDriveFileTransferEvent(always); 626 SendDriveFileTransferEvent(always);
617 627
618 // Forget about the job. 628 // Forget about the job.
619 drive_jobs_.erase(job_info.job_id); 629 drive_jobs_.erase(job_info.job_id);
620 } 630 }
621 631
632 void EventRouter::SendDriveFileTransferEventDelay(
633 const DriveJobInfoWithStatus& job_info_with_status,
634 base::Time call_time,
635 bool is_delayed) {
636 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
637 DCHECK_EQ(job_info_with_status.job_info.state, drive::STATE_NEW);
638
639 if (!is_delayed) {
640 last_post_delayed_task_ = base::Time::Now();
641 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
642 FROM_HERE,
643 base::Bind(&EventRouter::SendDriveFileTransferEventDelay,
644 weak_factory_.GetWeakPtr(),
645 job_info_with_status,
646 last_post_delayed_task_,
647 true /* is_delayed */),
648 startup_time_delta_);
649 return;
650 }
651
652 DCHECK(!last_post_delayed_task_.is_null());
653
654 if (call_time < last_post_delayed_task_) {
655 return;
656 }
657
658 // Convert the current |drive_jobs_| to IDL type.
659 std::vector<linked_ptr<file_browser_private::FileTransferStatus> >
660 status_list;
661
662 linked_ptr<file_browser_private::FileTransferStatus> status(
663 new file_browser_private::FileTransferStatus());
664 JobInfoToTransferStatus(profile_,
665 kFileManagerAppId,
666 job_info_with_status.status,
667 job_info_with_status.job_info,
668 status.get());
669 status_list.push_back(status);
670
671 BroadcastEvent(
672 profile_,
673 file_browser_private::OnFileTransfersUpdated::kEventName,
674 file_browser_private::OnFileTransfersUpdated::Create(status_list));
675 }
676
622 void EventRouter::SendDriveFileTransferEvent(bool always) { 677 void EventRouter::SendDriveFileTransferEvent(bool always) {
623 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 678 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
624 679
625 // When |always| flag is not set, we don't send the event until certain 680 // When |always| flag is not set, we don't send the event until certain
626 // amount of time passes after the previous one. This is to avoid 681 // amount of time passes after the previous one. This is to avoid
627 // flooding the IPC between extensions by many onFileTransferUpdated events. 682 // flooding the IPC between extensions by many onFileTransferUpdated events.
628 if (!ShouldSendProgressEvent(always, &last_file_transfer_event_)) 683 if (!ShouldSendProgressEvent(always, &last_file_transfer_event_))
629 return; 684 return;
630 685
631 // Convert the current |drive_jobs_| to IDL type. 686 // Convert the current |drive_jobs_| to IDL type.
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
977 } 1032 }
978 } 1033 }
979 1034
980 void EventRouter::OnOwnerEntryChanged(aura::Window* window) { 1035 void EventRouter::OnOwnerEntryChanged(aura::Window* window) {
981 BroadcastEvent(profile_, 1036 BroadcastEvent(profile_,
982 file_browser_private::OnDesktopChanged::kEventName, 1037 file_browser_private::OnDesktopChanged::kEventName,
983 file_browser_private::OnDesktopChanged::Create()); 1038 file_browser_private::OnDesktopChanged::Create());
984 } 1039 }
985 1040
986 } // namespace file_manager 1041 } // namespace file_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698