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 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 friend class WebDataRequest; | 321 friend class WebDataRequest; |
421 | 322 |
422 virtual ~WebDataService(); | 323 virtual ~WebDataService(); |
423 | 324 |
424 // This is invoked by the unit test; path is the path of the Web Data file. | 325 // This is invoked by the unit test; path is the path of the Web Data file. |
425 bool InitWithPath(const FilePath& path); | 326 bool InitWithPath(const FilePath& path); |
426 | 327 |
427 // Invoked by request implementations when a request has been processed. | 328 // Invoked by request implementations when a request has been processed. |
428 void RequestCompleted(Handle h); | 329 void RequestCompleted(Handle h); |
429 | 330 |
430 // Register the request as a pending request. | |
431 void RegisterRequest(WebDataRequest* request); | |
432 | |
433 ////////////////////////////////////////////////////////////////////////////// | 331 ////////////////////////////////////////////////////////////////////////////// |
434 // | 332 // |
435 // The following methods are only invoked in the web data service thread. | 333 // The following methods are only invoked in the web data service thread. |
436 // | 334 // |
437 ////////////////////////////////////////////////////////////////////////////// | 335 ////////////////////////////////////////////////////////////////////////////// |
438 private: | 336 private: |
439 friend struct content::BrowserThread::DeleteOnThread< | 337 friend struct content::BrowserThread::DeleteOnThread< |
440 content::BrowserThread::UI>; | 338 content::BrowserThread::UI>; |
441 friend class base::DeleteHelper<WebDataService>; | 339 friend class base::DeleteHelper<WebDataService>; |
442 | 340 |
(...skipping 21 matching lines...) Expand all Loading... |
464 // Commit the current transaction and creates a new one. | 362 // Commit the current transaction and creates a new one. |
465 void Commit(); | 363 void Commit(); |
466 | 364 |
467 // Schedule a task on our worker thread. | 365 // Schedule a task on our worker thread. |
468 void ScheduleTask(const tracked_objects::Location& from_here, | 366 void ScheduleTask(const tracked_objects::Location& from_here, |
469 const base::Closure& task); | 367 const base::Closure& task); |
470 | 368 |
471 // Schedule a commit if one is not already pending. | 369 // Schedule a commit if one is not already pending. |
472 void ScheduleCommit(); | 370 void ScheduleCommit(); |
473 | 371 |
474 // Return the next request handle. | |
475 int GetNextRequestHandle(); | |
476 | |
477 ////////////////////////////////////////////////////////////////////////////// | 372 ////////////////////////////////////////////////////////////////////////////// |
478 // | 373 // |
479 // Keywords. | 374 // Keywords. |
480 // | 375 // |
481 ////////////////////////////////////////////////////////////////////////////// | 376 ////////////////////////////////////////////////////////////////////////////// |
482 void AddKeywordImpl(GenericRequest<TemplateURLData>* request); | 377 void AddKeywordImpl(GenericRequest<TemplateURLData>* request); |
483 void RemoveKeywordImpl(GenericRequest<TemplateURLID>* request); | 378 void RemoveKeywordImpl(GenericRequest<TemplateURLID>* request); |
484 void UpdateKeywordImpl(GenericRequest<TemplateURLData>* request); | 379 void UpdateKeywordImpl(GenericRequest<TemplateURLData>* request); |
485 void GetKeywordsImpl(WebDataRequest* request); | 380 void GetKeywordsImpl(WebDataRequest* request); |
486 void SetDefaultSearchProviderImpl(GenericRequest<TemplateURLID>* r); | 381 void SetDefaultSearchProviderImpl(GenericRequest<TemplateURLID>* r); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 void RemoveAutofillProfileImpl(GenericRequest<std::string>* request); | 453 void RemoveAutofillProfileImpl(GenericRequest<std::string>* request); |
559 void GetAutofillProfilesImpl(WebDataRequest* request); | 454 void GetAutofillProfilesImpl(WebDataRequest* request); |
560 void EmptyMigrationTrashImpl(GenericRequest<bool>* request); | 455 void EmptyMigrationTrashImpl(GenericRequest<bool>* request); |
561 void AddCreditCardImpl(GenericRequest<CreditCard>* request); | 456 void AddCreditCardImpl(GenericRequest<CreditCard>* request); |
562 void UpdateCreditCardImpl(GenericRequest<CreditCard>* request); | 457 void UpdateCreditCardImpl(GenericRequest<CreditCard>* request); |
563 void RemoveCreditCardImpl(GenericRequest<std::string>* request); | 458 void RemoveCreditCardImpl(GenericRequest<std::string>* request); |
564 void GetCreditCardsImpl(WebDataRequest* request); | 459 void GetCreditCardsImpl(WebDataRequest* request); |
565 void RemoveAutofillProfilesAndCreditCardsModifiedBetweenImpl( | 460 void RemoveAutofillProfilesAndCreditCardsModifiedBetweenImpl( |
566 GenericRequest2<base::Time, base::Time>* request); | 461 GenericRequest2<base::Time, base::Time>* request); |
567 | 462 |
| 463 // Callbacks to ensure that sensitive info is destroyed if request is |
| 464 // cancelled. |
| 465 void DestroyAutofillProfileResult(const WDTypedResult* result); |
| 466 void DestroyAutofillCreditCardResult(const WDTypedResult* result); |
| 467 |
568 // True once initialization has started. | 468 // True once initialization has started. |
569 bool is_running_; | 469 bool is_running_; |
570 | 470 |
571 // The path with which to initialize the database. | 471 // The path with which to initialize the database. |
572 FilePath path_; | 472 FilePath path_; |
573 | 473 |
574 // Our database. We own the |db_|, but don't use a |scoped_ptr| because the | 474 // 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. | 475 // |db_| lifetime must be managed on the database thread. |
576 WebDatabase* db_; | 476 WebDatabase* db_; |
577 | 477 |
| 478 // Keeps track of all pending requests made to the db. |
| 479 WebDataRequestManager* request_manager_; |
| 480 |
578 // Syncable services for the database data. We own the services, but don't | 481 // 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 | 482 // use |scoped_ptr|s because the lifetimes must be managed on the database |
580 // thread. | 483 // thread. |
581 // Currently only Autocomplete and Autofill profiles use the new Sync API, but | 484 // Currently only Autocomplete and Autofill profiles use the new Sync API, but |
582 // all the database data should migrate to this API over time. | 485 // all the database data should migrate to this API over time. |
583 AutocompleteSyncableService* autocomplete_syncable_service_; | 486 AutocompleteSyncableService* autocomplete_syncable_service_; |
584 AutofillProfileSyncableService* autofill_profile_syncable_service_; | 487 AutofillProfileSyncableService* autofill_profile_syncable_service_; |
585 | 488 |
586 // Whether the database failed to initialize. We use this to avoid | 489 // Whether the database failed to initialize. We use this to avoid |
587 // continually trying to reinit. | 490 // continually trying to reinit. |
588 bool failed_init_; | 491 bool failed_init_; |
589 | 492 |
590 // Whether we should commit the database. | 493 // Whether we should commit the database. |
591 bool should_commit_; | 494 bool should_commit_; |
592 | 495 |
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. | 496 // MessageLoop the WebDataService is created on. |
603 MessageLoop* main_loop_; | 497 MessageLoop* main_loop_; |
604 | 498 |
605 DISALLOW_COPY_AND_ASSIGN(WebDataService); | 499 DISALLOW_COPY_AND_ASSIGN(WebDataService); |
606 }; | 500 }; |
607 | 501 |
608 #endif // CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_H__ | 502 #endif // CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_H__ |
OLD | NEW |