Home | History | Annotate | Download | only in src
      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 
     31 namespace android {
     32 namespace os {
     33 namespace incidentd {
     34 
     35 using namespace android;
     36 using namespace android::base;
     37 using namespace android::binder;
     38 using namespace android::os;
     39 
     40 // ================================================================================
     41 class ReportRequestQueue : public virtual RefBase {
     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 
     54 // ================================================================================
     55 class ReportHandler : public MessageHandler {
     56 public:
     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 
     74 private:
     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 // ================================================================================
     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,
    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 
    116 private:
    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