Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / history / android / android_history_types.h
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 CHROME_BROWSER_HISTORY_ANDROID_ANDROID_HISTORY_TYPES_H_
6 #define CHROME_BROWSER_HISTORY_ANDROID_ANDROID_HISTORY_TYPES_H_
7
8 #include <map>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/history/history_types.h"
12 #include "components/history/core/browser/keyword_id.h"
13 #include "sql/statement.h"
14
15 namespace sql {
16 class Statement;
17 }
18
19 namespace history {
20
21 typedef int64 AndroidURLID;
22 typedef int64 SearchTermID;
23
24 // Wraps all columns needed to support android.provider.Browser.BookmarkColumns.
25 // It is used in insert() and update() to specify the columns need to insert or
26 // update.
27 // The column is not valid until it set. Using is_valid() to find out whether
28 // the specific column could be used.
29 //
30 // The defult copy constructor is used.
31 class HistoryAndBookmarkRow {
32  public:
33   enum ColumnID {
34     ID,
35     URL,
36     TITLE,
37     CREATED,
38     LAST_VISIT_TIME,
39     VISIT_COUNT,
40     FAVICON,
41     BOOKMARK,
42     RAW_URL,
43     PARENT_ID,
44     URL_ID,
45     COLUMN_END // This must be the last.
46   };
47
48   HistoryAndBookmarkRow();
49   virtual ~HistoryAndBookmarkRow();
50
51   // Returns the column name defined in Android.
52   static std::string GetAndroidName(ColumnID id);
53
54   static ColumnID GetColumnID(const std::string& name);
55
56   // URLs for the page.
57   void set_url(const GURL& url) {
58     set_value_explicitly(URL);
59     url_ = url;
60   }
61   const GURL& url() const {
62     return url_;
63   }
64
65   // Raw input URL
66   void set_raw_url(const std::string& raw_url) {
67     set_value_explicitly(RAW_URL);
68     raw_url_ = raw_url;
69   }
70   const std::string& raw_url() const {
71     return raw_url_;
72   }
73
74   // The title of page.
75   void set_title(const base::string16& title) {
76     set_value_explicitly(TITLE);
77     title_ = title;
78   }
79   const base::string16& title() const {
80     return title_;
81   }
82
83   // The page's first visit time.
84   void set_created(const base::Time created) {
85     set_value_explicitly(CREATED);
86     created_ = created;
87   }
88   const base::Time& created() const {
89     return created_;
90   }
91
92   // The page's last visit time.
93   void set_last_visit_time(const base::Time last_visit_time) {
94     set_value_explicitly(LAST_VISIT_TIME);
95     last_visit_time_ = last_visit_time;
96   }
97   const base::Time& last_visit_time() const {
98     return last_visit_time_;
99   }
100
101   // The visit times
102   void set_visit_count(int visit_count) {
103     set_value_explicitly(VISIT_COUNT);
104     visit_count_ = visit_count;
105   }
106   int visit_count() const {
107     return visit_count_;
108   }
109
110   // Whether the page is bookmarked.
111   void set_is_bookmark(bool is_bookmark) {
112     set_value_explicitly(BOOKMARK);
113     is_bookmark_ = is_bookmark;
114   }
115   bool is_bookmark() const {
116     return is_bookmark_;
117   }
118
119   // The favicon related to page if any.
120   void set_favicon(const scoped_refptr<base::RefCountedMemory>& data) {
121     set_value_explicitly(FAVICON);
122     favicon_ = data;
123   }
124   const scoped_refptr<base::RefCountedMemory>& favicon() const {
125     return favicon_;
126   }
127
128   bool favicon_valid() const {
129     return favicon_.get() && favicon_->size();
130   }
131
132   // The id of android url.
133   void set_id(AndroidURLID id) {
134     set_value_explicitly(ID);
135     id_ = id;
136   }
137   AndroidURLID id() const {
138     return id_;
139   }
140
141   // The id of the parent folder containing the bookmark, if any.
142   void set_parent_id(int64 parent_id) {
143     set_value_explicitly(PARENT_ID);
144     parent_id_ = parent_id;
145   }
146   const int64 parent_id() const {
147     return parent_id_;
148   }
149
150   // The internal URLID
151   void set_url_id(URLID url_id) {
152     set_value_explicitly(URL_ID);
153     url_id_ = url_id;
154   }
155   URLID url_id() const {
156     return url_id_;
157   }
158
159   // Returns true if the given |id| has been set explicitly.
160   bool is_value_set_explicitly(ColumnID id) const {
161     return values_set_.find(id) != values_set_.end();
162   }
163
164  private:
165   void set_value_explicitly(ColumnID id) {
166     values_set_.insert(id);
167   }
168
169   AndroidURLID id_;
170   GURL url_;
171   std::string raw_url_;
172   base::string16 title_;
173   base::Time created_;
174   base::Time last_visit_time_;
175   scoped_refptr<base::RefCountedMemory> favicon_;
176   int visit_count_;
177   bool is_bookmark_;
178   int64 parent_id_;
179   URLID url_id_;
180
181   // Used to find whether a column has been set a value explicitly.
182   std::set<ColumnID> values_set_;
183
184   // We support the implicit copy constuctor and operator=.
185 };
186
187 // Wraps all columns needed to support android.provider.Browser.SearchColumns.
188 // It is used in insert() and update() to specify the columns need to insert or
189 // update.
190 //
191 // The column is not valid until it set. Using is_valid() to find out whether
192 // the specific column could be used.
193 //
194 // The defult copy constructor is used.
195 class SearchRow {
196  public:
197   enum ColumnID {
198     ID,
199     SEARCH_TERM,
200     SEARCH_TIME,
201     URL,
202     KEYWORD_ID,
203     COLUMN_END
204   };
205
206   SearchRow();
207   virtual ~SearchRow();
208
209   // Returns the column name defined in Android.
210   static std::string GetAndroidName(ColumnID id);
211
212   static ColumnID GetColumnID(const std::string& name);
213
214   SearchTermID id() const {
215     return id_;
216   }
217   void set_id(SearchTermID id) {
218     set_value_explicitly(SearchRow::ID);
219     id_ = id;
220   }
221
222   const base::string16& search_term() const {
223     return search_term_;
224   }
225   void set_search_term(const base::string16& search_term) {
226     set_value_explicitly(SearchRow::SEARCH_TERM);
227     search_term_ = search_term;
228   }
229
230   const base::Time search_time() const {
231     return search_time_;
232   }
233   void set_search_time(const base::Time& time) {
234     set_value_explicitly(SearchRow::SEARCH_TIME);
235     search_time_ = time;
236   }
237
238   const GURL& url() const {
239     return url_;
240   }
241   void set_url(const GURL& url) {
242     set_value_explicitly(SearchRow::URL);
243     url_ = url;
244   }
245
246   KeywordID keyword_id() const {
247     return keyword_id_;
248   }
249   void set_keyword_id(KeywordID keyword_id) {
250     set_value_explicitly(SearchRow::KEYWORD_ID);
251     keyword_id_ = keyword_id;
252   }
253
254  // Returns true if the given |id| has been set explicitly.
255   bool is_value_set_explicitly(ColumnID id) const {
256     return values_set_.find(id) != values_set_.end();
257   }
258
259  private:
260   void set_value_explicitly(ColumnID id) {
261     values_set_.insert(id);
262   }
263
264   SearchTermID id_;
265   base::string16 search_term_;
266   base::Time search_time_;
267   GURL url_;
268   KeywordID keyword_id_;
269
270   // Used to find whether a column has been set a value.
271   std::set<ColumnID> values_set_;
272
273   // We support the implicit copy constuctor and operator=.
274 };
275
276 // Defines the row stored in android_urls table.
277 struct AndroidURLRow {
278   AndroidURLRow();
279   ~AndroidURLRow();
280
281   // The unique id of the row
282   AndroidURLID id;
283   // The corresponding URLID in the url table.
284   URLID url_id;
285   // The orignal URL string passed in by client.
286   std::string raw_url;
287 };
288
289 // Defines the row of keyword_cache table.
290 struct SearchTermRow {
291   SearchTermRow();
292   ~SearchTermRow();
293
294   // The unique id of the row.
295   SearchTermID id;
296   // The keyword.
297   base::string16 term;
298   // The last visit time.
299   base::Time last_visit_time;
300 };
301
302 // This class wraps the sql statement and favicon column index in statement if
303 // any. It is returned by AndroidProviderBackend::Query().
304 //
305 // Using favicon_index() to get the index of favicon; The value of that column
306 // is the Favicon ID, Client should call HistoryService::GetFavicon() to get the
307 // actual value.
308 class AndroidStatement {
309  public:
310   AndroidStatement(sql::Statement* statement, int favicon_index);
311   ~AndroidStatement();
312
313   sql::Statement* statement() {
314     return statement_.get();
315   }
316
317   // The favicon index in statement; -1 is returned if favicon is not in
318   // the statement.
319   int favicon_index() const {
320     return favicon_index_;
321   }
322
323  private:
324   scoped_ptr<sql::Statement> statement_;
325   int favicon_index_;
326
327   DISALLOW_COPY_AND_ASSIGN(AndroidStatement);
328 };
329
330 }  // namespace history
331
332 #endif  // CHROME_BROWSER_HISTORY_ANDROID_ANDROID_HISTORY_TYPES_H_