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 <set> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/files/file_path.h" | |
14 #include "base/gtest_prod_util.h" | |
15 #include "base/stl_util.h" | |
16 #include "chrome/browser/profiles/profile_keyed_service.h" | |
17 #include "content/public/browser/notification_observer.h" | |
18 #include "content/public/browser/notification_registrar.h" | |
19 | |
20 class Profile; | |
21 class SavedFilesServiceUnitTest; | |
22 FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, EvictionTest); | |
23 FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, SequenceNumberCompactionTest); | |
24 FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, | |
25 SequenceNumberCompactionFirstAboveOneTest); | |
26 | |
27 namespace extensions { | |
28 class Extension; | |
29 } // namespace extensions | |
30 | |
31 namespace apps { | |
32 | |
33 // Represents a file entry that a user has given an app permission to | |
34 // access. Intended to be persisted to disk (in the Preferences file), so should | |
35 // remain serializable. | |
36 struct SavedFileEntry { | |
37 SavedFileEntry() | |
38 : writable(false), | |
39 sequence_number(0) {} | |
40 | |
41 SavedFileEntry(const std::string& id, | |
42 const base::FilePath& path, | |
43 bool writable, | |
44 int sequence_number) | |
45 : id(id), | |
46 path(path), | |
47 writable(writable), | |
48 sequence_number(sequence_number) {} | |
49 | |
50 // The opaque id of this file entry. | |
51 std::string id; | |
koz (OOO until 15th September)
2013/05/20 05:33:51
nit: newlines between these fields.
Sam McNally
2013/05/20 07:13:33
Done.
| |
52 // The path to a file entry that an extension had permission to access. | |
53 base::FilePath path; | |
54 // Whether or not an extension had write access to a file entry. | |
55 bool writable; | |
56 // The sequence number in the LRU of the file entry. | |
57 int sequence_number; | |
58 }; | |
59 | |
60 class SavedFilesService : public ProfileKeyedService, | |
61 public content::NotificationObserver { | |
62 public: | |
63 explicit SavedFilesService(Profile* profile); | |
64 virtual ~SavedFilesService(); | |
65 | |
66 static SavedFilesService* Get(Profile* profile); | |
67 | |
68 // Adds a file entry to be retained, but does not add it to the queue of | |
69 // entries to be retained between app runs. | |
70 void RetainFileEntry(const std::string& extension_id, | |
koz (OOO until 15th September)
2013/05/20 05:33:51
Sounds like SavedFilesService has two sets of file
Matt Giuca
2013/05/20 05:41:05
I don't like RetainFilePermanently because it does
Sam McNally
2013/05/20 07:13:33
Done.
| |
71 const std::string& id, | |
72 const base::FilePath& file_path, | |
73 bool writable); | |
74 | |
75 // Moves the file entry with the specified |id| to the front of the LRU, | |
76 // evicting the least recently used entry if the queue is full and the entry | |
77 // specified by |id| is not already in the queue. | |
78 void MoveEntryToFrontOfQueue(const std::string& extension_id, | |
79 const std::string& id); | |
80 | |
81 // Returns whether the file entry with the given |id| is retained. This does | |
82 // not consider whether or not the file entry is in the queue. | |
83 bool IsRetained(const std::string& extension_id, const std::string& id); | |
84 | |
85 // Stores the file entry with the specified |id| in |out| and returns true if | |
86 // the file entry is retained or returns false. | |
87 bool GetFileEntry(const std::string& extension_id, | |
88 const std::string& id, | |
89 SavedFileEntry* out); | |
90 | |
91 // Returns all retained file entries. | |
92 std::vector<SavedFileEntry> GetAllFileEntries( | |
93 const std::string& extension_id); | |
94 | |
95 // Clears all retained files that are not in the retained files queue. If the | |
96 // app does not have the fileSystem.retainFiles permission, the queue will | |
97 // also be cleared. | |
98 void ClearRetainedFiles(const extensions::Extension* extension); | |
Matt Giuca
2013/05/20 01:32:57
This name is not appropriate. Because it basically
Sam McNally
2013/05/20 04:25:20
Done.
| |
99 | |
100 private: | |
101 FRIEND_TEST_ALL_PREFIXES(::SavedFilesServiceUnitTest, EvictionTest); | |
102 FRIEND_TEST_ALL_PREFIXES(::SavedFilesServiceUnitTest, | |
103 SequenceNumberCompactionTest); | |
104 FRIEND_TEST_ALL_PREFIXES(::SavedFilesServiceUnitTest, | |
105 SequenceNumberCompactionFirstAboveOneTest); | |
106 friend class ::SavedFilesServiceUnitTest; | |
107 | |
108 class SavedFiles; | |
109 | |
110 // content::NotificationObserver. | |
111 virtual void Observe(int type, | |
112 const content::NotificationSource& source, | |
113 const content::NotificationDetails& details) OVERRIDE; | |
114 | |
115 SavedFiles* GetOrInsert(const std::string& extension_id); | |
116 | |
117 void Cleanup(const std::string& extension_id); | |
118 | |
119 static void SetMaxSequenceNumberForTest(int max_value); | |
120 static void ClearMaxSequenceNumberForTest(); | |
121 static void SetLruSizeForTest(int size); | |
122 static void ClearLruSizeForTest(); | |
123 | |
124 std::map<std::string, SavedFiles*> extension_id_to_saved_files_; | |
125 STLValueDeleter<std::map<std::string, SavedFiles*> > | |
126 extension_id_to_saved_files_deleter_; | |
127 content::NotificationRegistrar registrar_; | |
128 Profile* profile_; | |
129 | |
130 DISALLOW_COPY_AND_ASSIGN(SavedFilesService); | |
131 }; | |
132 | |
133 } // namespace apps | |
134 | |
135 #endif // APPS_SAVED_FILES_SERVICE_H_ | |
OLD | NEW |