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 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
29#include "Throttler.h"
30
31namespace android {
32namespace os {
33namespace incidentd {
34
35using namespace android;
36using namespace android::base;
37using namespace android::binder;
38using namespace android::os;
39
40// ================================================================================
41class ReportRequestQueue : public virtual RefBase {
42public:
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
54// ================================================================================
55class ReportHandler : public MessageHandler {
56public:
57    ReportHandler(const sp<Looper>& handlerLooper, const sp<ReportRequestQueue>& queue,
58                  const sp<Throttler>& throttler);
59    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;
79    sp<Throttler> mThrottler;
80
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
97// ================================================================================
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,
106                                          const sp<IIncidentReportStatusListener>& listener,
107                                          const unique_fd& stream);
108
109    virtual Status systemRunning();
110
111    // 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);
115
116private:
117    sp<ReportRequestQueue> mQueue;
118    sp<ReportHandler> mHandler;
119    sp<Throttler> mThrottler;
120
121    /**
122     * Commands print out help.
123     */
124    status_t cmd_help(FILE* out);
125
126    /**
127     * Commands related to privacy filtering.
128     */
129    status_t cmd_privacy(FILE* in, FILE* out, FILE* err, Vector<String8>& args);
130};
131
132}  // namespace incidentd
133}  // namespace os
134}  // namespace android
135
136#endif  // INCIDENT_SERVICE_H
137