1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_JOB_EVENT_ROUTER_H_
6 #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_JOB_EVENT_ROUTER_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/threading/thread_checker.h"
14 #include "base/time/time.h"
15 #include "base/values.h"
16 #include "chrome/common/extensions/api/file_manager_private.h"
17 #include "components/drive/job_list.h"
18 #include "extensions/browser/extension_event_histogram_value.h"
21 namespace file_manager
{
23 // Files.app's event router handling job related events.
24 class JobEventRouter
: public drive::JobListObserver
{
26 explicit JobEventRouter(const base::TimeDelta
& event_delay
);
27 ~JobEventRouter() override
;
29 // drive::JobListObserver overrides.
30 void OnJobAdded(const drive::JobInfo
& job_info
) override
;
31 void OnJobUpdated(const drive::JobInfo
& job_info
) override
;
32 void OnJobDone(const drive::JobInfo
& job_info
,
33 drive::FileError error
) override
;
36 // Helper method to convert drive path to file system URL.
37 virtual GURL
ConvertDrivePathToFileSystemUrl(const base::FilePath
& path
,
38 const std::string
& id
) const = 0;
40 // Helper method to dispatch events.
41 virtual void BroadcastEvent(
42 extensions::events::HistogramValue histogram_value
,
43 const std::string
& event_name
,
44 scoped_ptr
<base::ListValue
> event_args
) = 0;
47 // Request sending transfer event with |job_info| and |state|.
48 // If |immediate| is true, the event will be dispatched synchronously.
49 // Otherwise, the event is throttled, or may be skipped.
50 void ScheduleDriveFileTransferEvent(
51 const drive::JobInfo
& job_info
,
52 extensions::api::file_manager_private::TransferState state
,
55 // Send transfer event requested by ScheduleDriveFileTransferEvent at last.
56 void SendDriveFileTransferEvent();
58 // Update |num_completed_bytes_| and |num_total_bytes_| depends on |job|.
59 void UpdateBytes(const drive::JobInfo
& job_info
);
61 // Delay time before sending progress events.
62 base::TimeDelta event_delay_
;
64 // Set of job that are in the job schedular.
65 std::map
<drive::JobID
, linked_ptr
<drive::JobInfo
>> drive_jobs_
;
67 // Pending transfer event. |ScheduleDriveFileTransferEvent| registers timeout
68 // callback to dispatch this.
69 scoped_ptr
<extensions::api::file_manager_private::FileTransferStatus
>
72 // Computed bytes of tasks that have been processed. Once it completes all
73 // tasks, it clears the variable.
74 int64 num_completed_bytes_
;
76 // Total bytes of tasks that have been processed. Once it completes all tasks,
77 // it clears the variable.
78 int64 num_total_bytes_
;
81 base::ThreadChecker thread_checker_
;
83 // Note: This should remain the last member so it'll be destroyed and
84 // invalidate the weak pointers before any other members are destroyed.
85 base::WeakPtrFactory
<JobEventRouter
> weak_factory_
;
87 DISALLOW_COPY_AND_ASSIGN(JobEventRouter
);
90 } // namespace file_manager
92 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_JOB_EVENT_ROUTER_H_