Remove weave::Cloud interface
All methods were moved into weave::Device
BUG:24267885
Change-Id: I4809df462b4798dcf2e4ea6233ce3f25fbe73517
Reviewed-on: https://weave-review.googlesource.com/1219
Reviewed-by: Vitaly Buka <vitalybuka@google.com>
diff --git a/libweave/examples/ubuntu/main.cc b/libweave/examples/ubuntu/main.cc
index 11c3fac..cdeae8b 100644
--- a/libweave/examples/ubuntu/main.cc
+++ b/libweave/examples/ubuntu/main.cc
@@ -113,8 +113,7 @@
if (!registration_ticket.empty()) {
weave::ErrorPtr error;
- auto device_id =
- device->GetCloud()->RegisterDevice(registration_ticket, &error);
+ auto device_id = device->Register(registration_ticket, &error);
if (error != nullptr) {
LOG(ERROR) << "Fail to register device: " << error->GetMessage();
} else {
diff --git a/libweave/include/weave/cloud.h b/libweave/include/weave/cloud.h
deleted file mode 100644
index 3cf1318..0000000
--- a/libweave/include/weave/cloud.h
+++ /dev/null
@@ -1,45 +0,0 @@
-// Copyright 2015 The Chromium OS Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef LIBWEAVE_INCLUDE_WEAVE_CLOUD_H_
-#define LIBWEAVE_INCLUDE_WEAVE_CLOUD_H_
-
-#include <string>
-
-#include <base/callback.h>
-#include <base/values.h>
-#include <weave/error.h>
-#include <weave/settings.h>
-
-namespace weave {
-
-// See the DBus interface XML file for complete descriptions of these states.
-enum class RegistrationStatus {
- kUnconfigured, // We have no credentials.
- kConnecting, // We have credentials but not yet connected.
- kConnected, // We're registered and connected to the cloud.
- kInvalidCredentials, // Our registration has been revoked.
-};
-
-class Cloud {
- public:
- using OnRegistrationChangedCallback =
- base::Callback<void(RegistrationStatus satus)>;
-
- // Sets callback which is called when registration state is changed.
- virtual void AddOnRegistrationChangedCallback(
- const OnRegistrationChangedCallback& callback) = 0;
-
- // Registers the device.
- // Returns a device ID on success.
- virtual std::string RegisterDevice(const std::string& ticket_id,
- ErrorPtr* error) = 0;
-
- protected:
- virtual ~Cloud() = default;
-};
-
-} // namespace weave
-
-#endif // LIBWEAVE_INCLUDE_WEAVE_CLOUD_H_
diff --git a/libweave/include/weave/device.h b/libweave/include/weave/device.h
index 5517d10..260790f 100644
--- a/libweave/include/weave/device.h
+++ b/libweave/include/weave/device.h
@@ -9,7 +9,6 @@
#include <set>
#include <string>
-#include <weave/cloud.h>
#include <weave/commands.h>
#include <weave/export.h>
#include <weave/provider/bluetooth.h>
@@ -24,6 +23,14 @@
namespace weave {
+// States of Gcd connection.
+enum class GcdState {
+ kUnconfigured, // We have no credentials.
+ kConnecting, // We have credentials but not yet connected.
+ kConnected, // We're registered and connected to the cloud.
+ kInvalidCredentials, // Our registration has been revoked.
+};
+
class Device {
public:
virtual ~Device() = default;
@@ -49,7 +56,20 @@
virtual Commands* GetCommands() = 0;
virtual State* GetState() = 0;
- virtual Cloud* GetCloud() = 0;
+
+ // Returns current state of GCD connection.
+ virtual GcdState GetGcdState() const = 0;
+
+ // Callback type for GcdStatusCallback.
+ using GcdStateChangedCallback = base::Callback<void(GcdState state)>;
+ // Sets callback which is called when state of server connection changed.
+ virtual void AddGcdStateChangedCallback(
+ const GcdStateChangedCallback& callback) = 0;
+
+ // Registers the device. Returns a device ID on success.
+ // This is testing method and should not be used by applications.
+ virtual std::string Register(const std::string& ticket_id,
+ ErrorPtr* error) = 0;
// Handler should display pin code to the user.
using PairingBeginCallback =
diff --git a/libweave/include/weave/stream.h b/libweave/include/weave/stream.h
index f36e113..9d4d6fd 100644
--- a/libweave/include/weave/stream.h
+++ b/libweave/include/weave/stream.h
@@ -17,7 +17,7 @@
public:
virtual ~InputStream() = default;
- // Callbacks types for Read.
+ // Callback type for Read.
using ReadSuccessCallback = base::Callback<void(size_t size)>;
// Implementation should return immediately and post either success_callback
diff --git a/libweave/src/device_manager.cc b/libweave/src/device_manager.cc
index cec5e16..5a8e630 100644
--- a/libweave/src/device_manager.cc
+++ b/libweave/src/device_manager.cc
@@ -84,10 +84,6 @@
return device_info_->GetMutableConfig();
}
-Cloud* DeviceManager::GetCloud() {
- return device_info_.get();
-}
-
void DeviceManager::StartPrivet(provider::TaskRunner* task_runner,
provider::Network* network,
provider::DnsServiceDiscovery* dns_sd,
@@ -100,6 +96,20 @@
state_manager_.get());
}
+GcdState DeviceManager::GetGcdState() const {
+ return device_info_->GetGcdState();
+}
+
+void DeviceManager::AddGcdStateChangedCallback(
+ const GcdStateChangedCallback& callback) {
+ device_info_->AddGcdStateChangedCallback(callback);
+}
+
+std::string DeviceManager::Register(const std::string& ticket_id,
+ ErrorPtr* error) {
+ return device_info_->RegisterDevice(ticket_id, error);
+}
+
void DeviceManager::AddPairingChangedCallbacks(
const PairingBeginCallback& begin_callback,
const PairingEndCallback& end_callback) {
diff --git a/libweave/src/device_manager.h b/libweave/src/device_manager.h
index 6f68054..787a7be 100644
--- a/libweave/src/device_manager.h
+++ b/libweave/src/device_manager.h
@@ -40,7 +40,13 @@
const SettingsChangedCallback& callback) override;
Commands* GetCommands() override;
State* GetState() override;
- Cloud* GetCloud() override;
+ std::string RegisterDevice(const std::string& ticket_id,
+ ErrorPtr* error) override;
+
+ GcdState GetGcdState() const override;
+ void AddGcdStateChangedCallback(
+ const GcdStateChangedCallback& callback) override;
+
void AddPairingChangedCallbacks(
const PairingBeginCallback& begin_callback,
const PairingEndCallback& end_callback) override;
diff --git a/libweave/src/device_registration_info.cc b/libweave/src/device_registration_info.cc
index 1e5566a..610871f 100644
--- a/libweave/src/device_registration_info.cc
+++ b/libweave/src/device_registration_info.cc
@@ -284,7 +284,7 @@
void DeviceRegistrationInfo::ScheduleCloudConnection(
const base::TimeDelta& delay) {
- SetRegistrationStatus(RegistrationStatus::kConnecting);
+ SetGcdState(GcdState::kConnecting);
if (!task_runner_)
return; // Assume we're in test
task_runner_->PostDelayedTask(
@@ -322,7 +322,7 @@
}
if (error_code == "invalid_grant") {
LOG(INFO) << "The device's registration has been revoked.";
- SetRegistrationStatus(RegistrationStatus::kInvalidCredentials);
+ SetGcdState(GcdState::kInvalidCredentials);
}
// I have never actually seen an error_description returned.
if (!resp->GetString("error_description", &error_message)) {
@@ -470,10 +470,10 @@
primary_notification_channel_->Start(this);
}
-void DeviceRegistrationInfo::AddOnRegistrationChangedCallback(
- const OnRegistrationChangedCallback& callback) {
- on_registration_changed_.push_back(callback);
- callback.Run(registration_status_);
+void DeviceRegistrationInfo::AddGcdStateChangedCallback(
+ const Device::GcdStateChangedCallback& callback) {
+ gcd_state_changed_callbacks_.push_back(callback);
+ callback.Run(gcd_state_);
}
std::unique_ptr<base::DictionaryValue>
@@ -730,7 +730,7 @@
}
cloud_backoff_entry_->InformOfRequest(true);
- SetRegistrationStatus(RegistrationStatus::kConnected);
+ SetGcdState(GcdState::kConnected);
data->success_callback.Run(*json_resp);
}
@@ -745,7 +745,7 @@
void DeviceRegistrationInfo::RetryCloudRequest(
const std::shared_ptr<const CloudRequestData>& data) {
// TODO(avakulenko): Tie connecting/connected status to XMPP channel instead.
- SetRegistrationStatus(RegistrationStatus::kConnecting);
+ SetGcdState(GcdState::kConnecting);
cloud_backoff_entry_->InformOfRequest(false);
SendCloudRequest(data);
}
@@ -1176,13 +1176,12 @@
device_state_update_pending_ = false;
}
-void DeviceRegistrationInfo::SetRegistrationStatus(
- RegistrationStatus new_status) {
- VLOG_IF(1, new_status != registration_status_)
- << "Changing registration status to " << static_cast<int>(new_status);
- registration_status_ = new_status;
- for (const auto& cb : on_registration_changed_)
- cb.Run(registration_status_);
+void DeviceRegistrationInfo::SetGcdState(GcdState new_state) {
+ VLOG_IF(1, new_state != gcd_state_) << "Changing registration status to "
+ << EnumToString(new_state);
+ gcd_state_ = new_state;
+ for (const auto& cb : gcd_state_changed_callbacks_)
+ cb.Run(gcd_state_);
}
void DeviceRegistrationInfo::OnCommandDefsChanged() {
@@ -1297,7 +1296,7 @@
pull_channel_.reset();
}
notification_channel_starting_ = false;
- SetRegistrationStatus(RegistrationStatus::kInvalidCredentials);
+ SetGcdState(GcdState::kInvalidCredentials);
}
} // namespace weave
diff --git a/libweave/src/device_registration_info.h b/libweave/src/device_registration_info.h
index 38b324b..c797102 100644
--- a/libweave/src/device_registration_info.h
+++ b/libweave/src/device_registration_info.h
@@ -15,8 +15,8 @@
#include <base/macros.h>
#include <base/memory/weak_ptr.h>
#include <base/time/time.h>
+#include <weave/device.h>
#include <weave/error.h>
-#include <weave/cloud.h>
#include <weave/provider/http_client.h>
#include "src/backoff_entry.h"
@@ -47,8 +47,7 @@
extern const char kErrorDomainGCDServer[];
// The DeviceRegistrationInfo class represents device registration information.
-class DeviceRegistrationInfo : public Cloud,
- public NotificationDelegate,
+class DeviceRegistrationInfo : public NotificationDelegate,
public CloudCommandUpdateInterface {
public:
using CloudRequestCallback =
@@ -63,11 +62,9 @@
~DeviceRegistrationInfo() override;
- // Cloud overrides.
- void AddOnRegistrationChangedCallback(
- const OnRegistrationChangedCallback& callback) override;
- std::string RegisterDevice(const std::string& ticket_id,
- ErrorPtr* error) override;
+ void AddGcdStateChangedCallback(
+ const Device::GcdStateChangedCallback& callback);
+ std::string RegisterDevice(const std::string& ticket_id, ErrorPtr* error);
void UpdateDeviceInfo(const std::string& name,
const std::string& description,
@@ -128,6 +125,8 @@
const Config::Settings& GetSettings() const { return config_->GetSettings(); }
Config* GetMutableConfig() { return config_.get(); }
+ GcdState GetGcdState() const { return gcd_state_; }
+
private:
friend class DeviceRegistrationInfoTest;
@@ -261,7 +260,7 @@
// for all supported commands and current device state.
std::unique_ptr<base::DictionaryValue> BuildDeviceResource(ErrorPtr* error);
- void SetRegistrationStatus(RegistrationStatus new_status);
+ void SetGcdState(GcdState new_state);
void SetDeviceId(const std::string& cloud_id);
// Callback called when command definitions are changed to re-publish new CDD.
@@ -330,10 +329,10 @@
provider::Network* network_{nullptr};
- // Tracks our current registration status.
- RegistrationStatus registration_status_{RegistrationStatus::kUnconfigured};
+ // Tracks our GCD state.
+ GcdState gcd_state_{GcdState::kUnconfigured};
- std::vector<OnRegistrationChangedCallback> on_registration_changed_;
+ std::vector<Device::GcdStateChangedCallback> gcd_state_changed_callbacks_;
base::WeakPtrFactory<DeviceRegistrationInfo> weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(DeviceRegistrationInfo);
diff --git a/libweave/src/device_registration_info_unittest.cc b/libweave/src/device_registration_info_unittest.cc
index e17ecbe..34ea31a 100644
--- a/libweave/src/device_registration_info_unittest.cc
+++ b/libweave/src/device_registration_info_unittest.cc
@@ -180,9 +180,7 @@
void SetAccessToken() { dev_reg_->access_token_ = test_data::kAccessToken; }
- RegistrationStatus GetRegistrationStatus() const {
- return dev_reg_->registration_status_;
- }
+ GcdState GetGcdState() const { return dev_reg_->GetGcdState(); }
provider::test::MockConfigStore config_store_;
StrictMock<MockHttpClient> http_client_;
@@ -256,7 +254,7 @@
TEST_F(DeviceRegistrationInfoTest, CheckAuthenticationFailure) {
ReloadSettings();
- EXPECT_EQ(RegistrationStatus::kConnecting, GetRegistrationStatus());
+ EXPECT_EQ(GcdState::kConnecting, GetGcdState());
EXPECT_CALL(http_client_,
MockSendRequest(http::kPost, dev_reg_->GetOAuthURL("token"),
@@ -277,12 +275,12 @@
ErrorPtr error;
EXPECT_FALSE(RefreshAccessToken(&error));
EXPECT_TRUE(error->HasError(kErrorDomainOAuth2, "unable_to_authenticate"));
- EXPECT_EQ(RegistrationStatus::kConnecting, GetRegistrationStatus());
+ EXPECT_EQ(GcdState::kConnecting, GetGcdState());
}
TEST_F(DeviceRegistrationInfoTest, CheckDeregistration) {
ReloadSettings();
- EXPECT_EQ(RegistrationStatus::kConnecting, GetRegistrationStatus());
+ EXPECT_EQ(GcdState::kConnecting, GetGcdState());
EXPECT_CALL(http_client_,
MockSendRequest(http::kPost, dev_reg_->GetOAuthURL("token"),
@@ -303,7 +301,7 @@
ErrorPtr error;
EXPECT_FALSE(RefreshAccessToken(&error));
EXPECT_TRUE(error->HasError(kErrorDomainOAuth2, "invalid_grant"));
- EXPECT_EQ(RegistrationStatus::kInvalidCredentials, GetRegistrationStatus());
+ EXPECT_EQ(GcdState::kInvalidCredentials, GetGcdState());
}
TEST_F(DeviceRegistrationInfoTest, GetDeviceInfo) {
@@ -486,7 +484,7 @@
dev_reg_->RegisterDevice(test_data::kClaimTicketId, nullptr);
EXPECT_EQ(test_data::kDeviceId, cloud_id);
- EXPECT_EQ(RegistrationStatus::kConnecting, GetRegistrationStatus());
+ EXPECT_EQ(GcdState::kConnecting, GetGcdState());
// Validate the device info saved to storage...
EXPECT_EQ(test_data::kDeviceId, dev_reg_->GetSettings().cloud_id);
@@ -498,10 +496,10 @@
TEST_F(DeviceRegistrationInfoTest, OOBRegistrationStatus) {
// After we've been initialized, we should be either offline or
// unregistered, depending on whether or not we've found credentials.
- EXPECT_EQ(RegistrationStatus::kUnconfigured, GetRegistrationStatus());
+ EXPECT_EQ(GcdState::kUnconfigured, GetGcdState());
// Put some credentials into our state, make sure we call that offline.
ReloadSettings();
- EXPECT_EQ(RegistrationStatus::kConnecting, GetRegistrationStatus());
+ EXPECT_EQ(GcdState::kConnecting, GetGcdState());
}
TEST_F(DeviceRegistrationInfoTest, UpdateCommand) {
diff --git a/libweave/src/privet/cloud_delegate.cc b/libweave/src/privet/cloud_delegate.cc
index 217a4ed..2a60de0 100644
--- a/libweave/src/privet/cloud_delegate.cc
+++ b/libweave/src/privet/cloud_delegate.cc
@@ -49,7 +49,7 @@
state_manager_{state_manager} {
device_->GetMutableConfig()->AddOnChangedCallback(base::Bind(
&CloudDelegateImpl::OnConfigChanged, weak_factory_.GetWeakPtr()));
- device_->AddOnRegistrationChangedCallback(base::Bind(
+ device_->AddGcdStateChangedCallback(base::Bind(
&CloudDelegateImpl::OnRegistrationChanged, weak_factory_.GetWeakPtr()));
command_manager_->AddOnCommandDefChanged(base::Bind(
@@ -234,13 +234,13 @@
void OnConfigChanged(const Settings&) { NotifyOnDeviceInfoChanged(); }
- void OnRegistrationChanged(RegistrationStatus status) {
- if (status == RegistrationStatus::kUnconfigured) {
+ void OnRegistrationChanged(GcdState status) {
+ if (status == GcdState::kUnconfigured) {
connection_state_ = ConnectionState{ConnectionState::kUnconfigured};
- } else if (status == RegistrationStatus::kConnecting) {
+ } else if (status == GcdState::kConnecting) {
// TODO(vitalybuka): Find conditions for kOffline.
connection_state_ = ConnectionState{ConnectionState::kConnecting};
- } else if (status == RegistrationStatus::kConnected) {
+ } else if (status == GcdState::kConnected) {
connection_state_ = ConnectionState{ConnectionState::kOnline};
} else {
ErrorPtr error;
diff --git a/libweave/src/registration_status.cc b/libweave/src/registration_status.cc
index 35f93c4..167eb0f 100644
--- a/libweave/src/registration_status.cc
+++ b/libweave/src/registration_status.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include <weave/cloud.h>
+#include <weave/device.h>
#include <weave/enum_to_string.h>
#include <weave/export.h>
@@ -10,17 +10,17 @@
namespace {
-const EnumToStringMap<RegistrationStatus>::Map kMap[] = {
- {RegistrationStatus::kUnconfigured, "unconfigured"},
- {RegistrationStatus::kConnecting, "connecting"},
- {RegistrationStatus::kConnected, "connected"},
- {RegistrationStatus::kInvalidCredentials, "invalid_credentials"},
+const EnumToStringMap<GcdState>::Map kMap[] = {
+ {GcdState::kUnconfigured, "unconfigured"},
+ {GcdState::kConnecting, "connecting"},
+ {GcdState::kConnected, "connected"},
+ {GcdState::kInvalidCredentials, "invalid_credentials"},
};
} // namespace
template <>
-LIBWEAVE_EXPORT EnumToStringMap<RegistrationStatus>::EnumToStringMap()
+LIBWEAVE_EXPORT EnumToStringMap<GcdState>::EnumToStringMap()
: EnumToStringMap(kMap) {}
} // namespace weave
diff --git a/libweave/src/weave_unittest.cc b/libweave/src/weave_unittest.cc
index 8250d8c..e211392 100644
--- a/libweave/src/weave_unittest.cc
+++ b/libweave/src/weave_unittest.cc
@@ -238,9 +238,6 @@
device_->Start(&config_store_, &task_runner_, &http_client_, &network_,
&dns_sd_, &http_server_, &wifi_, &bluetooth_);
- cloud_ = device_->GetCloud();
- ASSERT_TRUE(cloud_);
-
for (const auto& cb : http_server_changed_cb_)
cb.Run(http_server_);
@@ -270,8 +267,6 @@
std::vector<provider::Network::ConnectionChangedCallback> network_callbacks_;
- weave::Cloud* cloud_{nullptr};
-
std::unique_ptr<weave::Device> device_;
};
@@ -341,7 +336,7 @@
InitDnsSdPublishing(true, "DB");
- EXPECT_EQ("CLOUD_ID", cloud_->RegisterDevice("TICKET_ID", nullptr));
+ EXPECT_EQ("CLOUD_ID", device_->RegisterDevice("TICKET_ID", nullptr));
}
class WeaveWiFiSetupTest : public WeaveTest {