Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 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 | |
| 17 | #ifndef REPORTER_H |
| 18 | #define REPORTER_H |
| 19 | |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame^] | 20 | #include "frameworks/base/libs/incident/proto/android/os/metadata.pb.h" |
| 21 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 22 | #include <android/os/IIncidentReportStatusListener.h> |
| 23 | #include <android/os/IncidentReportArgs.h> |
| 24 | |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame^] | 25 | #include <map> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include <time.h> |
| 30 | |
| 31 | using namespace android; |
| 32 | using namespace android::os; |
| 33 | using namespace std; |
| 34 | |
| 35 | // ================================================================================ |
| 36 | struct ReportRequest : public virtual RefBase |
| 37 | { |
| 38 | IncidentReportArgs args; |
| 39 | sp<IIncidentReportStatusListener> listener; |
| 40 | int fd; |
| 41 | status_t err; |
| 42 | |
| 43 | ReportRequest(const IncidentReportArgs& args, |
| 44 | const sp<IIncidentReportStatusListener> &listener, int fd); |
| 45 | virtual ~ReportRequest(); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 46 | |
| 47 | bool ok(); // returns true if the request is ok for write. |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | // ================================================================================ |
| 51 | class ReportRequestSet |
| 52 | { |
| 53 | public: |
| 54 | ReportRequestSet(); |
| 55 | ~ReportRequestSet(); |
| 56 | |
| 57 | void add(const sp<ReportRequest>& request); |
| 58 | void setMainFd(int fd); |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 59 | void setMainDest(int dest); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 60 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 61 | typedef vector<sp<ReportRequest>>::iterator iterator; |
| 62 | |
| 63 | iterator begin() { return mRequests.begin(); } |
| 64 | iterator end() { return mRequests.end(); } |
| 65 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 66 | int mainFd() { return mMainFd; } |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 67 | int mainDest() { return mMainDest; } |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame^] | 68 | IncidentMetadata& metadata() { return mMetadata; } |
| 69 | |
| 70 | bool containsSection(int id); |
| 71 | IncidentMetadata::SectionStats* sectionStats(int id); |
| 72 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 73 | private: |
| 74 | vector<sp<ReportRequest>> mRequests; |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 75 | IncidentReportArgs mSections; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 76 | int mMainFd; |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 77 | int mMainDest; |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame^] | 78 | |
| 79 | IncidentMetadata mMetadata; |
| 80 | map<int, IncidentMetadata::SectionStats*> mSectionStats; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | // ================================================================================ |
| 84 | class Reporter : public virtual RefBase |
| 85 | { |
| 86 | public: |
| 87 | enum run_report_status_t { |
| 88 | REPORT_FINISHED = 0, |
| 89 | REPORT_NEEDS_DROPBOX = 1 |
| 90 | }; |
| 91 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 92 | ReportRequestSet batch; |
| 93 | |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame^] | 94 | Reporter(); // PROD must use this constructor. |
| 95 | Reporter(const char* directory); // For testing purpose only. |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 96 | virtual ~Reporter(); |
| 97 | |
| 98 | // Run the report as described in the batch and args parameters. |
| 99 | run_report_status_t runReport(); |
| 100 | |
| 101 | static run_report_status_t upload_backlog(); |
| 102 | |
| 103 | private: |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 104 | String8 mIncidentDirectory; |
| 105 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 106 | string mFilename; |
| 107 | off_t mMaxSize; |
| 108 | size_t mMaxCount; |
| 109 | time_t mStartTime; |
| 110 | |
| 111 | status_t create_file(int* fd); |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 112 | |
| 113 | bool isTest = true; // default to true for testing |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | |
| 117 | #endif // REPORTER_H |