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 _ANDROID_OS_DROPBOXMANAGER_H
18 #define _ANDROID_OS_DROPBOXMANAGER_H
19 
20 #include <android-base/unique_fd.h>
21 #include <binder/Parcel.h>
22 #include <binder/Parcelable.h>
23 #include <binder/Status.h>
24 #include <utils/RefBase.h>
25 
26 #include <vector>
27 
28 namespace android {
29 namespace os {
30 
31 using namespace android;
32 using namespace android::base;
33 using namespace android::binder;
34 using namespace std;
35 
36 class DropBoxManager : public virtual RefBase
37 {
38 public:
39     enum {
40         IS_EMPTY = 1,
41         IS_TEXT = 2,
42         IS_GZIPPED = 4
43     };
44 
45     DropBoxManager();
46     virtual ~DropBoxManager();
47 
48     static sp<DropBoxManager> create();
49 
50     // Create a new entry with plain text contents.
51     Status addText(const String16& tag, const string& text);
52 
53     // Create a new Entry with byte array contents. Makes a copy of the data.
54     Status addData(const String16& tag, uint8_t const* data, size_t size, int flags);
55 
56     // Create a new Entry from a file. The file will be opened in this process
57     // and a handle will be passed to the system process, so no additional permissions
58     // are required from the system process.  Returns NULL if the file can't be opened.
59     Status addFile(const String16& tag, const string& filename, int flags);
60 
61     // Create a new Entry from an already opened file. Takes ownership of the
62     // file descriptor.
63     Status addFile(const String16& tag, int fd, int flags);
64 
65     class Entry : public virtual RefBase, public Parcelable {
66     public:
67         Entry();
68         virtual ~Entry();
69 
70         virtual status_t writeToParcel(Parcel* out) const;
71         virtual status_t readFromParcel(const Parcel* in);
72 
73         const vector<uint8_t>& getData() const;
74         const unique_fd& getFd() const;
75         int32_t getFlags() const;
76         int64_t getTimestamp() const;
77 
78     private:
79         Entry(const String16& tag, int32_t flags);
80         Entry(const String16& tag, int32_t flags, int fd);
81 
82         String16 mTag;
83         int64_t mTimeMillis;
84         int32_t mFlags;
85 
86         vector<uint8_t> mData;
87         unique_fd mFd;
88 
89         friend class DropBoxManager;
90     };
91 
92     // Get the next entry from the drop box after the specified time.
93     Status getNextEntry(const String16& tag, long msec, Entry* entry);
94 
95 private:
96     enum {
97         HAS_BYTE_ARRAY = 8
98     };
99 
100     Status add(const Entry& entry);
101 };
102 
103 }} // namespace android::os
104 
105 #endif // _ANDROID_OS_DROPBOXMANAGER_H
106 
107