| 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 #include "chrome/browser/webdata/autofill_profile_syncable_service.h" |    5 #include "chrome/browser/webdata/autofill_profile_syncable_service.h" | 
|    6  |    6  | 
|    7 #include "base/guid.h" |    7 #include "base/guid.h" | 
|    8 #include "base/location.h" |    8 #include "base/location.h" | 
|    9 #include "base/logging.h" |    9 #include "base/logging.h" | 
|   10 #include "base/utf_string_conversions.h" |   10 #include "base/utf_string_conversions.h" | 
|   11 #include "chrome/browser/webdata/autofill_table.h" |   11 #include "chrome/browser/webdata/autofill_table.h" | 
|   12 #include "chrome/browser/webdata/autofill_web_data_service.h" |   12 #include "chrome/browser/webdata/autofill_web_data_service.h" | 
|   13 #include "chrome/browser/webdata/web_database.h" |   13 #include "chrome/browser/webdata/web_database.h" | 
|   14 #include "chrome/common/chrome_notification_types.h" |  | 
|   15 #include "components/autofill/browser/autofill_profile.h" |   14 #include "components/autofill/browser/autofill_profile.h" | 
|   16 #include "components/autofill/browser/form_group.h" |   15 #include "components/autofill/browser/form_group.h" | 
|   17 #include "content/public/browser/browser_thread.h" |   16 #include "content/public/browser/browser_thread.h" | 
|   18 #include "content/public/browser/notification_details.h" |  | 
|   19 #include "content/public/browser/notification_source.h" |  | 
|   20 #include "sync/api/sync_error.h" |   17 #include "sync/api/sync_error.h" | 
|   21 #include "sync/api/sync_error_factory.h" |   18 #include "sync/api/sync_error_factory.h" | 
|   22 #include "sync/protocol/sync.pb.h" |   19 #include "sync/protocol/sync.pb.h" | 
|   23  |   20  | 
|   24 using content::BrowserThread; |   21 using content::BrowserThread; | 
|   25  |   22  | 
|   26 namespace { |   23 namespace { | 
|   27  |   24  | 
|   28 std::string LimitData(const std::string& data) { |   25 std::string LimitData(const std::string& data) { | 
|   29   std::string sanitized_value(data); |   26   std::string sanitized_value(data); | 
|   30   if (sanitized_value.length() > AutofillTable::kMaxDataLength) |   27   if (sanitized_value.length() > AutofillTable::kMaxDataLength) | 
|   31     sanitized_value.resize(AutofillTable::kMaxDataLength); |   28     sanitized_value.resize(AutofillTable::kMaxDataLength); | 
|   32   return sanitized_value; |   29   return sanitized_value; | 
|   33 } |   30 } | 
|   34  |   31  | 
|   35 void* UserDataKey() { |   32 void* UserDataKey() { | 
|   36   // Use the address of a static that COMDAT folding won't ever fold |   33   // Use the address of a static that COMDAT folding won't ever fold | 
|   37   // with something else. |   34   // with something else. | 
|   38   static int user_data_key = 0; |   35   static int user_data_key = 0; | 
|   39   return reinterpret_cast<void*>(&user_data_key); |   36   return reinterpret_cast<void*>(&user_data_key); | 
|   40 } |   37 } | 
|   41  |   38  | 
|   42 }  // namespace |   39 }  // namespace | 
|   43  |   40  | 
|   44 const char kAutofillProfileTag[] = "google_chrome_autofill_profiles"; |   41 const char kAutofillProfileTag[] = "google_chrome_autofill_profiles"; | 
|   45  |   42  | 
|   46 AutofillProfileSyncableService::AutofillProfileSyncableService( |   43 AutofillProfileSyncableService::AutofillProfileSyncableService( | 
|   47     AutofillWebDataService* web_data_service) |   44     AutofillWebDataService* web_data_service) | 
|   48     : web_data_service_(web_data_service) { |   45     : web_data_service_(web_data_service), | 
 |   46       ALLOW_THIS_IN_INITIALIZER_LIST(scoped_observer_(this)) { | 
|   49   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |   47   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 
|   50   DCHECK(web_data_service_); |   48   DCHECK(web_data_service_); | 
|   51   notification_registrar_.Add( |   49  | 
|   52       this, |   50   scoped_observer_.Add(web_data_service_); | 
|   53       chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, |  | 
|   54       content::Source<AutofillWebDataService>(web_data_service_)); |  | 
|   55 } |   51 } | 
|   56  |   52  | 
|   57 AutofillProfileSyncableService::~AutofillProfileSyncableService() { |   53 AutofillProfileSyncableService::~AutofillProfileSyncableService() { | 
|   58   DCHECK(CalledOnValidThread()); |   54   DCHECK(CalledOnValidThread()); | 
|   59 } |   55 } | 
|   60  |   56  | 
|   61 // static |   57 // static | 
|   62 void AutofillProfileSyncableService::CreateForWebDataService( |   58 void AutofillProfileSyncableService::CreateForWebDataService( | 
|   63     AutofillWebDataService* web_data_service) { |   59     AutofillWebDataService* web_data_service) { | 
|   64   web_data_service->GetDBUserData()->SetUserData( |   60   web_data_service->GetDBUserData()->SetUserData( | 
|   65       UserDataKey(), new AutofillProfileSyncableService(web_data_service)); |   61       UserDataKey(), new AutofillProfileSyncableService(web_data_service)); | 
|   66 } |   62 } | 
|   67  |   63  | 
|   68 // static |   64 // static | 
|   69 AutofillProfileSyncableService* |   65 AutofillProfileSyncableService* | 
|   70 AutofillProfileSyncableService::FromWebDataService( |   66 AutofillProfileSyncableService::FromWebDataService( | 
|   71     AutofillWebDataService* web_data_service) { |   67     AutofillWebDataService* web_data_service) { | 
|   72   return static_cast<AutofillProfileSyncableService*>( |   68   return static_cast<AutofillProfileSyncableService*>( | 
|   73       web_data_service->GetDBUserData()->GetUserData(UserDataKey())); |   69       web_data_service->GetDBUserData()->GetUserData(UserDataKey())); | 
|   74 } |   70 } | 
|   75  |   71  | 
|   76 AutofillProfileSyncableService::AutofillProfileSyncableService() |   72 AutofillProfileSyncableService::AutofillProfileSyncableService() | 
|   77     : web_data_service_(NULL) { |   73     : web_data_service_(NULL), | 
 |   74       ALLOW_THIS_IN_INITIALIZER_LIST(scoped_observer_(this)) { | 
|   78   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |   75   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 
|   79 } |   76 } | 
|   80  |   77  | 
|   81 syncer::SyncMergeResult |   78 syncer::SyncMergeResult | 
|   82 AutofillProfileSyncableService::MergeDataAndStartSyncing( |   79 AutofillProfileSyncableService::MergeDataAndStartSyncing( | 
|   83     syncer::ModelType type, |   80     syncer::ModelType type, | 
|   84     const syncer::SyncDataList& initial_sync_data, |   81     const syncer::SyncDataList& initial_sync_data, | 
|   85     scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |   82     scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | 
|   86     scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) { |   83     scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) { | 
|   87   DCHECK(CalledOnValidThread()); |   84   DCHECK(CalledOnValidThread()); | 
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  252     return sync_error_factory_->CreateAndUploadError( |  249     return sync_error_factory_->CreateAndUploadError( | 
|  253         FROM_HERE, |  250         FROM_HERE, | 
|  254         "Failed to update webdata."); |  251         "Failed to update webdata."); | 
|  255   } |  252   } | 
|  256  |  253  | 
|  257   AutofillWebDataService::NotifyOfMultipleAutofillChanges(web_data_service_); |  254   AutofillWebDataService::NotifyOfMultipleAutofillChanges(web_data_service_); | 
|  258  |  255  | 
|  259   return syncer::SyncError(); |  256   return syncer::SyncError(); | 
|  260 } |  257 } | 
|  261  |  258  | 
|  262 void AutofillProfileSyncableService::Observe(int type, |  259 void AutofillProfileSyncableService::AutofillProfileChanged( | 
|  263     const content::NotificationSource& source, |  260     const AutofillProfileChange& change) { | 
|  264     const content::NotificationDetails& details) { |  | 
|  265   DCHECK_EQ(type, chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED); |  | 
|  266   DCHECK_EQ(web_data_service_, |  | 
|  267             content::Source<AutofillWebDataService>(source).ptr()); |  | 
|  268   // Check if sync is on. If we receive notification prior to the sync being set |  261   // Check if sync is on. If we receive notification prior to the sync being set | 
|  269   // up we are going to process all when MergeData..() is called. If we receive |  262   // up we are going to process all when MergeData..() is called. If we receive | 
|  270   // notification after the sync exited, it will be sinced next time Chrome |  263   // notification after the sync exited, it will be sinced next time Chrome | 
|  271   // starts. |  264   // starts. | 
|  272   if (!sync_processor_.get()) |  265   if (sync_processor_.get()) | 
|  273     return; |  266     ActOnChange(change); | 
|  274  |  | 
|  275   AutofillProfileChange* change = |  | 
|  276       content::Details<AutofillProfileChange>(details).ptr(); |  | 
|  277   ActOnChange(*change); |  | 
|  278 } |  267 } | 
|  279  |  268  | 
|  280 bool AutofillProfileSyncableService::LoadAutofillData( |  269 bool AutofillProfileSyncableService::LoadAutofillData( | 
|  281     std::vector<AutofillProfile*>* profiles) { |  270     std::vector<AutofillProfile*>* profiles) { | 
|  282   return GetAutofillTable()->GetAutofillProfiles(profiles); |  271   return GetAutofillTable()->GetAutofillProfiles(profiles); | 
|  283 } |  272 } | 
|  284  |  273  | 
|  285 bool AutofillProfileSyncableService::SaveChangesToWebData( |  274 bool AutofillProfileSyncableService::SaveChangesToWebData( | 
|  286     const DataBundle& bundle) { |  275     const DataBundle& bundle) { | 
|  287   DCHECK(CalledOnValidThread()); |  276   DCHECK(CalledOnValidThread()); | 
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  567   return (merge_into->Compare(merge_from) != 0); |  556   return (merge_into->Compare(merge_from) != 0); | 
|  568 } |  557 } | 
|  569  |  558  | 
|  570 AutofillTable* AutofillProfileSyncableService::GetAutofillTable() const { |  559 AutofillTable* AutofillProfileSyncableService::GetAutofillTable() const { | 
|  571   return AutofillTable::FromWebDatabase(web_data_service_->GetDatabase()); |  560   return AutofillTable::FromWebDatabase(web_data_service_->GetDatabase()); | 
|  572 } |  561 } | 
|  573  |  562  | 
|  574 AutofillProfileSyncableService::DataBundle::DataBundle() {} |  563 AutofillProfileSyncableService::DataBundle::DataBundle() {} | 
|  575  |  564  | 
|  576 AutofillProfileSyncableService::DataBundle::~DataBundle() {} |  565 AutofillProfileSyncableService::DataBundle::~DataBundle() {} | 
| OLD | NEW |