Skip to content

Commit 4677c17

Browse files
jonsimantova-maurice
authored andcommitted
Unhook IID desktop.
This is accomplished by having InstanceIdDesktopImpl return nullptr for GetInstance(), and having callers (Remote Config, Instance ID client library) check for nullptr before using it. This also makes the Instance ID client library act as a stub if there is no InstanceIdDesktopImpl available. PiperOrigin-RevId: 249127105
1 parent 73c77e2 commit 4677c17

File tree

3 files changed

+103
-72
lines changed

3 files changed

+103
-72
lines changed

app/instance_id/instance_id_desktop_impl.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,15 @@ InstanceIdDesktopImpl::~InstanceIdDesktopImpl() {
213213
}
214214

215215
InstanceIdDesktopImpl* InstanceIdDesktopImpl::GetInstance(App* app) {
216+
#ifdef FIREBASE_EARLY_ACCESS_PREVIEW
216217
MutexLock lock(instance_id_by_app_mutex_);
217218
auto it = instance_id_by_app_.find(app);
218219
return it != instance_id_by_app_.end() ? it->second
219220
: new InstanceIdDesktopImpl(app);
221+
#else
222+
// Callers know that nullptr means "act as a stub".
223+
return nullptr;
224+
#endif // FIREBASE_EARLY_ACCESS_PREVIEW
220225
}
221226

222227
Future<std::string> InstanceIdDesktopImpl::GetId() {

instance_id/src/desktop/instance_id.cc

Lines changed: 94 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,29 @@ Future<std::string> InstanceId::GetId() const {
3333
const auto future_handle = instance_id_internal_->FutureAlloc<std::string>(
3434
InstanceIdInternal::kApiFunctionGetId);
3535

36-
const auto internal_future = instance_id_internal_->impl()->GetId();
37-
InstanceIdInternal::InternalRef& internal_ref =
38-
instance_id_internal_->safe_ref();
39-
internal_future.OnCompletion(
40-
[&internal_ref, future_handle](const Future<std::string>& result) {
41-
InstanceIdInternal::InternalRefLock lock(&internal_ref);
42-
if (lock.GetReference() == nullptr) {
43-
return; // deleted.
44-
}
45-
if (result.error() == 0) {
46-
lock.GetReference()->future_api().CompleteWithResult(
47-
future_handle, kErrorNone, "", std::string(*result.result()));
48-
} else {
49-
lock.GetReference()->future_api().Complete(
50-
future_handle, kErrorUnknown, result.error_message());
51-
}
52-
});
36+
if (instance_id_internal_->impl()) {
37+
const auto internal_future = instance_id_internal_->impl()->GetId();
38+
InstanceIdInternal::InternalRef& internal_ref =
39+
instance_id_internal_->safe_ref();
40+
internal_future.OnCompletion(
41+
[&internal_ref, future_handle](const Future<std::string>& result) {
42+
InstanceIdInternal::InternalRefLock lock(&internal_ref);
43+
if (lock.GetReference() == nullptr) {
44+
return; // deleted.
45+
}
46+
if (result.error() == 0) {
47+
lock.GetReference()->future_api().CompleteWithResult(
48+
future_handle, kErrorNone, "", std::string(*result.result()));
49+
} else {
50+
lock.GetReference()->future_api().Complete(
51+
future_handle, kErrorUnknown, result.error_message());
52+
}
53+
});
54+
} else {
55+
// If there is no InstanceIdDesktopImpl, run as a stub.
56+
instance_id_internal_->future_api().CompleteWithResult(
57+
future_handle, kErrorNone, "", std::string("FakeId"));
58+
}
5359
return MakeFuture(&instance_id_internal_->future_api(), future_handle);
5460
}
5561

@@ -59,24 +65,29 @@ Future<void> InstanceId::DeleteId() {
5965
const auto future_handle = instance_id_internal_->FutureAlloc<void>(
6066
InstanceIdInternal::kApiFunctionDeleteId);
6167

62-
const auto internal_future = instance_id_internal_->impl()->DeleteId();
63-
64-
InstanceIdInternal::InternalRef& internal_ref =
65-
instance_id_internal_->safe_ref();
66-
internal_future.OnCompletion([&internal_ref,
67-
future_handle](const Future<void>& result) {
68-
InstanceIdInternal::InternalRefLock lock(&internal_ref);
69-
if (lock.GetReference() == nullptr) {
70-
return; // deleted.
71-
}
72-
if (result.error() == 0) {
73-
lock.GetReference()->future_api().Complete(future_handle, kErrorNone, "");
74-
} else {
75-
lock.GetReference()->future_api().Complete(future_handle, kErrorUnknown,
76-
result.error_message());
77-
}
78-
});
79-
68+
if (instance_id_internal_->impl()) {
69+
const auto internal_future = instance_id_internal_->impl()->DeleteId();
70+
71+
InstanceIdInternal::InternalRef& internal_ref =
72+
instance_id_internal_->safe_ref();
73+
internal_future.OnCompletion(
74+
[&internal_ref, future_handle](const Future<void>& result) {
75+
InstanceIdInternal::InternalRefLock lock(&internal_ref);
76+
if (lock.GetReference() == nullptr) {
77+
return; // deleted.
78+
}
79+
if (result.error() == 0) {
80+
lock.GetReference()->future_api().Complete(future_handle,
81+
kErrorNone, "");
82+
} else {
83+
lock.GetReference()->future_api().Complete(
84+
future_handle, kErrorUnknown, result.error_message());
85+
}
86+
});
87+
} else {
88+
// If there is no InstanceIdDesktopImpl, run as a stub.
89+
instance_id_internal_->future_api().Complete(future_handle, kErrorNone, "");
90+
}
8091
return MakeFuture(&instance_id_internal_->future_api(), future_handle);
8192
}
8293

@@ -87,24 +98,30 @@ Future<std::string> InstanceId::GetToken(const char* entity,
8798
const auto future_handle = instance_id_internal_->FutureAlloc<std::string>(
8899
InstanceIdInternal::kApiFunctionGetToken);
89100

90-
const auto internal_future = instance_id_internal_->impl()->GetToken(scope);
91-
92-
InstanceIdInternal::InternalRef& internal_ref =
93-
instance_id_internal_->safe_ref();
94-
internal_future.OnCompletion(
95-
[&internal_ref, future_handle](const Future<std::string>& result) {
96-
InstanceIdInternal::InternalRefLock lock(&internal_ref);
97-
if (lock.GetReference() == nullptr) {
98-
return; // deleted.
99-
}
100-
if (result.error() == 0) {
101-
lock.GetReference()->future_api().CompleteWithResult(
102-
future_handle, kErrorNone, "", std::string(*result.result()));
103-
} else {
104-
lock.GetReference()->future_api().Complete(
105-
future_handle, kErrorUnknown, result.error_message());
106-
}
107-
});
101+
if (instance_id_internal_->impl()) {
102+
const auto internal_future = instance_id_internal_->impl()->GetToken(scope);
103+
104+
InstanceIdInternal::InternalRef& internal_ref =
105+
instance_id_internal_->safe_ref();
106+
internal_future.OnCompletion(
107+
[&internal_ref, future_handle](const Future<std::string>& result) {
108+
InstanceIdInternal::InternalRefLock lock(&internal_ref);
109+
if (lock.GetReference() == nullptr) {
110+
return; // deleted.
111+
}
112+
if (result.error() == 0) {
113+
lock.GetReference()->future_api().CompleteWithResult(
114+
future_handle, kErrorNone, "", std::string(*result.result()));
115+
} else {
116+
lock.GetReference()->future_api().Complete(
117+
future_handle, kErrorUnknown, result.error_message());
118+
}
119+
});
120+
} else {
121+
// If there is no InstanceIdDesktopImpl, run as a stub.
122+
instance_id_internal_->future_api().CompleteWithResult(
123+
future_handle, kErrorNone, "", std::string("FakeToken"));
124+
}
108125
return MakeFuture(&instance_id_internal_->future_api(), future_handle);
109126
}
110127

@@ -114,24 +131,30 @@ Future<void> InstanceId::DeleteToken(const char* entity, const char* scope) {
114131
const auto future_handle = instance_id_internal_->FutureAlloc<void>(
115132
InstanceIdInternal::kApiFunctionDeleteToken);
116133

117-
const auto internal_future =
118-
instance_id_internal_->impl()->DeleteToken(scope);
119-
InstanceIdInternal::InternalRef& internal_ref =
120-
instance_id_internal_->safe_ref();
121-
122-
internal_future.OnCompletion([&internal_ref,
123-
future_handle](const Future<void>& result) {
124-
InstanceIdInternal::InternalRefLock lock(&internal_ref);
125-
if (lock.GetReference() == nullptr) {
126-
return; // deleted.
127-
}
128-
if (result.error() == 0) {
129-
lock.GetReference()->future_api().Complete(future_handle, kErrorNone, "");
130-
} else {
131-
lock.GetReference()->future_api().Complete(future_handle, kErrorUnknown,
132-
result.error_message());
133-
}
134-
});
134+
if (instance_id_internal_->impl()) {
135+
const auto internal_future =
136+
instance_id_internal_->impl()->DeleteToken(scope);
137+
InstanceIdInternal::InternalRef& internal_ref =
138+
instance_id_internal_->safe_ref();
139+
140+
internal_future.OnCompletion(
141+
[&internal_ref, future_handle](const Future<void>& result) {
142+
InstanceIdInternal::InternalRefLock lock(&internal_ref);
143+
if (lock.GetReference() == nullptr) {
144+
return; // deleted.
145+
}
146+
if (result.error() == 0) {
147+
lock.GetReference()->future_api().Complete(future_handle,
148+
kErrorNone, "");
149+
} else {
150+
lock.GetReference()->future_api().Complete(
151+
future_handle, kErrorUnknown, result.error_message());
152+
}
153+
});
154+
} else {
155+
// If there is no InstanceIdDesktopImpl, run as a stub.
156+
instance_id_internal_->future_api().Complete(future_handle, kErrorNone, "");
157+
}
135158
return MakeFuture(&instance_id_internal_->future_api(), future_handle);
136159
}
137160

remote_config/src/desktop/rest.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ void RemoteConfigREST::TryGetInstanceIdAndToken(const App& app) {
106106
auto* iid_impl =
107107
firebase::instance_id::internal::InstanceIdDesktopImpl::GetInstance(
108108
non_const_app);
109-
assert(iid_impl);
109+
if (iid_impl == nullptr) {
110+
// Instance ID not supported.
111+
return;
112+
}
110113

111114
WaitForFuture(iid_impl->GetId(), &fetch_future_sem_, &app_instance_id_,
112115
"Get Instance Id");

0 commit comments

Comments
 (0)