Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: chrome/browser/chromeos/arc/fileapi/arc_file_system_operation_runner.h

Issue 2715493002: mediaview: Support watchers in ArcFileSystemOperationRunner. (Closed)
Patch Set: s/LOG/DLOG/ Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_FILE_SYSTEM_OPERATION_RUNNER_H_ 5 #ifndef CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_FILE_SYSTEM_OPERATION_RUNNER_H_
6 #define CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_FILE_SYSTEM_OPERATION_RUNNER_H_ 6 #define CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_FILE_SYSTEM_OPERATION_RUNNER_H_
7 7
8 #include <stdint.h>
9
10 #include <map>
8 #include <memory> 11 #include <memory>
9 #include <string> 12 #include <string>
10 #include <vector> 13 #include <vector>
11 14
12 #include "base/callback_forward.h" 15 #include "base/callback_forward.h"
13 #include "base/macros.h" 16 #include "base/macros.h"
14 #include "base/memory/weak_ptr.h" 17 #include "base/memory/weak_ptr.h"
18 #include "base/observer_list.h"
15 #include "chrome/browser/chromeos/arc/arc_session_manager.h" 19 #include "chrome/browser/chromeos/arc/arc_session_manager.h"
16 #include "components/arc/arc_service.h" 20 #include "components/arc/arc_service.h"
17 #include "components/arc/common/file_system.mojom.h" 21 #include "components/arc/common/file_system.mojom.h"
18 #include "components/arc/instance_holder.h" 22 #include "components/arc/instance_holder.h"
23 #include "mojo/public/cpp/bindings/binding.h"
24 #include "storage/browser/fileapi/watcher_manager.h"
19 25
20 namespace arc { 26 namespace arc {
21 27
22 // Runs ARC file system operations. 28 // Runs ARC file system operations.
23 // 29 //
24 // This is an abstraction layer on top of mojom::FileSystemInstance. All ARC 30 // This is an abstraction layer on top of mojom::FileSystemInstance. All ARC
25 // file system operations should go through this class, rather than invoking 31 // file system operations should go through this class, rather than invoking
26 // mojom::FileSystemInstance directly. 32 // mojom::FileSystemInstance directly.
27 // 33 //
28 // When ARC is disabled or ARC has already booted, file system operations are 34 // When ARC is disabled or ARC has already booted, file system operations are
29 // performed immediately. While ARC boot is under progress, file operations are 35 // performed immediately. While ARC boot is under progress, file operations are
30 // deferred until ARC boot finishes or the user disables ARC. 36 // deferred until ARC boot finishes or the user disables ARC.
31 // 37 //
32 // This file system operation runner provides better UX when the user attempts 38 // This file system operation runner provides better UX when the user attempts
33 // to perform file operations while ARC is booting. For example: 39 // to perform file operations while ARC is booting. For example:
34 // 40 //
35 // - Media views are mounted in Files app soon after the user logs into 41 // - Media views are mounted in Files app soon after the user logs into
36 // the system. If the user attempts to open media views before ARC boots, 42 // the system. If the user attempts to open media views before ARC boots,
37 // a spinner is shown until file system gets ready because ReadDirectory 43 // a spinner is shown until file system gets ready because ReadDirectory
38 // operations are deferred. 44 // operations are deferred.
39 // - When an Android content URL is opened soon after the user logs into 45 // - When an Android content URL is opened soon after the user logs into
40 // the system (because the user opened the tab before they logged out for 46 // the system (because the user opened the tab before they logged out for
41 // instance), the tab keeps loading until ARC boot finishes, instead of 47 // instance), the tab keeps loading until ARC boot finishes, instead of
42 // failing immediately. 48 // failing immediately.
43 // 49 //
44 // All member functions must be called on the UI thread. 50 // All member functions must be called on the UI thread.
45 class ArcFileSystemOperationRunner 51 class ArcFileSystemOperationRunner
46 : public ArcService, 52 : public ArcService,
53 public mojom::FileSystemHost,
47 public ArcSessionManager::Observer, 54 public ArcSessionManager::Observer,
48 public InstanceHolder<mojom::FileSystemInstance>::Observer { 55 public InstanceHolder<mojom::FileSystemInstance>::Observer {
49 public: 56 public:
50 using GetFileSizeCallback = mojom::FileSystemInstance::GetFileSizeCallback; 57 using GetFileSizeCallback = mojom::FileSystemInstance::GetFileSizeCallback;
51 using OpenFileToReadCallback = 58 using OpenFileToReadCallback =
52 mojom::FileSystemInstance::OpenFileToReadCallback; 59 mojom::FileSystemInstance::OpenFileToReadCallback;
53 using GetDocumentCallback = mojom::FileSystemInstance::GetDocumentCallback; 60 using GetDocumentCallback = mojom::FileSystemInstance::GetDocumentCallback;
54 using GetChildDocumentsCallback = 61 using GetChildDocumentsCallback =
55 mojom::FileSystemInstance::GetChildDocumentsCallback; 62 mojom::FileSystemInstance::GetChildDocumentsCallback;
63 using AddWatcherCallback = base::Callback<void(int64_t watcher_id)>;
64 using RemoveWatcherCallback = base::Callback<void(bool success)>;
65 using ChangeType = storage::WatcherManager::ChangeType;
66 using WatcherCallback = base::Callback<void(ChangeType type)>;
67
68 class Observer {
69 public:
70 virtual ~Observer() = default;
hidehiko 2017/03/02 14:43:47 Could you move this into protected? cf) https://c
Shuhei Takahashi 2017/03/03 04:22:15 Done.
Luis Héctor Chávez 2017/03/06 17:10:48 It is now in the style guide :)
71
72 // Called when the installed watchers are invalidated.
73 // This can happen when Android system restarts, for example.
74 // After this event is fired, watcher IDs issued before the event can be
75 // reused.
76 virtual void OnWatchersCleared() = 0;
77 };
56 78
57 // For supporting ArcServiceManager::GetService<T>(). 79 // For supporting ArcServiceManager::GetService<T>().
58 static const char kArcServiceName[]; 80 static const char kArcServiceName[];
59 81
60 // Creates an instance suitable for unit tests. 82 // Creates an instance suitable for unit tests.
61 // This instance will run all operations immediately without deferring by 83 // This instance will run all operations immediately without deferring by
62 // default. Also, deferring can be enabled/disabled by calling 84 // default. Also, deferring can be enabled/disabled by calling
63 // SetShouldDefer() from friend classes. 85 // SetShouldDefer() from friend classes.
64 static std::unique_ptr<ArcFileSystemOperationRunner> CreateForTesting( 86 static std::unique_ptr<ArcFileSystemOperationRunner> CreateForTesting(
65 ArcBridgeService* bridge_service); 87 ArcBridgeService* bridge_service);
66 88
67 // The standard constructor. A production instance should be created by 89 // The standard constructor. A production instance should be created by
68 // this constructor. 90 // this constructor.
69 ArcFileSystemOperationRunner(ArcBridgeService* bridge_service, 91 ArcFileSystemOperationRunner(ArcBridgeService* bridge_service,
70 const Profile* profile); 92 const Profile* profile);
71 ~ArcFileSystemOperationRunner() override; 93 ~ArcFileSystemOperationRunner() override;
72 94
95 // Adds or removes observers.
96 void AddObserver(Observer* observer);
97 void RemoveObserver(Observer* observer);
98
73 // Runs file system operations. See file_system.mojom for documentation. 99 // Runs file system operations. See file_system.mojom for documentation.
74 void GetFileSize(const GURL& url, const GetFileSizeCallback& callback); 100 void GetFileSize(const GURL& url, const GetFileSizeCallback& callback);
75 void OpenFileToRead(const GURL& url, const OpenFileToReadCallback& callback); 101 void OpenFileToRead(const GURL& url, const OpenFileToReadCallback& callback);
76 void GetDocument(const std::string& authority, 102 void GetDocument(const std::string& authority,
77 const std::string& document_id, 103 const std::string& document_id,
78 const GetDocumentCallback& callback); 104 const GetDocumentCallback& callback);
79 void GetChildDocuments(const std::string& authority, 105 void GetChildDocuments(const std::string& authority,
80 const std::string& parent_document_id, 106 const std::string& parent_document_id,
81 const GetChildDocumentsCallback& callback); 107 const GetChildDocumentsCallback& callback);
108 void AddWatcher(const std::string& authority,
109 const std::string& document_id,
110 const WatcherCallback& watcher_callback,
111 const AddWatcherCallback& callback);
112 void RemoveWatcher(int64_t watcher_id, const RemoveWatcherCallback& callback);
113
114 // FileSystemHost overrides:
115 void OnDocumentChanged(int64_t watcher_id, mojom::ChangeType type) override;
82 116
83 // ArcSessionManager::Observer overrides: 117 // ArcSessionManager::Observer overrides:
84 void OnArcPlayStoreEnabledChanged(bool enabled) override; 118 void OnArcPlayStoreEnabledChanged(bool enabled) override;
85 119
86 // InstanceHolder<mojom::FileSystemInstance>::Observer overrides: 120 // InstanceHolder<mojom::FileSystemInstance>::Observer overrides:
87 void OnInstanceReady() override; 121 void OnInstanceReady() override;
88 void OnInstanceClosed() override; 122 void OnInstanceClosed() override;
89 123
90 private: 124 private:
91 friend class ArcFileSystemOperationRunnerTest; 125 friend class ArcFileSystemOperationRunnerTest;
92 126
93 ArcFileSystemOperationRunner(ArcBridgeService* bridge_service, 127 ArcFileSystemOperationRunner(ArcBridgeService* bridge_service,
94 const Profile* profile, 128 const Profile* profile,
95 bool observe_events); 129 bool set_should_defer_by_events);
130
131 void OnWatcherAdded(const WatcherCallback& watcher_callback,
132 const AddWatcherCallback& callback,
133 int64_t watcher_id);
96 134
97 // Called whenever ARC states related to |should_defer_| are changed. 135 // Called whenever ARC states related to |should_defer_| are changed.
98 void OnStateChanged(); 136 void OnStateChanged();
99 137
100 // Enables/disables deferring. 138 // Enables/disables deferring.
101 // Friend unit tests can call this function to simulate enabling/disabling 139 // Friend unit tests can call this function to simulate enabling/disabling
102 // deferring. 140 // deferring.
103 void SetShouldDefer(bool should_defer); 141 void SetShouldDefer(bool should_defer);
104 142
105 // Profile this runner is associated with. This can be nullptr in 143 // Profile this runner is associated with. This can be nullptr in
106 // unit tests. 144 // unit tests.
107 const Profile* const profile_; 145 const Profile* const profile_;
108 146
109 // Indicates if this instance should register observers to receive events. 147 // Indicates if this instance should enable/disable deferring by events.
110 // Usually true, but set to false in unit tests. 148 // Usually true, but set to false in unit tests.
111 const bool observe_events_; 149 bool set_should_defer_by_events_;
112 150
113 // Set to true if operations should be deferred at this moment. 151 // Set to true if operations should be deferred at this moment.
114 // The default is set to false so that operations are not deferred in 152 // The default is set to false so that operations are not deferred in
115 // unit tests. 153 // unit tests.
116 bool should_defer_ = false; 154 bool should_defer_ = false;
117 155
118 // List of deferred operations. 156 // List of deferred operations.
119 std::vector<base::Closure> deferred_operations_; 157 std::vector<base::Closure> deferred_operations_;
120 158
159 // Map from a watcher ID to a watcher callback.
160 std::map<int64_t, WatcherCallback> watcher_callbacks_;
161
162 base::ObserverList<Observer> observer_list_;
163
164 mojo::Binding<mojom::FileSystemHost> binding_;
165
121 base::WeakPtrFactory<ArcFileSystemOperationRunner> weak_ptr_factory_; 166 base::WeakPtrFactory<ArcFileSystemOperationRunner> weak_ptr_factory_;
122 167
123 DISALLOW_COPY_AND_ASSIGN(ArcFileSystemOperationRunner); 168 DISALLOW_COPY_AND_ASSIGN(ArcFileSystemOperationRunner);
124 }; 169 };
125 170
126 } // namespace arc 171 } // namespace arc
127 172
128 #endif // CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_FILE_SYSTEM_OPERATION_RUNNER_ H_ 173 #endif // CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_FILE_SYSTEM_OPERATION_RUNNER_ H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698