1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/background_sync/background_sync_network_observer.h"
7 #include "content/public/browser/browser_thread.h"
11 BackgroundSyncNetworkObserver::BackgroundSyncNetworkObserver(
12 const base::Closure
& network_changed_callback
)
13 : connection_type_(net::NetworkChangeNotifier::GetConnectionType()),
14 network_changed_callback_(network_changed_callback
) {
15 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
17 net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
20 BackgroundSyncNetworkObserver::~BackgroundSyncNetworkObserver() {
21 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
23 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
26 bool BackgroundSyncNetworkObserver::NetworkSufficient(
27 SyncNetworkState network_state
) {
28 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
30 switch (network_state
) {
31 case NETWORK_STATE_ANY
:
33 case NETWORK_STATE_AVOID_CELLULAR
:
34 // Note that this returns true for CONNECTION_UNKNOWN to avoid never
36 return connection_type_
!= net::NetworkChangeNotifier::CONNECTION_NONE
&&
37 !net::NetworkChangeNotifier::IsConnectionCellular(
39 case NETWORK_STATE_ONLINE
:
40 return connection_type_
!= net::NetworkChangeNotifier::CONNECTION_NONE
;
47 void BackgroundSyncNetworkObserver::NotifyNetworkChanged() {
48 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
50 base::MessageLoop::current()->PostTask(FROM_HERE
, network_changed_callback_
);
53 void BackgroundSyncNetworkObserver::OnNetworkChanged(
54 net::NetworkChangeNotifier::ConnectionType connection_type
) {
55 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
57 if (connection_type
== connection_type_
)
60 connection_type_
= connection_type
;
61 NotifyNetworkChanged();
64 } // namespace content