1 // Copyright 2014 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. 4 5 #include "chrome/browser/chromeos/net/delay_network_call.h" 6 7 #include "base/bind.h" 8 #include "base/callback.h" 9 #include "chrome/browser/chromeos/net/network_portal_detector.h" 10 #include "chromeos/network/network_handler.h" 11 #include "chromeos/network/network_state.h" 12 #include "chromeos/network/network_state_handler.h" 13 #include "content/public/browser/browser_thread.h" 14 #include "third_party/cros_system_api/dbus/service_constants.h" 15 16 const unsigned chromeos::kDefaultNetworkRetryDelayMS = 3000; 17 18 void chromeos::DelayNetworkCall(const base::Closure& callback, 19 base::TimeDelta retry) { 20 bool delay_network_call = false; 21 const NetworkState* default_network = 22 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); 23 if (!default_network) { 24 delay_network_call = true; 25 DVLOG(1) << "DelayNetworkCall: No default network."; 26 } else { 27 std:: string default_connection_state = default_network->connection_state(); 28 if (!NetworkState::StateIsConnected(default_connection_state)) { 29 delay_network_call = true; 30 DVLOG(1) << "DelayNetworkCall: " 31 << "Default network: " << default_network->name() 32 << " State: " << default_connection_state; 33 } 34 } 35 if (!delay_network_call && NetworkPortalDetector::IsInitialized()) { 36 NetworkPortalDetector* detector = NetworkPortalDetector::Get(); 37 NetworkPortalDetector::CaptivePortalStatus status = 38 detector->GetCaptivePortalState(default_network->path()).status; 39 if (status != NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE) { 40 delay_network_call = true; 41 DVLOG(1) << "DelayNetworkCall: Captive portal status for " 42 << default_network->name() << ": " 43 << NetworkPortalDetector::CaptivePortalStatusString(status); 44 } 45 } 46 if (delay_network_call) { 47 content::BrowserThread::PostDelayedTask( 48 content::BrowserThread::UI, 49 FROM_HERE, 50 base::Bind(&chromeos::DelayNetworkCall, callback, retry), 51 retry); 52 } else { 53 callback.Run(); 54 } 55 } 56