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 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace os { |
| 33 | namespace incidentd { |
| 34 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 35 | using namespace android; |
| 36 | using namespace android::base; |
| 37 | using namespace android::binder; |
| 38 | using namespace android::os; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 39 | |
| 40 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 41 | class ReportRequestQueue : public virtual RefBase { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 42 | public: |
| 43 | ReportRequestQueue(); |
| 44 | virtual ~ReportRequestQueue(); |
| 45 | |
| 46 | void addRequest(const sp<ReportRequest>& request); |
| 47 | sp<ReportRequest> getNextRequest(); |
| 48 | |
| 49 | private: |
| 50 | mutex mLock; |
| 51 | deque<sp<ReportRequest> > mQueue; |
| 52 | }; |
| 53 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 54 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 55 | class ReportHandler : public MessageHandler { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 56 | public: |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 57 | ReportHandler(const sp<Looper>& handlerLooper, const sp<ReportRequestQueue>& queue, |
| 58 | const sp<Throttler>& throttler); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 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 | |
| 74 | private: |
| 75 | mutex mLock; |
| 76 | nsecs_t mBacklogDelay; |
| 77 | sp<Looper> mHandlerLooper; |
| 78 | sp<ReportRequestQueue> mQueue; |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 79 | sp<Throttler> mThrottler; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 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 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 97 | // ================================================================================ |
| 98 | class IncidentService : public BnIncidentManager { |
| 99 | public: |
| 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 Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 106 | const sp<IIncidentReportStatusListener>& listener, |
| 107 | const unique_fd& stream); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 108 | |
| 109 | virtual Status systemRunning(); |
| 110 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 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 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 116 | private: |
| 117 | sp<ReportRequestQueue> mQueue; |
| 118 | sp<ReportHandler> mHandler; |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 119 | sp<Throttler> mThrottler; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 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); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 132 | } // namespace incidentd |
| 133 | } // namespace os |
| 134 | } // namespace android |
| 135 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 136 | #endif // INCIDENT_SERVICE_H |