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