OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef APPS_SAVED_FILES_SERVICE_H_ | |
6 #define APPS_SAVED_FILES_SERVICE_H_ | |
7 | |
8 #include <map> | |
9 #include <memory> | |
10 #include <set> | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #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 | |
29 namespace extensions { | |
30 class Extension; | |
31 } | |
32 | |
33 namespace apps { | |
34 | |
35 // Represents a file entry that a user has given an app permission to | |
36 // access. Will be persisted to disk (in the Preferences file), so should remain | |
37 // serializable. | |
38 struct SavedFileEntry { | |
39 SavedFileEntry(); | |
40 | |
41 SavedFileEntry(const std::string& id, | |
42 const base::FilePath& path, | |
43 bool is_directory, | |
44 int sequence_number); | |
45 | |
46 // The opaque id of this file entry. | |
47 std::string id; | |
48 | |
49 // The path to a file entry that the app had permission to access. | |
50 base::FilePath path; | |
51 | |
52 // Whether or not the entry refers to a directory. | |
53 bool is_directory; | |
54 | |
55 // The sequence number in the LRU of the file entry. The value 0 indicates | |
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 | |
70 // 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 | |
72 // object construction are automatically registered. | |
73 void RegisterFileEntry(const std::string& extension_id, | |
74 const std::string& id, | |
75 const base::FilePath& file_path, | |
76 bool is_directory); | |
77 | |
78 // 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 | |
80 // 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 | |
82 // registered. | |
83 void EnqueueFileEntry(const std::string& extension_id, const std::string& id); | |
84 | |
85 // Returns whether the file entry with the given |id| has been registered. | |
86 bool IsRegistered(const std::string& extension_id, const std::string& id); | |
87 | |
88 // Gets a borrowed pointer to the file entry with the specified |id|. Returns | |
89 // NULL if the file entry has not been registered. | |
90 const SavedFileEntry* GetFileEntry(const std::string& extension_id, | |
91 const std::string& id); | |
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 }; | |
143 | |
144 } // namespace apps | |
145 | |
146 #endif // APPS_SAVED_FILES_SERVICE_H_ | |
OLD | NEW |