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