summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/extensions/file_system_provider/provider_function.h
blob: f4ed7d7055e69fb5d4aab73a60fe7c28938ff022 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_SYSTEM_PROVIDER_PROVIDER_FUNCTION_H_
#define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_SYSTEM_PROVIDER_PROVIDER_FUNCTION_H_

#include <string>

#include "base/files/file.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "chrome/common/extensions/api/file_system_provider.h"

namespace base {
class DictionaryValue;
}  // namespace base

namespace chromeos {
namespace file_system_provider {

class RequestManager;
class RequestValue;

}  // namespace file_system_provider
}  // namespace chromeos

namespace extensions {

// Error names from
// http://www.w3.org/TR/file-system-api/#errors-and-exceptions
extern const char kNotFoundErrorName[];
extern const char kSecurityErrorName[];

// Error messages.
extern const char kEmptyNameErrorMessage[];
extern const char kEmptyIdErrorMessage[];
extern const char kMountFailedErrorMessage[];
extern const char kUnmountFailedErrorMessage[];
extern const char kResponseFailedErrorMessage[];

// Creates a dictionary, which looks like a DOMError. The returned dictionary
// will be converted to a real DOMError object in
// file_system_provier_custom_bindings.js.
base::DictionaryValue* CreateError(const std::string& name,
                                   const std::string& message);

// Converts ProviderError to base::File::Error. This could be redundant, if it
// was possible to create DOMError instances in Javascript easily.
base::File::Error ProviderErrorToFileError(
    api::file_system_provider::ProviderError error);

// Base class for internal API functions handling request results, either
// a success or a failure.
class FileSystemProviderInternalFunction : public ChromeSyncExtensionFunction {
 public:
  FileSystemProviderInternalFunction();

 protected:
  virtual ~FileSystemProviderInternalFunction() {}

  // Rejects the request and sets a response for this API function.
  void RejectRequest(base::File::Error error);

  // Fulfills the request with parsed arguments of this API function
  // encapsulated as a RequestValue instance. Also, sets a response.
  // If |has_more| is set to true, then the function will be called again for
  // this request.
  void FulfillRequest(
      scoped_ptr<chromeos::file_system_provider::RequestValue> value,
      bool has_more);

  // Subclasses implement this for their functionality.
  // Called after Parse() is successful, such that |request_id_| and
  // |request_manager_| have been fully initialized.
  virtual bool RunWhenValid() = 0;

  // ChromeSyncExtensionFunction overrides.
  virtual bool RunSync() OVERRIDE;

 private:
  // Parses the request in order to extract the request manager. If fails, then
  // sets a response and returns false.
  bool Parse();

  // Sets an error message in case of a failure.
  void SetErrorResponse(const std::string& name, const std::string& message);

  int request_id_;
  chromeos::file_system_provider::RequestManager* request_manager_;
};

}  // namespace extensions

#endif  // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_SYSTEM_PROVIDER_PROVIDER_FUNCTION_H_