Chromium Code Reviews| 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 "components/browser_context_keyed_service/browser_context_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, RetainTwoFilesTest); | |
| 23 FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, EvictionTest); | |
| 24 FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, SequenceNumberCompactionTest); | |
| 25 FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, | |
| 26 SequenceNumberCompactionFirstAboveOneTest); | |
| 27 | |
| 28 namespace extensions { | |
| 29 class Extension; | |
| 30 } // namespace extensions | |
| 31 | |
| 32 namespace apps { | |
| 33 | |
| 34 // Represents a file entry that a user has given an app permission to | |
| 35 // access. Will be persisted to disk (in the Preferences file), so should remain | |
| 36 // serializable. | |
| 37 struct SavedFileEntry { | |
| 38 SavedFileEntry(); | |
| 39 | |
| 40 SavedFileEntry(const std::string& id, | |
| 41 const base::FilePath& path, | |
| 42 bool writable, | |
| 43 int sequence_number); | |
| 44 | |
| 45 // The opaque id of this file entry. | |
| 46 std::string id; | |
| 47 | |
| 48 // The path to a file entry that the app had permission to access. | |
| 49 base::FilePath path; | |
| 50 | |
| 51 // Whether or not the app had write access to a file entry. | |
| 52 bool writable; | |
| 53 | |
| 54 // The sequence number in the LRU of the file entry. The value 0 indicates | |
| 55 // that the entry is not in the LRU. | |
| 56 int sequence_number; | |
| 57 }; | |
| 58 | |
| 59 class SavedFilesService : public ProfileKeyedService, | |
| 60 public content::NotificationObserver { | |
| 61 public: | |
| 62 explicit SavedFilesService(Profile* profile); | |
| 63 virtual ~SavedFilesService(); | |
| 64 | |
| 65 static SavedFilesService* Get(Profile* profile); | |
| 66 | |
| 67 // Registers a file entry. File entries that are in the retained files queue | |
|
Matt Giuca
2013/05/23 04:53:43
Registers a file entry ...? (for what?)
Perhaps "
Sam McNally
2013/05/23 05:21:11
Done.
| |
| 68 // at object construction are automatically registered. | |
| 69 void RegisterFileEntry(const std::string& extension_id, | |
| 70 const std::string& id, | |
| 71 const base::FilePath& file_path, | |
| 72 bool writable); | |
| 73 | |
| 74 // If the file with |id| is not in the queue of files to be retained | |
| 75 // permanently, adds the file to the back of the queue, evicting the least | |
| 76 // recently used entry at the front of the queue if it is full. If it is | |
| 77 // already present, moves it to the back of the queue. This has no effect if | |
| 78 // |id| has not been registered. | |
|
Matt Giuca
2013/05/23 04:53:43
Shouldn't it be an error or CHECK fail if |id| has
Sam McNally
2013/05/23 05:21:11
Done.
| |
| 79 void EnqueueFileEntry(const std::string& extension_id, const std::string& id); | |
| 80 | |
| 81 // Returns whether the file entry with the given |id| has been registered. | |
| 82 bool IsRegistered(const std::string& extension_id, const std::string& id); | |
| 83 | |
| 84 // Gets a borrowed pointer to the file entry with the specified |id|. Returns | |
| 85 // NULL if the file entry has not been registered. | |
| 86 const SavedFileEntry* GetFileEntry(const std::string& extension_id, | |
| 87 const std::string& id); | |
| 88 | |
| 89 // Returns all registered file entries. | |
| 90 std::vector<SavedFileEntry> GetAllFileEntries( | |
| 91 const std::string& extension_id); | |
| 92 | |
| 93 // Clears all retained files if the app does not have the | |
| 94 // fileSystem.retainFiles permission. | |
| 95 void MaybeClearQueue(const extensions::Extension* extension); | |
| 96 | |
| 97 private: | |
| 98 FRIEND_TEST_ALL_PREFIXES(::SavedFilesServiceUnitTest, RetainTwoFilesTest); | |
| 99 FRIEND_TEST_ALL_PREFIXES(::SavedFilesServiceUnitTest, EvictionTest); | |
| 100 FRIEND_TEST_ALL_PREFIXES(::SavedFilesServiceUnitTest, | |
| 101 SequenceNumberCompactionTest); | |
| 102 FRIEND_TEST_ALL_PREFIXES(::SavedFilesServiceUnitTest, | |
| 103 SequenceNumberCompactionFirstAboveOneTest); | |
| 104 friend class ::SavedFilesServiceUnitTest; | |
| 105 | |
| 106 // A container for the registered files for an app. | |
| 107 class SavedFiles; | |
| 108 | |
| 109 // content::NotificationObserver. | |
| 110 virtual void Observe(int type, | |
| 111 const content::NotificationSource& source, | |
| 112 const content::NotificationDetails& details) OVERRIDE; | |
| 113 | |
| 114 // Returns the SavedFiles for |extension_id|, creating it if necessary. | |
| 115 SavedFiles* GetOrInsert(const std::string& extension_id); | |
| 116 | |
| 117 // Clears the SavedFiles for |extension_id|. | |
| 118 void Clear(const std::string& extension_id); | |
| 119 | |
| 120 static void SetMaxSequenceNumberForTest(int max_value); | |
| 121 static void ClearMaxSequenceNumberForTest(); | |
| 122 static void SetLruSizeForTest(int size); | |
| 123 static void ClearLruSizeForTest(); | |
| 124 | |
| 125 std::map<std::string, SavedFiles*> extension_id_to_saved_files_; | |
| 126 STLValueDeleter<std::map<std::string, SavedFiles*> > | |
| 127 extension_id_to_saved_files_deleter_; | |
| 128 content::NotificationRegistrar registrar_; | |
| 129 Profile* profile_; | |
| 130 | |
| 131 DISALLOW_COPY_AND_ASSIGN(SavedFilesService); | |
| 132 }; | |
| 133 | |
| 134 } // namespace apps | |
| 135 | |
| 136 #endif // APPS_SAVED_FILES_SERVICE_H_ | |
| OLD | NEW |