blob: 6252ad295224991084577890c08d8a6d07bdbed0 [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
23#include <android/os/BnIncidentManager.h>
24#include <utils/Looper.h>
25
26#include <deque>
27#include <mutex>
28
Yi Jin4e843102018-02-14 15:36:18 -080029#include "Throttler.h"
30
Yi Jin6cacbcb2018-03-30 14:04:52 -070031namespace android {
32namespace os {
33namespace incidentd {
34
Joe Onorato1754d742016-11-21 17:51:35 -080035using namespace android;
36using namespace android::base;
37using namespace android::binder;
38using namespace android::os;
Joe Onorato1754d742016-11-21 17:51:35 -080039
40// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -080041class ReportRequestQueue : public virtual RefBase {
Joe Onorato1754d742016-11-21 17:51:35 -080042public:
43 ReportRequestQueue();
44 virtual ~ReportRequestQueue();
45
46 void addRequest(const sp<ReportRequest>& request);
47 sp<ReportRequest> getNextRequest();
48
49private:
50 mutex mLock;
51 deque<sp<ReportRequest> > mQueue;
52};
53
Joe Onorato1754d742016-11-21 17:51:35 -080054// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -080055class ReportHandler : public MessageHandler {
Joe Onorato1754d742016-11-21 17:51:35 -080056public:
Yi Jin4e843102018-02-14 15:36:18 -080057 ReportHandler(const sp<Looper>& handlerLooper, const sp<ReportRequestQueue>& queue,
58 const sp<Throttler>& throttler);
Joe Onorato1754d742016-11-21 17:51:35 -080059 virtual ~ReportHandler();
60
61 virtual void handleMessage(const Message& message);
62
63 /**
64 * Adds a ReportRequest to the queue.
65 */
66 void scheduleRunReport(const sp<ReportRequest>& request);
67
68 /**
69 * Resets mBacklogDelay to the default and schedules sending
70 * the messages to dropbox.
71 */
72 void scheduleSendBacklogToDropbox();
73
74private:
75 mutex mLock;
76 nsecs_t mBacklogDelay;
77 sp<Looper> mHandlerLooper;
78 sp<ReportRequestQueue> mQueue;
Yi Jin4e843102018-02-14 15:36:18 -080079 sp<Throttler> mThrottler;
Joe Onorato1754d742016-11-21 17:51:35 -080080
81 /**
82 * Runs all of the reports that have been queued.
83 */
84 void run_report();
85
86 /**
87 * Schedules a dropbox task mBacklogDelay nanoseconds from now.
88 */
89 void schedule_send_backlog_to_dropbox_locked();
90
91 /**
92 * Sends the backlog to the dropbox service.
93 */
94 void send_backlog_to_dropbox();
95};
96
Joe Onorato1754d742016-11-21 17:51:35 -080097// ================================================================================
98class IncidentService : public BnIncidentManager {
99public:
100 IncidentService(const sp<Looper>& handlerLooper);
101 virtual ~IncidentService();
102
103 virtual Status reportIncident(const IncidentReportArgs& args);
104
105 virtual Status reportIncidentToStream(const IncidentReportArgs& args,
Yi Jinb592e3b2018-02-01 15:17:04 -0800106 const sp<IIncidentReportStatusListener>& listener,
107 const unique_fd& stream);
Joe Onorato1754d742016-11-21 17:51:35 -0800108
109 virtual Status systemRunning();
110
Yi Jinb592e3b2018-02-01 15:17:04 -0800111 // Implement commands for debugging purpose.
112 virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply,
113 uint32_t flags) override;
114 virtual status_t command(FILE* in, FILE* out, FILE* err, Vector<String8>& args);
Mike Ma85434ec2018-11-27 10:32:31 -0800115 virtual status_t dump(int fd, const Vector<String16>& args);
Yi Jinb592e3b2018-02-01 15:17:04 -0800116
Joe Onorato1754d742016-11-21 17:51:35 -0800117private:
118 sp<ReportRequestQueue> mQueue;
119 sp<ReportHandler> mHandler;
Yi Jin4e843102018-02-14 15:36:18 -0800120 sp<Throttler> mThrottler;
Yi Jinb592e3b2018-02-01 15:17:04 -0800121
122 /**
123 * Commands print out help.
124 */
125 status_t cmd_help(FILE* out);
126
127 /**
128 * Commands related to privacy filtering.
129 */
130 status_t cmd_privacy(FILE* in, FILE* out, FILE* err, Vector<String8>& args);
Joe Onorato1754d742016-11-21 17:51:35 -0800131};
132
Yi Jin6cacbcb2018-03-30 14:04:52 -0700133} // namespace incidentd
134} // namespace os
135} // namespace android
136
Yi Jinb592e3b2018-02-01 15:17:04 -0800137#endif // INCIDENT_SERVICE_H