1 : // Copyright 2014 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #ifndef SYZYGY_KASKO_REPORT_REPOSITORY_H_
16 : #define SYZYGY_KASKO_REPORT_REPOSITORY_H_
17 :
18 : #include <map>
19 : #include "base/callback.h"
20 : #include "base/macros.h"
21 : #include "base/files/file_path.h"
22 : #include "base/strings/string16.h"
23 : #include "base/time/time.h"
24 :
25 m : namespace kasko {
26 :
27 : // Manages a repository of crash reports that are pending upload. Tracks upload
28 : // attempts and retry intervals and delegates to a permanent failure handler
29 : // after three failed attempts for a given report.
30 : //
31 : // Any number of ReportRepository instances may be used to store reports (via
32 : // StoreReport). Only a single instance should be used for uploading (via
33 : // UploadPendingReport). It's the client's responsibility to enforce this
34 : // requirement.
35 m : class ReportRepository {
36 m : public:
37 : // Attempts to upload the minidump at the specified file path with the given
38 : // crash keys. Returns true if successful.
39 m : typedef base::Callback<bool(
40 m : const base::FilePath&,
41 m : const std::map<base::string16, base::string16>&)> Uploader;
42 :
43 : // Handles a report that has exceeded the maximum retry attempts. The two file
44 : // paths point to the minidump file and the crash keys file (formatted as a
45 : // JSON dictionary). The handler may move the files. If they are left after
46 : // handling they will be deleted.
47 m : typedef base::Callback<void(const base::FilePath&, const base::FilePath&)>
48 m : PermanentFailureHandler;
49 :
50 : // Provides the current time.
51 m : typedef base::Callback<base::Time(void)> TimeSource;
52 :
53 : // Instantiates a repository.
54 : // @param repository_path The directory where reports are to be stored.
55 : // @param retry_interval The minimum time that must elapse between upload
56 : // attempts for a given report.
57 : // @param time_source A source for the current time.
58 : // @param uploader Used to upload reports.
59 : // @param permanent_failure_handler Used to handle reports that have exceeded
60 : // the maximum retry attempts.
61 m : ReportRepository(const base::FilePath& repository_path,
62 m : const base::TimeDelta& retry_interval,
63 m : const TimeSource& time_source,
64 m : const Uploader& uploader,
65 m : const PermanentFailureHandler& permanent_failure_handler);
66 :
67 m : ~ReportRepository();
68 :
69 : // Stores the provided report in the repository. Does not attempt an upload at
70 : // this time. The provided file will be moved or deleted by this method.
71 : // @param minidump_path The path to the minidump file.
72 : // @param crash_keys The crash keys for the report.
73 m : void StoreReport(
74 m : const base::FilePath& minidump_path,
75 m : const std::map<base::string16, base::string16>& crash_keys);
76 :
77 : // Attempts to upload a pending report, if any. A report is pending if it has
78 : // never been submitted to an upload attempt or if its most recent upload
79 : // attempt is older than the configured retry interval.
80 : // @returns true if there are no pending reports or a report was successfully
81 : // uploaded.
82 m : bool UploadPendingReport();
83 :
84 : // @returns true if UploadPendingReport would attempt to upload a report.
85 m : bool HasPendingReports();
86 :
87 m : private:
88 m : base::FilePath repository_path_;
89 m : base::TimeDelta retry_interval_;
90 m : TimeSource time_source_;
91 m : Uploader uploader_;
92 m : PermanentFailureHandler permanent_failure_handler_;
93 :
94 m : DISALLOW_COPY_AND_ASSIGN(ReportRepository);
95 m : };
96 :
97 m : } // namespace kasko
98 :
99 : #endif // SYZYGY_KASKO_REPORT_REPOSITORY_H_
|