OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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. | |
4 | |
5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_SYSTEM_LOG_UPLOADER_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_POLICY_SYSTEM_LOG_UPLOADER_H_ | |
7 | |
8 #include "base/macros.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/memory/ref_counted_memory.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 "chrome/browser/chromeos/policy/upload_job.h" | |
16 | |
17 namespace base { | |
18 class SequencedTaskRunner; | |
19 } | |
20 | |
21 namespace policy { | |
22 | |
23 // Class responsible for periodically uploading system logs, it handles the | |
24 // server responses by UploadJob:Delegate callbacks. | |
25 class SystemLogUploader : public UploadJob::Delegate { | |
26 public: | |
27 // Structure that stores the system log files as pairs: (file name, loaded | |
28 // from the disk binary file data). | |
29 typedef std::vector<std::pair<std::string, std::string>> SystemLogs; | |
30 | |
31 // A delegate interface used by SystemLogUploader to read the system logs | |
32 // from the disk and create an upload job. | |
33 class Delegate { | |
34 public: | |
35 typedef base::Callback<void(const scoped_ptr<SystemLogs> system_logs)> | |
Andrew T Wilson (Slow)
2015/08/03 13:15:29
remove const
Polina Bondarenko
2015/08/03 15:47:51
Done.
| |
36 LogUploadCallback; | |
37 | |
38 virtual ~Delegate() {} | |
39 | |
40 // Loads system logs and invokes |upload_callback|. | |
41 virtual void LoadSystemLogs(const LogUploadCallback& upload_callback) = 0; | |
42 | |
43 // Creates a new fully configured instance of an UploadJob. This method | |
44 // will be called exactly once per every system log upload. | |
45 virtual scoped_ptr<UploadJob> CreateUploadJob( | |
46 const GURL& upload_url, | |
47 UploadJob::Delegate* delegate) = 0; | |
48 }; | |
49 | |
50 // Constructor. Callers can inject their own Delegate. A nullptr can be passed | |
51 // for |syslog_delegate| to use the default implementation. | |
52 explicit SystemLogUploader( | |
53 scoped_ptr<Delegate> syslog_delegate, | |
54 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | |
55 | |
56 ~SystemLogUploader() override; | |
57 | |
58 // Returns the time of the last upload attempt, or Time(0) if no upload has | |
59 // ever happened. | |
60 base::Time last_upload_attempt() const { return last_upload_attempt_; } | |
61 | |
62 // UploadJob::Delegate: | |
63 // Callbacks handle success and failure results of upload, destroy the | |
64 // upload job. | |
65 void OnSuccess() override; | |
66 void OnFailure(UploadJob::ErrorCode error_code) override; | |
67 | |
68 private: | |
69 // Starts the system log loading process. | |
70 void StartLogUpload(); | |
71 | |
72 // The callback is invoked by the Delegate if system logs have been loaded | |
73 // from disk, uploads system logs. | |
74 void UploadSystemLogs(const scoped_ptr<SystemLogs> system_logs); | |
Andrew T Wilson (Slow)
2015/08/03 13:15:29
remove const
Polina Bondarenko
2015/08/03 15:47:51
Done.
| |
75 | |
76 // Helper method that figures out when the next system log upload should | |
77 // be scheduled. | |
78 void ScheduleNextSystemLogUpload(base::TimeDelta frequency); | |
79 | |
80 // The number of consequent retries after the failed uploads. | |
81 int retry_count_; | |
82 | |
83 // How long to wait between system log uploads. | |
84 base::TimeDelta upload_frequency_; | |
85 | |
86 // The time the last upload attempt was performed. | |
87 base::Time last_upload_attempt_; | |
88 | |
89 // TaskRunner used for scheduling upload tasks. | |
90 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
91 | |
92 // The upload job that is re-created on every system log upload. | |
93 scoped_ptr<UploadJob> upload_job_; | |
94 | |
95 // The Delegate is used to load system logs and create UploadJobs. | |
96 scoped_ptr<Delegate> syslog_delegate_; | |
97 | |
98 base::ThreadChecker thread_checker_; | |
99 | |
100 // Note: This should remain the last member so it'll be destroyed and | |
101 // invalidate the weak pointers before any other members are destroyed. | |
102 scoped_ptr<base::WeakPtrFactory<SystemLogUploader>> weak_ptr_factory_; | |
Andrew T Wilson (Slow)
2015/08/03 13:15:29
this should not be a scoped_ptr
Polina Bondarenko
2015/08/03 15:47:51
Done.
| |
103 | |
104 DISALLOW_COPY_AND_ASSIGN(SystemLogUploader); | |
105 }; | |
106 | |
107 } // namespace policy | |
108 | |
109 #endif // CHROME_BROWSER_CHROMEOS_POLICY_SYSTEM_LOG_UPLOADER_H_ | |
OLD | NEW |