1// Copyright (c) 2012 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 COMPONENTS_WEBDATA_COMMON_WEB_DATA_RESULTS_H_
6#define COMPONENTS_WEBDATA_COMMON_WEB_DATA_RESULTS_H_
7
8#include "base/basictypes.h"
9#include "base/callback.h"
10#include "components/webdata/common/webdata_export.h"
11
12class WDTypedResult;
13
14//
15// Result types for WebDataService.
16//
17typedef enum {
18  BOOL_RESULT = 1,             // WDResult<bool>
19  KEYWORDS_RESULT,             // WDResult<WDKeywordsResult>
20  INT64_RESULT,                // WDResult<int64>
21#if defined(OS_WIN)
22  PASSWORD_IE7_RESULT,         // WDResult<IE7PasswordInfo>
23#endif
24  WEB_APP_IMAGES,              // WDResult<WDAppImagesResult>
25  TOKEN_RESULT,                // WDResult<std::vector<std::string>>
26  AUTOFILL_VALUE_RESULT,       // WDResult<std::vector<base::string16>>
27  AUTOFILL_CHANGES,            // WDResult<std::vector<AutofillChange>>
28  AUTOFILL_PROFILE_RESULT,     // WDResult<AutofillProfile>
29  AUTOFILL_PROFILES_RESULT,    // WDResult<std::vector<AutofillProfile*>>
30  AUTOFILL_CREDITCARD_RESULT,  // WDResult<CreditCard>
31  AUTOFILL_CREDITCARDS_RESULT, // WDResult<std::vector<CreditCard*>>
32  WEB_INTENTS_RESULT,          // WDResult<std::vector<WebIntentServiceData>>
33  WEB_INTENTS_DEFAULTS_RESULT, // WDResult<std::vector<DefaultWebIntentService>>
34} WDResultType;
35
36
37typedef base::Callback<void(const WDTypedResult*)> DestroyCallback;
38
39//
40// The top level class for a result.
41//
42class WEBDATA_EXPORT WDTypedResult {
43 public:
44  virtual ~WDTypedResult() {
45  }
46
47  // Return the result type.
48  WDResultType GetType() const {
49    return type_;
50  }
51
52  virtual void Destroy() {
53  }
54
55 protected:
56  explicit WDTypedResult(WDResultType type)
57    : type_(type) {
58  }
59
60 private:
61  WDResultType type_;
62  DISALLOW_COPY_AND_ASSIGN(WDTypedResult);
63};
64
65// A result containing one specific pointer or literal value.
66template <class T> class WDResult : public WDTypedResult {
67 public:
68  WDResult(WDResultType type, const T& v)
69      : WDTypedResult(type), value_(v) {
70  }
71
72  virtual ~WDResult() {
73  }
74
75  // Return a single value result.
76  T GetValue() const {
77    return value_;
78  }
79
80 private:
81  T value_;
82
83  DISALLOW_COPY_AND_ASSIGN(WDResult);
84};
85
86template <class T> class WDDestroyableResult : public WDTypedResult {
87 public:
88  WDDestroyableResult(
89      WDResultType type,
90      const T& v,
91      const DestroyCallback& callback)
92      : WDTypedResult(type),
93        value_(v),
94        callback_(callback) {
95  }
96
97  virtual ~WDDestroyableResult() {
98  }
99
100
101  virtual void Destroy()  OVERRIDE {
102    if (!callback_.is_null()) {
103      callback_.Run(this);
104    }
105  }
106
107  // Return a single value result.
108  T GetValue() const {
109    return value_;
110  }
111
112 private:
113  T value_;
114  DestroyCallback callback_;
115
116  DISALLOW_COPY_AND_ASSIGN(WDDestroyableResult);
117};
118
119template <class T> class WDObjectResult : public WDTypedResult {
120 public:
121  explicit WDObjectResult(WDResultType type)
122    : WDTypedResult(type) {
123  }
124
125  T* GetValue() const {
126    return &value_;
127  }
128
129 private:
130  // mutable to keep GetValue() const.
131  mutable T value_;
132  DISALLOW_COPY_AND_ASSIGN(WDObjectResult);
133};
134
135#endif  // COMPONENTS_WEBDATA_COMMON_WEB_DATA_RESULTS_H_
136