Skip to content

Commit d83d8ed

Browse files
smilesa-maurice
authored andcommitted
Improved instance_id_desktop_impl encapsulation
Also, fixed Kokoro Linux & Windows builds. PiperOrigin-RevId: 248738304
1 parent a2b067f commit d83d8ed

File tree

2 files changed

+58
-48
lines changed

2 files changed

+58
-48
lines changed

app/instance_id/instance_id_desktop_impl.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <assert.h>
1818

1919
#include "app/instance_id/iid_data_generated.h"
20+
#include "app/rest/transport_curl.h"
21+
#include "app/rest/transport_interface.h"
2022
#include "app/rest/util.h"
2123
#include "app/rest/www_form_url_encoded.h"
2224
#include "app/src/app_common.h"
@@ -27,13 +29,61 @@
2729
#include "app/src/time.h"
2830
#include "app/src/uuid.h"
2931
#include "flatbuffers/flexbuffers.h"
32+
#include "flatbuffers/stl_emulation.h"
3033

3134
namespace firebase {
3235
namespace instance_id {
3336
namespace internal {
3437

3538
using firebase::app::secure::UserSecureManager;
3639

40+
// Response that signals this class when it's complete or canceled.
41+
class SignalSemaphoreResponse : public rest::Response {
42+
public:
43+
explicit SignalSemaphoreResponse(Semaphore* complete)
44+
: complete_(complete) {}
45+
46+
void MarkCompleted() override {
47+
rest::Response::MarkCompleted();
48+
complete_->Post();
49+
}
50+
51+
void MarkCanceled() override {
52+
rest::Response::MarkCompleted();
53+
complete_->Post();
54+
}
55+
56+
void Wait() { complete_->Wait(); }
57+
58+
private:
59+
Semaphore* complete_;
60+
};
61+
62+
// State for the current network operation.
63+
struct NetworkOperation {
64+
NetworkOperation(const std::string& request_data, Semaphore* complete)
65+
: request(request_data.c_str(), request_data.length()),
66+
response(complete) {}
67+
68+
// Schedule the network operation.
69+
void Perform(rest::Transport* transport) {
70+
transport->Perform(request, &response, &controller);
71+
}
72+
73+
// Cancel the current operation.
74+
void Cancel() {
75+
rest::Controller* ctrl = controller.get();
76+
if (ctrl) ctrl->Cancel();
77+
}
78+
79+
// Data sent to the server.
80+
rest::Request request;
81+
// Data returned by the server.
82+
SignalSemaphoreResponse response;
83+
// Progress of the request and allows us to cancel the request.
84+
flatbuffers::unique_ptr<rest::Controller> controller;
85+
};
86+
3787
// Check-in backend.
3888
static const char kCheckinUrl[] =
3989
"https://device-provisioning.googleapis.com/checkin";

app/instance_id/instance_id_desktop_impl.h

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,27 @@
2020
#include <string>
2121

2222
#include "app/memory/unique_ptr.h"
23-
#include "app/rest/transport_curl.h"
2423
#include "app/src/future_manager.h"
2524
#include "app/src/include/firebase/app.h"
2625
#include "app/src/include/firebase/future.h"
2726
#include "app/src/mutex.h"
2827
#include "app/src/scheduler.h"
2928
#include "app/src/secure/user_secure_manager.h"
3029
#include "app/src/semaphore.h"
31-
#include "flatbuffers/stl_emulation.h"
3230

3331
namespace firebase {
32+
33+
namespace rest {
34+
class Request;
35+
class Transport;
36+
} // namespace rest
37+
3438
namespace instance_id {
3539
namespace internal {
3640

3741
class InstanceIdDesktopImplTest; // For testing.
42+
class NetworkOperation; // Defined in instance_id_desktop_impl.cc
43+
class SignalSemaphoreResponse; // Defined in instance_id_desktop_impl.cc
3844

3945
class InstanceIdDesktopImpl {
4046
public:
@@ -103,52 +109,6 @@ class InstanceIdDesktopImpl {
103109

104110
private:
105111
friend class InstanceIdDesktopImplTest;
106-
// Response that signals this class when it's complete or canceled.
107-
class SignalSemaphoreResponse : public rest::Response {
108-
public:
109-
explicit SignalSemaphoreResponse(Semaphore* complete)
110-
: complete_(complete) {}
111-
112-
void MarkCompleted() override {
113-
rest::Response::MarkCompleted();
114-
complete_->Post();
115-
}
116-
117-
void MarkCanceled() override {
118-
rest::Response::MarkCompleted();
119-
complete_->Post();
120-
}
121-
122-
void Wait() { complete_->Wait(); }
123-
124-
private:
125-
Semaphore* complete_;
126-
};
127-
128-
// State for the current network operation.
129-
struct NetworkOperation {
130-
NetworkOperation(const std::string& request_data, Semaphore* complete)
131-
: request(request_data.c_str(), request_data.length()),
132-
response(complete) {}
133-
134-
// Schedule the network operation.
135-
void Perform(rest::Transport* transport) {
136-
transport->Perform(request, &response, &controller);
137-
}
138-
139-
// Cancel the current operation.
140-
void Cancel() {
141-
rest::Controller* ctrl = controller.get();
142-
if (ctrl) ctrl->Cancel();
143-
}
144-
145-
// Data sent to the server.
146-
rest::Request request;
147-
// Data returned by the server.
148-
SignalSemaphoreResponse response;
149-
// Progress of the request and allows us to cancel the request.
150-
flatbuffers::unique_ptr<rest::Controller> controller;
151-
};
152112

153113
// Data cached from a check-in and required to perform instance ID operations.
154114
struct CheckinData {

0 commit comments

Comments
 (0)