Skip to content

Commit

Permalink
Use a static variable to store the Chrome Product Version
Browse files Browse the repository at this point in the history
The Chrome Product Version that is returned by
version_info::GetProductNameAndVersionForUserAgent is a static string
that can never change during the course of a running binary, since the
PRODUCT_VERSION it is based off of is a macro generated at build time.

Instead of recreating this string on every invocation, we store it in a
static constant.

This CL also cleans up some unnecessary namespace qualifications.

Change-Id: Ida7758f1e58c77778d4b2c0c664c85005b16a41b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3169075
Reviewed-by: Colin Blundell <blundell@chromium.org>
Commit-Queue: Ali Beyad <abeyad@chromium.org>
Cr-Commit-Position: refs/heads/main@{#923404}
  • Loading branch information
abeyad authored and Chromium LUCI CQ committed Sep 21, 2021
1 parent d85f6c0 commit 6295d78
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions components/version_info/version_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

namespace version_info {

std::string GetProductNameAndVersionForUserAgent() {
return "Chrome/" + GetVersionNumber();
const std::string& GetProductNameAndVersionForUserAgent() {
static const base::NoDestructor<std::string> product_and_version(
"Chrome/" + GetVersionNumber());
return *product_and_version;
}

std::string GetProductName() {
Expand Down
2 changes: 1 addition & 1 deletion components/version_info/version_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace version_info {

// Returns the product name and version information for UserAgent header,
// e.g. "Chrome/a.b.c.d".
std::string GetProductNameAndVersionForUserAgent();
const std::string& GetProductNameAndVersionForUserAgent();

// Returns the product name, e.g. "Chromium" or "Google Chrome".
std::string GetProductName();
Expand Down

0 comments on commit 6295d78

Please sign in to comment.