Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(704)

Side by Side Diff: chrome/browser/sync/internal_api/sync_manager.cc

Issue 8787006: Delay autofill commits to reduce client to server traffic. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: for commit. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/sync/internal_api/sync_manager.h" 5 #include "chrome/browser/sync/internal_api/sync_manager.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/json/json_writer.h" 14 #include "base/json/json_writer.h"
15 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
16 #include "base/observer_list.h" 16 #include "base/observer_list.h"
17 #include "base/string_number_conversions.h" 17 #include "base/string_number_conversions.h"
18 #include "base/values.h" 18 #include "base/values.h"
19 #include "chrome/browser/sync/engine/all_status.h" 19 #include "chrome/browser/sync/engine/all_status.h"
20 #include "chrome/browser/sync/engine/net/server_connection_manager.h" 20 #include "chrome/browser/sync/engine/net/server_connection_manager.h"
21 #include "chrome/browser/sync/engine/nigori_util.h" 21 #include "chrome/browser/sync/engine/nigori_util.h"
22 #include "chrome/browser/sync/engine/polling_constants.h"
22 #include "chrome/browser/sync/engine/syncapi_internal.h" 23 #include "chrome/browser/sync/engine/syncapi_internal.h"
23 #include "chrome/browser/sync/engine/syncer_types.h" 24 #include "chrome/browser/sync/engine/syncer_types.h"
24 #include "chrome/browser/sync/engine/sync_scheduler.h" 25 #include "chrome/browser/sync/engine/sync_scheduler.h"
25 #include "chrome/browser/sync/internal_api/base_node.h" 26 #include "chrome/browser/sync/internal_api/base_node.h"
26 #include "chrome/browser/sync/internal_api/change_reorder_buffer.h" 27 #include "chrome/browser/sync/internal_api/change_reorder_buffer.h"
27 #include "chrome/browser/sync/internal_api/configure_reason.h" 28 #include "chrome/browser/sync/internal_api/configure_reason.h"
28 #include "chrome/browser/sync/internal_api/debug_info_event_listener.h" 29 #include "chrome/browser/sync/internal_api/debug_info_event_listener.h"
29 #include "chrome/browser/sync/internal_api/read_node.h" 30 #include "chrome/browser/sync/internal_api/read_node.h"
30 #include "chrome/browser/sync/internal_api/read_transaction.h" 31 #include "chrome/browser/sync/internal_api/read_transaction.h"
31 #include "chrome/browser/sync/internal_api/syncapi_server_connection_manager.h" 32 #include "chrome/browser/sync/internal_api/syncapi_server_connection_manager.h"
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 NOTREACHED(); 114 NOTREACHED();
114 } 115 }
115 116
116 return GetUpdatesCallerInfo::UNKNOWN; 117 return GetUpdatesCallerInfo::UNKNOWN;
117 } 118 }
118 119
119 } // namespace 120 } // namespace
120 121
121 namespace sync_api { 122 namespace sync_api {
122 123
124 const int SyncManager::kDefaultNudgeDelayMilliseconds = 200;
125 const int SyncManager::kPreferencesNudgeDelayMilliseconds = 2000;
126
123 ////////////////////////////////////////////////////////////////////////// 127 //////////////////////////////////////////////////////////////////////////
124 // SyncManager's implementation: SyncManager::SyncInternal 128 // SyncManager's implementation: SyncManager::SyncInternal
125 class SyncManager::SyncInternal 129 class SyncManager::SyncInternal
126 : public net::NetworkChangeNotifier::IPAddressObserver, 130 : public net::NetworkChangeNotifier::IPAddressObserver,
127 public browser_sync::Cryptographer::Observer, 131 public browser_sync::Cryptographer::Observer,
128 public sync_notifier::SyncNotifierObserver, 132 public sync_notifier::SyncNotifierObserver,
129 public JsBackend, 133 public JsBackend,
130 public SyncEngineEventListener, 134 public SyncEngineEventListener,
131 public ServerConnectionEventListener, 135 public ServerConnectionEventListener,
132 public syncable::DirectoryChangeDelegate { 136 public syncable::DirectoryChangeDelegate {
133 static const int kDefaultNudgeDelayMilliseconds;
134 static const int kPreferencesNudgeDelayMilliseconds;
135 public: 137 public:
136 explicit SyncInternal(const std::string& name) 138 explicit SyncInternal(const std::string& name)
137 : name_(name), 139 : name_(name),
138 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), 140 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
139 registrar_(NULL), 141 registrar_(NULL),
140 change_delegate_(NULL), 142 change_delegate_(NULL),
141 initialized_(false), 143 initialized_(false),
142 setup_for_test_mode_(false), 144 setup_for_test_mode_(false),
143 observing_ip_address_changes_(false), 145 observing_ip_address_changes_(false),
144 created_on_loop_(MessageLoop::current()) { 146 created_on_loop_(MessageLoop::current()) {
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 virtual void StoreState(const std::string& cookie) OVERRIDE; 286 virtual void StoreState(const std::string& cookie) OVERRIDE;
285 287
286 void AddObserver(SyncManager::Observer* observer); 288 void AddObserver(SyncManager::Observer* observer);
287 void RemoveObserver(SyncManager::Observer* observer); 289 void RemoveObserver(SyncManager::Observer* observer);
288 290
289 // Accessors for the private members. 291 // Accessors for the private members.
290 DirectoryManager* dir_manager() { return share_.dir_manager.get(); } 292 DirectoryManager* dir_manager() { return share_.dir_manager.get(); }
291 SyncAPIServerConnectionManager* connection_manager() { 293 SyncAPIServerConnectionManager* connection_manager() {
292 return connection_manager_.get(); 294 return connection_manager_.get();
293 } 295 }
294 SyncScheduler* scheduler() { return scheduler_.get(); } 296 SyncScheduler* scheduler() const { return scheduler_.get(); }
295 UserShare* GetUserShare() { 297 UserShare* GetUserShare() {
296 DCHECK(initialized_); 298 DCHECK(initialized_);
297 return &share_; 299 return &share_;
298 } 300 }
299 301
300 // Return the currently active (validated) username for use with syncable 302 // Return the currently active (validated) username for use with syncable
301 // types. 303 // types.
302 const std::string& username_for_share() const { 304 const std::string& username_for_share() const {
303 return share_.name; 305 return share_.name;
304 } 306 }
305 307
306 Status GetStatus(); 308 Status GetStatus();
307 309
308 void RequestNudge(const tracked_objects::Location& nudge_location); 310 void RequestNudge(const tracked_objects::Location& nudge_location);
309 311
310 void RequestNudgeForDataType( 312 void RequestNudgeForDataType(
311 const tracked_objects::Location& nudge_location, 313 const tracked_objects::Location& nudge_location,
312 const ModelType& type); 314 const ModelType& type);
313 315
316 TimeDelta GetNudgeDelayTimeDelta(const ModelType& model_type);
317
314 // See SyncManager::Shutdown* for information. 318 // See SyncManager::Shutdown* for information.
315 void StopSyncingForShutdown(const base::Closure& callback); 319 void StopSyncingForShutdown(const base::Closure& callback);
316 void ShutdownOnSyncThread(); 320 void ShutdownOnSyncThread();
317 321
318 // If this is a deletion for a password, sets the legacy 322 // If this is a deletion for a password, sets the legacy
319 // ExtraPasswordChangeRecordData field of |buffer|. Otherwise sets 323 // ExtraPasswordChangeRecordData field of |buffer|. Otherwise sets
320 // |buffer|'s specifics field to contain the unencrypted data. 324 // |buffer|'s specifics field to contain the unencrypted data.
321 void SetExtraChangeRecordData(int64 id, 325 void SetExtraChangeRecordData(int64 id,
322 syncable::ModelType type, 326 syncable::ModelType type,
323 ChangeReorderBuffer* buffer, 327 ChangeReorderBuffer* buffer,
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 JsMessageHandlerMap js_message_handlers_; 560 JsMessageHandlerMap js_message_handlers_;
557 WeakHandle<JsEventHandler> js_event_handler_; 561 WeakHandle<JsEventHandler> js_event_handler_;
558 JsSyncManagerObserver js_sync_manager_observer_; 562 JsSyncManagerObserver js_sync_manager_observer_;
559 JsMutationEventObserver js_mutation_event_observer_; 563 JsMutationEventObserver js_mutation_event_observer_;
560 564
561 // This is for keeping track of client events to send to the server. 565 // This is for keeping track of client events to send to the server.
562 DebugInfoEventListener debug_info_event_listener_; 566 DebugInfoEventListener debug_info_event_listener_;
563 567
564 MessageLoop* const created_on_loop_; 568 MessageLoop* const created_on_loop_;
565 }; 569 };
566 const int SyncManager::SyncInternal::kDefaultNudgeDelayMilliseconds = 200; 570
567 const int SyncManager::SyncInternal::kPreferencesNudgeDelayMilliseconds = 2000; 571 // A class to calculate nudge delays for types.
572 class NudgeStrategy {
573 public:
574 static TimeDelta GetNudgeDelayTimeDelta(const ModelType& model_type,
575 SyncManager::SyncInternal* core) {
576 NudgeDelayStrategy delay_type = GetNudgeDelayStrategy(model_type);
577 return GetNudgeDelayTimeDeltaFromType(delay_type,
578 model_type,
579 core);
580 }
581
582 private:
583 // Possible types of nudge delay for datatypes.
584 // Note: These are just hints. If a sync happens then all dirty entries
585 // would be committed as part of the sync.
586 enum NudgeDelayStrategy {
587 // Sync right away.
588 IMMEDIATE,
589
590 // Sync this change while syncing another change.
591 ACCOMPANY_ONLY,
592
593 // The datatype does not use one of the predefined wait times but defines
594 // its own wait time logic for nudge.
595 CUSTOM,
596 };
597
598 static NudgeDelayStrategy GetNudgeDelayStrategy(const ModelType& type) {
599 switch (type) {
600 case syncable::AUTOFILL:
601 case syncable::AUTOFILL_PROFILE:
602 return ACCOMPANY_ONLY;
603 case syncable::PREFERENCES:
604 case syncable::SESSIONS:
605 return CUSTOM;
606 default:
607 return IMMEDIATE;
608 }
609 }
610
611 static TimeDelta GetNudgeDelayTimeDeltaFromType(
612 const NudgeDelayStrategy& delay_type, const ModelType& model_type,
613 const SyncManager::SyncInternal* core) {
614 CHECK(core);
615 TimeDelta delay = TimeDelta::FromMilliseconds(
616 SyncManager::kDefaultNudgeDelayMilliseconds);
617 switch (delay_type) {
618 case IMMEDIATE:
619 delay = TimeDelta::FromMilliseconds(
620 SyncManager::kDefaultNudgeDelayMilliseconds);
621 break;
622 case ACCOMPANY_ONLY:
623 delay = TimeDelta::FromSeconds(
624 browser_sync::kDefaultShortPollIntervalSeconds);
625 break;
626 case CUSTOM:
627 switch (model_type) {
628 case syncable::PREFERENCES:
629 delay = TimeDelta::FromMilliseconds(
630 SyncManager::kPreferencesNudgeDelayMilliseconds);
631 break;
632 case syncable::SESSIONS:
633 delay = core->scheduler()->sessions_commit_delay();
634 break;
635 default:
636 NOTREACHED();
637 }
638 break;
639 default:
640 NOTREACHED();
641 }
642 return delay;
643 }
644 };
568 645
569 SyncManager::ChangeDelegate::~ChangeDelegate() {} 646 SyncManager::ChangeDelegate::~ChangeDelegate() {}
570 647
571 SyncManager::ChangeObserver::~ChangeObserver() {} 648 SyncManager::ChangeObserver::~ChangeObserver() {}
572 649
573 SyncManager::Observer::~Observer() {} 650 SyncManager::Observer::~Observer() {}
574 651
575 SyncManager::SyncManager(const std::string& name) 652 SyncManager::SyncManager(const std::string& name)
576 : data_(new SyncInternal(name)) {} 653 : data_(new SyncInternal(name)) {}
577 654
(...skipping 1036 matching lines...) Expand 10 before | Expand all | Expand 10 after
1614 } 1691 }
1615 1692
1616 void SyncManager::SyncInternal::RequestNudge( 1693 void SyncManager::SyncInternal::RequestNudge(
1617 const tracked_objects::Location& location) { 1694 const tracked_objects::Location& location) {
1618 if (scheduler()) 1695 if (scheduler())
1619 scheduler()->ScheduleNudge( 1696 scheduler()->ScheduleNudge(
1620 TimeDelta::FromMilliseconds(0), browser_sync::NUDGE_SOURCE_LOCAL, 1697 TimeDelta::FromMilliseconds(0), browser_sync::NUDGE_SOURCE_LOCAL,
1621 ModelTypeSet(), location); 1698 ModelTypeSet(), location);
1622 } 1699 }
1623 1700
1701 TimeDelta SyncManager::SyncInternal::GetNudgeDelayTimeDelta(
1702 const ModelType& model_type) {
1703 return NudgeStrategy::GetNudgeDelayTimeDelta(model_type, this);
1704 }
1705
1624 void SyncManager::SyncInternal::RequestNudgeForDataType( 1706 void SyncManager::SyncInternal::RequestNudgeForDataType(
1625 const tracked_objects::Location& nudge_location, 1707 const tracked_objects::Location& nudge_location,
1626 const ModelType& type) { 1708 const ModelType& type) {
1627 if (!scheduler()) { 1709 if (!scheduler()) {
1628 NOTREACHED(); 1710 NOTREACHED();
1629 return; 1711 return;
1630 } 1712 }
1631 base::TimeDelta nudge_delay; 1713
1632 switch (type) { 1714 base::TimeDelta nudge_delay = NudgeStrategy::GetNudgeDelayTimeDelta(type,
1633 case syncable::PREFERENCES: 1715 this);
1634 nudge_delay = 1716 syncable::ModelTypeSet types(type);
1635 TimeDelta::FromMilliseconds(kPreferencesNudgeDelayMilliseconds);
1636 break;
1637 case syncable::SESSIONS:
1638 nudge_delay = scheduler()->sessions_commit_delay();
1639 break;
1640 default:
1641 nudge_delay =
1642 TimeDelta::FromMilliseconds(kDefaultNudgeDelayMilliseconds);
1643 break;
1644 }
1645 scheduler()->ScheduleNudge(nudge_delay, 1717 scheduler()->ScheduleNudge(nudge_delay,
1646 browser_sync::NUDGE_SOURCE_LOCAL, 1718 browser_sync::NUDGE_SOURCE_LOCAL,
1647 ModelTypeSet(type), 1719 ModelTypeSet(type),
1648 nudge_location); 1720 nudge_location);
1649 } 1721 }
1650 1722
1651 void SyncManager::SyncInternal::OnSyncEngineEvent( 1723 void SyncManager::SyncInternal::OnSyncEngineEvent(
1652 const SyncEngineEvent& event) { 1724 const SyncEngineEvent& event) {
1653 DCHECK(thread_checker_.CalledOnValidThread()); 1725 DCHECK(thread_checker_.CalledOnValidThread());
1654 // Only send an event if this is due to a cycle ending and this cycle 1726 // Only send an event if this is due to a cycle ending and this cycle
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after
2065 done_callback)); 2137 done_callback));
2066 } 2138 }
2067 2139
2068 void SyncManager::DoneRefreshNigori(const base::Closure& done_callback, 2140 void SyncManager::DoneRefreshNigori(const base::Closure& done_callback,
2069 bool is_ready) { 2141 bool is_ready) {
2070 if (is_ready) 2142 if (is_ready)
2071 data_->RefreshEncryption(); 2143 data_->RefreshEncryption();
2072 done_callback.Run(); 2144 done_callback.Run();
2073 } 2145 }
2074 2146
2147 TimeDelta SyncManager::GetNudgeDelayTimeDelta(
2148 const ModelType& model_type) {
2149 return data_->GetNudgeDelayTimeDelta(model_type);
2150 }
2151
2075 syncable::ModelTypeSet SyncManager::GetEncryptedDataTypesForTest() const { 2152 syncable::ModelTypeSet SyncManager::GetEncryptedDataTypesForTest() const {
2076 ReadTransaction trans(FROM_HERE, GetUserShare()); 2153 ReadTransaction trans(FROM_HERE, GetUserShare());
2077 return GetEncryptedTypes(&trans); 2154 return GetEncryptedTypes(&trans);
2078 } 2155 }
2079 2156
2080 bool SyncManager::ReceivedExperimentalTypes(syncable::ModelTypeSet* to_add) 2157 bool SyncManager::ReceivedExperimentalTypes(syncable::ModelTypeSet* to_add)
2081 const { 2158 const {
2082 ReadTransaction trans(FROM_HERE, GetUserShare()); 2159 ReadTransaction trans(FROM_HERE, GetUserShare());
2083 ReadNode node(&trans); 2160 ReadNode node(&trans);
2084 if (!node.InitByTagLookup(kNigoriTag)) { 2161 if (!node.InitByTagLookup(kNigoriTag)) {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
2167 lookup->GetDownloadProgress(i.Get(), &marker); 2244 lookup->GetDownloadProgress(i.Get(), &marker);
2168 2245
2169 if (marker.token().empty()) 2246 if (marker.token().empty())
2170 result.Put(i.Get()); 2247 result.Put(i.Get());
2171 2248
2172 } 2249 }
2173 return result; 2250 return result;
2174 } 2251 }
2175 2252
2176 } // namespace sync_api 2253 } // namespace sync_api
OLDNEW
« no previous file with comments | « chrome/browser/sync/internal_api/sync_manager.h ('k') | chrome/browser/sync/internal_api/syncapi_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698