|
| 1 | +// Copyright 2026 Dao Browser Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "dao/browser/telemetry/dao_telemetry_service.h" |
| 6 | + |
| 7 | +#include <utility> |
| 8 | + |
| 9 | +#include "base/functional/bind.h" |
| 10 | +#include "base/json/json_writer.h" |
| 11 | +#include "base/logging.h" |
| 12 | +#include "base/memory/raw_ptr.h" |
| 13 | +#include "base/memory/weak_ptr.h" |
| 14 | +#include "base/no_destructor.h" |
| 15 | +#include "base/values.h" |
| 16 | +#include "chrome/browser/profiles/profile.h" |
| 17 | +#include "content/public/browser/storage_partition.h" |
| 18 | +#include "net/base/load_flags.h" |
| 19 | +#include "net/traffic_annotation/network_traffic_annotation.h" |
| 20 | +#include "services/network/public/cpp/resource_request.h" |
| 21 | +#include "services/network/public/cpp/shared_url_loader_factory.h" |
| 22 | +#include "services/network/public/cpp/simple_url_loader.h" |
| 23 | +#include "services/network/public/mojom/url_response_head.mojom.h" |
| 24 | +#include "url/gurl.h" |
| 25 | + |
| 26 | +namespace dao { |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +// Tianji application tracking endpoint and identifier. See |
| 31 | +// https://tianji.dev/docs/application/tracking for the protocol. |
| 32 | +constexpr char kTianjiEventEndpoint[] = |
| 33 | + "https://app.tianji.dev/api/application/send"; |
| 34 | +constexpr char kTianjiApplicationId[] = "cmpb4zdf0he9p78rcrkg6j7vg"; |
| 35 | + |
| 36 | +} // namespace |
| 37 | + |
| 38 | +class DaoTelemetryService::Impl { |
| 39 | + public: |
| 40 | + Impl() = default; |
| 41 | + ~Impl() = default; |
| 42 | + |
| 43 | + Impl(const Impl&) = delete; |
| 44 | + Impl& operator=(const Impl&) = delete; |
| 45 | + |
| 46 | + void SetProfile(Profile* profile) { profile_ = profile; } |
| 47 | + |
| 48 | + void ReportBrowserOpenedOnce() { |
| 49 | + if (browser_opened_reported_) { |
| 50 | + return; |
| 51 | + } |
| 52 | + browser_opened_reported_ = true; |
| 53 | + ReportEvent("open", base::DictValue()); |
| 54 | + } |
| 55 | + |
| 56 | + void ReportEvent(const std::string& event_name, |
| 57 | + base::DictValue event_data) { |
| 58 | + if (!profile_) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + base::DictValue payload; |
| 63 | + payload.Set("application", kTianjiApplicationId); |
| 64 | + payload.Set("name", event_name); |
| 65 | + payload.Set("data", std::move(event_data)); |
| 66 | + |
| 67 | + base::DictValue envelope; |
| 68 | + envelope.Set("type", "event"); |
| 69 | + envelope.Set("payload", std::move(payload)); |
| 70 | + |
| 71 | + std::string body; |
| 72 | + if (!base::JSONWriter::Write(envelope, &body)) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + auto request = std::make_unique<network::ResourceRequest>(); |
| 77 | + request->url = GURL(kTianjiEventEndpoint); |
| 78 | + request->method = "POST"; |
| 79 | + request->credentials_mode = network::mojom::CredentialsMode::kOmit; |
| 80 | + request->load_flags = net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE; |
| 81 | + |
| 82 | + static const net::NetworkTrafficAnnotationTag annotation = |
| 83 | + net::DefineNetworkTrafficAnnotation("dao_telemetry_tianji", R"( |
| 84 | + semantics { |
| 85 | + sender: "Dao Browser Telemetry" |
| 86 | + description: |
| 87 | + "Reports anonymous application events (browser opened, " |
| 88 | + "agent message sent, etc.) to the Tianji application " |
| 89 | + "tracking endpoint so the Dao Browser team can understand " |
| 90 | + "aggregate feature usage." |
| 91 | + trigger: |
| 92 | + "Browser startup, or specific user-initiated actions like " |
| 93 | + "sending a message to the agent." |
| 94 | + data: |
| 95 | + "An application identifier, an event name (e.g. 'open'), " |
| 96 | + "and optionally a small dict of event metadata. No URLs, " |
| 97 | + "no page content, no user credentials, no PII." |
| 98 | + destination: WEBSITE |
| 99 | + } |
| 100 | + policy { |
| 101 | + cookies_allowed: NO |
| 102 | + setting: |
| 103 | + "There is no in-product toggle yet; telemetry can be " |
| 104 | + "disabled by blocking app.tianji.dev at the network layer." |
| 105 | + policy_exception_justification: |
| 106 | + "Anonymous aggregate usage metrics for product " |
| 107 | + "improvement; no user identifiers are sent." |
| 108 | + })"); |
| 109 | + |
| 110 | + std::unique_ptr<network::SimpleURLLoader> loader = |
| 111 | + network::SimpleURLLoader::Create(std::move(request), annotation); |
| 112 | + loader->SetTimeoutDuration(base::Seconds(10)); |
| 113 | + loader->AttachStringForUpload(body, "application/json"); |
| 114 | + |
| 115 | + network::SimpleURLLoader* loader_ptr = loader.get(); |
| 116 | + inflight_[loader_ptr] = std::move(loader); |
| 117 | + |
| 118 | + scoped_refptr<network::SharedURLLoaderFactory> factory = |
| 119 | + profile_->GetDefaultStoragePartition() |
| 120 | + ->GetURLLoaderFactoryForBrowserProcess(); |
| 121 | + |
| 122 | + loader_ptr->DownloadToString( |
| 123 | + factory.get(), |
| 124 | + base::BindOnce(&Impl::OnRequestComplete, weak_factory_.GetWeakPtr(), |
| 125 | + loader_ptr), |
| 126 | + /*max_body_size=*/8 * 1024); |
| 127 | + } |
| 128 | + |
| 129 | + private: |
| 130 | + void OnRequestComplete(network::SimpleURLLoader* loader_ptr, |
| 131 | + std::optional<std::string> body) { |
| 132 | + inflight_.erase(loader_ptr); |
| 133 | + } |
| 134 | + |
| 135 | + raw_ptr<Profile> profile_ = nullptr; |
| 136 | + bool browser_opened_reported_ = false; |
| 137 | + std::map<network::SimpleURLLoader*, std::unique_ptr<network::SimpleURLLoader>> |
| 138 | + inflight_; |
| 139 | + base::WeakPtrFactory<Impl> weak_factory_{this}; |
| 140 | +}; |
| 141 | + |
| 142 | +// static |
| 143 | +DaoTelemetryService* DaoTelemetryService::GetInstance() { |
| 144 | + static base::NoDestructor<DaoTelemetryService> instance; |
| 145 | + return instance.get(); |
| 146 | +} |
| 147 | + |
| 148 | +DaoTelemetryService::DaoTelemetryService() : impl_(std::make_unique<Impl>()) {} |
| 149 | +DaoTelemetryService::~DaoTelemetryService() = default; |
| 150 | + |
| 151 | +void DaoTelemetryService::SetProfile(Profile* profile) { |
| 152 | + impl_->SetProfile(profile); |
| 153 | +} |
| 154 | + |
| 155 | +void DaoTelemetryService::ReportBrowserOpenedOnce() { |
| 156 | + impl_->ReportBrowserOpenedOnce(); |
| 157 | +} |
| 158 | + |
| 159 | +void DaoTelemetryService::ReportEvent(const std::string& event_name, |
| 160 | + base::DictValue event_data) { |
| 161 | + impl_->ReportEvent(event_name, std::move(event_data)); |
| 162 | +} |
| 163 | + |
| 164 | +} // namespace dao |
0 commit comments