Skip to content

Commit bce0a4d

Browse files
jonsimantova-maurice
authored andcommitted
Switch Dynamic Links C++/Unity to use Domain URI Prefix rather than Dynamic Links Domain, which is now marked as deprecated. This allows custom domains to be used.
Includes docs changes pointing users at how to set up a DDL custom domain. Tested via custom domain set up on this project: http://firebase/u/0/project/fdl-cd-test-1 PiperOrigin-RevId: 252880145
1 parent c5b65a9 commit bce0a4d

File tree

3 files changed

+66
-29
lines changed

3 files changed

+66
-29
lines changed

dynamic_links/src/dynamic_links_android.cc

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@
1515
#include <assert.h>
1616
#include <jni.h>
1717

18-
#include "dynamic_links/src/common.h"
19-
#include "dynamic_links/src/include/firebase/dynamic_links.h"
20-
#include "dynamic_links/src/include/firebase/dynamic_links/components.h"
21-
2218
#include "app/src/assert.h"
2319
#include "app/src/include/firebase/app.h"
2420
#include "app/src/include/firebase/version.h"
2521
#include "app/src/reference_counted_future_impl.h"
2622
#include "app/src/util.h"
2723
#include "app/src/util_android.h"
24+
#include "dynamic_links/src/common.h"
25+
#include "dynamic_links/src/include/firebase/dynamic_links.h"
26+
#include "dynamic_links/src/include/firebase/dynamic_links/components.h"
2827

2928
namespace firebase {
3029
namespace dynamic_links {
@@ -72,7 +71,7 @@ METHOD_LOOKUP_DEFINITION(dlink,
7271
X(SetLink, "setLink", \
7372
"(Landroid/net/Uri;)" \
7473
"Lcom/google/firebase/dynamiclinks/DynamicLink$Builder;"), \
75-
X(SetDynamicLinkDomain, "setDynamicLinkDomain", \
74+
X(SetDomainUriPrefix, "setDomainUriPrefix", \
7675
"(Ljava/lang/String;)" \
7776
"Lcom/google/firebase/dynamiclinks/DynamicLink$Builder;"), \
7877
X(SetAndroidParameters, "setAndroidParameters", \
@@ -679,10 +678,12 @@ static jobject PopulateLinkBuilder(JNIEnv* jni_env,
679678
*error_out = "Link is missing.";
680679
return nullptr;
681680
}
682-
if (!components.dynamic_link_domain || !*components.dynamic_link_domain) {
681+
if ((!components.dynamic_link_domain || !*components.dynamic_link_domain) &&
682+
(!components.domain_uri_prefix || !*components.domain_uri_prefix)) {
683683
*error_out =
684-
"DynamicLinkComponents.dynamic_link_domain "
685-
" is required and cannot be empty.";
684+
"DynamicLinkComponents.domain_uri_prefix is required and cannot be "
685+
"empty (unless you set DynamicLinkComponents.dynamic_link_domain, "
686+
"which is deprecated).";
686687
return nullptr;
687688
}
688689

@@ -699,11 +700,16 @@ static jobject PopulateLinkBuilder(JNIEnv* jni_env,
699700
return nullptr;
700701
}
701702

703+
static const char kHttpsPrefix[] = "https://";
704+
std::string domain =
705+
components.domain_uri_prefix != nullptr
706+
? components.domain_uri_prefix
707+
: std::string(kHttpsPrefix) + components.dynamic_link_domain;
702708
link_builder = SetBuilderString(
703-
jni_env, link_builder, components.dynamic_link_domain,
704-
dlink_builder::GetMethodId(dlink_builder::kSetDynamicLinkDomain));
709+
jni_env, link_builder, domain.c_str(),
710+
dlink_builder::GetMethodId(dlink_builder::kSetDomainUriPrefix));
705711
if (util::GetExceptionMessage(jni_env, error_out)) {
706-
// setDynamicLinkDomain() threw an exception.
712+
// setDomainUriPrefix() threw an exception.
707713
jni_env->DeleteLocalRef(link_builder);
708714
return nullptr;
709715
}

dynamic_links/src/dynamic_links_ios.mm

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,23 @@ static void AddUrlEncodingWarning(
112112
generated_link->error = std::string("Link ") + kMissingFieldPostfix;
113113
return nil;
114114
}
115-
if (!components.dynamic_link_domain) {
116-
generated_link->error = std::string("Domain ") + kMissingFieldPostfix;
115+
if ((!components.dynamic_link_domain || !*components.dynamic_link_domain) &&
116+
(!components.domain_uri_prefix || !*components.domain_uri_prefix)) {
117+
generated_link->error =
118+
std::string("Domain URI Prefix or Dynamic Links Domain ") + kMissingFieldPostfix;
117119
return nil;
118120
}
119121
NSURL *link_url = EncodeUrlFromString(components.link);
120122
if (!link_url) {
121123
generated_link->error = kUrlEncodingError;
122124
return nil;
123125
}
124-
// If components.dynamic_link_domain doesn't start with "https://", add it.
125126
static const char kHttpsPrefix[] = "https://";
126-
static const size_t kHttpsPrefixLength = sizeof(kHttpsPrefix) - 1;
127-
std::string dynamic_link_domain =
128-
strncmp(components.dynamic_link_domain, kHttpsPrefix, kHttpsPrefixLength) == 0
129-
? components.dynamic_link_domain
130-
: std::string(kHttpsPrefix) + components.dynamic_link_domain;
127+
std::string domain = components.domain_uri_prefix != nullptr ? components.domain_uri_prefix
128+
: std::string(kHttpsPrefix) + components.dynamic_link_domain;
131129
FIRDynamicLinkComponents* fir_components =
132130
[FIRDynamicLinkComponents componentsWithLink:link_url
133-
domainURIPrefix:@(dynamic_link_domain.c_str())];
131+
domainURIPrefix:@(domain.c_str())];
134132
{
135133
auto* cpp_params = components.google_analytics_parameters;
136134
if (cpp_params) {

dynamic_links/src/include/firebase/dynamic_links/components.h

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef FIREBASE_DYNAMIC_LINKS_CLIENT_CPP_SRC_INCLUDE_FIREBASE_DYNAMIC_LINKS_COMPONENTS_H_
1616
#define FIREBASE_DYNAMIC_LINKS_CLIENT_CPP_SRC_INCLUDE_FIREBASE_DYNAMIC_LINKS_COMPONENTS_H_
1717

18+
#include <cstring>
1819
#include <string>
1920
#include <vector>
2021

@@ -230,12 +231,23 @@ struct DynamicLinkComponents {
230231
/// URL, be properly URL-encoded, and use the HTTP or HTTPS scheme.
231232
/// Note, this field is required.
232233
const char* link;
233-
/// The domain (of the form "xyz.app.goo.gl") to use for this Dynamic Link.
234+
/// Deprecated: The domain (of the form "xyz.app.goo.gl") to use for this
235+
/// Dynamic Link.
234236
///
235-
/// You can find this value in the Dynamic Links section of the Firebase
237+
/// @deprecated The dynamic_link_domain field is deprecated. Use
238+
/// domain_uri_prefix instead, which also supports custom domains.
239+
FIREBASE_DEPRECATED const char* dynamic_link_domain;
240+
/// The domain (of the form "https://xyz.app.goo.gl") to use for this Dynamic
241+
/// Link. You can find this value in the Dynamic Links section of the Firebase
236242
/// console.
243+
///
244+
/// If you have set up custom domains on your project, set this to your
245+
/// project's custom domain as listed in the Firebase console.
246+
///
247+
/// Only https:// links are supported.
248+
///
237249
/// Note, this field is required.
238-
const char* dynamic_link_domain;
250+
const char* domain_uri_prefix;
239251
/// The Google Analytics parameters.
240252
GoogleAnalyticsParameters* google_analytics_parameters;
241253
/// The iOS parameters.
@@ -251,6 +263,7 @@ struct DynamicLinkComponents {
251263
DynamicLinkComponents()
252264
: link(nullptr),
253265
dynamic_link_domain(nullptr),
266+
domain_uri_prefix(nullptr),
254267
google_analytics_parameters(nullptr),
255268
ios_parameters(nullptr),
256269
itunes_connect_analytics_parameters(nullptr),
@@ -260,17 +273,37 @@ struct DynamicLinkComponents {
260273
/// Constructor that initializes with the given link and domain.
261274
///
262275
/// @param link_ The link your app will open.
263-
/// @param dynamic_link_domain_ The domain (of the form "xyz.app.goo.gl") to
264-
/// use for this Dynamic Link. You can find this value in the Dynamic Links
265-
/// section of the Firebase console.
266-
DynamicLinkComponents(const char* link_, const char* dynamic_link_domain_)
276+
/// @param domain_uri_prefix_ The domain (of the form
277+
/// "https://xyz.app.goo.gl") to use for this Dynamic Link. You can find this
278+
/// value in the Dynamic Links section of the Firebase console. If you have
279+
/// set up custom domains on your project, set this to your project's custom
280+
/// domain as listed in the Firebase console. Note: If you do not specify
281+
/// "https://" as the URI scheme, it will be added.
282+
DynamicLinkComponents(const char* link_, const char* domain_uri_prefix_)
267283
: link(link_),
268-
dynamic_link_domain(dynamic_link_domain_),
284+
dynamic_link_domain(nullptr),
285+
domain_uri_prefix(domain_uri_prefix_),
269286
google_analytics_parameters(nullptr),
270287
ios_parameters(nullptr),
271288
itunes_connect_analytics_parameters(nullptr),
272289
android_parameters(nullptr),
273-
social_meta_tag_parameters(nullptr) {}
290+
social_meta_tag_parameters(nullptr) {
291+
// For backwards compatibility with dynamic_link_domain, if
292+
// domain_uri_prefix doesn't start with "https://", add it.
293+
static const char kHttpsPrefix[] = "https://";
294+
static const size_t kHttpsPrefixLength = sizeof(kHttpsPrefix) - 1;
295+
if (strncmp(domain_uri_prefix, kHttpsPrefix, kHttpsPrefixLength) != 0) {
296+
domain_uri_prefix_with_scheme =
297+
std::string(kHttpsPrefix) + domain_uri_prefix;
298+
domain_uri_prefix = domain_uri_prefix_with_scheme.c_str();
299+
}
300+
}
301+
302+
#ifndef INTERNAL_EXPERIMENTAL
303+
304+
private:
305+
#endif // INTERNAL_EXPERIMENTAL
306+
std::string domain_uri_prefix_with_scheme;
274307
};
275308

276309
/// Creates a long Dynamic Link from the given parameters.

0 commit comments

Comments
 (0)