Roll DEPS for PDFium to 19ae17578f99621100a26dac3e2c7c3dbf7c7cd1
[chromium-blink-merge.git] / content / browser / background_sync / background_sync_network_observer.cc
blobfafb1e68a1f9e4c3e98c92ba8ebf57651c85ee2d
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"
9 namespace content {
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:
32 return true;
33 case NETWORK_STATE_AVOID_CELLULAR:
34 // Note that this returns true for CONNECTION_UNKNOWN to avoid never
35 // firing.
36 return connection_type_ != net::NetworkChangeNotifier::CONNECTION_NONE &&
37 !net::NetworkChangeNotifier::IsConnectionCellular(
38 connection_type_);
39 case NETWORK_STATE_ONLINE:
40 return connection_type_ != net::NetworkChangeNotifier::CONNECTION_NONE;
43 NOTREACHED();
44 return false;
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_)
58 return;
60 connection_type_ = connection_type;
61 NotifyNetworkChanged();
64 } // namespace content