Home | History | Annotate | Download | only in storage
      1 // Copyright 2014 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 CHROME_BROWSER_LOCAL_DISCOVERY_STORAGE_PRIVET_FILESYSTEM_OPERATIONS_H_
      6 #define CHROME_BROWSER_LOCAL_DISCOVERY_STORAGE_PRIVET_FILESYSTEM_OPERATIONS_H_
      7 
      8 #include "chrome/browser/local_discovery/privet_device_resolver.h"
      9 #include "chrome/browser/local_discovery/privet_http.h"
     10 #include "chrome/browser/local_discovery/privet_http_asynchronous_factory.h"
     11 #include "chrome/browser/local_discovery/service_discovery_shared_client.h"
     12 #include "chrome/browser/local_discovery/storage/path_util.h"
     13 #include "chrome/browser/local_discovery/storage/privet_filesystem_attribute_cache.h"
     14 #include "content/public/browser/browser_context.h"
     15 #include "content/public/browser/browser_thread.h"
     16 #include "net/url_request/url_request_context.h"
     17 #include "webkit/browser/fileapi/async_file_util.h"
     18 #include "webkit/browser/fileapi/file_system_url.h"
     19 
     20 namespace local_discovery {
     21 
     22 class PrivetFileSystemAsyncOperation {
     23  public:
     24   virtual ~PrivetFileSystemAsyncOperation() {}
     25 
     26   virtual void Start() = 0;
     27 };
     28 
     29 class PrivetFileSystemAsyncOperationContainer {
     30  public:
     31   virtual ~PrivetFileSystemAsyncOperationContainer() {}
     32 
     33   virtual void RemoveOperation(
     34       PrivetFileSystemAsyncOperation* operation) = 0;
     35   virtual void RemoveAllOperations() = 0;
     36 };
     37 
     38 // This object is a counterpart to PrivetFileSystemAsyncUtil that lives on the
     39 // UI thread.
     40 class PrivetFileSystemOperationFactory
     41     : public PrivetFileSystemAsyncOperationContainer {
     42  public:
     43   explicit PrivetFileSystemOperationFactory(
     44       content::BrowserContext* browser_context);
     45   virtual ~PrivetFileSystemOperationFactory();
     46 
     47   void GetFileInfo(const fileapi::FileSystemURL& url,
     48                    const fileapi::AsyncFileUtil::GetFileInfoCallback& callback);
     49   void ReadDirectory(
     50       const fileapi::FileSystemURL& url,
     51       const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback);
     52 
     53   base::WeakPtr<PrivetFileSystemOperationFactory> GetWeakPtr() {
     54     return weak_factory_.GetWeakPtr();
     55   }
     56 
     57  private:
     58   virtual void RemoveOperation(PrivetFileSystemAsyncOperation* operation)
     59       OVERRIDE;
     60   virtual void RemoveAllOperations() OVERRIDE;
     61 
     62   PrivetFileSystemAttributeCache attribute_cache_;
     63   std::set<PrivetFileSystemAsyncOperation*> async_operations_;
     64   content::BrowserContext* browser_context_;
     65   base::WeakPtrFactory<PrivetFileSystemOperationFactory> weak_factory_;
     66 };
     67 
     68 class PrivetFileSystemAsyncOperationUtil {
     69  public:
     70   class Delegate {
     71    public:
     72     Delegate() {}
     73     virtual ~Delegate() {}
     74 
     75     // |http_client| is the client for the resolved service, or NULL if
     76     // resolution failed. |path| is the canonical path within the privet
     77     // service.
     78     virtual void PrivetFileSystemResolved(PrivetV1HTTPClient* http_client,
     79                                           const std::string& path) = 0;
     80   };
     81 
     82   PrivetFileSystemAsyncOperationUtil(const base::FilePath& full_path,
     83                                      content::BrowserContext* browser_context,
     84                                      Delegate* delegate);
     85   ~PrivetFileSystemAsyncOperationUtil();
     86 
     87   void Start();
     88 
     89  private:
     90   void OnGotDeviceDescription(bool success,
     91                               const DeviceDescription& device_description);
     92   void OnGotPrivetHTTP(scoped_ptr<PrivetHTTPClient> privet_http_client);
     93 
     94   ParsedPrivetPath parsed_path_;
     95   scoped_refptr<net::URLRequestContextGetter> request_context_;
     96   content::BrowserContext* browser_context_;
     97   Delegate* delegate_;
     98 
     99   scoped_refptr<ServiceDiscoverySharedClient> service_discovery_client_;
    100   scoped_ptr<PrivetDeviceResolver> privet_device_resolver_;
    101   scoped_ptr<PrivetHTTPAsynchronousFactory> privet_async_factory_;
    102   scoped_ptr<PrivetHTTPResolution> privet_http_resolution_;
    103   scoped_ptr<PrivetV1HTTPClient> privet_client_;
    104 
    105   base::WeakPtrFactory<PrivetFileSystemAsyncOperationUtil> weak_factory_;
    106 };
    107 
    108 class PrivetFileSystemListOperation
    109     : public PrivetFileSystemAsyncOperationUtil::Delegate,
    110       public local_discovery::PrivetFileSystemAsyncOperation {
    111  public:
    112   PrivetFileSystemListOperation(
    113       const base::FilePath& full_path,
    114       content::BrowserContext* browser_context,
    115       PrivetFileSystemAsyncOperationContainer* container,
    116       PrivetFileSystemAttributeCache* attribute_cache,
    117       const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback);
    118   virtual ~PrivetFileSystemListOperation();
    119 
    120   virtual void Start() OVERRIDE;
    121 
    122   virtual void PrivetFileSystemResolved(PrivetV1HTTPClient* http_client,
    123                                         const std::string& path) OVERRIDE;
    124 
    125  private:
    126   void OnStorageListResult(const base::DictionaryValue* value);
    127   void SignalError();
    128   void TriggerCallbackAndDestroy(
    129       base::File::Error result,
    130       const fileapi::AsyncFileUtil::EntryList& file_list,
    131       bool has_more);
    132 
    133   PrivetFileSystemAsyncOperationUtil core_;
    134   base::FilePath full_path_;
    135   PrivetFileSystemAsyncOperationContainer* container_;
    136   PrivetFileSystemAttributeCache* attribute_cache_;
    137   fileapi::AsyncFileUtil::ReadDirectoryCallback callback_;
    138 
    139   scoped_ptr<PrivetJSONOperation> list_operation_;
    140 };
    141 
    142 class PrivetFileSystemDetailsOperation
    143     : public PrivetFileSystemAsyncOperationUtil::Delegate,
    144       public local_discovery::PrivetFileSystemAsyncOperation {
    145  public:
    146   PrivetFileSystemDetailsOperation(
    147       const base::FilePath& full_path,
    148       content::BrowserContext* browser_context,
    149       PrivetFileSystemAsyncOperationContainer* container,
    150       PrivetFileSystemAttributeCache* attribute_cache,
    151       const fileapi::AsyncFileUtil::GetFileInfoCallback& callback);
    152   virtual ~PrivetFileSystemDetailsOperation();
    153 
    154   virtual void Start() OVERRIDE;
    155 
    156   virtual void PrivetFileSystemResolved(PrivetV1HTTPClient* http_client,
    157                                         const std::string& path) OVERRIDE;
    158 
    159  private:
    160   void OnStorageListResult(const base::DictionaryValue* value);
    161   void SignalError();
    162   void TriggerCallbackAndDestroy(base::File::Error result,
    163                                  const base::File::Info& info);
    164 
    165   PrivetFileSystemAsyncOperationUtil core_;
    166   base::FilePath full_path_;
    167   PrivetFileSystemAsyncOperationContainer* container_;
    168   PrivetFileSystemAttributeCache* attribute_cache_;
    169   fileapi::AsyncFileUtil::GetFileInfoCallback callback_;
    170 
    171   scoped_ptr<PrivetJSONOperation> list_operation_;
    172 };
    173 
    174 }  // namespace local_discovery
    175 
    176 #endif  // CHROME_BROWSER_LOCAL_DISCOVERY_STORAGE_PRIVET_FILESYSTEM_OPERATIONS_H_
    177