Skip to content

Commit 3f79c78

Browse files
authored
Desktop implementation of platform logging v2 (#1089)
Implement Heartbeat controller and storage. Integrate with firestore and auth.
1 parent 28f44de commit 3f79c78

File tree

74 files changed

+2295
-702
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+2295
-702
lines changed

app/CMakeLists.txt

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ set(FLATBUFFERS_FLATC_SCHEMA_EXTRA_ARGS
6565
set(FLATC_ARGS "${FLATBUFFERS_FLATC_SCHEMA_EXTRA_ARGS}")
6666
build_flatbuffers("${CMAKE_CURRENT_LIST_DIR}/google_services.fbs"
6767
""
68-
"app_generated_includes"
68+
"app_generated_google_services_includes"
6969
"${FIREBASE_FLATBUFFERS_DEPENDENCIES}"
7070
"${FIREBASE_GEN_FILE_DIR}/app"
7171
""
@@ -169,12 +169,39 @@ if (PLATFORM STREQUAL TVOS OR PLATFORM STREQUAL SIMULATOR_TVOS)
169169
src/invites/ios/invites_ios_startup.mm)
170170
endif()
171171

172+
173+
# Flatbuffer schemas used by the desktop implementation.
174+
set(desktop_flatbuffers_schema_dir
175+
"${CMAKE_CURRENT_LIST_DIR}/src/heartbeat/schemas")
176+
set(desktop_flatbuffers_schemas
177+
"${desktop_flatbuffers_schema_dir}/logged_heartbeats.fbs")
178+
set(FLATBUFFERS_FLATC_SCHEMA_EXTRA_ARGS
179+
"--no-union-value-namespacing"
180+
"--gen-generated"
181+
"--gen-object-api"
182+
"--cpp-ptr-type" "flatbuffers::unique_ptr")
183+
# Because of a bug in the version of Flatbuffers we are pinned to,
184+
# additional flags need to be set.
185+
# also see if app_generated_includes will conflict
186+
set(FLATC_ARGS "${FLATBUFFERS_FLATC_SCHEMA_EXTRA_ARGS}")
187+
build_flatbuffers("${desktop_flatbuffers_schemas}"
188+
"${CMAKE_CURRENT_LIST_DIR}"
189+
"app_generated_includes"
190+
"${FIREBASE_FLATBUFFERS_DEPENDENCIES}"
191+
"${FIREBASE_GEN_FILE_DIR}/app"
192+
""
193+
"")
194+
172195
set(app_desktop_SRCS
173196
src/app_desktop.cc
174-
src/heartbeat_date_storage_desktop.cc
175-
src/heartbeat_info_desktop.cc
176197
src/invites/stub/invites_receiver_internal_stub.cc
177-
src/variant_util.cc)
198+
src/variant_util.cc
199+
src/heartbeat/date_provider.cc
200+
src/heartbeat/heartbeat_storage_desktop.cc
201+
src/heartbeat/heartbeat_controller_desktop.cc
202+
rest/gzipheader.cc
203+
rest/zlibwrapper.cc
204+
${FIREBASE_GEN_FILE_DIR}/app/logged_heartbeats_generated.h)
178205
if(ANDROID)
179206
set(app_platform_SRCS
180207
"${app_android_SRCS}")
@@ -244,8 +271,6 @@ set(utility_common_HDRS
244271
src/future_manager.h
245272
src/intrusive_list.h
246273
src/log.h
247-
src/heartbeat_date_storage_desktop.h
248-
src/heartbeat_info_desktop.h
249274
src/optional.h
250275
src/path.h
251276
src/pthread_condvar.h
@@ -347,6 +372,14 @@ target_include_directories(firebase_app
347372
${FLATBUFFERS_SOURCE_DIR}/include
348373
${LIBSECRET_INCLUDE_DIRS}
349374
)
375+
376+
if (DESKTOP)
377+
target_include_directories(firebase_app
378+
PRIVATE
379+
${ZLIB_SOURCE_DIR}/..
380+
${ZLIB_BINARY_DIR})
381+
endif()
382+
350383
target_compile_definitions(firebase_app
351384
PRIVATE
352385
-DINTERNAL_EXPERIMENTAL=1
@@ -390,6 +423,9 @@ elseif(IOS)
390423
FirebaseDynamicLinks
391424
FirebaseInstanceID
392425
)
426+
else()
427+
target_link_libraries(firebase_app
428+
PRIVATE zlibstatic)
393429
endif()
394430

395431
if (NOT ANDROID AND NOT IOS)

app/src/app_common.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ const char* kBuildSource = "custom_built";
169169
// clang-format=on
170170

171171
const char* kApiClientHeader = "x-firebase-client";
172+
const char* kXFirebaseGmpIdHeader = "X-Firebase-GMPID";
172173

173174
SystemLogger g_system_logger; // NOLINT
174175

app/src/app_common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ extern const char* kCppRuntimeOrStl;
4343
extern const char* kCpuArchitecture;
4444
extern const char* kBuildSource;
4545

46-
// Extended API client header for Google user agent strings.
46+
// Request headers used by platform monitoring.
4747
extern const char* kApiClientHeader;
48+
extern const char* kXFirebaseGmpIdHeader;
4849

4950
// Add an app to the set of apps.
5051
App* AddApp(App* app, std::map<std::string, InitResult>* results);

app/src/app_desktop.cc

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include "app/src/app_desktop.h"
18+
1719
#include <string.h>
1820

1921
#include <fstream>
22+
#include <memory>
23+
#include <string>
2024

2125
#include "app/src/app_common.h"
2226
#include "app/src/function_registry.h"
27+
#include "app/src/heartbeat/heartbeat_controller_desktop.h"
2328
#include "app/src/include/firebase/app.h"
2429
#include "app/src/include/firebase/internal/common.h"
2530
#include "app/src/include/firebase/version.h"
@@ -29,24 +34,6 @@
2934
namespace firebase {
3035
DEFINE_FIREBASE_VERSION_STRING(Firebase);
3136

32-
namespace internal {
33-
34-
class AppInternal {
35-
public:
36-
// A registry that modules can use to expose functions to each other, without
37-
// requiring a linkage dependency.
38-
// todo - make all the implementations use something like this, for internal
39-
// or implementation-specific code. b/70229654
40-
FunctionRegistry function_registry;
41-
};
42-
43-
// When Create() is invoked without arguments, it checks for a file named
44-
// google-services-desktop.json, to load options from. This specifies the
45-
// path to search.
46-
static std::string g_default_config_path; // NOLINT
47-
48-
} // namespace internal
49-
5037
namespace {
5138

5239
// Size is arbitrary, just making sure that there is a sane limit.
@@ -150,6 +137,15 @@ App* App::Create(const AppOptions& options, const char* name) { // NOLINT
150137
app->name_ = name;
151138
app->options_ = options_with_defaults;
152139
app = app_common::AddApp(app, &app->init_results_);
140+
app->internal_->heartbeat_controller_ =
141+
std::make_shared<heartbeat::HeartbeatController>(
142+
name, *app_common::FindAppLoggerByName(name),
143+
app->internal_->date_provider_);
144+
#ifndef SWIG
145+
// Log a heartbeat after creating an App. In the Unity SDK this will happen
146+
// at a later time, after additional user agents have been registered.
147+
app->internal_->heartbeat_controller_->LogHeartbeat();
148+
#endif // SWIG
153149
}
154150
return app;
155151
}
@@ -166,7 +162,7 @@ App* App::GetInstance(const char* name) { // NOLINT
166162
internal::FunctionRegistry* App::function_registry() {
167163
return &internal_->function_registry;
168164
}
169-
#endif
165+
#endif // INTERNAL_EXPERIMENTAL
170166

171167
void App::RegisterLibrary(const char* library, const char* version) {
172168
app_common::RegisterLibrary(library, version);
@@ -191,6 +187,21 @@ void App::SetDefaultConfigPath(const char* path) {
191187
}
192188
}
193189

190+
void App::LogHeartbeat() const {
191+
if (internal_ != nullptr && internal_->heartbeat_controller_) {
192+
internal_->heartbeat_controller_->LogHeartbeat();
193+
}
194+
}
195+
196+
std::shared_ptr<heartbeat::HeartbeatController> App::GetHeartbeatController()
197+
const {
198+
if (internal_ != nullptr) {
199+
return internal_->heartbeat_controller_;
200+
} else {
201+
return std::shared_ptr<heartbeat::HeartbeatController>();
202+
}
203+
}
204+
194205
// Desktop support is for developer workflow only, so automatic data collection
195206
// is always enabled.
196207
void App::SetDataCollectionDefaultEnabled(bool /* enabled */) {}

app/src/app_desktop.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <memory>
18+
19+
#include "app/src/app_common.h"
20+
#include "app/src/function_registry.h"
21+
#include "app/src/heartbeat/heartbeat_controller_desktop.h"
22+
#include "app/src/include/firebase/app.h"
23+
#include "app/src/include/firebase/internal/common.h"
24+
#include "app/src/include/firebase/version.h"
25+
26+
namespace firebase {
27+
// DEFINE_FIREBASE_VERSION_STRING(Firebase);
28+
29+
namespace internal {
30+
31+
class AppInternal {
32+
public:
33+
// A registry that modules can use to expose functions to each other, without
34+
// requiring a linkage dependency.
35+
// todo - make all the implementations use something like this, for internal
36+
// or implementation-specific code. b/70229654
37+
FunctionRegistry function_registry;
38+
39+
// HeartbeatController provides methods to log heartbeats and fetch payloads.
40+
std::shared_ptr<heartbeat::HeartbeatController> heartbeat_controller_;
41+
42+
// Has a method to get the current date. Used by heartbeat_controller.
43+
firebase::heartbeat::DateProviderImpl date_provider_;
44+
};
45+
46+
// When Create() is invoked without arguments, it checks for a file named
47+
// google-services-desktop.json, to load options from. This specifies the
48+
// path to search.
49+
static std::string g_default_config_path; // NOLINT
50+
51+
} // namespace internal
52+
} // namespace firebase

app/src/heartbeat/date_provider.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "app/src/heartbeat/date_provider.h"
18+
19+
#include <iomanip>
20+
#include <sstream>
21+
#include <string>
22+
23+
namespace firebase {
24+
namespace heartbeat {
25+
26+
std::string DateProviderImpl::GetDate() const {
27+
std::time_t t = std::time(nullptr);
28+
// Use UTC time so that local time zone changes are ignored.
29+
std::tm* tm = std::gmtime(&t);
30+
std::ostringstream ss;
31+
// Format the date as yyyy-mm-dd, independent of locale.
32+
ss << std::put_time(tm, "%Y-%m-%d");
33+
return ss.str();
34+
}
35+
36+
} // namespace heartbeat
37+
} // namespace firebase

app/src/heartbeat/date_provider.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FIREBASE_APP_SRC_HEARTBEAT_DATE_PROVIDER_H_
18+
#define FIREBASE_APP_SRC_HEARTBEAT_DATE_PROVIDER_H_
19+
20+
#include <string>
21+
22+
namespace firebase {
23+
namespace heartbeat {
24+
25+
class DateProvider {
26+
public:
27+
virtual std::string GetDate() const = 0;
28+
};
29+
30+
class DateProviderImpl : public DateProvider {
31+
public:
32+
std::string GetDate() const override;
33+
};
34+
35+
} // namespace heartbeat
36+
} // namespace firebase
37+
38+
#endif // FIREBASE_APP_SRC_HEARTBEAT_DATE_PROVIDER_H_

0 commit comments

Comments
 (0)