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 INCIDENT_SERVICE_H
18#define INCIDENT_SERVICE_H
19
20#include "Reporter.h"
21
22#include <android/os/BnIncidentManager.h>
23#include <utils/Looper.h>
24
25#include <deque>
26#include <mutex>
27
28using namespace android;
29using namespace android::base;
30using namespace android::binder;
31using namespace android::os;
32using namespace std;
33
34// ================================================================================
35class ReportRequestQueue : public virtual RefBase
36{
37public:
38    ReportRequestQueue();
39    virtual ~ReportRequestQueue();
40
41    void addRequest(const sp<ReportRequest>& request);
42    sp<ReportRequest> getNextRequest();
43
44private:
45    mutex mLock;
46    deque<sp<ReportRequest> > mQueue;
47};
48
49
50// ================================================================================
51class ReportHandler : public MessageHandler
52{
53public:
54    ReportHandler(const sp<Looper>& handlerLooper, const sp<ReportRequestQueue>& queue);
55    virtual ~ReportHandler();
56
57    virtual void handleMessage(const Message& message);
58
59    /**
60     * Adds a ReportRequest to the queue.
61     */
62    void scheduleRunReport(const sp<ReportRequest>& request);
63
64    /**
65     * Resets mBacklogDelay to the default and schedules sending
66     * the messages to dropbox.
67     */
68    void scheduleSendBacklogToDropbox();
69
70private:
71    mutex mLock;
72    nsecs_t mBacklogDelay;
73    sp<Looper> mHandlerLooper;
74    sp<ReportRequestQueue> mQueue;
75
76    /**
77     * Runs all of the reports that have been queued.
78     */
79    void run_report();
80
81    /**
82     * Schedules a dropbox task mBacklogDelay nanoseconds from now.
83     */
84    void schedule_send_backlog_to_dropbox_locked();
85
86    /**
87     * Sends the backlog to the dropbox service.
88     */
89    void send_backlog_to_dropbox();
90};
91
92
93// ================================================================================
94class IncidentService : public BnIncidentManager {
95public:
96    IncidentService(const sp<Looper>& handlerLooper);
97    virtual ~IncidentService();
98
99    virtual Status reportIncident(const IncidentReportArgs& args);
100
101    virtual Status reportIncidentToStream(const IncidentReportArgs& args,
102            const sp<IIncidentReportStatusListener>& listener, const unique_fd& stream);
103
104    virtual Status systemRunning();
105
106private:
107    sp<ReportRequestQueue> mQueue;
108    sp<ReportHandler> mHandler;
109};
110
111
112#endif // INCIDENT_SERVICE_H
113