[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 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 | |
limasdf | 918cc2e9 | 2015-11-20 04:28:57 | [diff] [blame] | 8 | #include <map> |
mostynb | ecb4a22b | 2016-04-04 06:08:01 | [diff] [blame] | 9 | #include <memory> |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 10 | #include <set> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "base/files/file_path.h" |
| 15 | #include "base/gtest_prod_util.h" |
[email protected] | 12b7af3 | 2014-03-13 05:28:20 | [diff] [blame] | 16 | #include "components/keyed_service/core/keyed_service.h" |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 17 | #include "content/public/browser/notification_observer.h" |
| 18 | #include "content/public/browser/notification_registrar.h" |
michaelpg | 868a94be | 2017-06-26 16:55:25 | [diff] [blame] | 19 | #include "extensions/browser/api/file_system/saved_files_service_interface.h" |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 20 | |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 21 | namespace content { |
| 22 | class BrowserContext; |
| 23 | } |
| 24 | |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 25 | class SavedFilesServiceUnitTest; |
| 26 | FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, RetainTwoFilesTest); |
| 27 | FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, EvictionTest); |
| 28 | FORWARD_DECLARE_TEST(SavedFilesServiceUnitTest, SequenceNumberCompactionTest); |
| 29 | |
| 30 | namespace extensions { |
| 31 | class Extension; |
michaelpg | 868a94be | 2017-06-26 16:55:25 | [diff] [blame] | 32 | struct SavedFileEntry; |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | namespace apps { |
| 36 | |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 37 | // Tracks the files that apps have retained access to both while running and |
| 38 | // when suspended. |
michaelpg | 868a94be | 2017-06-26 16:55:25 | [diff] [blame] | 39 | class SavedFilesService : public extensions::SavedFilesServiceInterface, |
| 40 | public KeyedService, |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 41 | public content::NotificationObserver { |
| 42 | public: |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 43 | explicit SavedFilesService(content::BrowserContext* context); |
David Bienvenu | b4b5b57 | 2020-09-28 00:49:27 | [diff] [blame] | 44 | SavedFilesService(const SavedFilesService&) = delete; |
| 45 | SavedFilesService& operator=(const SavedFilesService&) = delete; |
dcheng | 2f5be41 | 2014-10-22 01:46:08 | [diff] [blame] | 46 | ~SavedFilesService() override; |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 47 | |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 48 | static SavedFilesService* Get(content::BrowserContext* context); |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 49 | |
michaelpg | 868a94be | 2017-06-26 16:55:25 | [diff] [blame] | 50 | // extensions::SavedFilesServiceInterface: |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 51 | void RegisterFileEntry(const std::string& extension_id, |
| 52 | const std::string& id, |
[email protected] | 6b7ecdd | 2013-08-29 14:16:24 | [diff] [blame] | 53 | const base::FilePath& file_path, |
michaelpg | 868a94be | 2017-06-26 16:55:25 | [diff] [blame] | 54 | bool is_directory) override; |
| 55 | void EnqueueFileEntry(const std::string& extension_id, |
| 56 | const std::string& id) override; |
| 57 | bool IsRegistered(const std::string& extension_id, |
| 58 | const std::string& id) override; |
| 59 | const extensions::SavedFileEntry* GetFileEntry( |
| 60 | const std::string& extension_id, |
| 61 | const std::string& id) override; |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 62 | |
| 63 | // Returns all registered file entries. |
michaelpg | 868a94be | 2017-06-26 16:55:25 | [diff] [blame] | 64 | std::vector<extensions::SavedFileEntry> GetAllFileEntries( |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 65 | const std::string& extension_id); |
| 66 | |
| 67 | // Clears all retained files if the app does not have the |
[email protected] | 73f73a0 | 2013-07-04 02:15:12 | [diff] [blame] | 68 | // fileSystem.retainEntries permission. |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 69 | void ClearQueueIfNoRetainPermission(const extensions::Extension* extension); |
| 70 | |
[email protected] | a2886e8b | 2013-06-08 05:15:02 | [diff] [blame] | 71 | // Clears all retained files. |
| 72 | void ClearQueue(const extensions::Extension* extension); |
| 73 | |
michaelpg | dbdcdcc | 2017-04-06 01:40:56 | [diff] [blame] | 74 | // Called to notify that the application has begun to exit. |
| 75 | void OnApplicationTerminating(); |
| 76 | |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 77 | private: |
| 78 | FRIEND_TEST_ALL_PREFIXES(::SavedFilesServiceUnitTest, RetainTwoFilesTest); |
| 79 | FRIEND_TEST_ALL_PREFIXES(::SavedFilesServiceUnitTest, EvictionTest); |
| 80 | FRIEND_TEST_ALL_PREFIXES(::SavedFilesServiceUnitTest, |
| 81 | SequenceNumberCompactionTest); |
| 82 | friend class ::SavedFilesServiceUnitTest; |
| 83 | |
| 84 | // A container for the registered files for an app. |
| 85 | class SavedFiles; |
| 86 | |
| 87 | // content::NotificationObserver. |
dcheng | 2f5be41 | 2014-10-22 01:46:08 | [diff] [blame] | 88 | void Observe(int type, |
| 89 | const content::NotificationSource& source, |
| 90 | const content::NotificationDetails& details) override; |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 91 | |
[email protected] | a2886e8b | 2013-06-08 05:15:02 | [diff] [blame] | 92 | // Returns the SavedFiles for |extension_id| or NULL if one does not exist. |
| 93 | SavedFiles* Get(const std::string& extension_id) const; |
| 94 | |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 95 | // Returns the SavedFiles for |extension_id|, creating it if necessary. |
| 96 | SavedFiles* GetOrInsert(const std::string& extension_id); |
| 97 | |
| 98 | // Clears the SavedFiles for |extension_id|. |
| 99 | void Clear(const std::string& extension_id); |
| 100 | |
| 101 | static void SetMaxSequenceNumberForTest(int max_value); |
| 102 | static void ClearMaxSequenceNumberForTest(); |
| 103 | static void SetLruSizeForTest(int size); |
| 104 | static void ClearLruSizeForTest(); |
| 105 | |
mostynb | ecb4a22b | 2016-04-04 06:08:01 | [diff] [blame] | 106 | std::map<std::string, std::unique_ptr<SavedFiles>> |
| 107 | extension_id_to_saved_files_; |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 108 | content::NotificationRegistrar registrar_; |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 109 | content::BrowserContext* context_; |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | } // namespace apps |
| 113 | |
| 114 | #endif // APPS_SAVED_FILES_SERVICE_H_ |