1 //
2 // Copyright (C) 2014 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "shill/connection_tester.h"
18
19 #include <string>
20
21 #include <base/bind.h>
22 #include <base/strings/string_util.h>
23 #include <base/strings/stringprintf.h>
24 #if defined(__ANDROID__)
25 #include <dbus/service_constants.h>
26 #else
27 #include <chromeos/dbus/service_constants.h>
28 #endif // __ANDROID__
29
30 #include "shill/connection.h"
31 #include "shill/connectivity_trial.h"
32 #include "shill/logging.h"
33
34 using base::Bind;
35 using base::Callback;
36 using base::StringPrintf;
37 using std::string;
38
39 namespace shill {
40
41 namespace Logging {
42 static auto kModuleLogScope = ScopeLogger::kPortal;
ObjectID(Connection * c)43 static string ObjectID(Connection* c) { return c->interface_name(); }
44 }
45
46 const int ConnectionTester::kTrialTimeoutSeconds = 5;
47
ConnectionTester(ConnectionRefPtr connection,EventDispatcher * dispatcher,const Callback<void ()> & callback)48 ConnectionTester::ConnectionTester(
49 ConnectionRefPtr connection,
50 EventDispatcher* dispatcher,
51 const Callback<void()>& callback)
52 : connection_(connection),
53 dispatcher_(dispatcher),
54 weak_ptr_factory_(this),
55 tester_callback_(callback),
56 connectivity_trial_(
57 new ConnectivityTrial(connection_,
58 dispatcher_,
59 kTrialTimeoutSeconds,
60 Bind(&ConnectionTester::CompleteTest,
61 weak_ptr_factory_.GetWeakPtr()))) { }
62
~ConnectionTester()63 ConnectionTester::~ConnectionTester() {
64 Stop();
65 connectivity_trial_.reset();
66 }
67
Start()68 void ConnectionTester::Start() {
69 SLOG(connection_.get(), 3) << "In " << __func__;
70 if (!connectivity_trial_->Start(ConnectivityTrial::kDefaultURL, 0))
71 LOG(ERROR) << StringPrintf("ConnectivityTrial failed to parse default "
72 "URL %s", ConnectivityTrial::kDefaultURL);
73 }
74
Stop()75 void ConnectionTester::Stop() {
76 SLOG(connection_.get(), 3) << "In " << __func__;
77 connectivity_trial_->Stop();
78 }
79
CompleteTest(ConnectivityTrial::Result result)80 void ConnectionTester::CompleteTest(ConnectivityTrial::Result result) {
81 LOG(INFO) << StringPrintf("ConnectivityTester completed with phase==%s, "
82 "status==%s",
83 ConnectivityTrial::PhaseToString(
84 result.phase).c_str(),
85 ConnectivityTrial::StatusToString(
86 result.status).c_str());
87 Stop();
88 tester_callback_.Run();
89 }
90
91 } // namespace shill
92
93