Remove AddOnStateChangedCallback from HTTP server Implementation should have http server running before creating weave::Device BUG:24267885 Change-Id: Ia53cd3f669eea80120a5e8673fe8e549a2c42ccf Reviewed-on: https://weave-review.googlesource.com/1271 Reviewed-by: Alex Vakulenko <avakulenko@google.com>
diff --git a/libweave/examples/ubuntu/event_http_server.cc b/libweave/examples/ubuntu/event_http_server.cc index 9c0dc78..4a7156a 100644 --- a/libweave/examples/ubuntu/event_http_server.cc +++ b/libweave/examples/ubuntu/event_http_server.cc
@@ -199,11 +199,6 @@ evhttp_send_reply(req, status_code, "None", buf.get()); } -void HttpServerImpl::AddOnStateChangedCallback( - const OnStateChangedCallback& callback) { - callback.Run(*this); -} - void HttpServerImpl::AddRequestHandler(const std::string& path_prefix, const OnRequestCallback& callback) { handlers_.emplace(path_prefix, callback);
diff --git a/libweave/examples/ubuntu/event_http_server.h b/libweave/examples/ubuntu/event_http_server.h index 1005edc..82c51dd 100644 --- a/libweave/examples/ubuntu/event_http_server.h +++ b/libweave/examples/ubuntu/event_http_server.h
@@ -28,8 +28,6 @@ explicit HttpServerImpl(EventTaskRunner* task_runner); - void AddOnStateChangedCallback( - const OnStateChangedCallback& callback) override; void AddRequestHandler(const std::string& path_prefix, const OnRequestCallback& callback) override; uint16_t GetHttpPort() const override;
diff --git a/libweave/include/weave/provider/http_server.h b/libweave/include/weave/provider/http_server.h index 1d4ab1e..44c3e1d 100644 --- a/libweave/include/weave/provider/http_server.h +++ b/libweave/include/weave/provider/http_server.h
@@ -27,8 +27,6 @@ virtual ~Request() = default; }; - using OnStateChangedCallback = base::Callback<void(const HttpServer& server)>; - using OnReplyCallback = base::Callback<void(int status_code, const std::string& data, const std::string& mime_type)>; @@ -37,10 +35,6 @@ base::Callback<void(const Request& request, const OnReplyCallback& callback)>; - // Adds notification callback for server started/stopped serving requests. - virtual void AddOnStateChangedCallback( - const OnStateChangedCallback& callback) = 0; - // Adds callback called on new http/https requests with the given path prefix. virtual void AddRequestHandler(const std::string& path_prefix, const OnRequestCallback& callback) = 0;
diff --git a/libweave/include/weave/provider/test/mock_http_server.h b/libweave/include/weave/provider/test/mock_http_server.h index 3beb4ae..c6fc755 100644 --- a/libweave/include/weave/provider/test/mock_http_server.h +++ b/libweave/include/weave/provider/test/mock_http_server.h
@@ -18,7 +18,6 @@ class MockHttpServer : public HttpServer { public: - MOCK_METHOD1(AddOnStateChangedCallback, void(const OnStateChangedCallback&)); MOCK_METHOD2(AddRequestHandler, void(const std::string&, const OnRequestCallback&));
diff --git a/libweave/src/privet/device_delegate.cc b/libweave/src/privet/device_delegate.cc index 3b2e247..00c1075 100644 --- a/libweave/src/privet/device_delegate.cc +++ b/libweave/src/privet/device_delegate.cc
@@ -15,7 +15,8 @@ class DeviceDelegateImpl : public DeviceDelegate { public: - DeviceDelegateImpl() = default; + DeviceDelegateImpl(uint16_t http_port, uint16_t https_port) + : http_port_{http_port}, https_port_{https_port} {} ~DeviceDelegateImpl() override = default; std::pair<uint16_t, uint16_t> GetHttpEnpoint() const override { @@ -45,8 +46,11 @@ DeviceDelegate::~DeviceDelegate() {} // static -std::unique_ptr<DeviceDelegate> DeviceDelegate::CreateDefault() { - return std::unique_ptr<DeviceDelegate>(new DeviceDelegateImpl()); +std::unique_ptr<DeviceDelegate> DeviceDelegate::CreateDefault( + uint16_t http_port, + uint16_t https_port) { + return std::unique_ptr<DeviceDelegate>( + new DeviceDelegateImpl(http_port, https_port)); } } // namespace privet
diff --git a/libweave/src/privet/device_delegate.h b/libweave/src/privet/device_delegate.h index 12996d7..5d5402a 100644 --- a/libweave/src/privet/device_delegate.h +++ b/libweave/src/privet/device_delegate.h
@@ -38,7 +38,8 @@ virtual void SetHttpsPort(uint16_t port) = 0; // Create default instance. - static std::unique_ptr<DeviceDelegate> CreateDefault(); + static std::unique_ptr<DeviceDelegate> CreateDefault(uint16_t http_port, + uint16_t https_port); }; } // namespace privet
diff --git a/libweave/src/privet/privet_manager.cc b/libweave/src/privet/privet_manager.cc index ac47c6c..10b914a 100644 --- a/libweave/src/privet/privet_manager.cc +++ b/libweave/src/privet/privet_manager.cc
@@ -49,13 +49,16 @@ StateManager* state_manager) { disable_security_ = device->GetSettings().disable_security; - device_ = DeviceDelegate::CreateDefault(); + device_ = DeviceDelegate::CreateDefault(http_server->GetHttpPort(), + http_server->GetHttpsPort()); cloud_ = CloudDelegate::CreateDefault(task_runner, device, command_manager, state_manager); cloud_observer_.Add(cloud_.get()); security_.reset(new SecurityManager( device->GetSettings().secret, device->GetSettings().pairing_modes, device->GetSettings().embedded_code, disable_security_, task_runner)); + security_->SetCertificateFingerprint( + http_server->GetHttpsCertificateFingerprint()); if (device->GetSettings().secret.empty()) { // TODO(vitalybuka): Post all Config::Transaction to avoid following. task_runner->PostDelayedTask( @@ -83,8 +86,6 @@ security_.get(), wifi_bootstrap_manager_.get())); - http_server->AddOnStateChangedCallback(base::Bind( - &Manager::OnHttpServerStatusChanged, weak_ptr_factory_.GetWeakPtr())); http_server->AddRequestHandler("/privet/", base::Bind(&Manager::PrivetRequestHandler, weak_ptr_factory_.GetWeakPtr())); @@ -152,16 +153,6 @@ OnChanged(); } -void Manager::OnHttpServerStatusChanged(const HttpServer& server) { - if (device_->GetHttpEnpoint().first != server.GetHttpPort()) { - device_->SetHttpPort(server.GetHttpPort()); - if (publisher_) // Only HTTP port is published - publisher_->Update(); - } - device_->SetHttpsPort(server.GetHttpsPort()); - security_->SetCertificateFingerprint(server.GetHttpsCertificateFingerprint()); -} - void Manager::SaveDeviceSecret(Config* config) { Config::Transaction transaction(config); transaction.set_secret(security_->GetSecret());
diff --git a/libweave/src/privet/privet_manager.h b/libweave/src/privet/privet_manager.h index 35fb6f9..db6c6da 100644 --- a/libweave/src/privet/privet_manager.h +++ b/libweave/src/privet/privet_manager.h
@@ -78,7 +78,6 @@ void OnChanged(); void OnConnectivityChanged(); - void OnHttpServerStatusChanged(const provider::HttpServer& server); void SaveDeviceSecret(Config* config); bool disable_security_{false};
diff --git a/libweave/src/weave_unittest.cc b/libweave/src/weave_unittest.cc index a44101a..b6f9207 100644 --- a/libweave/src/weave_unittest.cc +++ b/libweave/src/weave_unittest.cc
@@ -207,11 +207,6 @@ const provider::HttpServer::OnRequestCallback& cb) { http_server_request_cb_.push_back(cb); })); - EXPECT_CALL(http_server_, AddOnStateChangedCallback(_)) - .WillRepeatedly(Invoke( - [this](const provider::HttpServer::OnStateChangedCallback& cb) { - http_server_changed_cb_.push_back(cb); - })); } void InitDefaultExpectations() { @@ -232,9 +227,6 @@ device_->AddStateDefinitionsFromJson(kStateDefs); device_->SetStatePropertiesFromJson(kStateDefaults, nullptr); - for (const auto& cb : http_server_changed_cb_) - cb.Run(http_server_); - task_runner_.Run(); } @@ -246,8 +238,6 @@ } } - std::vector<provider::HttpServer::OnStateChangedCallback> - http_server_changed_cb_; std::vector<provider::HttpServer::OnRequestCallback> http_server_request_cb_; StrictMock<provider::test::MockConfigStore> config_store_; @@ -289,9 +279,6 @@ &bluetooth_); device_->AddCommandDefinitionsFromJson(kCommandDefs); - for (const auto& cb : http_server_changed_cb_) - cb.Run(http_server_); - task_runner_.Run(); }