OLD | NEW |
---|---|
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_DEFERRED_FILE_SYSTEM_OPERATION_R UNNER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_FILE_SYSTEM_OPERATION_RUNNER_H_ |
6 #define CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_DEFERRED_FILE_SYSTEM_OPERATION_R UNNER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_FILE_SYSTEM_OPERATION_RUNNER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "chrome/browser/chromeos/arc/arc_session_manager.h" | 14 #include "chrome/browser/chromeos/arc/arc_session_manager.h" |
15 #include "components/arc/arc_service.h" | 15 #include "components/arc/arc_service.h" |
16 #include "components/arc/common/file_system.mojom.h" | 16 #include "components/arc/common/file_system.mojom.h" |
17 #include "components/arc/file_system/arc_file_system_operation_runner.h" | |
18 #include "components/arc/instance_holder.h" | 17 #include "components/arc/instance_holder.h" |
19 | 18 |
20 namespace arc { | 19 namespace arc { |
21 | 20 |
22 // Implements deferred ARC file system operations. | 21 // Runs ARC file system operations. |
22 // | |
23 // This is an abstraction layer on top of mojom::FileSystemInstance. All ARC | |
hidehiko
2017/01/26 07:24:20
I like this is explicitly documented :D. Thank you
| |
24 // file system operations should go through this interface, rather than | |
hidehiko
2017/01/26 07:24:20
Optional: maybe s/interface/class/ is clearer here
Shuhei Takahashi
2017/01/27 09:55:10
Done.
| |
25 // invoking mojom::FileSystemInstance directly. | |
23 // | 26 // |
24 // When ARC is disabled or ARC has already booted, file system operations are | 27 // When ARC is disabled or ARC has already booted, file system operations are |
25 // performed immediately. While ARC boot is under progress, file operations are | 28 // performed immediately. While ARC boot is under progress, file operations are |
26 // deferred until ARC boot finishes or the user disables ARC. | 29 // deferred until ARC boot finishes or the user disables ARC. |
27 // | 30 // |
28 // This file system operation runner provides better UX when the user attempts | 31 // This file system operation runner provides better UX when the user attempts |
29 // to perform file operations while ARC is booting. For example: | 32 // to perform file operations while ARC is booting. For example: |
30 // | 33 // |
31 // - Media views are mounted in Files app soon after the user logs into | 34 // - Media views are mounted in Files app soon after the user logs into |
32 // the system. If the user attempts to open media views before ARC boots, | 35 // the system. If the user attempts to open media views before ARC boots, |
33 // a spinner is shown until file system gets ready because ReadDirectory | 36 // a spinner is shown until file system gets ready because ReadDirectory |
34 // operations are deferred. | 37 // operations are deferred. |
35 // - When an Android content URL is opened soon after the user logs into | 38 // - When an Android content URL is opened soon after the user logs into |
36 // the system (because the user opened the tab before they logged out for | 39 // the system (because the user opened the tab before they logged out for |
37 // instance), the tab keeps loading until ARC boot finishes, instead of | 40 // instance), the tab keeps loading until ARC boot finishes, instead of |
38 // failing immediately. | 41 // failing immediately. |
39 // | 42 // |
40 // All member functions must be called on the UI thread. | 43 // All member functions must be called on the UI thread. |
41 class ArcDeferredFileSystemOperationRunner | 44 class ArcFileSystemOperationRunner |
42 : public ArcFileSystemOperationRunner, | 45 : public ArcService, |
43 public ArcSessionManager::Observer, | 46 public ArcSessionManager::Observer, |
44 public InstanceHolder<mojom::FileSystemInstance>::Observer { | 47 public InstanceHolder<mojom::FileSystemInstance>::Observer { |
45 public: | 48 public: |
46 explicit ArcDeferredFileSystemOperationRunner( | 49 using GetFileSizeCallback = mojom::FileSystemInstance::GetFileSizeCallback; |
50 using OpenFileToReadCallback = | |
51 mojom::FileSystemInstance::OpenFileToReadCallback; | |
52 using GetDocumentCallback = mojom::FileSystemInstance::GetDocumentCallback; | |
53 using GetChildDocumentsCallback = | |
54 mojom::FileSystemInstance::GetChildDocumentsCallback; | |
55 | |
56 // For supporting ArcServiceManager::GetService<T>(). | |
57 static const char kArcServiceName[]; | |
58 | |
59 // Creates an instance suitable for unit tests. | |
60 // This instance will run all operations immediately without deferring by | |
61 // default. Also, deferring can be enabled/disabled by calling | |
62 // SetShouldDefer() from friend classes. | |
63 static ArcFileSystemOperationRunner* CreateForTesting( | |
hidehiko
2017/01/26 07:24:20
Could you return std::unique_ptr<ArcFileSystemOper
Shuhei Takahashi
2017/01/27 09:55:10
Done.
| |
47 ArcBridgeService* bridge_service); | 64 ArcBridgeService* bridge_service); |
48 ~ArcDeferredFileSystemOperationRunner() override; | |
49 | 65 |
50 // ArcFileSystemOperationRunner overrides: | 66 // The standard constructor. A production instance should be created by |
51 void GetFileSize(const GURL& url, | 67 // this constructor. |
52 const GetFileSizeCallback& callback) override; | 68 explicit ArcFileSystemOperationRunner(ArcBridgeService* bridge_service); |
53 void OpenFileToRead(const GURL& url, | 69 ~ArcFileSystemOperationRunner() override; |
54 const OpenFileToReadCallback& callback) override; | 70 |
71 // Runs file system operations. See file_system.mojom for documentation. | |
72 void GetFileSize(const GURL& url, const GetFileSizeCallback& callback); | |
73 void OpenFileToRead(const GURL& url, const OpenFileToReadCallback& callback); | |
55 void GetDocument(const std::string& authority, | 74 void GetDocument(const std::string& authority, |
56 const std::string& document_id, | 75 const std::string& document_id, |
57 const GetDocumentCallback& callback) override; | 76 const GetDocumentCallback& callback); |
58 void GetChildDocuments(const std::string& authority, | 77 void GetChildDocuments(const std::string& authority, |
59 const std::string& parent_document_id, | 78 const std::string& parent_document_id, |
60 const GetChildDocumentsCallback& callback) override; | 79 const GetChildDocumentsCallback& callback); |
61 | 80 |
62 // ArcSessionManager::Observer overrides: | 81 // ArcSessionManager::Observer overrides: |
63 void OnArcOptInChanged(bool enabled) override; | 82 void OnArcOptInChanged(bool enabled) override; |
64 | 83 |
65 // InstanceHolder<mojom::FileSystemInstance>::Observer overrides: | 84 // InstanceHolder<mojom::FileSystemInstance>::Observer overrides: |
66 void OnInstanceReady() override; | 85 void OnInstanceReady() override; |
67 void OnInstanceClosed() override; | 86 void OnInstanceClosed() override; |
68 | 87 |
69 private: | 88 private: |
70 friend class ArcDeferredFileSystemOperationRunnerTest; | 89 friend class ArcFileSystemOperationRunnerTest; |
71 | 90 |
72 // Constructor called from unit tests. | 91 ArcFileSystemOperationRunner(ArcBridgeService* bridge_service, |
73 ArcDeferredFileSystemOperationRunner(ArcBridgeService* bridge_service, | 92 bool observe_events); |
74 bool observe_events); | |
75 | 93 |
76 // Called whenever ARC states related to |should_defer_| are changed. | 94 // Called whenever ARC states related to |should_defer_| are changed. |
77 void OnStateChanged(); | 95 void OnStateChanged(); |
78 | 96 |
79 // Enables/disables deferring. | 97 // Enables/disables deferring. |
98 // Friend unit tests can call this function to simulate enabling/disabling | |
99 // deferring. | |
80 void SetShouldDefer(bool should_defer); | 100 void SetShouldDefer(bool should_defer); |
81 | 101 |
82 // Indicates if this instance should register observers to receive events. | 102 // Indicates if this instance should register observers to receive events. |
83 // Usually true, but set to false in unit tests. | 103 // Usually true, but set to false in unit tests. |
84 bool observe_events_; | 104 bool observe_events_; |
85 | 105 |
86 // Set to true if operations should be deferred at this moment. | 106 // Set to true if operations should be deferred at this moment. |
87 bool should_defer_ = true; | 107 // The default is set to false so that operations are not deferred in |
108 // unit tests. | |
109 bool should_defer_ = false; | |
88 | 110 |
89 // List of deferred operations. | 111 // List of deferred operations. |
90 std::vector<base::Closure> deferred_operations_; | 112 std::vector<base::Closure> deferred_operations_; |
91 | 113 |
92 base::WeakPtrFactory<ArcDeferredFileSystemOperationRunner> weak_ptr_factory_; | 114 base::WeakPtrFactory<ArcFileSystemOperationRunner> weak_ptr_factory_; |
93 | 115 |
94 DISALLOW_COPY_AND_ASSIGN(ArcDeferredFileSystemOperationRunner); | 116 DISALLOW_COPY_AND_ASSIGN(ArcFileSystemOperationRunner); |
95 }; | 117 }; |
96 | 118 |
97 } // namespace arc | 119 } // namespace arc |
98 | 120 |
99 #endif // CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_DEFERRED_FILE_SYSTEM_OPERATIO N_RUNNER_H_ | 121 #endif // CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_FILE_SYSTEM_OPERATION_RUNNER_ H_ |
OLD | NEW |