blob: 6481321bbd69c204a67dcadb0389b24271a1ba84 [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
43// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -080044class ReportHandler : public MessageHandler {
Joe Onorato1754d742016-11-21 17:51:35 -080045public:
Joe Onorato99598ee2019-02-11 15:55:13 +000046 ReportHandler(const sp<WorkDirectory>& workDirectory,
47 const sp<Broadcaster>& broadcaster, const sp<Looper>& handlerLooper,
48 const sp<Throttler>& throttler);
Joe Onorato1754d742016-11-21 17:51:35 -080049 virtual ~ReportHandler();
50
51 virtual void handleMessage(const Message& message);
52
53 /**
Joe Onorato99598ee2019-02-11 15:55:13 +000054 * Schedule a report for the "main" report, where it will be delivered to
55 * the uploaders and/or dropbox.
Joe Onorato1754d742016-11-21 17:51:35 -080056 */
Joe Onorato99598ee2019-02-11 15:55:13 +000057 void schedulePersistedReport(const IncidentReportArgs& args);
58
59 /**
60 * Adds a ReportRequest to the queue for one that has a listener an and fd
61 */
62 void scheduleStreamingReport(const IncidentReportArgs& args,
63 const sp<IIncidentReportStatusListener>& listener,
64 int streamFd);
Joe Onorato1754d742016-11-21 17:51:35 -080065
66 /**
67 * Resets mBacklogDelay to the default and schedules sending
68 * the messages to dropbox.
69 */
Joe Onorato99598ee2019-02-11 15:55:13 +000070 void scheduleSendBacklog();
Joe Onorato1754d742016-11-21 17:51:35 -080071
72private:
73 mutex mLock;
Joe Onorato99598ee2019-02-11 15:55:13 +000074
75 sp<WorkDirectory> mWorkDirectory;
76 sp<Broadcaster> mBroadcaster;
77
Joe Onorato1754d742016-11-21 17:51:35 -080078 sp<Looper> mHandlerLooper;
Joe Onorato99598ee2019-02-11 15:55:13 +000079 nsecs_t mBacklogDelay;
Yi Jin4e843102018-02-14 15:36:18 -080080 sp<Throttler> mThrottler;
Joe Onorato1754d742016-11-21 17:51:35 -080081
Joe Onorato99598ee2019-02-11 15:55:13 +000082 sp<ReportBatch> mBatch;
83
Joe Onorato1754d742016-11-21 17:51:35 -080084 /**
85 * Runs all of the reports that have been queued.
86 */
Joe Onorato99598ee2019-02-11 15:55:13 +000087 void take_report();
Joe Onorato1754d742016-11-21 17:51:35 -080088
89 /**
Joe Onorato99598ee2019-02-11 15:55:13 +000090 * Schedules permission controller approve the reports.
Joe Onorato1754d742016-11-21 17:51:35 -080091 */
Joe Onorato99598ee2019-02-11 15:55:13 +000092 void schedule_send_approvals_locked();
Joe Onorato1754d742016-11-21 17:51:35 -080093
94 /**
Joe Onorato99598ee2019-02-11 15:55:13 +000095 * Sends the approvals to the PermissionController
Joe Onorato1754d742016-11-21 17:51:35 -080096 */
Joe Onorato99598ee2019-02-11 15:55:13 +000097 void send_approvals();
98
99 /**
100 * Schedules the broadcasts that reports are complete mBacklogDelay nanoseconds from now.
101 * The delay is because typically when an incident report is taken, the system is not
102 * really in a happy state. So we wait a bit before sending the report to let things
103 * quiet down if they can. The urgency is in taking the report, not sharing the report.
104 * However, we don
105 */
106 void schedule_send_broadcasts_locked();
107
108 /**
109 * Sends the broadcasts to the dropbox service.
110 */
111 void send_broadcasts();
Joe Onorato1754d742016-11-21 17:51:35 -0800112};
113
Joe Onorato1754d742016-11-21 17:51:35 -0800114// ================================================================================
115class IncidentService : public BnIncidentManager {
116public:
Chih-Hung Hsieh7a88a932018-12-20 13:45:04 -0800117 explicit IncidentService(const sp<Looper>& handlerLooper);
Joe Onorato1754d742016-11-21 17:51:35 -0800118 virtual ~IncidentService();
119
120 virtual Status reportIncident(const IncidentReportArgs& args);
121
122 virtual Status reportIncidentToStream(const IncidentReportArgs& args,
Yi Jinb592e3b2018-02-01 15:17:04 -0800123 const sp<IIncidentReportStatusListener>& listener,
124 const unique_fd& stream);
Joe Onorato1754d742016-11-21 17:51:35 -0800125
126 virtual Status systemRunning();
127
Joe Onorato99598ee2019-02-11 15:55:13 +0000128 virtual Status getIncidentReportList(const String16& pkg, const String16& cls,
129 vector<String16>* result);
130
131 virtual Status getIncidentReport(const String16& pkg, const String16& cls,
132 const String16& id, IncidentManager::IncidentReport* result);
133
134 virtual Status deleteIncidentReports(const String16& pkg, const String16& cls,
135 const String16& id);
136
137 virtual Status deleteAllIncidentReports(const String16& pkg);
138
Yi Jinb592e3b2018-02-01 15:17:04 -0800139 // Implement commands for debugging purpose.
140 virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply,
141 uint32_t flags) override;
142 virtual status_t command(FILE* in, FILE* out, FILE* err, Vector<String8>& args);
Mike Ma85434ec2018-11-27 10:32:31 -0800143 virtual status_t dump(int fd, const Vector<String16>& args);
Yi Jinb592e3b2018-02-01 15:17:04 -0800144
Joe Onorato1754d742016-11-21 17:51:35 -0800145private:
Joe Onorato99598ee2019-02-11 15:55:13 +0000146 sp<WorkDirectory> mWorkDirectory;
147 sp<Broadcaster> mBroadcaster;
Joe Onorato1754d742016-11-21 17:51:35 -0800148 sp<ReportHandler> mHandler;
Yi Jin4e843102018-02-14 15:36:18 -0800149 sp<Throttler> mThrottler;
Yi Jinb592e3b2018-02-01 15:17:04 -0800150
151 /**
152 * Commands print out help.
153 */
154 status_t cmd_help(FILE* out);
155
156 /**
157 * Commands related to privacy filtering.
158 */
159 status_t cmd_privacy(FILE* in, FILE* out, FILE* err, Vector<String8>& args);
Joe Onorato1754d742016-11-21 17:51:35 -0800160};
161
Yi Jin6cacbcb2018-03-30 14:04:52 -0700162} // namespace incidentd
163} // namespace os
164} // namespace android
165
Yi Jinb592e3b2018-02-01 15:17:04 -0800166#endif // INCIDENT_SERVICE_H