| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/background_sync/background_sync_network_observer.h" | 5 #include "content/browser/background_sync/background_sync_network_observer.h" |
| 6 | 6 |
| 7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
| 9 #include "base/thread_task_runner_handle.h" | 9 #include "base/thread_task_runner_handle.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 | 13 |
| 14 // static | |
| 15 bool BackgroundSyncNetworkObserver::ignore_network_change_notifier_ = false; | |
| 16 | |
| 17 // static | |
| 18 void BackgroundSyncNetworkObserver::SetIgnoreNetworkChangeNotifierForTests( | |
| 19 bool ignore) { | |
| 20 ignore_network_change_notifier_ = ignore; | |
| 21 } | |
| 22 | |
| 23 BackgroundSyncNetworkObserver::BackgroundSyncNetworkObserver( | 14 BackgroundSyncNetworkObserver::BackgroundSyncNetworkObserver( |
| 24 const base::Closure& network_changed_callback) | 15 const base::Closure& network_changed_callback) |
| 25 : connection_type_(net::NetworkChangeNotifier::GetConnectionType()), | 16 : connection_type_(net::NetworkChangeNotifier::GetConnectionType()), |
| 26 network_changed_callback_(network_changed_callback) { | 17 network_changed_callback_(network_changed_callback) { |
| 27 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 18 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 28 | 19 |
| 29 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); | 20 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); |
| 30 } | 21 } |
| 31 | 22 |
| 32 BackgroundSyncNetworkObserver::~BackgroundSyncNetworkObserver() { | 23 BackgroundSyncNetworkObserver::~BackgroundSyncNetworkObserver() { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 49 !net::NetworkChangeNotifier::IsConnectionCellular( | 40 !net::NetworkChangeNotifier::IsConnectionCellular( |
| 50 connection_type_); | 41 connection_type_); |
| 51 case NETWORK_STATE_ONLINE: | 42 case NETWORK_STATE_ONLINE: |
| 52 return connection_type_ != net::NetworkChangeNotifier::CONNECTION_NONE; | 43 return connection_type_ != net::NetworkChangeNotifier::CONNECTION_NONE; |
| 53 } | 44 } |
| 54 | 45 |
| 55 NOTREACHED(); | 46 NOTREACHED(); |
| 56 return false; | 47 return false; |
| 57 } | 48 } |
| 58 | 49 |
| 50 void BackgroundSyncNetworkObserver::NotifyNetworkChanged() { |
| 51 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 52 |
| 53 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 54 network_changed_callback_); |
| 55 } |
| 56 |
| 59 void BackgroundSyncNetworkObserver::OnNetworkChanged( | 57 void BackgroundSyncNetworkObserver::OnNetworkChanged( |
| 60 net::NetworkChangeNotifier::ConnectionType connection_type) { | 58 net::NetworkChangeNotifier::ConnectionType connection_type) { |
| 61 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 59 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 62 | 60 |
| 63 if (ignore_network_change_notifier_) | |
| 64 return; | |
| 65 NotifyManagerIfNetworkChanged(connection_type); | |
| 66 } | |
| 67 | |
| 68 void BackgroundSyncNetworkObserver::NotifyManagerIfNetworkChanged( | |
| 69 net::NetworkChangeNotifier::ConnectionType connection_type) { | |
| 70 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 71 if (connection_type == connection_type_) | 61 if (connection_type == connection_type_) |
| 72 return; | 62 return; |
| 73 | 63 |
| 74 connection_type_ = connection_type; | 64 connection_type_ = connection_type; |
| 75 NotifyNetworkChanged(); | 65 NotifyNetworkChanged(); |
| 76 } | 66 } |
| 77 | 67 |
| 78 void BackgroundSyncNetworkObserver::NotifyNetworkChanged() { | |
| 79 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 80 | |
| 81 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 82 network_changed_callback_); | |
| 83 } | |
| 84 | |
| 85 } // namespace content | 68 } // namespace content |
| OLD | NEW |