blob: 49fc566d71af96352e28291255a05c5eef7b2d6a [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
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 */
Yi Jinb592e3b2018-02-01 15:17:04 -080016#pragma once
Joe Onorato1754d742016-11-21 17:51:35 -080017
18#ifndef INCIDENT_SERVICE_H
19#define INCIDENT_SERVICE_H
20
21#include "Reporter.h"
22
Joe Onorato99598ee2019-02-11 15:55:13 +000023#include "Broadcaster.h"
24#include "Throttler.h"
25#include "WorkDirectory.h"
26
Joe Onorato1754d742016-11-21 17:51:35 -080027#include <android/os/BnIncidentManager.h>
28#include <utils/Looper.h>
29
Joe Onorato99598ee2019-02-11 15:55:13 +000030#include <vector>
Joe Onorato1754d742016-11-21 17:51:35 -080031#include <mutex>
32
Yi Jin4e843102018-02-14 15:36:18 -080033
Yi Jin6cacbcb2018-03-30 14:04:52 -070034namespace android {
35namespace os {
36namespace incidentd {
37
Joe Onorato1754d742016-11-21 17:51:35 -080038using namespace android;
39using namespace android::base;
40using namespace android::binder;
41using namespace android::os;
Joe Onorato1754d742016-11-21 17:51:35 -080042
Mike Ma643de922019-12-17 10:56:17 -080043class BringYourOwnSection;
44
Joe Onorato1754d742016-11-21 17:51:35 -080045// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -080046class ReportHandler : public MessageHandler {
Joe Onorato1754d742016-11-21 17:51:35 -080047public:
Joe Onorato99598ee2019-02-11 15:55:13 +000048 ReportHandler(const sp<WorkDirectory>& workDirectory,
Mike Ma643de922019-12-17 10:56:17 -080049 const sp<Broadcaster>& broadcaster,
50 const sp<Looper>& handlerLooper,
51 const sp<Throttler>& throttler,
52 const vector<BringYourOwnSection*>& registeredSections);
Joe Onorato1754d742016-11-21 17:51:35 -080053 virtual ~ReportHandler();
54
55 virtual void handleMessage(const Message& message);
56
57 /**
Joe Onorato99598ee2019-02-11 15:55:13 +000058 * Schedule a report for the "main" report, where it will be delivered to
59 * the uploaders and/or dropbox.
Joe Onorato1754d742016-11-21 17:51:35 -080060 */
Joe Onorato99598ee2019-02-11 15:55:13 +000061 void schedulePersistedReport(const IncidentReportArgs& args);
62
63 /**
64 * Adds a ReportRequest to the queue for one that has a listener an and fd
65 */
66 void scheduleStreamingReport(const IncidentReportArgs& args,
67 const sp<IIncidentReportStatusListener>& listener,
68 int streamFd);
Joe Onorato1754d742016-11-21 17:51:35 -080069
70 /**
71 * Resets mBacklogDelay to the default and schedules sending
72 * the messages to dropbox.
73 */
Joe Onorato99598ee2019-02-11 15:55:13 +000074 void scheduleSendBacklog();
Joe Onorato1754d742016-11-21 17:51:35 -080075
76private:
77 mutex mLock;
Joe Onorato99598ee2019-02-11 15:55:13 +000078
79 sp<WorkDirectory> mWorkDirectory;
80 sp<Broadcaster> mBroadcaster;
81
Joe Onorato1754d742016-11-21 17:51:35 -080082 sp<Looper> mHandlerLooper;
Joe Onorato99598ee2019-02-11 15:55:13 +000083 nsecs_t mBacklogDelay;
Yi Jin4e843102018-02-14 15:36:18 -080084 sp<Throttler> mThrottler;
Joe Onorato1754d742016-11-21 17:51:35 -080085
Mike Ma643de922019-12-17 10:56:17 -080086 const vector<BringYourOwnSection*>& mRegisteredSections;
87
Joe Onorato99598ee2019-02-11 15:55:13 +000088 sp<ReportBatch> mBatch;
89
Joe Onorato1754d742016-11-21 17:51:35 -080090 /**
91 * Runs all of the reports that have been queued.
92 */
Joe Onorato99598ee2019-02-11 15:55:13 +000093 void take_report();
Joe Onorato1754d742016-11-21 17:51:35 -080094
95 /**
Joe Onorato99598ee2019-02-11 15:55:13 +000096 * Schedules permission controller approve the reports.
Joe Onorato1754d742016-11-21 17:51:35 -080097 */
Joe Onorato99598ee2019-02-11 15:55:13 +000098 void schedule_send_approvals_locked();
Joe Onorato1754d742016-11-21 17:51:35 -080099
100 /**
Joe Onorato99598ee2019-02-11 15:55:13 +0000101 * Sends the approvals to the PermissionController
Joe Onorato1754d742016-11-21 17:51:35 -0800102 */
Joe Onorato99598ee2019-02-11 15:55:13 +0000103 void send_approvals();
104
105 /**
106 * Schedules the broadcasts that reports are complete mBacklogDelay nanoseconds from now.
107 * The delay is because typically when an incident report is taken, the system is not
108 * really in a happy state. So we wait a bit before sending the report to let things
109 * quiet down if they can. The urgency is in taking the report, not sharing the report.
110 * However, we don
111 */
112 void schedule_send_broadcasts_locked();
113
114 /**
115 * Sends the broadcasts to the dropbox service.
116 */
117 void send_broadcasts();
Joe Onorato1754d742016-11-21 17:51:35 -0800118};
119
Joe Onorato1754d742016-11-21 17:51:35 -0800120// ================================================================================
121class IncidentService : public BnIncidentManager {
122public:
Chih-Hung Hsieh7a88a932018-12-20 13:45:04 -0800123 explicit IncidentService(const sp<Looper>& handlerLooper);
Joe Onorato1754d742016-11-21 17:51:35 -0800124 virtual ~IncidentService();
125
126 virtual Status reportIncident(const IncidentReportArgs& args);
127
128 virtual Status reportIncidentToStream(const IncidentReportArgs& args,
Yi Jinb592e3b2018-02-01 15:17:04 -0800129 const sp<IIncidentReportStatusListener>& listener,
Jiyong Parkb8ba2342019-11-25 11:03:38 +0900130 unique_fd stream);
Joe Onorato1754d742016-11-21 17:51:35 -0800131
Jiyong Parkb8ba2342019-11-25 11:03:38 +0900132 virtual Status reportIncidentToDumpstate(unique_fd stream,
Mike Ma5a57d792019-08-21 14:52:46 -0700133 const sp<IIncidentReportStatusListener>& listener);
134
Mike Ma643de922019-12-17 10:56:17 -0800135 virtual Status registerSection(int id, const String16& name,
136 const sp<IIncidentDumpCallback>& callback);
137
138 virtual Status unregisterSection(int id);
139
Joe Onorato1754d742016-11-21 17:51:35 -0800140 virtual Status systemRunning();
141
Joe Onorato99598ee2019-02-11 15:55:13 +0000142 virtual Status getIncidentReportList(const String16& pkg, const String16& cls,
143 vector<String16>* result);
144
145 virtual Status getIncidentReport(const String16& pkg, const String16& cls,
146 const String16& id, IncidentManager::IncidentReport* result);
147
148 virtual Status deleteIncidentReports(const String16& pkg, const String16& cls,
149 const String16& id);
150
151 virtual Status deleteAllIncidentReports(const String16& pkg);
152
Yi Jinb592e3b2018-02-01 15:17:04 -0800153 // Implement commands for debugging purpose.
154 virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply,
155 uint32_t flags) override;
156 virtual status_t command(FILE* in, FILE* out, FILE* err, Vector<String8>& args);
157
Joe Onorato1754d742016-11-21 17:51:35 -0800158private:
Joe Onorato99598ee2019-02-11 15:55:13 +0000159 sp<WorkDirectory> mWorkDirectory;
160 sp<Broadcaster> mBroadcaster;
Joe Onorato1754d742016-11-21 17:51:35 -0800161 sp<ReportHandler> mHandler;
Yi Jin4e843102018-02-14 15:36:18 -0800162 sp<Throttler> mThrottler;
Mike Ma643de922019-12-17 10:56:17 -0800163 vector<BringYourOwnSection*> mRegisteredSections;
Yi Jinb592e3b2018-02-01 15:17:04 -0800164
165 /**
166 * Commands print out help.
167 */
168 status_t cmd_help(FILE* out);
169
170 /**
171 * Commands related to privacy filtering.
172 */
173 status_t cmd_privacy(FILE* in, FILE* out, FILE* err, Vector<String8>& args);
Joe Onorato1754d742016-11-21 17:51:35 -0800174};
175
Yi Jin6cacbcb2018-03-30 14:04:52 -0700176} // namespace incidentd
177} // namespace os
178} // namespace android
179
Yi Jinb592e3b2018-02-01 15:17:04 -0800180#endif // INCIDENT_SERVICE_H