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 
     17 #ifndef REPORTER_H
     18 #define REPORTER_H
     19 
     20 #include <android/os/IIncidentReportStatusListener.h>
     21 #include <android/os/IncidentReportArgs.h>
     22 
     23 #include <string>
     24 #include <vector>
     25 
     26 #include <time.h>
     27 
     28 using namespace android;
     29 using namespace android::os;
     30 using namespace std;
     31 
     32 // ================================================================================
     33 struct ReportRequest : public virtual RefBase
     34 {
     35     IncidentReportArgs args;
     36     sp<IIncidentReportStatusListener> listener;
     37     int fd;
     38     status_t err;
     39 
     40     ReportRequest(const IncidentReportArgs& args,
     41             const sp<IIncidentReportStatusListener> &listener, int fd);
     42     virtual ~ReportRequest();
     43 };
     44 
     45 // ================================================================================
     46 class ReportRequestSet
     47 {
     48 public:
     49     ReportRequestSet();
     50     ~ReportRequestSet();
     51 
     52     void add(const sp<ReportRequest>& request);
     53     void setMainFd(int fd);
     54 
     55     // Write to all of the fds for the requests. If a write fails, it stops
     56     // writing to that fd and returns NO_ERROR. When we are out of fds to write
     57     // to it returns an error.
     58     status_t write(uint8_t const* buf, size_t size);
     59 
     60     typedef vector<sp<ReportRequest>>::iterator iterator;
     61 
     62     iterator begin() { return mRequests.begin(); }
     63     iterator end() { return mRequests.end(); }
     64 
     65 private:
     66     vector<sp<ReportRequest>> mRequests;
     67     int mWritableCount;
     68     int mMainFd;
     69 };
     70 
     71 // ================================================================================
     72 class Reporter : public virtual RefBase
     73 {
     74 public:
     75     enum run_report_status_t {
     76         REPORT_FINISHED = 0,
     77         REPORT_NEEDS_DROPBOX = 1
     78     };
     79 
     80     IncidentReportArgs args;
     81     ReportRequestSet batch;
     82 
     83     Reporter();
     84     virtual ~Reporter();
     85 
     86     // Run the report as described in the batch and args parameters.
     87     run_report_status_t runReport();
     88 
     89     static run_report_status_t upload_backlog();
     90 
     91 private:
     92     string mFilename;
     93     off_t mMaxSize;
     94     size_t mMaxCount;
     95     time_t mStartTime;
     96 
     97     status_t create_file(int* fd);
     98 };
     99 
    100 
    101 #endif // REPORTER_H
    102