Skip to content

Commit

Permalink
[Sync] Make Login class listen to connection and DNS changes
Browse files Browse the repository at this point in the history
Make sync_listen_notifications listen for network changes.

Add a scoped NSAutoreleasePool to sync_listen_notifications.

Relax DEPS for sync/tools a bit.

BUG=106034
TEST=


Review URL: https://chromiumcodereview.appspot.com/10675012

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@144382 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
akalin@chromium.org committed Jun 27, 2012
1 parent d4f55d7 commit 80ddf5d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
29 changes: 24 additions & 5 deletions jingle/notifier/communicator/login.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,19 @@ Login::Login(Delegate* delegate,
try_ssltcp_first,
auth_mechanism) {
net::NetworkChangeNotifier::AddIPAddressObserver(this);
net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
net::NetworkChangeNotifier::AddDNSObserver(this);
ResetReconnectState();
}

Login::~Login() {
net::NetworkChangeNotifier::RemoveDNSObserver(this);
net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
}

void Login::StartConnection() {
VLOG(1) << "Starting connection...";
DVLOG(1) << "Starting connection...";
single_attempt_.reset(new SingleLoginAttempt(login_settings_, this));
}

Expand Down Expand Up @@ -87,7 +91,22 @@ void Login::OnSettingsExhausted() {
}

void Login::OnIPAddressChanged() {
VLOG(1) << "Detected IP address change";
DVLOG(1) << "Detected IP address change";
OnNetworkEvent();
}

void Login::OnConnectionTypeChanged(
net::NetworkChangeNotifier::ConnectionType type) {
DVLOG(1) << "Detected connection type change";
OnNetworkEvent();
}

void Login::OnDNSChanged(unsigned detail) {
DVLOG(1) << "Detected DNS change";
OnNetworkEvent();
}

void Login::OnNetworkEvent() {
// Reconnect in 1 to 9 seconds (vary the time a little to try to
// avoid spikey behavior on network hiccups).
reconnect_interval_ = base::TimeDelta::FromSeconds(base::RandInt(1, 9));
Expand All @@ -105,8 +124,8 @@ void Login::TryReconnect() {
DCHECK_GT(reconnect_interval_.InSeconds(), 0);
single_attempt_.reset();
reconnect_timer_.Stop();
VLOG(1) << "Reconnecting in "
<< reconnect_interval_.InSeconds() << " seconds";
DVLOG(1) << "Reconnecting in "
<< reconnect_interval_.InSeconds() << " seconds";
reconnect_timer_.Start(
FROM_HERE, reconnect_interval_, this, &Login::DoReconnect);
}
Expand All @@ -118,7 +137,7 @@ void Login::DoReconnect() {
reconnect_interval_ *= 2;
if (reconnect_interval_ > kMaxReconnectInterval)
reconnect_interval_ = kMaxReconnectInterval;
VLOG(1) << "Reconnecting...";
DVLOG(1) << "Reconnecting...";
StartConnection();
}

Expand Down
14 changes: 13 additions & 1 deletion jingle/notifier/communicator/login.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ class LoginSettings;
// Does the login, keeps it alive (with refreshing cookies and
// reattempting login when disconnected), and figures out what actions
// to take on the various errors that may occur.
//
// TODO(akalin): Make this observe proxy config changes also.
class Login : public net::NetworkChangeNotifier::IPAddressObserver,
public net::NetworkChangeNotifier::ConnectionTypeObserver,
public net::NetworkChangeNotifier::DNSObserver,
public SingleLoginAttempt::Delegate {
public:
class Delegate {
Expand Down Expand Up @@ -81,6 +85,13 @@ class Login : public net::NetworkChangeNotifier::IPAddressObserver,
// net::NetworkChangeNotifier::IPAddressObserver implementation.
virtual void OnIPAddressChanged() OVERRIDE;

// net::NetworkChangeNotifier::ConnectionTypeObserver implementation.
virtual void OnConnectionTypeChanged(
net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;

// net::NetworkChangeNotifier::DNSObserver implementation.
virtual void OnDNSChanged(unsigned detail) OVERRIDE;

// SingleLoginAttempt::Delegate implementation.
virtual void OnConnect(
base::WeakPtr<buzz::XmppTaskParentInterface> base_task) OVERRIDE;
Expand All @@ -89,7 +100,8 @@ class Login : public net::NetworkChangeNotifier::IPAddressObserver,
virtual void OnSettingsExhausted() OVERRIDE;

private:
void OnLogoff();
// Called by the various network notifications.
void OnNetworkEvent();

// Stops any existing reconnect timer and sets an initial reconnect
// interval.
Expand Down
4 changes: 1 addition & 3 deletions sync/tools/DEPS
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
include_rules = [
"+jingle/notifier/base",
"+net/base/host_port_pair.h",
"+net/base/host_resolver.h",
"+net/url_request/url_request_test_util.h",
"+net",
"+sync/notifier",
"+sync/internal_api/public/syncable",
]
12 changes: 12 additions & 0 deletions sync/tools/sync_listen_notifications.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "jingle/notifier/base/notifier_options.h"
#include "net/base/host_port_pair.h"
#include "net/base/host_resolver.h"
#include "net/base/network_change_notifier.h"
#include "net/url_request/url_request_test_util.h"
#include "sync/internal_api/public/syncable/model_type.h"
#include "sync/internal_api/public/syncable/model_type_payload_map.h"
Expand All @@ -28,6 +29,10 @@
#include "sync/notifier/sync_notifier_factory.h"
#include "sync/notifier/sync_notifier_observer.h"

#if defined(OS_MACOSX)
#include "base/mac/scoped_nsautorelease_pool.h"
#endif

// This is a simple utility that initializes a sync notifier and
// listens to any received notifications.

Expand Down Expand Up @@ -174,6 +179,9 @@ class MyTestURLRequestContextGetter : public TestURLRequestContextGetter {
} // namespace

int main(int argc, char* argv[]) {
#if defined(OS_MACOSX)
base::mac::ScopedNSAutoreleasePool pool;
#endif
base::AtExitManager exit_manager;
CommandLine::Init(argc, argv);
logging::InitLogging(
Expand Down Expand Up @@ -210,6 +218,10 @@ int main(int argc, char* argv[]) {
return -1;
}

// Set up objects that monitor the network.
scoped_ptr<net::NetworkChangeNotifier> network_change_notifier(
net::NetworkChangeNotifier::Create());

const notifier::NotifierOptions& notifier_options =
ParseNotifierOptions(
command_line,
Expand Down

0 comments on commit 80ddf5d

Please sign in to comment.