OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Chromium settings and storage represent user-selected preferences and | 5 // Chromium settings and storage represent user-selected preferences and |
6 // information and MUST not be extracted, overwritten or modified except | 6 // information and MUST not be extracted, overwritten or modified except |
7 // through Chromium defined APIs. | 7 // through Chromium defined APIs. |
8 | 8 |
9 #ifndef CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_H__ | 9 #ifndef CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_H__ |
10 #define CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_H__ | 10 #define CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_H__ |
11 | 11 |
12 #include <map> | 12 #include <map> |
13 #include <string> | 13 #include <string> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/callback_forward.h" | 16 #include "base/callback_forward.h" |
17 #include "base/file_path.h" | 17 #include "base/file_path.h" |
18 #include "base/location.h" | 18 #include "base/location.h" |
19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
20 #include "base/sequenced_task_runner_helpers.h" | 20 #include "base/sequenced_task_runner_helpers.h" |
21 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
22 #include "chrome/browser/api/webdata/autofill_web_data_service.h" | 22 #include "chrome/browser/api/webdata/autofill_web_data_service.h" |
23 #include "chrome/browser/api/webdata/web_data_results.h" | 23 #include "chrome/browser/api/webdata/web_data_results.h" |
24 #include "chrome/browser/api/webdata/web_data_service_base.h" | 24 #include "chrome/browser/api/webdata/web_data_service_base.h" |
25 #include "chrome/browser/api/webdata/web_data_service_consumer.h" | 25 #include "chrome/browser/api/webdata/web_data_service_consumer.h" |
26 #include "chrome/browser/profiles/refcounted_profile_keyed_service.h" | 26 #include "chrome/browser/profiles/refcounted_profile_keyed_service.h" |
27 #include "chrome/browser/search_engines/template_url.h" | 27 #include "chrome/browser/search_engines/template_url.h" |
28 #include "chrome/browser/search_engines/template_url_id.h" | 28 #include "chrome/browser/search_engines/template_url_id.h" |
29 #include "chrome/browser/webdata/keyword_table.h" | 29 #include "chrome/browser/webdata/keyword_table.h" |
30 #include "chrome/browser/webdata/web_data_request_manager.h" | |
30 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
31 #include "sql/init_status.h" | 32 #include "sql/init_status.h" |
32 | 33 |
33 class AutocompleteSyncableService; | 34 class AutocompleteSyncableService; |
34 class AutofillChange; | 35 class AutofillChange; |
35 class AutofillProfileSyncableService; | 36 class AutofillProfileSyncableService; |
36 struct DefaultWebIntentService; | 37 struct DefaultWebIntentService; |
37 class GURL; | 38 class GURL; |
38 #if defined(OS_WIN) | 39 #if defined(OS_WIN) |
39 struct IE7PasswordInfo; | 40 struct IE7PasswordInfo; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 int builtin_keyword_version; | 94 int builtin_keyword_version; |
94 }; | 95 }; |
95 | 96 |
96 class WebDataServiceConsumer; | 97 class WebDataServiceConsumer; |
97 | 98 |
98 class WebDataService | 99 class WebDataService |
99 : public WebDataServiceBase, | 100 : public WebDataServiceBase, |
100 public AutofillWebData, | 101 public AutofillWebData, |
101 public RefcountedProfileKeyedService { | 102 public RefcountedProfileKeyedService { |
102 public: | 103 public: |
103 ////////////////////////////////////////////////////////////////////////////// | |
104 // | |
105 // Internal requests | |
106 // | |
107 // Every request is processed using a request object. The object contains | |
108 // both the request parameters and the results. | |
109 ////////////////////////////////////////////////////////////////////////////// | |
110 class WebDataRequest { | |
111 public: | |
112 WebDataRequest(WebDataService* service, | |
113 Handle handle, | |
114 WebDataServiceConsumer* consumer); | |
115 | |
116 virtual ~WebDataRequest(); | |
117 | |
118 Handle GetHandle() const; | |
119 | |
120 // Retrieves the |consumer_| set in the constructor. If the request was | |
121 // cancelled via the |Cancel()| method then |true| is returned and | |
122 // |*consumer| is set to NULL. The |consumer| parameter may be set to NULL | |
123 // if only the return value is desired. | |
124 bool IsCancelled(WebDataServiceConsumer** consumer) const; | |
125 | |
126 // This can be invoked from any thread. From this point we assume that | |
127 // our consumer_ reference is invalid. | |
128 void Cancel(); | |
129 | |
130 // Invoked by the service when this request has been completed. | |
131 // This will notify the service in whatever thread was used to create this | |
132 // request. | |
133 void RequestComplete(); | |
134 | |
135 // The result is owned by the request. | |
136 void SetResult(WDTypedResult* r); | |
137 const WDTypedResult* GetResult() const; | |
138 | |
139 private: | |
140 scoped_refptr<WebDataService> service_; | |
141 MessageLoop* message_loop_; | |
142 Handle handle_; | |
143 | |
144 // A lock to protect against simultaneous cancellations of the request. | |
145 // Cancellation affects both the |cancelled_| flag and |consumer_|. | |
146 mutable base::Lock cancel_lock_; | |
147 bool cancelled_; | |
148 WebDataServiceConsumer* consumer_; | |
149 | |
150 WDTypedResult* result_; | |
151 | |
152 DISALLOW_COPY_AND_ASSIGN(WebDataRequest); | |
153 }; | |
154 | |
155 // | |
156 // Internally we use instances of the following template to represent | |
157 // requests. | |
158 // | |
159 template <class T> | |
160 class GenericRequest : public WebDataRequest { | |
161 public: | |
162 GenericRequest(WebDataService* service, | |
163 Handle handle, | |
164 WebDataServiceConsumer* consumer, | |
165 const T& arg) | |
166 : WebDataRequest(service, handle, consumer), | |
167 arg_(arg) { | |
168 } | |
169 | |
170 virtual ~GenericRequest() { | |
171 } | |
172 | |
173 const T& arg() const { return arg_; } | |
174 | |
175 private: | |
176 T arg_; | |
177 }; | |
178 | |
179 template <class T, class U> | |
180 class GenericRequest2 : public WebDataRequest { | |
181 public: | |
182 GenericRequest2(WebDataService* service, | |
183 Handle handle, | |
184 WebDataServiceConsumer* consumer, | |
185 const T& arg1, | |
186 const U& arg2) | |
187 : WebDataRequest(service, handle, consumer), | |
188 arg1_(arg1), | |
189 arg2_(arg2) { | |
190 } | |
191 | |
192 virtual ~GenericRequest2() { } | |
193 | |
194 const T& arg1() const { return arg1_; } | |
195 | |
196 const U& arg2() const { return arg2_; } | |
197 | |
198 private: | |
199 T arg1_; | |
200 U arg2_; | |
201 }; | |
202 | |
203 WebDataService(); | 104 WebDataService(); |
204 | 105 |
205 // WebDataServiceBase implementation. | 106 // WebDataServiceBase implementation. |
206 virtual void CancelRequest(Handle h) OVERRIDE; | 107 virtual void CancelRequest(Handle h) OVERRIDE; |
207 virtual content::NotificationSource GetNotificationSource() OVERRIDE; | 108 virtual content::NotificationSource GetNotificationSource() OVERRIDE; |
208 | 109 |
209 // Notifies listeners on the UI thread that multiple changes have been made to | 110 // Notifies listeners on the UI thread that multiple changes have been made to |
210 // to Autofill records of the database. | 111 // to Autofill records of the database. |
211 // NOTE: This method is intended to be called from the DB thread. It | 112 // NOTE: This method is intended to be called from the DB thread. It |
212 // it asynchronously notifies listeners on the UI thread. | 113 // it asynchronously notifies listeners on the UI thread. |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
401 // Returns the syncable service for Autofill addresses and credit cards stored | 302 // Returns the syncable service for Autofill addresses and credit cards stored |
402 // in this table. The returned service is owned by |this| object. | 303 // in this table. The returned service is owned by |this| object. |
403 virtual AutofillProfileSyncableService* | 304 virtual AutofillProfileSyncableService* |
404 GetAutofillProfileSyncableService() const; | 305 GetAutofillProfileSyncableService() const; |
405 | 306 |
406 // Returns the syncable service for field autocomplete stored in this table. | 307 // Returns the syncable service for field autocomplete stored in this table. |
407 // The returned service is owned by |this| object. | 308 // The returned service is owned by |this| object. |
408 virtual AutocompleteSyncableService* | 309 virtual AutocompleteSyncableService* |
409 GetAutocompleteSyncableService() const; | 310 GetAutocompleteSyncableService() const; |
410 | 311 |
312 virtual void DestroyAutofillProfileResult(const WDTypedResult* result); | |
dhollowa
2013/01/04 23:57:36
Can these be private?
Cait (Slow)
2013/01/07 17:08:54
Done.
| |
313 virtual void DestroyAutofillCreditCardResult(const WDTypedResult* result); | |
411 // Testing | 314 // Testing |
412 #ifdef UNIT_TEST | 315 #ifdef UNIT_TEST |
413 void set_failed_init(bool value) { failed_init_ = value; } | 316 void set_failed_init(bool value) { failed_init_ = value; } |
414 #endif | 317 #endif |
415 | 318 |
416 protected: | 319 protected: |
417 friend class TemplateURLServiceTest; | 320 friend class TemplateURLServiceTest; |
418 friend class TemplateURLServiceTestingProfile; | 321 friend class TemplateURLServiceTestingProfile; |
419 friend class WebDataServiceTest; | 322 friend class WebDataServiceTest; |
420 friend class WebDataRequest; | 323 friend class WebDataRequest; |
421 | 324 |
422 virtual ~WebDataService(); | 325 virtual ~WebDataService(); |
423 | 326 |
424 // This is invoked by the unit test; path is the path of the Web Data file. | 327 // This is invoked by the unit test; path is the path of the Web Data file. |
425 bool InitWithPath(const FilePath& path); | 328 bool InitWithPath(const FilePath& path); |
426 | 329 |
427 // Invoked by request implementations when a request has been processed. | 330 // Invoked by request implementations when a request has been processed. |
428 void RequestCompleted(Handle h); | 331 void RequestCompleted(Handle h); |
429 | 332 |
430 // Register the request as a pending request. | |
431 void RegisterRequest(WebDataRequest* request); | |
432 | |
433 ////////////////////////////////////////////////////////////////////////////// | 333 ////////////////////////////////////////////////////////////////////////////// |
434 // | 334 // |
435 // The following methods are only invoked in the web data service thread. | 335 // The following methods are only invoked in the web data service thread. |
436 // | 336 // |
437 ////////////////////////////////////////////////////////////////////////////// | 337 ////////////////////////////////////////////////////////////////////////////// |
438 private: | 338 private: |
439 friend struct content::BrowserThread::DeleteOnThread< | 339 friend struct content::BrowserThread::DeleteOnThread< |
440 content::BrowserThread::UI>; | 340 content::BrowserThread::UI>; |
441 friend class base::DeleteHelper<WebDataService>; | 341 friend class base::DeleteHelper<WebDataService>; |
442 | 342 |
(...skipping 21 matching lines...) Expand all Loading... | |
464 // Commit the current transaction and creates a new one. | 364 // Commit the current transaction and creates a new one. |
465 void Commit(); | 365 void Commit(); |
466 | 366 |
467 // Schedule a task on our worker thread. | 367 // Schedule a task on our worker thread. |
468 void ScheduleTask(const tracked_objects::Location& from_here, | 368 void ScheduleTask(const tracked_objects::Location& from_here, |
469 const base::Closure& task); | 369 const base::Closure& task); |
470 | 370 |
471 // Schedule a commit if one is not already pending. | 371 // Schedule a commit if one is not already pending. |
472 void ScheduleCommit(); | 372 void ScheduleCommit(); |
473 | 373 |
474 // Return the next request handle. | |
475 int GetNextRequestHandle(); | |
476 | |
477 ////////////////////////////////////////////////////////////////////////////// | 374 ////////////////////////////////////////////////////////////////////////////// |
478 // | 375 // |
479 // Keywords. | 376 // Keywords. |
480 // | 377 // |
481 ////////////////////////////////////////////////////////////////////////////// | 378 ////////////////////////////////////////////////////////////////////////////// |
482 void AddKeywordImpl(GenericRequest<TemplateURLData>* request); | 379 void AddKeywordImpl(GenericRequest<TemplateURLData>* request); |
483 void RemoveKeywordImpl(GenericRequest<TemplateURLID>* request); | 380 void RemoveKeywordImpl(GenericRequest<TemplateURLID>* request); |
484 void UpdateKeywordImpl(GenericRequest<TemplateURLData>* request); | 381 void UpdateKeywordImpl(GenericRequest<TemplateURLData>* request); |
485 void GetKeywordsImpl(WebDataRequest* request); | 382 void GetKeywordsImpl(WebDataRequest* request); |
486 void SetDefaultSearchProviderImpl(GenericRequest<TemplateURLID>* r); | 383 void SetDefaultSearchProviderImpl(GenericRequest<TemplateURLID>* r); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
568 // True once initialization has started. | 465 // True once initialization has started. |
569 bool is_running_; | 466 bool is_running_; |
570 | 467 |
571 // The path with which to initialize the database. | 468 // The path with which to initialize the database. |
572 FilePath path_; | 469 FilePath path_; |
573 | 470 |
574 // Our database. We own the |db_|, but don't use a |scoped_ptr| because the | 471 // Our database. We own the |db_|, but don't use a |scoped_ptr| because the |
575 // |db_| lifetime must be managed on the database thread. | 472 // |db_| lifetime must be managed on the database thread. |
576 WebDatabase* db_; | 473 WebDatabase* db_; |
577 | 474 |
475 // Keeps track of all pending requests made to the db. | |
476 scoped_ptr<WebDataRequestManager> request_manager_; | |
477 | |
578 // Syncable services for the database data. We own the services, but don't | 478 // Syncable services for the database data. We own the services, but don't |
579 // use |scoped_ptr|s because the lifetimes must be managed on the database | 479 // use |scoped_ptr|s because the lifetimes must be managed on the database |
580 // thread. | 480 // thread. |
581 // Currently only Autocomplete and Autofill profiles use the new Sync API, but | 481 // Currently only Autocomplete and Autofill profiles use the new Sync API, but |
582 // all the database data should migrate to this API over time. | 482 // all the database data should migrate to this API over time. |
583 AutocompleteSyncableService* autocomplete_syncable_service_; | 483 AutocompleteSyncableService* autocomplete_syncable_service_; |
584 AutofillProfileSyncableService* autofill_profile_syncable_service_; | 484 AutofillProfileSyncableService* autofill_profile_syncable_service_; |
585 | 485 |
586 // Whether the database failed to initialize. We use this to avoid | 486 // Whether the database failed to initialize. We use this to avoid |
587 // continually trying to reinit. | 487 // continually trying to reinit. |
588 bool failed_init_; | 488 bool failed_init_; |
589 | 489 |
590 // Whether we should commit the database. | 490 // Whether we should commit the database. |
591 bool should_commit_; | 491 bool should_commit_; |
592 | 492 |
593 // A lock to protect pending requests and next request handle. | |
594 base::Lock pending_lock_; | |
595 | |
596 // Next handle to be used for requests. Incremented for each use. | |
597 Handle next_request_handle_; | |
598 | |
599 typedef std::map<Handle, WebDataRequest*> RequestMap; | |
600 RequestMap pending_requests_; | |
601 | |
602 // MessageLoop the WebDataService is created on. | 493 // MessageLoop the WebDataService is created on. |
603 MessageLoop* main_loop_; | 494 MessageLoop* main_loop_; |
604 | 495 |
605 DISALLOW_COPY_AND_ASSIGN(WebDataService); | 496 DISALLOW_COPY_AND_ASSIGN(WebDataService); |
606 }; | 497 }; |
607 | 498 |
608 #endif // CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_H__ | 499 #endif // CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_H__ |
OLD | NEW |