| 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_H_ |
| 6 #define APPS_SAVED_FILES_SERVICE_H_ | 6 #define APPS_SAVED_FILES_SERVICE_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_interface.h" |
| 19 | 20 |
| 20 namespace content { | 21 namespace content { |
| 21 class BrowserContext; | 22 class BrowserContext; |
| 22 } | 23 } |
| 23 | 24 |
| 24 class SavedFilesServiceUnitTest; | 25 class SavedFilesServiceUnitTest; |
| 25 FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, RetainTwoFilesTest); | 26 FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, RetainTwoFilesTest); |
| 26 FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, EvictionTest); | 27 FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, EvictionTest); |
| 27 FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, SequenceNumberCompactionTest); | 28 FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, SequenceNumberCompactionTest); |
| 28 | 29 |
| 29 namespace extensions { | 30 namespace extensions { |
| 30 class Extension; | 31 class Extension; |
| 32 struct SavedFileEntry; |
| 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 SavedFilesService : public extensions::SavedFilesServiceInterface, |
| 40 public KeyedService, |
| 63 public content::NotificationObserver { | 41 public content::NotificationObserver { |
| 64 public: | 42 public: |
| 65 explicit SavedFilesService(content::BrowserContext* context); | 43 explicit SavedFilesService(content::BrowserContext* context); |
| 66 ~SavedFilesService() override; | 44 ~SavedFilesService() override; |
| 67 | 45 |
| 68 static SavedFilesService* Get(content::BrowserContext* context); | 46 static SavedFilesService* Get(content::BrowserContext* context); |
| 69 | 47 |
| 70 // Registers a file entry with the saved files service, making it eligible to | 48 // extensions::SavedFilesServiceInterface: |
| 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 extensions::SavedFileEntry* GetFileEntry( |
| 82 // registered. | 58 const std::string& extension_id, |
| 83 void EnqueueFileEntry(const std::string& extension_id, const std::string& id); | 59 const std::string& id) override; |
| 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 | 60 |
| 93 // Returns all registered file entries. | 61 // Returns all registered file entries. |
| 94 std::vector<SavedFileEntry> GetAllFileEntries( | 62 std::vector<extensions::SavedFileEntry> GetAllFileEntries( |
| 95 const std::string& extension_id); | 63 const std::string& extension_id); |
| 96 | 64 |
| 97 // Clears all retained files if the app does not have the | 65 // Clears all retained files if the app does not have the |
| 98 // fileSystem.retainEntries permission. | 66 // fileSystem.retainEntries permission. |
| 99 void ClearQueueIfNoRetainPermission(const extensions::Extension* extension); | 67 void ClearQueueIfNoRetainPermission(const extensions::Extension* extension); |
| 100 | 68 |
| 101 // Clears all retained files. | 69 // Clears all retained files. |
| 102 void ClearQueue(const extensions::Extension* extension); | 70 void ClearQueue(const extensions::Extension* extension); |
| 103 | 71 |
| 104 // Called to notify that the application has begun to exit. | 72 // Called to notify that the application has begun to exit. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 extension_id_to_saved_files_; | 105 extension_id_to_saved_files_; |
| 138 content::NotificationRegistrar registrar_; | 106 content::NotificationRegistrar registrar_; |
| 139 content::BrowserContext* context_; | 107 content::BrowserContext* context_; |
| 140 | 108 |
| 141 DISALLOW_COPY_AND_ASSIGN(SavedFilesService); | 109 DISALLOW_COPY_AND_ASSIGN(SavedFilesService); |
| 142 }; | 110 }; |
| 143 | 111 |
| 144 } // namespace apps | 112 } // namespace apps |
| 145 | 113 |
| 146 #endif // APPS_SAVED_FILES_SERVICE_H_ | 114 #endif // APPS_SAVED_FILES_SERVICE_H_ |
| OLD | NEW |