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( 64 scoped_ptr<chromeos::file_system_provider::RequestValue> value, 65 base::File::Error error); 66 67 // Fulfills the request with parsed arguments of this API function 68 // encapsulated as a RequestValue instance. Also, sets a response. 69 // If |has_more| is set to true, then the function will be called again for 70 // this request. 71 void FulfillRequest( 72 scoped_ptr<chromeos::file_system_provider::RequestValue> value, 73 bool has_more); 74 75 // Subclasses implement this for their functionality. 76 // Called after Parse() is successful, such that |request_id_| and 77 // |request_manager_| have been fully initialized. 78 virtual bool RunWhenValid() = 0; 79 80 // ChromeSyncExtensionFunction overrides. 81 virtual bool RunSync() OVERRIDE; 82 83 private: 84 // Parses the request in order to extract the request manager. If fails, then 85 // sets a response and returns false. 86 bool Parse(); 87 88 // Sets an error message in case of a failure. 89 void SetErrorResponse(const std::string& name, const std::string& message); 90 91 int request_id_; 92 chromeos::file_system_provider::RequestManager* request_manager_; 93 }; 94 95 } // namespace extensions 96 97 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_SYSTEM_PROVIDER_PROVIDER_FUNCTION_H_ 98