1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #ifndef REPORTER_H
19 #define REPORTER_H
20 
21 #include <android/os/IIncidentReportStatusListener.h>
22 #include <android/os/IncidentReportArgs.h>
23 
24 #include <map>
25 #include <string>
26 #include <vector>
27 
28 #include <time.h>
29 
30 #include "Throttler.h"
31 #include "frameworks/base/libs/incident/proto/android/os/metadata.pb.h"
32 
33 namespace android {
34 namespace os {
35 namespace incidentd {
36 
37 // ================================================================================
38 struct ReportRequest : public virtual RefBase {
39     IncidentReportArgs args;
40     sp<IIncidentReportStatusListener> listener;
41     int fd;
42     status_t err;
43 
44     ReportRequest(const IncidentReportArgs& args, const sp<IIncidentReportStatusListener>& listener,
45                   int fd);
46     virtual ~ReportRequest();
47 
48     bool ok();  // returns true if the request is ok for write.
49 };
50 
51 // ================================================================================
52 class ReportRequestSet {
53 public:
54     ReportRequestSet();
55     ~ReportRequestSet();
56 
57     void add(const sp<ReportRequest>& request);
58     void setMainFd(int fd);
59     void setMainDest(int dest);
60 
61     typedef vector<sp<ReportRequest>>::iterator iterator;
62 
begin()63     iterator begin() { return mRequests.begin(); }
end()64     iterator end() { return mRequests.end(); }
65 
mainFd()66     int mainFd() { return mMainFd; }
mainDest()67     int mainDest() { return mMainDest; }
metadata()68     IncidentMetadata& metadata() { return mMetadata; }
allSectionStats()69     map<int, IncidentMetadata::SectionStats>& allSectionStats() { return mSectionStats; }
70 
71     bool containsSection(int id);
72     IncidentMetadata::SectionStats* sectionStats(int id);
73 
74 private:
75     vector<sp<ReportRequest>> mRequests;
76     IncidentReportArgs mSections;
77     int mMainFd;
78     int mMainDest;
79 
80     IncidentMetadata mMetadata;
81     map<int, IncidentMetadata::SectionStats> mSectionStats;
82 };
83 
84 // ================================================================================
85 class Reporter : public virtual RefBase {
86 public:
87     enum run_report_status_t { REPORT_FINISHED = 0, REPORT_NEEDS_DROPBOX = 1 };
88 
89     ReportRequestSet batch;
90 
91     Reporter();                       // PROD must use this constructor.
92     Reporter(const char* directory);  // For testing purpose only.
93     virtual ~Reporter();
94 
95     // Run the report as described in the batch and args parameters.
96     run_report_status_t runReport(size_t* reportByteSize);
97 
98     static run_report_status_t upload_backlog();
99 
100 private:
101     String8 mIncidentDirectory;
102 
103     string mFilename;
104     off_t mMaxSize;
105     size_t mMaxCount;
106     time_t mStartTime;
107 
108     status_t create_file(int* fd);
109 
110     bool isTest = true;  // default to true for testing
111 };
112 
113 }  // namespace incidentd
114 }  // namespace os
115 }  // namespace android
116 
117 #endif  // REPORTER_H
118