Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2019 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 #include "WorkDirectory.h"
     19 
     20 #include <android/os/BnIncidentAuthListener.h>
     21 #include <android/os/IIncidentCompanion.h>
     22 #include <frameworks/base/cmds/incidentd/src/report_file.pb.h>
     23 
     24 namespace android {
     25 namespace os {
     26 namespace incidentd {
     27 
     28 using android::binder::Status;
     29 using android::os::BnIncidentAuthListener;
     30 using android::os::IIncidentCompanion;
     31 
     32 class ReportHandler;
     33 
     34 class Broadcaster : public virtual RefBase {
     35 public:
     36     enum broadcast_status_t {
     37         BROADCASTS_FINISHED = 0,
     38         BROADCASTS_REPEAT = 1,
     39         BROADCASTS_BACKOFF = 2
     40     };
     41 
     42     Broadcaster(const sp<WorkDirectory>& workDirectory);
     43 
     44     void setHandler(const sp<ReportHandler>& handler);
     45 
     46     /**
     47      * Reset the beginning timestamp for broadcasts.  Call this when
     48      * the system_server restarts.
     49      */
     50     void reset();
     51 
     52     /**
     53      * Remove the history record for the broadcasts, including pending authorizations
     54      * if necessary.
     55      */
     56     void clearBroadcasts(const string& pkg, const string& cls, const string& id);
     57     void clearPackageBroadcasts(const string& pkg);
     58 
     59     /**
     60      * Send whichever broadcasts have been pending.
     61      */
     62     broadcast_status_t sendBroadcasts();
     63 
     64 private:
     65     struct ReportId {
     66         ReportId();
     67         ReportId(const ReportId& that);
     68         ReportId(const string& i, const string& p, const string& c);
     69         ~ReportId();
     70 
     71         bool operator<(const ReportId& that) const;
     72 
     73         string id;
     74         string pkg;
     75         string cls;
     76     };
     77 
     78     class ConsentListener : public BnIncidentAuthListener {
     79       public:
     80         ConsentListener(const sp<Broadcaster>& broadcaster, const ReportId& reportId);
     81         virtual ~ConsentListener();
     82         virtual Status onReportApproved();
     83         virtual Status onReportDenied();
     84       private:
     85         sp<Broadcaster> mBroadcaster;
     86         ReportId mId;
     87     };
     88 
     89     struct ReportStatus {
     90         ReportStatus();
     91         ReportStatus(const ReportStatus& that);
     92         ~ReportStatus();
     93 
     94         bool approval_sent;
     95         bool ready_sent;
     96         sp<ConsentListener> listener;
     97     };
     98 
     99     sp<ReportHandler> mReportHandler;
    100     sp<WorkDirectory> mWorkDirectory;
    101 
    102     // protected by mLock
    103     mutex mLock;
    104     map<ReportId,ReportStatus> mHistory; // what we sent so we don't send it again
    105     int64_t mLastSent;
    106 
    107     void set_last_sent(int64_t timestamp);
    108     int64_t get_last_sent();
    109     void print_report_statuses() const;
    110     status_t send_approval_broadcasts(const string& id, const string& pkg, const string& cls);
    111     void report_approved(const ReportId& reportId);
    112     void report_denied(const ReportId& reportId);
    113     status_t send_report_ready_broadcasts(const string& id, const string& pkg, const string& cls);
    114     status_t send_to_dropbox(const sp<ReportFile>& file, const IncidentReportArgs& args);
    115     bool was_approval_sent(const string& id, const string& pkg, const string& cls);
    116     void set_approval_sent(const string& id, const string& pkg, const string& cls,
    117             const sp<ConsentListener>& listener);
    118     bool was_ready_sent(const string& id, const string& pkg, const string& cls);
    119     void set_ready_sent(const string& id, const string& pkg, const string& cls);
    120     sp<IIncidentCompanion> get_incident_companion();
    121 };
    122 
    123 
    124 }  // namespace incidentd
    125 }  // namespace os
    126 }  // namespace android
    127 
    128