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 #include <android/os/IncidentManager.h>
     18 
     19 namespace android {
     20 namespace os {
     21 
     22 // ============================================================
     23 IncidentManager::IncidentReport::IncidentReport()
     24         :mTimestampNs(0),
     25          mPrivacyPolicy(0),
     26          mFileDescriptor(-1) {
     27 }
     28 
     29 IncidentManager::IncidentReport::~IncidentReport() {
     30     if (mFileDescriptor >= 0) {
     31         close(mFileDescriptor);
     32     }
     33 }
     34 
     35 status_t IncidentManager::IncidentReport::writeToParcel(Parcel* out) const {
     36     status_t err;
     37 
     38     err = out->writeInt64(mTimestampNs);
     39     if (err != NO_ERROR) {
     40         return err;
     41     }
     42 
     43 
     44     err = out->writeInt32(mPrivacyPolicy);
     45     if (err != NO_ERROR) {
     46         return err;
     47     }
     48 
     49     if (mFileDescriptor >= 0) {
     50         err = out->writeInt32(1);
     51         if (err != NO_ERROR) {
     52             return err;
     53         }
     54 
     55         err = out->writeDupParcelFileDescriptor(mFileDescriptor);
     56         if (err != NO_ERROR) {
     57             return err;
     58         }
     59 
     60     } else {
     61         err = out->writeInt32(0);
     62         if (err != NO_ERROR) {
     63             return err;
     64         }
     65     }
     66 
     67     return NO_ERROR;
     68 }
     69 
     70 status_t IncidentManager::IncidentReport::readFromParcel(const Parcel* in) {
     71     status_t err;
     72     int32_t hasField;
     73 
     74     err = in->readInt64(&mTimestampNs);
     75     if (err != NO_ERROR) {
     76         return err;
     77     }
     78 
     79     err = in->readInt32(&mPrivacyPolicy);
     80     if (err != NO_ERROR) {
     81         return err;
     82     }
     83 
     84     err = in->readInt32(&hasField);
     85     if (err != NO_ERROR) {
     86         return err;
     87     }
     88 
     89     if (hasField) {
     90         int fd = in->readParcelFileDescriptor();
     91         if (fd >= 0) {
     92             mFileDescriptor = dup(fd);
     93             if (mFileDescriptor < 0) {
     94                 return -errno;
     95             }
     96         }
     97     }
     98 
     99     return NO_ERROR;
    100 }
    101 
    102 status_t IncidentManager::IncidentReport::setFileDescriptor(int fd) {
    103     if (mFileDescriptor >= 0) {
    104         close(mFileDescriptor);
    105     }
    106     if (fd < 0) {
    107         mFileDescriptor = -1;
    108     } else {
    109         mFileDescriptor = dup(fd);
    110         if (mFileDescriptor < 0) {
    111             return -errno;
    112         }
    113     }
    114     return NO_ERROR;
    115 }
    116 
    117 void IncidentManager::IncidentReport::takeFileDescriptor(int fd) {
    118     if (mFileDescriptor >= 0) {
    119         close(mFileDescriptor);
    120     }
    121     if (fd < 0) {
    122         mFileDescriptor = -1;
    123     } else {
    124         mFileDescriptor = fd;
    125     }
    126 }
    127 
    128 // ============================================================
    129 IncidentManager::~IncidentManager() {
    130 }
    131 
    132 }
    133 }
    134 
    135 
    136