• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
2 // source code may only be used and distributed under the Widevine Master
3 // License Agreement.
4 //
5 #ifndef CLEARKEY_MEMORY_FILE_SYSTEM_H_
6 #define CLEARKEY_MEMORY_FILE_SYSTEM_H_
7 
8 #include <android-base/thread_annotations.h>
9 #include <map>
10 #include <mutex>
11 #include <string>
12 
13 #include "ClearKeyTypes.h"
14 
15 namespace android {
16 namespace hardware {
17 namespace drm {
18 namespace V1_4 {
19 namespace clearkey {
20 
21 // Using android file system requires clearkey plugin to update
22 // its sepolicy. However, we are unable to update sepolicy for
23 // older vendor partitions. To provide backward compatibility,
24 // clearkey plugin implements a very simple file system in memory.
25 // This memory file system does not support directory structure.
26 class MemoryFileSystem {
27  public:
28     struct MemoryFile {
29         std::string fileName;  // excludes path
30         std::string content;
31         size_t fileSize;
32 
getContentMemoryFile33         std::string getContent() const { return content; }
getFileSizeMemoryFile34         size_t getFileSize() const { return fileSize; }
setContentMemoryFile35         void setContent(const std::string& file) { content = file; }
setFileNameMemoryFile36         void setFileName(const std::string& name) { fileName = name; }
setFileSizeMemoryFile37         void setFileSize(size_t size) {
38             content.resize(size); fileSize = size;
39         }
40     };
41 
MemoryFileSystem()42     MemoryFileSystem() {};
~MemoryFileSystem()43     virtual ~MemoryFileSystem() {};
44 
45     bool FileExists(const std::string& fileName) const;
46     ssize_t GetFileSize(const std::string& fileName) const;
47     std::vector<std::string> ListFiles() const;
48     size_t Read(const std::string& pathName, std::string* buffer);
49     bool RemoveAllFiles();
50     bool RemoveFile(const std::string& fileName);
51     size_t Write(const std::string& pathName, const MemoryFile& memoryFile);
52 
53  private:
54     mutable std::mutex mMemoryFileSystemLock;
55 
56     // License file name is made up of a unique keySetId, therefore,
57     // the filename can be used as the key to locate licenses in the
58     // memory file system.
59     std::map<std::string, MemoryFile> mMemoryFileSystem GUARDED_BY(mMemoryFileSystemLock);
60 
61     std::string GetFileName(const std::string& path);
62 
63     CLEARKEY_DISALLOW_COPY_AND_ASSIGN(MemoryFileSystem);
64 };
65 
66 } // namespace clearkey
67 } // namespace V1_4
68 } // namespace drm
69 } // namespace hardware
70 } // namespace android
71 
72 #endif  // CLEARKEY_MEMORY_FILE_SYSTEM_H_
73