OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef APPS_SAVED_FILES_SERVICE_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_FILE_SYSTEM_SAVED_FILES_SERVICE_H_ |
6 #define APPS_SAVED_FILES_SERVICE_H_ | 6 #define EXTENSIONS_BROWSER_API_FILE_SYSTEM_SAVED_FILES_SERVICE_H_ |
7 | 7 |
8 #include <map> | |
9 #include <memory> | |
10 #include <set> | |
11 #include <string> | 8 #include <string> |
12 #include <vector> | |
13 | 9 |
14 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
15 #include "base/gtest_prod_util.h" | |
16 #include "components/keyed_service/core/keyed_service.h" | |
17 #include "content/public/browser/notification_observer.h" | |
18 #include "content/public/browser/notification_registrar.h" | |
19 | |
20 namespace content { | |
21 class BrowserContext; | |
22 } | |
23 | |
24 class SavedFilesServiceUnitTest; | |
25 FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, RetainTwoFilesTest); | |
26 FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, EvictionTest); | |
27 FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, SequenceNumberCompactionTest); | |
28 | 11 |
29 namespace extensions { | 12 namespace extensions { |
30 class Extension; | |
31 } | |
32 | 13 |
33 namespace apps { | 14 // Provides an LRU of saved file entries that persist across app reloads. |
| 15 class SavedFilesService { |
| 16 public: |
| 17 // Represents a file entry that a user has given an app permission to |
| 18 // access. Must be serializable for persisting to disk. |
| 19 struct Entry { |
| 20 Entry(); |
34 | 21 |
35 // Represents a file entry that a user has given an app permission to | 22 Entry(const std::string& id, |
36 // access. Will be persisted to disk (in the Preferences file), so should remain | 23 const base::FilePath& path, |
37 // serializable. | 24 bool is_directory, |
38 struct SavedFileEntry { | 25 int sequence_number); |
39 SavedFileEntry(); | |
40 | 26 |
41 SavedFileEntry(const std::string& id, | 27 // The opaque id of this file entry. |
42 const base::FilePath& path, | 28 std::string id; |
43 bool is_directory, | |
44 int sequence_number); | |
45 | 29 |
46 // The opaque id of this file entry. | 30 // The path to a file entry that the app had permission to access. |
47 std::string id; | 31 base::FilePath path; |
48 | 32 |
49 // The path to a file entry that the app had permission to access. | 33 // Whether or not the entry refers to a directory. |
50 base::FilePath path; | 34 bool is_directory; |
51 | 35 |
52 // Whether or not the entry refers to a directory. | 36 // The sequence number in the LRU of the file entry. The value 0 indicates |
53 bool is_directory; | 37 // that the entry is not in the LRU. |
| 38 int sequence_number; |
| 39 }; |
54 | 40 |
55 // The sequence number in the LRU of the file entry. The value 0 indicates | 41 virtual ~SavedFilesService() {} |
56 // that the entry is not in the LRU. | |
57 int sequence_number; | |
58 }; | |
59 | |
60 // Tracks the files that apps have retained access to both while running and | |
61 // when suspended. | |
62 class SavedFilesService : public KeyedService, | |
63 public content::NotificationObserver { | |
64 public: | |
65 explicit SavedFilesService(content::BrowserContext* context); | |
66 ~SavedFilesService() override; | |
67 | |
68 static SavedFilesService* Get(content::BrowserContext* context); | |
69 | 42 |
70 // Registers a file entry with the saved files service, making it eligible to | 43 // Registers a file entry with the saved files service, making it eligible to |
71 // be put into the queue. File entries that are in the retained files queue at | 44 // be put into the queue. File entries that are in the retained files queue at |
72 // object construction are automatically registered. | 45 // object construction are automatically registered. |
73 void RegisterFileEntry(const std::string& extension_id, | 46 virtual void RegisterFileEntry(const std::string& extension_id, |
74 const std::string& id, | 47 const std::string& id, |
75 const base::FilePath& file_path, | 48 const base::FilePath& file_path, |
76 bool is_directory); | 49 bool is_directory) = 0; |
77 | 50 |
78 // If the file with |id| is not in the queue of files to be retained | 51 // If the file with |id| is not in the queue of files to be retained |
79 // permanently, adds the file to the back of the queue, evicting the least | 52 // permanently, adds the file to the back of the queue, evicting the least |
80 // recently used entry at the front of the queue if it is full. If it is | 53 // recently used entry at the front of the queue if it is full. If it is |
81 // already present, moves it to the back of the queue. The |id| must have been | 54 // already present, moves it to the back of the queue. The |id| must have been |
82 // registered. | 55 // registered. |
83 void EnqueueFileEntry(const std::string& extension_id, const std::string& id); | 56 virtual void EnqueueFileEntry(const std::string& extension_id, |
| 57 const std::string& id) = 0; |
84 | 58 |
85 // Returns whether the file entry with the given |id| has been registered. | 59 // Returns whether the file entry with the given |id| has been registered. |
86 bool IsRegistered(const std::string& extension_id, const std::string& id); | 60 virtual bool IsRegistered(const std::string& extension_id, |
| 61 const std::string& id) = 0; |
87 | 62 |
88 // Gets a borrowed pointer to the file entry with the specified |id|. Returns | 63 // Gets a borrowed pointer to the file entry with the specified |id|. Returns |
89 // NULL if the file entry has not been registered. | 64 // nullptr if the file entry has not been registered. |
90 const SavedFileEntry* GetFileEntry(const std::string& extension_id, | 65 virtual const Entry* GetFileEntry(const std::string& extension_id, |
91 const std::string& id); | 66 const std::string& id) = 0; |
92 | |
93 // Returns all registered file entries. | |
94 std::vector<SavedFileEntry> GetAllFileEntries( | |
95 const std::string& extension_id); | |
96 | |
97 // Clears all retained files if the app does not have the | |
98 // fileSystem.retainEntries permission. | |
99 void ClearQueueIfNoRetainPermission(const extensions::Extension* extension); | |
100 | |
101 // Clears all retained files. | |
102 void ClearQueue(const extensions::Extension* extension); | |
103 | |
104 // Called to notify that the application has begun to exit. | |
105 void OnApplicationTerminating(); | |
106 | |
107 private: | |
108 FRIEND_TEST_ALL_PREFIXES(::SavedFilesServiceUnitTest, RetainTwoFilesTest); | |
109 FRIEND_TEST_ALL_PREFIXES(::SavedFilesServiceUnitTest, EvictionTest); | |
110 FRIEND_TEST_ALL_PREFIXES(::SavedFilesServiceUnitTest, | |
111 SequenceNumberCompactionTest); | |
112 friend class ::SavedFilesServiceUnitTest; | |
113 | |
114 // A container for the registered files for an app. | |
115 class SavedFiles; | |
116 | |
117 // content::NotificationObserver. | |
118 void Observe(int type, | |
119 const content::NotificationSource& source, | |
120 const content::NotificationDetails& details) override; | |
121 | |
122 // Returns the SavedFiles for |extension_id| or NULL if one does not exist. | |
123 SavedFiles* Get(const std::string& extension_id) const; | |
124 | |
125 // Returns the SavedFiles for |extension_id|, creating it if necessary. | |
126 SavedFiles* GetOrInsert(const std::string& extension_id); | |
127 | |
128 // Clears the SavedFiles for |extension_id|. | |
129 void Clear(const std::string& extension_id); | |
130 | |
131 static void SetMaxSequenceNumberForTest(int max_value); | |
132 static void ClearMaxSequenceNumberForTest(); | |
133 static void SetLruSizeForTest(int size); | |
134 static void ClearLruSizeForTest(); | |
135 | |
136 std::map<std::string, std::unique_ptr<SavedFiles>> | |
137 extension_id_to_saved_files_; | |
138 content::NotificationRegistrar registrar_; | |
139 content::BrowserContext* context_; | |
140 | |
141 DISALLOW_COPY_AND_ASSIGN(SavedFilesService); | |
142 }; | 67 }; |
143 | 68 |
144 } // namespace apps | 69 } // namespace extensions |
145 | 70 |
146 #endif // APPS_SAVED_FILES_SERVICE_H_ | 71 #endif // EXTENSIONS_BROWSER_API_FILE_SYSTEM_SAVED_FILES_SERVICE_H_ |
OLD | NEW |