Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / system_log_uploader.h
blobead2eba392246a7747edfefffe2d60aa5a49bc9e
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.
5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_SYSTEM_LOG_UPLOADER_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_SYSTEM_LOG_UPLOADER_H_
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 #include "chrome/browser/chromeos/settings/cros_settings.h"
18 namespace base {
19 class SequencedTaskRunner;
22 namespace policy {
24 // Class responsible for periodically uploading system logs, it handles the
25 // server responses by UploadJob:Delegate callbacks.
26 class SystemLogUploader : public UploadJob::Delegate {
27 public:
28 // Structure that stores the system log files as pairs: (file name, loaded
29 // from the disk binary file data).
30 typedef std::vector<std::pair<std::string, std::string>> SystemLogs;
32 // Refresh constants.
33 static const int64 kDefaultUploadDelayMs;
34 static const int64 kErrorUploadDelayMs;
36 // Http header constants to upload.
37 static const char* const kNameFieldTemplate;
38 static const char* const kFileTypeHeaderName;
39 static const char* const kFileTypeLogFile;
40 static const char* const kContentTypePlainText;
42 // A delegate interface used by SystemLogUploader to read the system logs
43 // from the disk and create an upload job.
44 class Delegate {
45 public:
46 typedef base::Callback<void(scoped_ptr<SystemLogs> system_logs)>
47 LogUploadCallback;
49 virtual ~Delegate() {}
51 // Loads system logs and invokes |upload_callback|.
52 virtual void LoadSystemLogs(const LogUploadCallback& upload_callback) = 0;
54 // Creates a new fully configured instance of an UploadJob. This method
55 // will be called exactly once per every system log upload.
56 virtual scoped_ptr<UploadJob> CreateUploadJob(
57 const GURL& upload_url,
58 UploadJob::Delegate* delegate) = 0;
61 // Constructor. Callers can inject their own Delegate. A nullptr can be passed
62 // for |syslog_delegate| to use the default implementation.
63 explicit SystemLogUploader(
64 scoped_ptr<Delegate> syslog_delegate,
65 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
67 ~SystemLogUploader() override;
69 // Returns the time of the last upload attempt, or Time(0) if no upload has
70 // ever happened.
71 base::Time last_upload_attempt() const { return last_upload_attempt_; }
73 // UploadJob::Delegate:
74 // Callbacks handle success and failure results of upload, destroy the
75 // upload job.
76 void OnSuccess() override;
77 void OnFailure(UploadJob::ErrorCode error_code) override;
79 // Remove lines from |data| that contain common PII (IP addresses, SSIDs, URLs
80 // e-mail addresses).
81 static std::string RemoveSensitiveData(const std::string& data);
83 private:
84 // Updates the system log upload enabled field from settings.
85 void RefreshUploadSettings();
87 // Starts the system log loading process.
88 void StartLogUpload();
90 // The callback is invoked by the Delegate if system logs have been loaded
91 // from disk, uploads system logs.
92 void UploadSystemLogs(scoped_ptr<SystemLogs> system_logs);
94 // Helper method that figures out when the next system log upload should
95 // be scheduled.
96 void ScheduleNextSystemLogUpload(base::TimeDelta frequency);
98 // The number of consequent retries after the failed uploads.
99 int retry_count_;
101 // How long to wait between system log uploads.
102 base::TimeDelta upload_frequency_;
104 // The time the last upload attempt was performed.
105 base::Time last_upload_attempt_;
107 // TaskRunner used for scheduling upload tasks.
108 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
110 // The upload job that is re-created on every system log upload.
111 scoped_ptr<UploadJob> upload_job_;
113 // The Delegate is used to load system logs and create UploadJobs.
114 scoped_ptr<Delegate> syslog_delegate_;
116 // True if system log upload is enabled. Kept cached in this object because
117 // CrosSettings can switch to an unstrusted state temporarily, and we want to
118 // use the last-known trusted values.
119 bool upload_enabled_;
121 // Observer to changes in system log upload settings.
122 scoped_ptr<chromeos::CrosSettings::ObserverSubscription>
123 upload_enabled_observer_;
125 base::ThreadChecker thread_checker_;
127 // Note: This should remain the last member so it'll be destroyed and
128 // invalidate the weak pointers before any other members are destroyed.
129 base::WeakPtrFactory<SystemLogUploader> weak_factory_;
131 DISALLOW_COPY_AND_ASSIGN(SystemLogUploader);
134 } // namespace policy
136 #endif // CHROME_BROWSER_CHROMEOS_POLICY_SYSTEM_LOG_UPLOADER_H_