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_CHROMEOS_EXTENSIONS_FILE_SYSTEM_PROVIDER_PROVIDER_FUNCTION_H_ 6 #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_SYSTEM_PROVIDER_PROVIDER_FUNCTION_H_ 7 8 #include <string> 9 10 #include "base/files/file.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "chrome/browser/extensions/chrome_extension_function.h" 13 #include "chrome/common/extensions/api/file_system_provider.h" 14 15 namespace base { 16 class DictionaryValue; 17 } // namespace base 18 19 namespace chromeos { 20 namespace file_system_provider { 21 22 class RequestManager; 23 class RequestValue; 24 25 } // namespace file_system_provider 26 } // namespace chromeos 27 28 namespace extensions { 29 30 // Error names from 31 // http://www.w3.org/TR/file-system-api/#errors-and-exceptions 32 extern const char kNotFoundErrorName[]; 33 extern const char kSecurityErrorName[]; 34 35 // Error messages. 36 extern const char kEmptyNameErrorMessage[]; 37 extern const char kEmptyIdErrorMessage[]; 38 extern const char kMountFailedErrorMessage[]; 39 extern const char kUnmountFailedErrorMessage[]; 40 extern const char kResponseFailedErrorMessage[]; 41 42 // Creates a dictionary, which looks like a DOMError. The returned dictionary 43 // will be converted to a real DOMError object in 44 // file_system_provier_custom_bindings.js. 45 base::DictionaryValue* CreateError(const std::string& name, 46 const std::string& message); 47 48 // Converts ProviderError to base::File::Error. This could be redundant, if it 49 // was possible to create DOMError instances in Javascript easily. 50 base::File::Error ProviderErrorToFileError( 51 api::file_system_provider::ProviderError error); 52 53 // Base class for internal API functions handling request results, either 54 // a success or a failure. 55 class FileSystemProviderInternalFunction : public ChromeSyncExtensionFunction { 56 public: 57 FileSystemProviderInternalFunction(); 58 59 protected: 60 virtual ~FileSystemProviderInternalFunction() {} 61 62 // Rejects the request and sets a response for this API function. 63 void RejectRequest(base::File::Error error); 64 65 // Fulfills the request with parsed arguments of this API function 66 // encapsulated as a RequestValue instance. Also, sets a response. 67 // If |has_more| is set to true, then the function will be called again for 68 // this request. 69 void FulfillRequest( 70 scoped_ptr<chromeos::file_system_provider::RequestValue> value, 71 bool has_more); 72 73 // Subclasses implement this for their functionality. 74 // Called after Parse() is successful, such that |request_id_| and 75 // |request_manager_| have been fully initialized. 76 virtual bool RunWhenValid() = 0; 77 78 // ChromeSyncExtensionFunction overrides. 79 virtual bool RunSync() OVERRIDE; 80 81 private: 82 // Parses the request in order to extract the request manager. If fails, then 83 // sets a response and returns false. 84 bool Parse(); 85 86 // Sets an error message in case of a failure. 87 void SetErrorResponse(const std::string& name, const std::string& message); 88 89 int request_id_; 90 chromeos::file_system_provider::RequestManager* request_manager_; 91 }; 92 93 } // namespace extensions 94 95 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_SYSTEM_PROVIDER_PROVIDER_FUNCTION_H_ 96