Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 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 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 16 | #pragma once |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 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 | |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 29 | #include "Throttler.h" |
| 30 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 31 | using namespace android; |
| 32 | using namespace android::base; |
| 33 | using namespace android::binder; |
| 34 | using namespace android::os; |
| 35 | using namespace std; |
| 36 | |
| 37 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 38 | class ReportRequestQueue : public virtual RefBase { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 39 | public: |
| 40 | ReportRequestQueue(); |
| 41 | virtual ~ReportRequestQueue(); |
| 42 | |
| 43 | void addRequest(const sp<ReportRequest>& request); |
| 44 | sp<ReportRequest> getNextRequest(); |
| 45 | |
| 46 | private: |
| 47 | mutex mLock; |
| 48 | deque<sp<ReportRequest> > mQueue; |
| 49 | }; |
| 50 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 51 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 52 | class ReportHandler : public MessageHandler { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 53 | public: |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 54 | ReportHandler(const sp<Looper>& handlerLooper, const sp<ReportRequestQueue>& queue, |
| 55 | const sp<Throttler>& throttler); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 56 | virtual ~ReportHandler(); |
| 57 | |
| 58 | virtual void handleMessage(const Message& message); |
| 59 | |
| 60 | /** |
| 61 | * Adds a ReportRequest to the queue. |
| 62 | */ |
| 63 | void scheduleRunReport(const sp<ReportRequest>& request); |
| 64 | |
| 65 | /** |
| 66 | * Resets mBacklogDelay to the default and schedules sending |
| 67 | * the messages to dropbox. |
| 68 | */ |
| 69 | void scheduleSendBacklogToDropbox(); |
| 70 | |
| 71 | private: |
| 72 | mutex mLock; |
| 73 | nsecs_t mBacklogDelay; |
| 74 | sp<Looper> mHandlerLooper; |
| 75 | sp<ReportRequestQueue> mQueue; |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 76 | sp<Throttler> mThrottler; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * Runs all of the reports that have been queued. |
| 80 | */ |
| 81 | void run_report(); |
| 82 | |
| 83 | /** |
| 84 | * Schedules a dropbox task mBacklogDelay nanoseconds from now. |
| 85 | */ |
| 86 | void schedule_send_backlog_to_dropbox_locked(); |
| 87 | |
| 88 | /** |
| 89 | * Sends the backlog to the dropbox service. |
| 90 | */ |
| 91 | void send_backlog_to_dropbox(); |
| 92 | }; |
| 93 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 94 | // ================================================================================ |
| 95 | class IncidentService : public BnIncidentManager { |
| 96 | public: |
| 97 | IncidentService(const sp<Looper>& handlerLooper); |
| 98 | virtual ~IncidentService(); |
| 99 | |
| 100 | virtual Status reportIncident(const IncidentReportArgs& args); |
| 101 | |
| 102 | virtual Status reportIncidentToStream(const IncidentReportArgs& args, |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 103 | const sp<IIncidentReportStatusListener>& listener, |
| 104 | const unique_fd& stream); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 105 | |
| 106 | virtual Status systemRunning(); |
| 107 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 108 | // Implement commands for debugging purpose. |
| 109 | virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 110 | uint32_t flags) override; |
| 111 | virtual status_t command(FILE* in, FILE* out, FILE* err, Vector<String8>& args); |
| 112 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 113 | private: |
| 114 | sp<ReportRequestQueue> mQueue; |
| 115 | sp<ReportHandler> mHandler; |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 116 | sp<Throttler> mThrottler; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * Commands print out help. |
| 120 | */ |
| 121 | status_t cmd_help(FILE* out); |
| 122 | |
| 123 | /** |
| 124 | * Commands related to privacy filtering. |
| 125 | */ |
| 126 | status_t cmd_privacy(FILE* in, FILE* out, FILE* err, Vector<String8>& args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 129 | #endif // INCIDENT_SERVICE_H |