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 "base/location.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "content/public/browser/browser_thread.h"
14 BackgroundSyncNetworkObserver::BackgroundSyncNetworkObserver(
15 const base::Closure
& network_changed_callback
)
16 : connection_type_(net::NetworkChangeNotifier::GetConnectionType()),
17 network_changed_callback_(network_changed_callback
) {
18 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
20 net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
23 BackgroundSyncNetworkObserver::~BackgroundSyncNetworkObserver() {
24 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
26 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
29 bool BackgroundSyncNetworkObserver::NetworkSufficient(
30 SyncNetworkState network_state
) {
31 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
33 switch (network_state
) {
34 case NETWORK_STATE_ANY
:
36 case NETWORK_STATE_AVOID_CELLULAR
:
37 // Note that this returns true for CONNECTION_UNKNOWN to avoid never
39 return connection_type_
!= net::NetworkChangeNotifier::CONNECTION_NONE
&&
40 !net::NetworkChangeNotifier::IsConnectionCellular(
42 case NETWORK_STATE_ONLINE
:
43 return connection_type_
!= net::NetworkChangeNotifier::CONNECTION_NONE
;
50 void BackgroundSyncNetworkObserver::NotifyNetworkChanged() {
51 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
53 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
,
54 network_changed_callback_
);
57 void BackgroundSyncNetworkObserver::OnNetworkChanged(
58 net::NetworkChangeNotifier::ConnectionType connection_type
) {
59 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
61 if (connection_type
== connection_type_
)
64 connection_type_
= connection_type
;
65 NotifyNetworkChanged();
68 } // namespace content