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 |
| 26 namespace extensions { |
| 27 class Extension; |
| 28 } |
| 29 |
| 30 namespace apps { |
| 31 |
| 32 // Represents a file entry that a user has given an app permission to |
| 33 // access. Will be persisted to disk (in the Preferences file), so should remain |
| 34 // serializable. |
| 35 struct SavedFileEntry { |
| 36 SavedFileEntry(); |
| 37 |
| 38 SavedFileEntry(const std::string& id, |
| 39 const base::FilePath& path, |
| 40 bool writable, |
| 41 int sequence_number); |
| 42 |
| 43 // The opaque id of this file entry. |
| 44 std::string id; |
| 45 |
| 46 // The path to a file entry that the app had permission to access. |
| 47 base::FilePath path; |
| 48 |
| 49 // Whether or not the app had write access to a file entry. |
| 50 bool writable; |
| 51 |
| 52 // The sequence number in the LRU of the file entry. The value 0 indicates |
| 53 // that the entry is not in the LRU. |
| 54 int sequence_number; |
| 55 }; |
| 56 |
| 57 // Tracks the files that apps have retained access to both while running and |
| 58 // when suspended. |
| 59 class SavedFilesService : public BrowserContextKeyedService, |
| 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 with the saved files service, making it eligible to |
| 68 // be put into the queue. File entries that are in the retained files queue at |
| 69 // object construction are automatically registered. |
| 70 void RegisterFileEntry(const std::string& extension_id, |
| 71 const std::string& id, |
| 72 const base::FilePath& file_path, |
| 73 bool writable); |
| 74 |
| 75 // If the file with |id| is not in the queue of files to be retained |
| 76 // permanently, adds the file to the back of the queue, evicting the least |
| 77 // recently used entry at the front of the queue if it is full. If it is |
| 78 // already present, moves it to the back of the queue. The |id| must have been |
| 79 // registered. |
| 80 void EnqueueFileEntry(const std::string& extension_id, const std::string& id); |
| 81 |
| 82 // Returns whether the file entry with the given |id| has been registered. |
| 83 bool IsRegistered(const std::string& extension_id, const std::string& id); |
| 84 |
| 85 // Gets a borrowed pointer to the file entry with the specified |id|. Returns |
| 86 // NULL if the file entry has not been registered. |
| 87 const SavedFileEntry* GetFileEntry(const std::string& extension_id, |
| 88 const std::string& id); |
| 89 |
| 90 // Returns all registered file entries. |
| 91 std::vector<SavedFileEntry> GetAllFileEntries( |
| 92 const std::string& extension_id); |
| 93 |
| 94 // Clears all retained files if the app does not have the |
| 95 // fileSystem.retainFiles permission. |
| 96 void ClearQueueIfNoRetainPermission(const extensions::Extension* extension); |
| 97 |
| 98 private: |
| 99 FRIEND_TEST_ALL_PREFIXES(::SavedFilesServiceUnitTest, RetainTwoFilesTest); |
| 100 FRIEND_TEST_ALL_PREFIXES(::SavedFilesServiceUnitTest, EvictionTest); |
| 101 FRIEND_TEST_ALL_PREFIXES(::SavedFilesServiceUnitTest, |
| 102 SequenceNumberCompactionTest); |
| 103 friend class ::SavedFilesServiceUnitTest; |
| 104 |
| 105 // A container for the registered files for an app. |
| 106 class SavedFiles; |
| 107 |
| 108 // content::NotificationObserver. |
| 109 virtual void Observe(int type, |
| 110 const content::NotificationSource& source, |
| 111 const content::NotificationDetails& details) OVERRIDE; |
| 112 |
| 113 // Returns the SavedFiles for |extension_id|, creating it if necessary. |
| 114 SavedFiles* GetOrInsert(const std::string& extension_id); |
| 115 |
| 116 // Clears the SavedFiles for |extension_id|. |
| 117 void Clear(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 |