diff --git a/browser/brave_rewards/rewards_tab_helper.cc b/browser/brave_rewards/rewards_tab_helper.cc index 8b4c42909f3f..20c9320e8b1e 100644 --- a/browser/brave_rewards/rewards_tab_helper.cc +++ b/browser/brave_rewards/rewards_tab_helper.cc @@ -173,7 +173,12 @@ bool RewardsTabHelper::BrowserHasWebContents(Browser* browser) { void RewardsTabHelper::OnRewardsInitialized(RewardsService* rewards_service) { MaybeSavePublisherInfo(); + + // When Rewards is initialized for the current profile, we need to inform the + // utility process about the currently active tab so that it can start + // measuring auto-contribute correctly. if (rewards_service_) { + rewards_service_->OnShow(tab_id_); rewards_service_->OnLoad(tab_id_, GetWebContents().GetLastCommittedURL()); } } diff --git a/browser/net/brave_request_handler.cc b/browser/net/brave_request_handler.cc index ece7a15496a2..595e343794af 100644 --- a/browser/net/brave_request_handler.cc +++ b/browser/net/brave_request_handler.cc @@ -22,7 +22,6 @@ #include "brave/browser/net/brave_stp_util.h" #include "brave/browser/net/decentralized_dns_network_delegate_helper.h" #include "brave/browser/net/global_privacy_control_network_delegate_helper.h" -#include "brave/components/brave_rewards/browser/net/network_delegate_helper.h" #include "brave/components/brave_shields/common/features.h" #include "brave/components/brave_webtorrent/browser/buildflags/buildflags.h" #include "brave/components/constants/pref_names.h" @@ -79,9 +78,6 @@ void BraveRequestHandler::SetupCallbacks() { decentralized_dns::OnBeforeURLRequest_DecentralizedDnsPreRedirectWork); before_url_request_callbacks_.push_back(callback); - callback = base::BindRepeating(brave_rewards::OnBeforeURLRequest); - before_url_request_callbacks_.push_back(callback); - #if BUILDFLAG(ENABLE_IPFS) if (base::FeatureList::IsEnabled(ipfs::features::kIpfsFeature)) { callback = base::BindRepeating(ipfs::OnBeforeURLRequest_IPFSRedirectWork); diff --git a/chromium_presubmit_config.json5 b/chromium_presubmit_config.json5 index acb1673b06b9..bc731cd964ea 100644 --- a/chromium_presubmit_config.json5 +++ b/chromium_presubmit_config.json5 @@ -119,8 +119,6 @@ "browser/ui/webui/private_new_tab_page/brave_private_new_tab_page_browsertest\\.cc", "chromium_src/chrome/install_static/brave_install_util_unittest\\.cc", "chromium_src/components/search_engines/brave_template_url_prepopulate_data_unittest\\.cc", - "components/brave_rewards/browser/net/network_delegate_helper\\.cc", - "components/brave_rewards/browser/net/network_delegate_helper\\.h", "components/brave_rewards/browser/test/rewards_state_browsertest\\.cc", "components/brave_shields/browser/ad_block_engine\\.cc", "components/brave_shields/browser/https_everywhere_service\\.cc", diff --git a/components/brave_rewards/browser/BUILD.gn b/components/brave_rewards/browser/BUILD.gn index f37941efc1fc..2e53528d3e29 100644 --- a/components/brave_rewards/browser/BUILD.gn +++ b/components/brave_rewards/browser/BUILD.gn @@ -1,3 +1,8 @@ +# Copyright (c) 2023 The Brave Authors. All rights reserved. +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this file, +# You can obtain one at https://mozilla.org/MPL/2.0/. + import("//brave/components/ipfs/buildflags/buildflags.gni") import("//brave/vendor/bat-native-ledger/config.gni") import("//extensions/buildflags/buildflags.gni") @@ -13,8 +18,6 @@ static_library("browser") { "diagnostic_log.cc", "diagnostic_log.h", "logging.h", - "net/network_delegate_helper.cc", - "net/network_delegate_helper.h", "publisher_utils.cc", "publisher_utils.h", "rewards_notification_service.cc", diff --git a/components/brave_rewards/browser/net/network_delegate_helper.cc b/components/brave_rewards/browser/net/network_delegate_helper.cc deleted file mode 100644 index 5993e2767dd7..000000000000 --- a/components/brave_rewards/browser/net/network_delegate_helper.cc +++ /dev/null @@ -1,69 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include "brave/components/brave_rewards/browser/net/network_delegate_helper.h" - -#include -#include - -#include "brave/components/brave_rewards/browser/rewards_service.h" -#include "brave/browser/brave_rewards/rewards_service_factory.h" -#include "chrome/browser/profiles/profile.h" -#include "components/sessions/content/session_tab_helper.h" -#include "content/public/browser/browser_task_traits.h" -#include "content/public/browser/browser_thread.h" -#include "content/public/browser/render_frame_host.h" -#include "content/public/browser/web_contents.h" -#include "url/gurl.h" - -namespace brave_rewards { - -namespace { - -void DispatchOnUI( - const std::string post_data, - const GURL url, - const GURL first_party_url, - const std::string referrer, - int frame_tree_node_id) { - DCHECK_CURRENTLY_ON(content::BrowserThread::UI); - auto* web_contents = - content::WebContents::FromFrameTreeNodeId(frame_tree_node_id); - if (!web_contents) - return; - - auto* tab_helper = sessions::SessionTabHelper::FromWebContents(web_contents); - if (!tab_helper) - return; - - auto* rewards_service = RewardsServiceFactory::GetForProfile( - Profile::FromBrowserContext(web_contents->GetBrowserContext())); - if (rewards_service) - rewards_service->OnPostData(tab_helper->session_id(), - url, first_party_url, - GURL(referrer), post_data); -} - -} // namespace - -int OnBeforeURLRequest( - const brave::ResponseCallback& next_callback, - std::shared_ptr ctx) { - DCHECK_CURRENTLY_ON(content::BrowserThread::UI); - - if (IsMediaLink(ctx->request_url, ctx->tab_origin, ctx->referrer)) { - if (!ctx->upload_data.empty()) { - DispatchOnUI(ctx->upload_data, ctx->request_url, ctx->tab_url, - ctx->referrer.spec(), ctx->frame_tree_node_id); - } - } - - return net::OK; -} - -} // namespace brave_rewards - - - diff --git a/components/brave_rewards/browser/net/network_delegate_helper.h b/components/brave_rewards/browser/net/network_delegate_helper.h deleted file mode 100644 index 6096294ac48c..000000000000 --- a/components/brave_rewards/browser/net/network_delegate_helper.h +++ /dev/null @@ -1,18 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#ifndef BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_NET_NETWORK_HELPER_DELEGATE_H_ -#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_NET_NETWORK_HELPER_DELEGATE_H_ - -#include "brave/browser/net/url_context.h" - -namespace brave_rewards { - -int OnBeforeURLRequest( - const brave::ResponseCallback& next_callback, - std::shared_ptr ctx); - -} // namespace brave_rewards - -#endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_NET_NETWORK_HELPER_DELEGATE_H_ diff --git a/components/brave_rewards/browser/publisher_utils.cc b/components/brave_rewards/browser/publisher_utils.cc index aed695206204..f0e1c539f2a8 100644 --- a/components/brave_rewards/browser/publisher_utils.cc +++ b/components/brave_rewards/browser/publisher_utils.cc @@ -38,7 +38,10 @@ absl::optional GetPublisherIdFromURL(const GURL& url) { if (IsMediaPlatformURL(url)) { return absl::nullopt; } + return GetPublisherDomainFromURL(url); +} +absl::optional GetPublisherDomainFromURL(const GURL& url) { #if BUILDFLAG(ENABLE_IPFS) if (url.SchemeIs(ipfs::kIPNSScheme)) { std::string domain = ipfs::GetRegistryDomainFromIPNS(url); @@ -62,4 +65,12 @@ absl::optional GetPublisherIdFromURL(const GURL& url) { return domain; } +bool IsAutoContributeHandledByContentScript(const GURL& url) { +#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) + return false; +#else + return IsMediaPlatformURL(url); +#endif +} + } // namespace brave_rewards diff --git a/components/brave_rewards/browser/publisher_utils.h b/components/brave_rewards/browser/publisher_utils.h index 9aee94bbaf0e..ecbea2c1f417 100644 --- a/components/brave_rewards/browser/publisher_utils.h +++ b/components/brave_rewards/browser/publisher_utils.h @@ -19,6 +19,13 @@ namespace brave_rewards { // platform where multiple publishers can be registered. absl::optional GetPublisherIdFromURL(const GURL& url); +// Returns the publisher domain for the specified URL. For social media +// platforms, the site domain will be returned (e.g "twitter.com"). +absl::optional GetPublisherDomainFromURL(const GURL& url); + +// Returns a value indicating whether content scripting is used to measure AC. +bool IsAutoContributeHandledByContentScript(const GURL& url); + } // namespace brave_rewards #endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_PUBLISHER_UTILS_H_ diff --git a/components/brave_rewards/browser/rewards_service.h b/components/brave_rewards/browser/rewards_service.h index 366624e0f8ea..1a1ba4008a24 100644 --- a/components/brave_rewards/browser/rewards_service.h +++ b/components/brave_rewards/browser/rewards_service.h @@ -32,10 +32,6 @@ class NavigationHandle; namespace brave_rewards { -bool IsMediaLink(const GURL& url, - const GURL& first_party_url, - const GURL& referrer); - class RewardsNotificationService; class RewardsServiceObserver; @@ -203,11 +199,6 @@ class RewardsService : public KeyedService { const GURL& url, const GURL& first_party_url, const GURL& referrer) = 0; - virtual void OnPostData(SessionID tab_id, - const GURL& url, - const GURL& first_party_url, - const GURL& referrer, - const std::string& post_data) = 0; virtual void GetReconcileStamp(GetReconcileStampCallback callback) = 0; virtual void GetPublisherMinVisitTime( diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index fec5f6218cd3..e839ce833fff 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -43,6 +43,7 @@ #include "brave/components/brave_rewards/browser/android_util.h" #include "brave/components/brave_rewards/browser/diagnostic_log.h" #include "brave/components/brave_rewards/browser/logging.h" +#include "brave/components/brave_rewards/browser/publisher_utils.h" #include "brave/components/brave_rewards/browser/rewards_notification_service.h" #include "brave/components/brave_rewards/browser/rewards_notification_service_impl.h" #include "brave/components/brave_rewards/browser/rewards_p3a.h" @@ -53,7 +54,6 @@ #include "brave/components/brave_rewards/common/features.h" #include "brave/components/brave_rewards/common/pref_names.h" #include "brave/components/brave_rewards/resources/grit/brave_rewards_resources.h" -#include "brave/components/ipfs/buildflags/buildflags.h" #include "brave/components/services/bat_ledger/public/cpp/ledger_client_mojo_bridge.h" #include "brave/grit/brave_generated_resources.h" #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service_factory.h" @@ -69,7 +69,6 @@ #include "content/public/browser/service_process_host.h" #include "content/public/browser/storage_partition.h" #include "content/public/browser/url_data_source.h" -#include "net/base/registry_controlled_domains/registry_controlled_domain.h" #include "net/base/url_util.h" #include "net/http/http_status_code.h" #include "services/data_decoder/public/cpp/json_sanitizer.h" @@ -81,17 +80,9 @@ #include "ui/base/resource/resource_bundle.h" #include "ui/gfx/image/image.h" #include "url/gurl.h" -#include "url/origin.h" #include "url/url_canon_stdstring.h" #include "url/url_util.h" -#if BUILDFLAG(ENABLE_IPFS) -#include "brave/components/ipfs/ipfs_constants.h" -#include "brave/components/ipfs/ipfs_utils.h" -#endif - -using net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES; - namespace brave_rewards { static const unsigned int kRetriesCountOnNetworkChange = 1; @@ -250,29 +241,6 @@ ledger::mojom::InlineTipsPlatforms ConvertInlineTipStringToPlatform( return ledger::mojom::InlineTipsPlatforms::TWITTER; } -bool ProcessPublisher(const GURL& url) { -#if !BUILDFLAG(IS_ANDROID) - // we should always process publisher on desktop - return true; -#else - - const std::vector excluded = { - GURL("https://twitter.com") - }; - - for (const auto& domain : excluded) { - if (net::registry_controlled_domains::SameDomainOrHost( - url, - domain, - net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) { - return false; - } - } - - return true; -#endif -} - bool IsAdsOrAutoContributeEnabled(Profile* profile) { DCHECK(profile); auto* prefs = profile->GetPrefs(); @@ -302,15 +270,6 @@ void DeferCallback(base::Location location, Callback callback, Args&&... args) { } // namespace -bool IsMediaLink(const GURL& url, - const GURL& first_party_url, - const GURL& referrer) { - return ledger::Ledger::IsMediaLink(url.spec(), - first_party_url.spec(), - referrer.spec()); -} - - // read comment about file pathes at src\base\files\file_path.h #if BUILDFLAG(IS_WIN) const base::FilePath::StringType kDiagnosticLogPath(L"Rewards.log"); @@ -785,32 +744,21 @@ void RewardsServiceImpl::OnLoad(SessionID tab_id, const GURL& url) { return; } - if (!ProcessPublisher(url)) { + if (IsAutoContributeHandledByContentScript(url)) { return; } - auto origin = url.host(); - std::string baseDomain = - GetDomainAndRegistry(url.host(), INCLUDE_PRIVATE_REGISTRIES); -#if BUILDFLAG(ENABLE_IPFS) - if (baseDomain.empty()) { - baseDomain = ipfs::GetRegistryDomainFromIPNS(url); - if (!baseDomain.empty()) { - origin = baseDomain; - } - } -#endif - if (baseDomain == "") + auto publisher_domain = GetPublisherDomainFromURL(url); + if (!publisher_domain) { return; - - const std::string publisher_url = url.scheme() + "://" + baseDomain + "/"; + } ledger::mojom::VisitDataPtr data = ledger::mojom::VisitData::New(); - data->tld = data->name = baseDomain; - data->domain = origin; + data->domain = *publisher_domain; + data->name = *publisher_domain; data->path = url.path(); data->tab_id = tab_id.id(); - data->url = publisher_url; + data->url = url.scheme() + "://" + *publisher_domain + "/"; bat_ledger_->OnLoad(std::move(data), GetCurrentTimestamp()); } @@ -854,42 +802,6 @@ void RewardsServiceImpl::OnBackground(SessionID tab_id) { bat_ledger_->OnBackground(tab_id.id(), GetCurrentTimestamp()); } -void RewardsServiceImpl::OnPostData(SessionID tab_id, - const GURL& url, - const GURL& first_party_url, - const GURL& referrer, - const std::string& post_data) { - if (!Connected()) { - return; - } - - if (!ProcessPublisher(url)) { - return; - } - - std::string output; - url::RawCanonOutputW<1024> canonOutput; - url::DecodeURLEscapeSequences(post_data.c_str(), - post_data.length(), - url::DecodeURLMode::kUTF8OrIsomorphic, - &canonOutput); - output = base::UTF16ToUTF8(base::StringPiece16(canonOutput.data(), - canonOutput.length())); - - if (output.empty()) - return; - - ledger::mojom::VisitDataPtr data = ledger::mojom::VisitData::New(); - data->path = url.spec(), - data->tab_id = tab_id.id(); - - bat_ledger_->OnPostData(url.spec(), - first_party_url.spec(), - referrer.spec(), - output, - std::move(data)); -} - void RewardsServiceImpl::OnXHRLoad(SessionID tab_id, const GURL& url, const GURL& first_party_url, @@ -898,7 +810,8 @@ void RewardsServiceImpl::OnXHRLoad(SessionID tab_id, return; } - if (!ProcessPublisher(url)) { + if (IsAutoContributeHandledByContentScript(url) || + !GetPublisherDomainFromURL(url)) { return; } @@ -1816,24 +1729,13 @@ void RewardsServiceImpl::GetPublisherActivityFromUrl( const std::string& favicon_url, const std::string& publisher_blob) { GURL parsed_url(url); - - if (!parsed_url.is_valid() || !ProcessPublisher(parsed_url)) { + if (!parsed_url.is_valid() || + IsAutoContributeHandledByContentScript(parsed_url)) { return; } - auto origin = url::Origin::Create(parsed_url).Serialize(); - std::string baseDomain = - GetDomainAndRegistry(parsed_url.host(), INCLUDE_PRIVATE_REGISTRIES); - std::string path = parsed_url.has_path() ? parsed_url.PathForRequest() : ""; -#if BUILDFLAG(ENABLE_IPFS) - if (baseDomain.empty()) { - baseDomain = ipfs::GetRegistryDomainFromIPNS(parsed_url); - if (!baseDomain.empty()) { - origin = parsed_url.scheme() + "://" + baseDomain + "/"; - } - } -#endif - if (baseDomain == "") { + auto publisher_domain = GetPublisherDomainFromURL(parsed_url); + if (!publisher_domain) { ledger::mojom::PublisherInfoPtr info; OnPanelPublisherInfo(ledger::mojom::Result::NOT_FOUND, std::move(info), windowId); @@ -1845,15 +1747,14 @@ void RewardsServiceImpl::GetPublisherActivityFromUrl( } ledger::mojom::VisitDataPtr visit_data = ledger::mojom::VisitData::New(); - visit_data->domain = visit_data->name = baseDomain; - visit_data->path = path; - visit_data->url = origin; + visit_data->domain = *publisher_domain; + visit_data->name = *publisher_domain; + visit_data->path = parsed_url.has_path() ? parsed_url.PathForRequest() : ""; + visit_data->url = parsed_url.scheme() + "://" + *publisher_domain + "/"; visit_data->favicon_url = favicon_url; - bat_ledger_->GetPublisherActivityFromUrl( - windowId, - std::move(visit_data), - publisher_blob); + bat_ledger_->GetPublisherActivityFromUrl(windowId, std::move(visit_data), + publisher_blob); } void RewardsServiceImpl::OnPanelPublisherInfo( diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index ac4a3f3a501a..bdf41d9d47af 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -164,11 +164,6 @@ class RewardsServiceImpl : public RewardsService, const GURL& url, const GURL& first_party_url, const GURL& referrer) override; - void OnPostData(SessionID tab_id, - const GURL& url, - const GURL& first_party_url, - const GURL& referrer, - const std::string& post_data) override; std::string URIEncode(const std::string& value) override; void GetReconcileStamp(GetReconcileStampCallback callback) override; void GetAutoContributeEnabled( diff --git a/components/services/bat_ledger/bat_ledger_impl.cc b/components/services/bat_ledger/bat_ledger_impl.cc index c1c9f713cd6c..7fd62f6a53b1 100644 --- a/components/services/bat_ledger/bat_ledger_impl.cc +++ b/components/services/bat_ledger/bat_ledger_impl.cc @@ -122,15 +122,6 @@ void BatLedgerImpl::OnBackground(uint32_t tab_id, uint64_t current_time) { ledger_->OnBackground(tab_id, current_time); } -void BatLedgerImpl::OnPostData(const std::string& url, - const std::string& first_party_url, - const std::string& referrer, - const std::string& post_data, - ledger::mojom::VisitDataPtr visit_data) { - ledger_->OnPostData( - url, first_party_url, referrer, post_data, std::move(visit_data)); -} - void BatLedgerImpl::OnXHRLoad( uint32_t tab_id, const std::string& url, diff --git a/components/services/bat_ledger/bat_ledger_impl.h b/components/services/bat_ledger/bat_ledger_impl.h index de51aad1e6a0..a49e81b697f2 100644 --- a/components/services/bat_ledger/bat_ledger_impl.h +++ b/components/services/bat_ledger/bat_ledger_impl.h @@ -62,12 +62,6 @@ class BatLedgerImpl : void OnHide(uint32_t tab_id, uint64_t current_time) override; void OnForeground(uint32_t tab_id, uint64_t current_time) override; void OnBackground(uint32_t tab_id, uint64_t current_time) override; - - void OnPostData(const std::string& url, - const std::string& first_party_url, - const std::string& referrer, - const std::string& post_data, - ledger::mojom::VisitDataPtr visit_data) override; void OnXHRLoad(uint32_t tab_id, const std::string& url, const base::flat_map& parts, diff --git a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom index 3397e464222e..aee52172f20f 100644 --- a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom +++ b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom @@ -50,12 +50,6 @@ interface BatLedger { OnHide(uint32 tab_id, uint64 current_time); OnForeground(uint32 tab_id, uint64 current_time); OnBackground(uint32 tab_id, uint64 current_time); - - OnPostData(string url, - string first_party_url, - string referrer, - string post_data, - ledger.mojom.VisitData visit_data); OnXHRLoad(uint32 tab_id, string url, map parts, diff --git a/ios/browser/api/ledger/brave_ledger.h b/ios/browser/api/ledger/brave_ledger.h index cb5998c20b84..4a20ca9178ef 100644 --- a/ios/browser/api/ledger/brave_ledger.h +++ b/ios/browser/api/ledger/brave_ledger.h @@ -243,10 +243,6 @@ OBJC_EXPORT #pragma mark - Misc -+ (bool)isMediaURL:(NSURL*)url - firstPartyURL:(nullable NSURL*)firstPartyURL - referrerURL:(nullable NSURL*)referrerURL; - - (void)rewardsInternalInfo: (void (^)(LedgerRewardsInternalsInfo* _Nullable info))completion; @@ -270,12 +266,6 @@ OBJC_EXPORT firstPartyURL:(NSURL*)firstPartyURL referrerURL:(nullable NSURL*)referrerURL; -- (void)reportPostData:(NSData*)postData - url:(NSURL*)url - tabId:(UInt32)tabId - firstPartyURL:(NSURL*)firstPartyURL - referrerURL:(nullable NSURL*)referrerURL; - /// Report that a tab with a given id navigated or was closed by the user - (void)reportTabNavigationOrClosedWithTabId:(UInt32)tabId NS_SWIFT_NAME(reportTabNavigationOrClosed(tabId:)); diff --git a/ios/browser/api/ledger/brave_ledger.mm b/ios/browser/api/ledger/brave_ledger.mm index 70926378ce4f..da1745fc9e2f 100644 --- a/ios/browser/api/ledger/brave_ledger.mm +++ b/ios/browser/api/ledger/brave_ledger.mm @@ -1063,17 +1063,6 @@ - (void)onReconcileComplete:(ledger::mojom::Result)result #pragma mark - Misc -+ (bool)isMediaURL:(NSURL*)url - firstPartyURL:(NSURL*)firstPartyURL - referrerURL:(NSURL*)referrerURL { - std::string referrer = - referrerURL != nil ? base::SysNSStringToUTF8(referrerURL.absoluteString) - : ""; - return ledger::Ledger::IsMediaLink( - base::SysNSStringToUTF8(url.absoluteString), - base::SysNSStringToUTF8(firstPartyURL.absoluteString), referrer); -} - - (void)rewardsInternalInfo: (void (^)(LedgerRewardsInternalsInfo* _Nullable info))completion { ledger->GetRewardsInternalsInfo( @@ -1157,7 +1146,7 @@ - (void)reportLoadedPageWithURL:(NSURL*)url tabId:(UInt32)tabId { const std::string publisher_url = origin.scheme() + "://" + baseDomain + "/"; ledger::mojom::VisitDataPtr data = ledger::mojom::VisitData::New(); - data->tld = data->name = baseDomain; + data->name = baseDomain; data->domain = origin.host(); data->path = parsedUrl.path(); data->tab_id = tabId; @@ -1198,39 +1187,6 @@ - (void)reportXHRLoad:(NSURL*)url partsMap, fpu, ref, std::move(visit)); } -- (void)reportPostData:(NSData*)postData - url:(NSURL*)url - tabId:(UInt32)tabId - firstPartyURL:(NSURL*)firstPartyURL - referrerURL:(NSURL*)referrerURL { - if (!self.initialized) { - return; - } - - GURL parsedUrl(base::SysNSStringToUTF8(url.absoluteString)); - if (!parsedUrl.is_valid()) { - return; - } - - const auto postDataString = [[[NSString alloc] - initWithData:postData - encoding:NSUTF8StringEncoding] stringByRemovingPercentEncoding]; - - auto visit = ledger::mojom::VisitData::New(); - visit->path = parsedUrl.spec(); - visit->tab_id = tabId; - - std::string ref = referrerURL != nil - ? base::SysNSStringToUTF8(referrerURL.absoluteString) - : ""; - std::string fpu = firstPartyURL != nil - ? base::SysNSStringToUTF8(firstPartyURL.absoluteString) - : ""; - - ledger->OnPostData(parsedUrl.spec(), fpu, ref, - base::SysNSStringToUTF8(postDataString), std::move(visit)); -} - - (void)reportTabNavigationOrClosedWithTabId:(UInt32)tabId { if (!self.initialized) { return; diff --git a/vendor/bat-native-ledger/BUILD.gn b/vendor/bat-native-ledger/BUILD.gn index 1e633d0e9086..991a8e65e194 100644 --- a/vendor/bat-native-ledger/BUILD.gn +++ b/vendor/bat-native-ledger/BUILD.gn @@ -450,12 +450,6 @@ source_set("ledger") { "src/bat/ledger/internal/legacy/media/helper.h", "src/bat/ledger/internal/legacy/media/media.cc", "src/bat/ledger/internal/legacy/media/media.h", - "src/bat/ledger/internal/legacy/media/reddit.cc", - "src/bat/ledger/internal/legacy/media/reddit.h", - "src/bat/ledger/internal/legacy/media/twitch.cc", - "src/bat/ledger/internal/legacy/media/twitch.h", - "src/bat/ledger/internal/legacy/media/vimeo.cc", - "src/bat/ledger/internal/legacy/media/vimeo.h", "src/bat/ledger/internal/legacy/media/youtube.cc", "src/bat/ledger/internal/legacy/media/youtube.h", "src/bat/ledger/internal/legacy/publisher_settings_properties.cc", diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger.h b/vendor/bat-native-ledger/include/bat/ledger/ledger.h index 2cf19c2eeb80..5443a5d95e58 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger.h @@ -127,10 +127,6 @@ using GetDrainCallback = class LEDGER_EXPORT Ledger { public: - static bool IsMediaLink(const std::string& url, - const std::string& first_party_url, - const std::string& referrer); - Ledger() = default; virtual ~Ledger() = default; @@ -168,12 +164,6 @@ class LEDGER_EXPORT Ledger { const std::string& referrer, mojom::VisitDataPtr visit_data) = 0; - virtual void OnPostData(const std::string& url, - const std::string& first_party_url, - const std::string& referrer, - const std::string& post_data, - mojom::VisitDataPtr visit_data) = 0; - virtual void GetActivityInfoList(uint32_t start, uint32_t limit, mojom::ActivityInfoFilterPtr filter, diff --git a/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom b/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom index 2948a8b15640..3267fd05cf8e 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom +++ b/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom @@ -103,7 +103,6 @@ struct PendingContributionInfo { }; struct VisitData { - string tld; string domain; string path; uint32 tab_id; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/common/security_util.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/common/security_util.cc index a13a5bb7f1c3..e423edb1a586 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/common/security_util.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/common/security_util.cc @@ -8,8 +8,9 @@ #include #include -#include #include +#include +#include #include #include "base/base64.h" diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc index e970352eefb9..993e5a435ff4 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc @@ -341,7 +341,7 @@ void LedgerImpl::OnHide(uint32_t tab_id, uint64_t current_time) { return; } - const std::string type = media()->GetLinkType(iter->second.tld, "", ""); + const std::string type = media()->GetLinkType(iter->second.domain, "", ""); uint64_t duration = current_time - last_tab_active_time_; last_tab_active_time_ = 0; @@ -352,7 +352,7 @@ void LedgerImpl::OnHide(uint32_t tab_id, uint64_t current_time) { return; } - publisher()->SaveVisit(iter->second.tld, iter->second, duration, true, 0, + publisher()->SaveVisit(iter->second.domain, iter->second, duration, true, 0, [](mojom::Result, mojom::PublisherInfoPtr) {}); } @@ -391,40 +391,6 @@ void LedgerImpl::OnXHRLoad( media()->ProcessMedia(parts, type, std::move(visit_data)); } -void LedgerImpl::OnPostData(const std::string& url, - const std::string& first_party_url, - const std::string& referrer, - const std::string& post_data, - mojom::VisitDataPtr visit_data) { - if (!IsReady()) - return; - - std::string type = media()->GetLinkType(url, first_party_url, referrer); - - if (type.empty()) { - return; - } - - if (type == TWITCH_MEDIA_TYPE) { - std::vector> twitch_parts; - braveledger_media::GetTwitchParts(post_data, &twitch_parts); - for (auto& twitch_part : twitch_parts) { - media()->ProcessMedia(twitch_part, type, visit_data.Clone()); - } - return; - } - - if (type == VIMEO_MEDIA_TYPE) { - std::vector> parts; - braveledger_media::GetVimeoParts(post_data, &parts); - - for (auto& part : parts) { - media()->ProcessMedia(part, type, visit_data.Clone()); - } - return; - } -} - void LedgerImpl::GetActivityInfoList(uint32_t start, uint32_t limit, mojom::ActivityInfoFilterPtr filter, diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h index d0d9cbc0b881..ed63787913ee 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h @@ -122,12 +122,6 @@ class LedgerImpl : public Ledger { const std::string& referrer, mojom::VisitDataPtr visit_data) override; - void OnPostData(const std::string& url, - const std::string& first_party_url, - const std::string& referrer, - const std::string& post_data, - mojom::VisitDataPtr visit_data) override; - void GetActivityInfoList(uint32_t start, uint32_t limit, mojom::ActivityInfoFilterPtr filter, diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_helper.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_helper.cc index d6238fd58aed..9cb5c3eec7e6 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_helper.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_helper.cc @@ -27,16 +27,6 @@ namespace braveledger_bat_helper { -namespace { -constexpr char kFieldEvent[] = "event"; -constexpr char kFieldProperties[] = "properties"; -constexpr char kFieldChannel[] = "channel"; -constexpr char kFieldVod[] = "vod"; -constexpr char kFieldTime[] = "time"; -} // namespace - -///////////////////////////////////////////////////////////////////////////// - bool getJSONValue(const std::string& field_name, const std::string& json, std::string* value) { @@ -54,39 +44,6 @@ bool getJSONValue(const std::string& field_name, return false; } -bool getJSONTwitchProperties( - const std::string& json, - std::vector>* parts) { - auto result = - base::JSONReader::Read(json, base::JSON_PARSE_CHROMIUM_EXTENSIONS | - base::JSONParserOptions::JSON_PARSE_RFC); - if (!result || !result->is_list()) - return false; - - for (auto& item : result->GetList()) { - auto& dict = item.GetDict(); - base::flat_map event_map; - if (auto* value = dict.FindString(kFieldEvent)) { - event_map[kFieldEvent] = *value; - } - - if (auto* properties = dict.FindDict(kFieldProperties)) { - event_map[kFieldProperties] = ""; - - if (auto* channel = properties->FindString(kFieldChannel)) - event_map[kFieldChannel] = *channel; - - if (auto* vod = properties->FindString(kFieldVod)) - event_map[kFieldVod] = *vod; - - if (auto time = properties->FindDouble(kFieldTime)) - event_map[kFieldTime] = base::NumberToString(*time); - } - parts->push_back(std::move(event_map)); - } - return true; -} - std::string getBase64(const std::vector& in) { std::string res; size_t size = 0; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_helper.h b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_helper.h index d43cd0f7284b..875346b17a21 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_helper.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_helper.h @@ -8,12 +8,6 @@ #include #include -#include - -#include "base/containers/flat_map.h" -#include "bat/ledger/internal/legacy/wallet_info_properties.h" -#include "bat/ledger/internal/constants.h" -#include "bat/ledger/ledger.h" namespace braveledger_bat_helper { @@ -21,10 +15,6 @@ bool getJSONValue(const std::string& field_name, const std::string& json, std::string* value); -bool getJSONTwitchProperties( - const std::string& json, - std::vector>* parts); - std::string getBase64(const std::vector& in); // Sign using ed25519 algorithm diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_helper_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_helper_unittest.cc index a17b107753bb..4f92b0ad8e25 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_helper_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_helper_unittest.cc @@ -4,7 +4,6 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "bat/ledger/internal/legacy/bat_helper.h" -#include "bat/ledger/ledger.h" #include "testing/gtest/include/gtest/gtest.h" // npm run test -- brave_unit_tests --filter=BatHelperTest.* diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/github.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/github.cc index 2f5fedfb3acf..95b621c727bb 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/github.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/github.cc @@ -35,7 +35,7 @@ std::string GitHub::GetLinkType(const std::string& url) { return ""; } - return url.find(GITHUB_TLD) != std::string::npos ? GITHUB_MEDIA_TYPE : ""; + return url.find(GITHUB_DOMAIN) != std::string::npos ? GITHUB_MEDIA_TYPE : ""; } // static @@ -271,7 +271,7 @@ void GitHub::OnMediaPublisherActivity( } void GitHub::OnMediaActivityError(uint64_t window_id) { - std::string url = GITHUB_TLD; + std::string url = GITHUB_DOMAIN; std::string name = GITHUB_MEDIA_TYPE; DCHECK(!url.empty()); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/helper.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/helper.cc index 25d142cfa2fc..7e5c0f29430c 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/helper.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/helper.cc @@ -5,10 +5,6 @@ #include "bat/ledger/internal/legacy/media/helper.h" -#include "base/base64.h" -#include "base/json/json_reader.h" -#include "bat/ledger/internal/legacy/bat_helper.h" - namespace braveledger_media { std::string GetMediaKey(const std::string& mediaId, const std::string& type) { @@ -19,24 +15,6 @@ std::string GetMediaKey(const std::string& mediaId, const std::string& type) { return type + "_" + mediaId; } -void GetTwitchParts( - const std::string& query, - std::vector>* parts) { - size_t pos = query.find("data="); - - if (std::string::npos == pos || query.length() <= 5) { - return; - } - - std::string varValue = query.substr(5); - std::string decoded; - bool succeded = base::Base64Decode(varValue, &decoded); - if (succeded) { - braveledger_bat_helper::getJSONTwitchProperties(decoded, parts); - } -} - -// static std::string ExtractData(const std::string& data, const std::string& match_after, const std::string& match_until) { @@ -68,40 +46,4 @@ std::string ExtractData(const std::string& data, return match; } -void GetVimeoParts( - const std::string& query, - std::vector>* parts) { - absl::optional data = base::JSONReader::Read(query); - if (!data || !data->is_list()) { - return; - } - - for (const auto& item : data->GetList()) { - if (item.is_dict()) { - base::flat_map part; - const auto* name = item.FindStringKey("name"); - if (name) { - part.emplace("event", *name); - } - - const auto clip_id = item.FindIntKey("clip_id"); - if (clip_id) { - part.emplace("video_id", std::to_string(*clip_id)); - } - - const auto* product = item.FindStringKey("product"); - if (product) { - part.emplace("type", *product); - } - - const auto video_time = item.FindDoubleKey("video_time"); - if (video_time) { - part.emplace("time", std::to_string(*video_time)); - } - - parts->push_back(part); - } - } -} - } // namespace braveledger_media diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/helper.h b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/helper.h index e915ef3c02a3..1a8d60f54391 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/helper.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/helper.h @@ -3,31 +3,19 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef BRAVELEDGER_MEDIA_HELPER_H_ -#define BRAVELEDGER_MEDIA_HELPER_H_ +#ifndef BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_LEGACY_MEDIA_HELPER_H_ +#define BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_LEGACY_MEDIA_HELPER_H_ -#include #include -#include - -#include "base/containers/flat_map.h" namespace braveledger_media { std::string GetMediaKey(const std::string& mediaId, const std::string& type); -void GetTwitchParts( - const std::string& query, - std::vector>* parts); - std::string ExtractData(const std::string& data, const std::string& match_after, const std::string& match_until); -void GetVimeoParts( - const std::string& query, - std::vector>* parts); - } // namespace braveledger_media -#endif // BRAVELEDGER_MEDIA_HELPER_H_ +#endif // BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_LEGACY_MEDIA_HELPER_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/helper_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/helper_unittest.cc index bd3c4eb2ebef..20ca600a679a 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/helper_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/helper_unittest.cc @@ -4,9 +4,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include -#include -#include "base/containers/flat_map.h" #include "bat/ledger/internal/legacy/media/helper.h" #include "bat/ledger/ledger.h" #include "testing/gtest/include/gtest/gtest.h" @@ -32,87 +30,6 @@ TEST(MediaHelperTest, GetMediaKey) { ASSERT_EQ(result, "youtube_key"); } -TEST(MediaHelperTest, GetTwitchParts) { - std::vector> twitch_parts; - - // string is empty - braveledger_media::GetTwitchParts("", &twitch_parts); - ASSERT_EQ(twitch_parts.size(), 0u); - - // random string - braveledger_media::GetTwitchParts("this is random string", &twitch_parts); - ASSERT_EQ(twitch_parts.size(), 0u); - - const std::string post_data = - "data=W3siZXZlbnQiOiJtaW51dGUtd2F0Y2hlZCIsInByb3BlcnRpZXMiOnsiYXBwX3ZlcnN" - "pb24iOiI5LjI0LjAiLCJmbGFzaF92ZXJzaW9uIjoiMCwwLDAiLCJyZWZlcnJlcl91cmwiOiI" - "iLCJyZWZlcnJlcl9ob3N0IjoiIiwicmVmZXJyZXJfZG9tYWluIjoiIiwiYnJvd3NlciI6IjU" - "uMCAoTWFjaW50b3NoOyBJbnRlbCBNYWMgT1MgWCAxMF8xNF8zKSBBcHBsZVdlYktpdC81Mzc" - "uMzYgKEtIVE1MLCBsaWtlIEdlY2tvKSBDaHJvbWUvNzQuMC4zNzI5LjYxIFNhZmFyaS81Mzc" - "uMzYiLCJicm93c2VyX2ZhbWlseSI6ImNocm9tZSIsImJyb3dzZXJfdmVyc2lvbiI6Ijc0LjA" - "iLCJvc19uYW1lIjoibWFjT1MiLCJvc192ZXJzaW9uIjoiMTAuMTQuMyIsInVzZXJfYWdlbnQ" - "iOiJNb3ppbGxhLzUuMCAoTWFjaW50b3NoOyBJbnRlbCBNYWMgT1MgWCAxMF8xNF8zKSBBcHB" - "sZVdlYktpdC81MzcuMzYgKEtIVE1MLCBsaWtlIEdlY2tvKSBDaHJvbWUvNzQuMC4zNzI5LjY" - "xIFNhZmFyaS81MzcuMzYiLCJkZXZpY2VfaWQiOiIwOTM4MjFlYTZhYmQyN2MyIiwiZGlzdGl" - "uY3RfaWQiOiIwOTM4MjFlYTZhYmQyN2MyIiwic2Vzc2lvbl9kZXZpY2VfaWQiOiJITnpZRHp" - "3VzczWFlGUnJkRWttMkg1R1lQdktRWlFQbSIsImNsaWVudF9hcHAiOiJ0d2lsaWdodCIsImN" - "saWVudF9idWlsZF9pZCI6IjczZmQ4NTdjLWJmNWMtNGUyYS05OWQwLWFlYzBkZjNmODVlNSI" - "sInBsYXRmb3JtIjoid2ViIiwiY2hhbm5lbCI6ImRha290YXoiLCJiZW5jaG1hcmtfc2Vzc2l" - "vbl9pZCI6IlZhNjBDdjlqbFA1UmxoalY1QXpjNHFYNGhVcVk1Smt5IiwibXNlX3N1cHBvcnQ" - "iOnRydWUsImNvbnRlbnRfaWQiOiIiLCJjdXN0b21lcl9pZCI6IiIsInR1cmJvIjpmYWxzZSw" - "iYmFja2VuZCI6Im1lZGlhcGxheWVyIiwiYmFja2VuZF92ZXJzaW9uIjoiMi45LjMtNWM0NjU" - "0ODgiLCJnYW1lIjoiRm9ydG5pdGUiLCJicm9hZGNhc3Rlcl9zb2Z0d2FyZSI6InVua25vd25" - "fcnRtcCIsImxpdmUiOnRydWUsImNoYW5uZWxfaWQiOjM5Mjk4MjE4LCJjb250ZW50X21vZGU" - "iOiJsaXZlIiwicGFydG5lciI6dHJ1ZSwicXVhbGl0eSI6IkF1dG8iLCJzdHJlYW1fZm9ybWF" - "0IjoiY2h1bmtlZCIsImJhbmR3aWR0aCI6NzA3OS45ODgsImNsdXN0ZXIiOiJ2aWUwMSIsImN" - "1cnJlbnRfYml0cmF0ZSI6NzA3OS45ODgsImN1cnJlbnRfZnBzIjo2MCwiZGVjb2RlZF9mcmF" - "tZXMiOjE0NTMsImRyb3BwZWRfZnJhbWVzIjowLCJobHNfbGF0ZW5jeV9icm9hZGNhc3RlciI" - "6ODA1MiwiaGxzX2xhdGVuY3lfZW5jb2RlciI6ODAwMCwiaGxzX3RhcmdldF9kdXJhdGlvbiI" - "6NSwibWFuaWZlc3RfY2x1c3RlciI6InZpZTAxIiwibWFuaWZlc3Rfbm9kZSI6InZpZGVvLXd" - "lYXZlci52aWUwMSIsIm1hbmlmZXN0X25vZGVfdHlwZSI6IndlYXZlcl9jbHVzdGVyIiwic2V" - "ydmluZ19pZCI6ImIzYmNkN2VmNjc0ZjQxYTE5MTM0NmY5NWNiMWU0NzRiIiwibm9kZSI6InZ" - "pZGVvLWVkZ2UtNjljMTUwLnZpZTAxIiwidXNlcl9pcCI6Ijc3LjM4LjUxLjE5MSIsInZpZF9" - "kaXNwbGF5X2hlaWdodCI6MzI2LCJ2aWRfZGlzcGxheV93aWR0aCI6NTgwLCJ2aWRfaGVpZ2h" - "0Ijo5MDAsInZpZF93aWR0aCI6MTYwMCwidmlkZW9fYnVmZmVyX3NpemUiOjYuODE4NDE3OTk" - "5OTk5OTk4LCJ2b2RfY2RuX29yaWdpbiI6IiIsInZvZF9jZG5fcmVnaW9uIjoiIiwidm9sdW1" - "lIjowLjUsIm11dGVkIjpmYWxzZSwiaXNfaHR0cHMiOnRydWUsImF1dG9wbGF5ZWQiOnRydWU" - "sImF2ZXJhZ2VfYml0cmF0ZSI6MCwiYnJvYWRjYXN0X2lkIjozMzY5OTgzMjczNiwiY2FwdGl" - "vbnNfZW5hYmxlZCI6ZmFsc2UsImxhbmd1YWdlIjoiZW4tVVMiLCJtYW5pZmVzdF9icm9hZGN" - "hc3RfaWQiOjMzNjk5ODMyNzM2LCJtaW51dGVzX2xvZ2dlZCI6MSwiY2xvY2tfZHJpZnQiOi0" - "xMSwicGxheWVyX3NpemVfbW9kZSI6IiIsInF1YWxpdHlfY2hhbmdlX2NvdW50IjoyLCJzZWN" - "vbmRzX29mZnNldCI6MjUuMzU0LCJzdHJlYW1UeXBlIjoibGl2ZSIsInRhYl9zZXNzaW9uX2l" - "kIjoiZTM4MDBiZTIwOGU1OTcyZCIsInRpbWVfc3BlbnRfaGlkZGVuIjowLCJ0cmFuc2NvZGV" - "yX3R5cGUiOiIyMDE3VHJhbnNjb2RlWDI2NF9WMiIsImF1dG9fbXV0ZWQiOmZhbHNlLCJkZXZ" - "pY2VfcGl4ZWxfcmF0aW8iOjIsImVzdGltYXRlZF9iYW5kd2lkdGgiOjEyNjQ5NjQ5NywicGx" - "heWJhY2tfcmF0ZSI6MSwidHJhbnNwb3J0X3NlZ21lbnRzIjoxNSwidHJhbnNwb3J0X2ZpcnN" - "0X2J5dGVfbGF0ZW5jeSI6ODAxLCJ0cmFuc3BvcnRfc2VnbWVudF9kdXJhdGlvbiI6Mjk5OTE" - "sInRyYW5zcG9ydF9kb3dubG9hZF9kdXJhdGlvbiI6MTgwMCwidHJhbnNwb3J0X2Rvd25sb2F" - "kX2J5dGVzIjoyNzYxMDA1NiwiY29udGVudCI6ImdhbWVfYm94YXJ0IiwibWVkaXVtIjoidHd" - "pdGNoX2hvbWUiLCJjb2xsYXBzZV9sZWZ0IjpmYWxzZSwiY29sbGFwc2VfcmlnaHQiOmZhbHN" - "lLCJpdGVtX3RyYWNraW5nX2lkIjoiMDk5YmMyMGFmOGVmYTI4ZiIsIml0ZW1fcG9zaXRpb24" - "iOjAsInJvd19uYW1lIjoiVG9wR2FtZXNGb3JZb3UiLCJyb3dfcG9zaXRpb24iOjEsInRhZ19" - "maWx0ZXJfc2V0IjoiW10iLCJ0YWdfc2V0IjoiW1wiNmVhNmJjYTQtNDcxMi00YWI5LWE5MDY" - "tZTMzMzZhOWQ4MDM5XCJdIiwidGFnX3N0cmVhbWVyX3NldCI6IltdIiwiYXBwX3Nlc3Npb25" - "faWQiOiJlMzgwMGJlMjA4ZTU5NzJkIiwicGFnZV9zZXNzaW9uX2lkIjoiODkwNjc1OWZhOWE" - "wNzg4NSIsInJlZmVycmVyIjoiIiwicGxheV9zZXNzaW9uX2lkIjoiZGJXUVRwRkhSZU9jUEF" - "YV2M3bThoV3VRdmJ3OVZQR08iLCJ1cmwiOiJodHRwczovL3d3dy50d2l0Y2gudHYvZGFrb3R" - "heiIsImhvc3QiOiJ3d3cudHdpdGNoLnR2IiwiZG9tYWluIjoidHdpdGNoLnR2IiwibGF0ZW5" - "jeV9tb2RlX3RvZ2dsZSI6dHJ1ZSwibG93X2xhdGVuY3kiOmZhbHNlLCJoaWRkZW4iOmZhbHN" - "lLCJwbGF5ZXIiOiJzaXRlIiwiZW5jcnlwdGVkIjpmYWxzZSwidGltZSI6MTU1NTMxNjQ0NS4" - "0Mzd9fV0="; - - const std::vector> result = { - {{"channel", "dakotaz"}, - {"event", "minute-watched"}, - {"properties", ""}, - {"time", "1555316445.437"}}}; - - // all ok - braveledger_media::GetTwitchParts(post_data, &twitch_parts); - ASSERT_EQ(twitch_parts.size(), 1u); - ASSERT_EQ(twitch_parts, result); -} - TEST(MediaHelperTest, ExtractData) { // // string empty std::string result = braveledger_media::ExtractData("", "/", "!"); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/media.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/media.cc index 9c56b99b2e58..dbb407998731 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/media.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/media.cc @@ -3,43 +3,20 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include #include -#include "bat/ledger/internal/constants.h" +#include "base/containers/flat_map.h" #include "bat/ledger/internal/ledger_impl.h" #include "bat/ledger/internal/legacy/media/media.h" #include "bat/ledger/internal/legacy/static_values.h" #include "build/build_config.h" -using std::placeholders::_1; -using std::placeholders::_2; -using std::placeholders::_3; - -namespace { - -bool HandledByGreaselion(const std::string media_type) { -#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) - return false; -#else - return media_type == "github" || media_type == "reddit" || - media_type == "twitch" || media_type == "twitter" || - media_type == "vimeo" || media_type == "youtube"; -#endif -} - -} // namespace - namespace braveledger_media { -Media::Media(ledger::LedgerImpl* ledger): - ledger_(ledger), - media_youtube_(new braveledger_media::YouTube(ledger)), - media_twitch_(new braveledger_media::Twitch(ledger)), - media_reddit_(new braveledger_media::Reddit(ledger)), - media_vimeo_(new braveledger_media::Vimeo(ledger)), - media_github_(new braveledger_media::GitHub(ledger)) { -} // namespace braveledger_media +Media::Media(ledger::LedgerImpl* ledger) + : ledger_(ledger), + media_youtube_(new braveledger_media::YouTube(ledger)), + media_github_(new braveledger_media::GitHub(ledger)) {} Media::~Media() = default; @@ -49,20 +26,6 @@ std::string Media::GetLinkType( const std::string& first_party_url, const std::string& referrer) { std::string type = braveledger_media::YouTube::GetLinkType(url); - if (HandledByGreaselion(type)) { - return std::string(); - } - - if (type.empty()) { - type = braveledger_media::Twitch::GetLinkType( - url, - first_party_url, - referrer); - } - - if (type.empty()) { - type = braveledger_media::Vimeo::GetLinkType(url); - } if (type.empty()) { type = braveledger_media::GitHub::GetLinkType(url); @@ -78,25 +41,11 @@ void Media::ProcessMedia(const base::flat_map& parts, return; } - if (HandledByGreaselion(type)) { - return; - } - if (type == YOUTUBE_MEDIA_TYPE) { media_youtube_->ProcessMedia(parts, *visit_data); return; } - if (type == TWITCH_MEDIA_TYPE) { - media_twitch_->ProcessMedia(parts, *visit_data); - return; - } - - if (type == VIMEO_MEDIA_TYPE) { - media_vimeo_->ProcessMedia(parts); - return; - } - if (type == GITHUB_MEDIA_TYPE) { media_github_->ProcessMedia(parts, *visit_data); return; @@ -107,20 +56,8 @@ void Media::GetMediaActivityFromUrl(uint64_t window_id, ledger::mojom::VisitDataPtr visit_data, const std::string& type, const std::string& publisher_blob) { - if (HandledByGreaselion(type)) { - return; - } - if (type == YOUTUBE_MEDIA_TYPE) { media_youtube_->ProcessActivityFromUrl(window_id, *visit_data); - } else if (type == TWITCH_MEDIA_TYPE) { - media_twitch_->ProcessActivityFromUrl(window_id, - *visit_data, - publisher_blob); - } else if (type == REDDIT_MEDIA_TYPE) { - media_reddit_->ProcessActivityFromUrl(window_id, *visit_data); - } else if (type == VIMEO_MEDIA_TYPE) { - media_vimeo_->ProcessActivityFromUrl(window_id, *visit_data); } else if (type == GITHUB_MEDIA_TYPE) { media_github_->ProcessActivityFromUrl(window_id, *visit_data); } else { @@ -131,27 +68,12 @@ void Media::GetMediaActivityFromUrl(uint64_t window_id, void Media::OnMediaActivityError(ledger::mojom::VisitDataPtr visit_data, const std::string& type, uint64_t window_id) { - if (HandledByGreaselion(type)) { - return; - } - std::string url; std::string name; - if (type == TWITCH_MEDIA_TYPE) { - url = TWITCH_TLD; - name = TWITCH_MEDIA_TYPE; - } else if (type == REDDIT_MEDIA_TYPE) { - url = REDDIT_TLD; - name = REDDIT_MEDIA_TYPE; - } else if (type == VIMEO_MEDIA_TYPE) { - url = VIMEO_TLD; - name = VIMEO_MEDIA_TYPE; - } else { - if (type == YOUTUBE_MEDIA_TYPE) { - url = YOUTUBE_TLD; - name = YOUTUBE_MEDIA_TYPE; - } + if (type == YOUTUBE_MEDIA_TYPE) { + url = YOUTUBE_DOMAIN; + name = YOUTUBE_MEDIA_TYPE; } if (url.empty()) { @@ -173,10 +95,7 @@ void Media::OnMediaActivityError(ledger::mojom::VisitDataPtr visit_data, void Media::SaveMediaInfo(const std::string& type, const base::flat_map& data, ledger::PublisherInfoCallback callback) { - if (type == REDDIT_MEDIA_TYPE) { - media_reddit_->SaveMediaInfo(data, callback); - return; - } else if (type == GITHUB_MEDIA_TYPE) { + if (type == GITHUB_MEDIA_TYPE) { media_github_->SaveMediaInfo(data, callback); return; } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/media.h b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/media.h index dc8de3e8131f..a2c1934b2e96 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/media.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/media.h @@ -11,9 +11,6 @@ #include "base/containers/flat_map.h" #include "bat/ledger/internal/legacy/media/github.h" -#include "bat/ledger/internal/legacy/media/reddit.h" -#include "bat/ledger/internal/legacy/media/twitch.h" -#include "bat/ledger/internal/legacy/media/vimeo.h" #include "bat/ledger/internal/legacy/media/youtube.h" #include "bat/ledger/ledger.h" @@ -57,9 +54,6 @@ class Media { ledger::LedgerImpl* ledger_; // NOT OWNED std::unique_ptr media_youtube_; - std::unique_ptr media_twitch_; - std::unique_ptr media_reddit_; - std::unique_ptr media_vimeo_; std::unique_ptr media_github_; }; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/reddit.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/reddit.cc deleted file mode 100644 index 8239e1a052bb..000000000000 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/reddit.cc +++ /dev/null @@ -1,354 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include -#include -#include - -#include "base/strings/string_split.h" -#include "base/strings/stringprintf.h" -#include "bat/ledger/internal/ledger_impl.h" -#include "bat/ledger/internal/legacy/media/helper.h" -#include "bat/ledger/internal/legacy/media/reddit.h" -#include "bat/ledger/internal/legacy/static_values.h" -#include "bat/ledger/internal/constants.h" -#include "net/http/http_status_code.h" -#include "url/gurl.h" -#include "url/url_canon.h" - -using std::placeholders::_1; -using std::placeholders::_2; -using std::placeholders::_3; - -namespace braveledger_media { - -Reddit::Reddit(ledger::LedgerImpl* ledger): ledger_(ledger) { -} - -Reddit::~Reddit() = default; - -void Reddit::ProcessActivityFromUrl( - uint64_t window_id, - const ledger::mojom::VisitData& visit_data) { - if (visit_data.path.find("/user/") != std::string::npos) { - UserPath(window_id, visit_data); - return; - } - OnMediaActivityError(visit_data, window_id); -} - -void Reddit::OnMediaActivityError(const ledger::mojom::VisitData& visit_data, - uint64_t window_id) { - ledger::mojom::VisitData new_visit_data; - new_visit_data.domain = REDDIT_TLD; - new_visit_data.url = "https://" + (std::string)REDDIT_TLD; - new_visit_data.path = "/"; - new_visit_data.name = REDDIT_MEDIA_TYPE; - - ledger_->publisher()->GetPublisherActivityFromUrl( - window_id, ledger::mojom::VisitData::New(new_visit_data), std::string()); -} - -void Reddit::UserPath(uint64_t window_id, - const ledger::mojom::VisitData& visit_data) { - const std::string user = GetUserNameFromUrl(visit_data.path); - - if (user.empty()) { - OnMediaActivityError(visit_data, window_id); - return; - } - - const std::string media_key = (std::string)REDDIT_MEDIA_TYPE + "_" + user; - ledger_->database()->GetMediaPublisherInfo( - media_key, - std::bind(&Reddit::OnUserActivity, - this, - window_id, - visit_data, - _1, - _2)); -} - -void Reddit::OnUserActivity(uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr publisher_info) { - if (!publisher_info || result == ledger::mojom::Result::NOT_FOUND) { - const std::string user_name = GetUserNameFromUrl(visit_data.path); - const std::string url = GetProfileUrl(user_name); - auto url_callback = std::bind(&Reddit::OnUserPage, - this, - window_id, - visit_data, - _1); - FetchDataFromUrl(visit_data.url, url_callback); - } else { - GetPublisherPanelInfo( - window_id, - visit_data, - publisher_info->id); - } -} - -void Reddit::OnPageDataFetched(const std::string& user_name, - ledger::PublisherInfoCallback callback, - const ledger::mojom::UrlResponse& response) { - if (response.status_code != net::HTTP_OK) { - callback(ledger::mojom::Result::TIP_ERROR, nullptr); - return; - } - - SavePublisherInfo( - 0, - user_name, - callback, - response.body); -} - -void Reddit::FetchDataFromUrl(const std::string& url, - ledger::client::LegacyLoadURLCallback callback) { - /* if user is on old reddit, sub the url to get the icon - since old reddit didn't have user icons */ - GURL reddit_url(url); - if (reddit_url.DomainIs(OLD_REDDIT_DOMAIN)) { - // Canonicalize away 'old.reddit.com' and replace with 'www.reddit.com'. - std::string new_host = reddit_url.host(); - new_host.replace(0, 3, "www"); - GURL::Replacements replacements; - replacements.SetHostStr(new_host); - reddit_url = reddit_url.ReplaceComponents(replacements); - } - - auto request = ledger::mojom::UrlRequest::New(); - request->url = reddit_url.spec(); - request->skip_log = true; - ledger_->LoadURL(std::move(request), callback); -} - -// static -std::string Reddit::GetUserNameFromUrl(const std::string& path) { - if (path.empty()) { - return std::string(); - } - - const std::vector parts = base::SplitString( - path, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); - - if (parts.size() > 1) { - return parts.at(1); - } - - return std::string(); -} - -// static -std::string Reddit::GetProfileUrl(const std::string& screen_name) { - if (screen_name.empty()) { - return std::string(); - } - const std::string url_part = "https://" + (std::string)REDDIT_TLD + - "/user/%s/"; - return base::StringPrintf(url_part.c_str(), screen_name.c_str()); -} - -void Reddit::GetPublisherPanelInfo(uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - const std::string& publisher_key) { - auto filter = ledger_->publisher()->CreateActivityFilter( - publisher_key, ledger::mojom::ExcludeFilter::FILTER_ALL, false, - ledger_->state()->GetReconcileStamp(), true, false); - ledger_->database()->GetPanelPublisherInfo(std::move(filter), - std::bind(&Reddit::OnPublisherPanelInfo, - this, - window_id, - visit_data, - publisher_key, - _1, - _2)); -} - -void Reddit::OnPublisherPanelInfo(uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - const std::string& publisher_key, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr info) { - if (!info || result == ledger::mojom::Result::NOT_FOUND) { - auto url_callback = std::bind(&Reddit::OnUserPage, - this, - window_id, - visit_data, - _1); - - FetchDataFromUrl(visit_data.url, url_callback); - } else { - ledger_->ledger_client()->OnPanelPublisherInfo( - result, - std::move(info), - window_id); - } -} - -// static -std::string Reddit::GetUserId(const std::string& response) { - if (response.empty()) { - return std::string(); - } - const std::string pattern = braveledger_media::ExtractData( - response, "hideFromRobots\":", "\"isEmployee\""); - std::string id = braveledger_media::ExtractData( - pattern, "\"id\":\"t2_", "\""); - - if (id.empty()) { - id = braveledger_media::ExtractData( - response, "target_fullname\": \"t2_", "\""); // old reddit - } - return id; -} - -// static -std::string Reddit::GetPublisherName(const std::string& response) { - if (response.empty()) { - return std::string(); - } - - std::string user_name(braveledger_media::ExtractData( - response, "username\":\"", "\"")); - - if (user_name.empty()) { - user_name = braveledger_media::ExtractData( - response, "target_name\": \"", "\""); // old reddit - } - return user_name; -} - -void Reddit::OnRedditSaved(ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr publisher_info) {} - -void Reddit::OnUserPage(uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - const ledger::mojom::UrlResponse& response) { - if (response.status_code != net::HTTP_OK) { - OnMediaActivityError(visit_data, window_id); - return; - } - - const std::string user_name = GetUserNameFromUrl(visit_data.path); - SavePublisherInfo( - window_id, - user_name, - std::bind(&Reddit::OnRedditSaved, - this, - _1, - _2), - response.body); -} - -// static -std::string Reddit::GetPublisherKey(const std::string& key) { - if (key.empty()) { - return std::string(); - } - return (std::string)REDDIT_MEDIA_TYPE + "#channel:" + key; -} - -// static -std::string Reddit::GetProfileImageUrl(const std::string& response) { - if (response.empty()) { - return std::string(); - } - - const std::string image_url(braveledger_media::ExtractData( - response, "accountIcon\":\"", "?")); - return image_url; // old reddit does not use account icons -} - -void Reddit::OnMediaPublisherInfo( - const std::string& user_name, - ledger::PublisherInfoCallback callback, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr publisher_info) { - if (result != ledger::mojom::Result::LEDGER_OK && - result != ledger::mojom::Result::NOT_FOUND) { - callback(ledger::mojom::Result::LEDGER_ERROR, nullptr); - return; - } - GURL url(REDDIT_USER_URL + ledger_->ledger_client()->URIEncode(user_name)); - if (!url.is_valid()) { - callback(ledger::mojom::Result::TIP_ERROR, nullptr); - return; - } - - if (!publisher_info || result == ledger::mojom::Result::NOT_FOUND) { - FetchDataFromUrl(url.spec(), - std::bind(&Reddit::OnPageDataFetched, - this, - user_name, - callback, - _1)); - } else { - callback(result, std::move(publisher_info)); - } -} - -void Reddit::SavePublisherInfo( - uint64_t window_id, - const std::string& user_name, - ledger::PublisherInfoCallback callback, - const std::string& data) { - const std::string user_id = GetUserId(data); - const std::string publisher_key = GetPublisherKey(user_id); - const std::string media_key = GetMediaKey(user_name, REDDIT_MEDIA_TYPE); - if (publisher_key.empty()) { - callback(ledger::mojom::Result::LEDGER_ERROR, nullptr); - BLOG(0, "Publisher key is missing"); - return; - } - - const std::string url = GetProfileUrl(user_name); - const std::string favicon_url = GetProfileImageUrl(data); - - ledger::mojom::VisitDataPtr visit_data = ledger::mojom::VisitData::New(); - visit_data->provider = REDDIT_MEDIA_TYPE; - visit_data->url = url; - visit_data->favicon_url = favicon_url; - visit_data->name = user_name; - - ledger_->publisher()->SaveVisit( - publisher_key, - *visit_data, - 0, - true, - window_id, - callback); - - if (!media_key.empty()) { - ledger_->database()->SaveMediaPublisherInfo( - media_key, publisher_key, [](const ledger::mojom::Result) {}); - } -} - -void Reddit::SaveMediaInfo( - const base::flat_map& data, - ledger::PublisherInfoCallback callback) { - auto user_name = data.find("user_name"); - if (user_name == data.end()) { - callback(ledger::mojom::Result::LEDGER_ERROR, nullptr); - return; - } - - const std::string media_key = - braveledger_media::GetMediaKey(user_name->second, REDDIT_MEDIA_TYPE); - - ledger_->database()->GetMediaPublisherInfo( - media_key, - std::bind(&Reddit::OnMediaPublisherInfo, - this, - user_name->second, - callback, - _1, - _2)); -} - -} // namespace braveledger_media diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/reddit.h b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/reddit.h deleted file mode 100644 index 4cddf5387e95..000000000000 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/reddit.h +++ /dev/null @@ -1,109 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#ifndef BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_LEGACY_MEDIA_REDDIT_H_ -#define BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_LEGACY_MEDIA_REDDIT_H_ - -#include -#include - -#include "base/containers/flat_map.h" -#include "base/gtest_prod_util.h" -#include "bat/ledger/internal/legacy/media/helper.h" -#include "bat/ledger/ledger.h" - -namespace ledger { -class LedgerImpl; -} - -namespace braveledger_media { - -class Reddit { - public: - explicit Reddit(ledger::LedgerImpl* ledger); - - ~Reddit(); - - void ProcessActivityFromUrl(uint64_t window_id, - const ledger::mojom::VisitData& visit_data); - - void SaveMediaInfo( - const base::flat_map& data, - ledger::PublisherInfoCallback callback); - - private: - void OnMediaActivityError(const ledger::mojom::VisitData& visit_data, - uint64_t window_id); - - void UserPath(uint64_t window_id, const ledger::mojom::VisitData& visit_data); - - void OnUserActivity(uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr publisher_info); - - void FetchDataFromUrl(const std::string& url, - ledger::client::LegacyLoadURLCallback callback); - - void GetPublisherPanelInfo(uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - const std::string& publisher_key); - - void OnUserPage(uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - const ledger::mojom::UrlResponse& response); - - void OnPublisherPanelInfo(uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - const std::string& publisher_key, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr info); - - void OnMediaPublisherInfo(const std::string& user_name, - ledger::PublisherInfoCallback callback, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr publisher_info); - - void SavePublisherInfo( - uint64_t window_id, - const std::string& user_name, - ledger::PublisherInfoCallback callback, - const std::string& data); - - void OnRedditSaved(ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr publisher_info); - - static std::string GetUserNameFromUrl(const std::string& path); - - static std::string GetProfileUrl(const std::string& screen_name); - - static std::string GetUserId(const std::string& response); - - static std::string GetPublisherName(const std::string& response); - - static std::string GetPublisherKey(const std::string& key); - - static std::string GetProfileImageUrl(const std::string& response); - - void OnPageDataFetched(const std::string& user_name, - ledger::PublisherInfoCallback callback, - const ledger::mojom::UrlResponse& response); - - ledger::LedgerImpl* ledger_; // NOT OWNED - - // For testing purposes - friend class MediaRedditTest; - FRIEND_TEST_ALL_PREFIXES(MediaRedditTest, GetProfileUrl); - FRIEND_TEST_ALL_PREFIXES(MediaRedditTest, GetProfileImageUrl); - FRIEND_TEST_ALL_PREFIXES(MediaRedditTest, GetPublisherKey); - FRIEND_TEST_ALL_PREFIXES(MediaRedditTest, GetMediaKey); - FRIEND_TEST_ALL_PREFIXES(MediaRedditTest, GetUserNameFromUrl); - FRIEND_TEST_ALL_PREFIXES(MediaRedditTest, GetUserId); - FRIEND_TEST_ALL_PREFIXES(MediaRedditTest, GetPublisherName); -}; - -} // namespace braveledger_media - -#endif // BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_LEGACY_MEDIA_REDDIT_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/reddit_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/reddit_unittest.cc deleted file mode 100644 index d1d027449350..000000000000 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/reddit_unittest.cc +++ /dev/null @@ -1,126 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include - -#include "bat/ledger/internal/legacy/media/reddit.h" -#include "bat/ledger/internal/constants.h" -#include "bat/ledger/ledger.h" -#include "testing/gtest/include/gtest/gtest.h" - -// npm run test -- brave_unit_tests --filter=MediaRedditTest.* - -namespace braveledger_media { - -class MediaRedditTest : public testing::Test { -}; - -TEST(MediaRedditTest, GetProfileUrl) { - // empty - std::string result = - braveledger_media::Reddit::GetProfileUrl(std::string()); - ASSERT_TRUE(result.empty()); - - result = braveledger_media::Reddit::GetProfileUrl("jsadler-brave"); - ASSERT_EQ(result, "https://reddit.com/user/jsadler-brave/"); -} - -TEST(MediaRedditTest, GetProfileImageUrl) { - // empty - std::string result = - braveledger_media::Reddit::GetProfileImageUrl(std::string()); - ASSERT_TRUE(result.empty()); - - result = braveledger_media::Reddit::GetProfileImageUrl( - "\"accountIcon\":\"https://www.someredditmediacdn.com/somephoto.png?" - "somequerystringparams"); - ASSERT_EQ(result, "https://www.someredditmediacdn.com/somephoto.png"); -} - -TEST(MediaRedditTest, GetPublisherKey) { - // empty - std::string result = - braveledger_media::Reddit::GetPublisherKey(std::string()); - ASSERT_TRUE(result.empty()); - - result = - braveledger_media::Reddit::GetPublisherKey("test_publisher_key"); - ASSERT_EQ(result, "reddit#channel:test_publisher_key"); -} - -TEST(MediaRedditTest, GetUserNameFromUrl) { - // empty - std::string result = - braveledger_media::Reddit::GetUserNameFromUrl(std::string()); - ASSERT_TRUE(result.empty()); - - // empty path - result = braveledger_media::Reddit:: - GetUserNameFromUrl("/"); - ASSERT_TRUE(result.empty()); - - // simple path - result = braveledger_media::Reddit:: - GetUserNameFromUrl("/jsadler-brave"); - ASSERT_TRUE(result.empty()); - - // long path - result = braveledger_media::Reddit:: - GetUserNameFromUrl("/user/jsadler-brave"); - ASSERT_EQ(result, "jsadler-brave"); -} - -TEST(MediaRedditTest, GetUserId) { - const char reddit_new[] = - "\"hideFromRobots\":false,\"id\":\"t2_78910\""; - const char reddit_old[] = - "\"target_fullname\": \"t2_123456\""; - - // empty - std::string result = - braveledger_media::Reddit::GetUserId(std::string()); - ASSERT_TRUE(result.empty()); - - // incorrect scrape - result = - braveledger_media::Reddit::GetUserId("Some random text"); - ASSERT_TRUE(result.empty()); - - // support for current Reddit - result = - braveledger_media::Reddit::GetUserId(reddit_old); - ASSERT_EQ(result, "123456"); - - // support for new Reddit - result = - braveledger_media::Reddit::GetUserId(reddit_new); - ASSERT_EQ(result, "78910"); -} - -TEST(MediaRedditTest, GetPublisherName) { - const char reddit_new[] = "\"username\":\"jsadler-brave\""; - const char reddit_old[] = "\"target_name\": \"jsadler-brave\""; - // empty - std::string result = - braveledger_media::Reddit::GetPublisherName(std::string()); - ASSERT_TRUE(result.empty()); - - // incorrect scrape - result = braveledger_media::Reddit:: - GetPublisherName("some random text"); - ASSERT_TRUE(result.empty()); - - // current reddit - result = braveledger_media::Reddit:: - GetPublisherName(reddit_new); - ASSERT_EQ(result, "jsadler-brave"); - - // old reddit - result = braveledger_media::Reddit:: - GetPublisherName(reddit_old); - ASSERT_EQ(result, "jsadler-brave"); -} - -} // namespace braveledger_media diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/twitch.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/twitch.cc deleted file mode 100644 index b6fe6a5cfc11..000000000000 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/twitch.cc +++ /dev/null @@ -1,654 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include -#include -#include -#include - -#include "base/strings/string_split.h" -#include "base/strings/string_util.h" -#include "bat/ledger/global_constants.h" -#include "bat/ledger/internal/ledger_impl.h" -#include "bat/ledger/internal/legacy/bat_helper.h" -#include "bat/ledger/internal/legacy/media/twitch.h" -#include "bat/ledger/internal/legacy/static_values.h" -#include "net/http/http_status_code.h" - -using std::placeholders::_1; -using std::placeholders::_2; -using std::placeholders::_3; - -namespace braveledger_media { - -static const std::vector _twitch_events = { - "buffer-empty", - "buffer-refill", - "video_end", - "minute-watched", - "video_pause", - "player_click_vod_seek", - "video-play", - "video_error"}; - -Twitch::Twitch(ledger::LedgerImpl* ledger): - ledger_(ledger) { -} - -Twitch::~Twitch() = default; - -// static -std::pair Twitch::GetMediaIdFromParts( - const base::flat_map& parts) { - std::string id; - std::string user_id; - base::flat_map::const_iterator iter = - parts.find("event"); - if (iter != parts.end() && parts.find("properties") != parts.end()) { - unsigned int size = _twitch_events.size(); - for (size_t i = 0; i < size; i++) { - if (iter->second == _twitch_events[i]) { - auto channel_iter = parts.find("channel"); - if (channel_iter != parts.end()) { - id = channel_iter->second; - user_id = id; - } - auto vod_iter = parts.find("vod"); - if (vod_iter != parts.end()) { - std::string idAddition(vod_iter->second); - if (idAddition.find('v') != std::string::npos) { - auto additional_ids = base::SplitString( - idAddition, - "v", - base::TRIM_WHITESPACE, - base::SPLIT_WANT_NONEMPTY); - if (additional_ids.size() == 1) { - id += "_vod_" + additional_ids[0]; - } - } - } - break; - } - } - } - return std::make_pair(id, user_id); -} - -// static -std::string Twitch::GetMediaURL(const std::string& media_id) { - std::string res; - - if (media_id.empty()) { - return std::string(); - } - - return "https://www.twitch.tv/" + media_id; -} - -// static -std::string Twitch::GetTwitchStatus( - const ledger::mojom::MediaEventInfo& old_event, - const ledger::mojom::MediaEventInfo& new_event) { - std::string status = "playing"; - - if ( - ( - new_event.event == "video_pause" && - old_event.event != "video_pause") || - // User clicked pause (we need to exclude seeking while paused) - ( - new_event.event == "video_pause" && - old_event.event == "video_pause" && - old_event.status == "playing") || - // User clicked pause as soon as he clicked play - ( - new_event.event == "player_click_vod_seek" && - old_event.status == "paused") - // Seeking a video while it is paused - ) { - status = "paused"; - } - - // User pauses a video, then seeks it and plays it again - if (new_event.event == "video_pause" && - old_event.event == "player_click_vod_seek" && - old_event.status == "paused") { - status = "playing"; - } - - return status; -} - -// static -uint64_t Twitch::GetTwitchDuration( - const ledger::mojom::MediaEventInfo& old_event, - const ledger::mojom::MediaEventInfo& new_event) { - // Remove duplicated events - if (old_event.event == new_event.event && - old_event.time == new_event.time) { - return 0; - } - - // Start event - if (new_event.event == "video-play") { - return TWITCH_MINIMUM_SECONDS; - } - - double time = 0; - std::stringstream tempTime(new_event.time); - double currentTime = 0; - tempTime >> currentTime; - std::stringstream tempOld(old_event.time); - double oldTime = 0; - tempOld >> oldTime; - - if (old_event.event == "video-play") { - time = currentTime - oldTime - TWITCH_MINIMUM_SECONDS; - } else if (new_event.event == "minute-watched" || // Minute watched - new_event.event == "buffer-empty" || // Run out of buffer - new_event.event == "video_error" || // Video has some problems - new_event.event == "video_end" || // Video ended - (new_event.event == "player_click_vod_seek" && - old_event.status == "paused") || // Vod seek - ( - new_event.event == "video_pause" && - ( - ( - old_event.event != "video_pause" && - old_event.event != "player_click_vod_seek") || - old_event.status == "playing") - ) // User paused video - ) { - time = currentTime - oldTime; - } - - if (time < 0) { - return 0; - } - - // if autoplay is off and play is pressed - if (old_event.status.empty()) { - return 0; - } - - if (time > TWITCH_MAXIMUM_SECONDS_CHUNK) { - time = TWITCH_MAXIMUM_SECONDS_CHUNK; - } - - return static_cast(std::round(time)); -} - -// static -std::string Twitch::GetLinkType(const std::string& url, - const std::string& first_party_url, - const std::string& referrer) { - std::string type; - bool is_valid_twitch_path = - braveledger_bat_helper::HasSameDomainAndPath( - url, "ttvnw.net", "/v1/segment/"); - - if ( - ( - (first_party_url.find("https://www.twitch.tv/") == 0 || - first_party_url.find("https://m.twitch.tv/") == 0) || - (referrer.find("https://player.twitch.tv/") == 0)) && - is_valid_twitch_path - ) { - type = TWITCH_MEDIA_TYPE; - } - - return type; -} - -// static -std::string Twitch::GetMediaIdFromUrl( - const std::string& url, - const std::string& publisher_blob) { - std::string mediaId = braveledger_media::ExtractData(url, "twitch.tv/", "/"); - - if (url.find("twitch.tv/videos/") != std::string::npos) { - mediaId = braveledger_media::ExtractData(publisher_blob, - "data-a-target=\"videos-channel-header-item\" href=\"/", "/"); - } - return mediaId; -} - -// static -std::string Twitch::GetMediaKeyFromUrl( - const std::string& id, - const std::string& url) { - if (id == "twitch" || id.empty()) { - return std::string(); - } - - if (url.find("twitch.tv/videos/") != std::string::npos) { - std::string vod_id = braveledger_media::ExtractData(url, - "twitch.tv/videos/", - "/"); - return (std::string)TWITCH_MEDIA_TYPE + "_" + id + "_vod_" + vod_id; - } - return (std::string)TWITCH_MEDIA_TYPE + "_" + id; -} - -// static -void Twitch::UpdatePublisherData( - std::string* publisher_name, - std::string* publisher_favicon_url, - const std::string& publisher_blob) { - *publisher_name = GetPublisherName(publisher_blob); - *publisher_favicon_url = GetFaviconUrl(publisher_blob, *publisher_name); -} - -// static -std::string Twitch::GetPublisherName( - const std::string& publisher_blob) { - return braveledger_media::ExtractData(publisher_blob, - "
", "
"); -} - -// static -std::string Twitch::GetFaviconUrl( - const std::string& publisher_blob, - const std::string& handle) { - if (handle.empty()) { - return std::string(); - } - - const std::string wrapper = braveledger_media::ExtractData(publisher_blob, - "class=\"tw-avatar tw-avatar--size-36\"", - ""); - - return braveledger_media::ExtractData(wrapper, "src=\"", "\""); -} - -// static -std::string Twitch::GetPublisherKey(const std::string& key) { - if (key.empty()) { - return std::string(); - } - - return (std::string)TWITCH_MEDIA_TYPE + "#author:" + key; -} - -void Twitch::OnMediaActivityError(const ledger::mojom::VisitData& visit_data, - uint64_t window_id) { - std::string url = TWITCH_TLD; - std::string name = TWITCH_MEDIA_TYPE; - - if (!url.empty()) { - ledger::mojom::VisitData new_visit_data; - new_visit_data.domain = url; - new_visit_data.url = "https://" + url; - new_visit_data.path = "/"; - new_visit_data.name = name; - - ledger_->publisher()->GetPublisherActivityFromUrl( - window_id, ledger::mojom::VisitData::New(new_visit_data), - std::string()); - } else { - BLOG(0, "Media activity error"); - } -} - -void Twitch::ProcessMedia(const base::flat_map& parts, - const ledger::mojom::VisitData& visit_data) { - std::pair site_ids(GetMediaIdFromParts(parts)); - std::string media_id = site_ids.first; - std::string user_id = site_ids.second; - if (media_id.empty()) { - return; - } - - std::string media_key = braveledger_media::GetMediaKey(media_id, - TWITCH_MEDIA_TYPE); - - ledger::mojom::MediaEventInfo twitch_info; - base::flat_map::const_iterator iter = - parts.find("event"); - if (iter != parts.end()) { - twitch_info.event = iter->second; - } - - iter = parts.find("time"); - if (iter != parts.end()) { - twitch_info.time = iter->second; - } - - ledger_->database()->GetMediaPublisherInfo(media_key, - std::bind(&Twitch::OnMediaPublisherInfo, - this, - media_id, - media_key, - twitch_info, - visit_data, - 0, - user_id, - _1, - _2)); -} - -void Twitch::ProcessActivityFromUrl(uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - const std::string& publisher_blob) { - if (publisher_blob.empty() || - publisher_blob == ledger::constant::kIgnorePublisherBlob) { - OnMediaActivityError(visit_data, window_id); - return; - } - - std::string media_id = GetMediaIdFromUrl(visit_data.url, - publisher_blob); - - media_id = base::ToLowerASCII(media_id); - std::string media_key = GetMediaKeyFromUrl(media_id, visit_data.url); - - if (media_key.empty() || media_id.empty()) { - OnMediaActivityError(visit_data, window_id); - return; - } - - ledger_->database()->GetMediaPublisherInfo( - media_key, - std::bind(&Twitch::OnMediaPublisherActivity, - this, - window_id, - visit_data, - media_key, - media_id, - publisher_blob, - _1, - _2)); -} - -void Twitch::OnMediaPublisherInfo( - const std::string& media_id, - const std::string& media_key, - const ledger::mojom::MediaEventInfo& twitch_info, - const ledger::mojom::VisitData& visit_data, - const uint64_t window_id, - const std::string& user_id, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr publisher_info) { - if (result != ledger::mojom::Result::LEDGER_OK && - result != ledger::mojom::Result::NOT_FOUND) { - BLOG(0, "Failed to get publisher info"); - return; - } - - if (publisher_info) { - ledger::mojom::MediaEventInfo old_event; - base::flat_map::const_iterator - iter = twitch_events.find(media_key); - if (iter != twitch_events.end()) { - old_event = iter->second; - } - - ledger::mojom::MediaEventInfo new_event(twitch_info); - new_event.status = GetTwitchStatus(old_event, new_event); - - const uint64_t real_duration = GetTwitchDuration(old_event, new_event); - twitch_events[media_key] = new_event; - - SavePublisherInfo(real_duration, - std::string(), - publisher_info->url, - publisher_info->name, - visit_data, - window_id, - publisher_info->favicon_url, - std::string(), - publisher_info->id); - return; - } - - if (media_id.empty()) { - return; - } - - ledger::mojom::MediaEventInfo old_event; - auto iter = twitch_events.find(media_key); - if (iter != twitch_events.end()) { - old_event = iter->second; - } - - ledger::mojom::MediaEventInfo new_event(twitch_info); - new_event.status = GetTwitchStatus(old_event, new_event); - - const uint64_t real_duration = GetTwitchDuration(old_event, new_event); - twitch_events[media_key] = new_event; - - if (real_duration == 0) { - return; - } - - if (media_id.find("_vod_") != std::string::npos) { - // VOD - auto media_props = base::SplitString( - media_id, - MEDIA_DELIMITER, - base::TRIM_WHITESPACE, - base::SPLIT_WANT_NONEMPTY); - - if (media_props.empty()) { - return; - } - - std::string oembed_url = - (std::string)TWITCH_VOD_URL + media_props[media_props.size() - 1]; - - auto callback = std::bind(&Twitch::OnEmbedResponse, - this, - real_duration, - media_key, - visit_data, - window_id, - user_id, - _1); - - const std::string url = (std::string)TWITCH_PROVIDER_URL + "?json&url=" + - ledger_->ledger_client()->URIEncode(oembed_url); - - FetchDataFromUrl(url, callback); - return; - } - - // Live stream - SavePublisherInfo(real_duration, - media_key, - "", - media_id, - visit_data, - window_id, - std::string(), - media_id); -} - -void Twitch::FetchDataFromUrl(const std::string& url, - ledger::client::LegacyLoadURLCallback callback) { - auto request = ledger::mojom::UrlRequest::New(); - request->url = url; - request->skip_log = true; - ledger_->LoadURL(std::move(request), callback); -} - -void Twitch::OnEmbedResponse(const uint64_t duration, - const std::string& media_key, - const ledger::mojom::VisitData& visit_data, - const uint64_t window_id, - const std::string& user_id, - const ledger::mojom::UrlResponse& response) { - if (response.status_code != net::HTTP_OK) { - // TODO(anyone): add error handler - return; - } - - std::string fav_icon; - braveledger_bat_helper::getJSONValue( - "author_thumbnail_url", - response.body, - &fav_icon); - std::string author_name; - braveledger_bat_helper::getJSONValue( - "author_name", - response.body, - &author_name); - - SavePublisherInfo(duration, - media_key, - "", - author_name, - visit_data, - window_id, - fav_icon, - user_id); -} - -void Twitch::OnMediaPublisherActivity( - uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - const std::string& media_key, - const std::string& media_id, - const std::string& publisher_blob, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr info) { - if (result != ledger::mojom::Result::LEDGER_OK && - result != ledger::mojom::Result::NOT_FOUND) { - OnMediaActivityError(visit_data, window_id); - return; - } - - if (!info || result == ledger::mojom::Result::NOT_FOUND) { - // first see if we have the publisher a different way (VOD vs. live stream - ledger_->database()->GetPublisherInfo( - GetPublisherKey(media_id), - std::bind(&Twitch::OnPublisherInfo, - this, - window_id, - visit_data, - media_key, - media_id, - publisher_blob, - _1, - _2)); - } else { - const auto add = ledger_->publisher()->IsVerified(info->status); - if (add && info->favicon_url.empty()) { - std::string publisher_name; - std::string publisher_favicon_url; - - UpdatePublisherData(&publisher_name, - &publisher_favicon_url, - publisher_blob); - - if (!publisher_favicon_url.empty()) { - SavePublisherInfo(0, - media_key, - "", - publisher_name, - visit_data, - window_id, - publisher_favicon_url, - media_id); - return; - } - } - - ledger_->ledger_client()->OnPanelPublisherInfo( - result, - std::move(info), - window_id); - } -} - -void Twitch::OnPublisherInfo(uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - const std::string& media_key, - const std::string& media_id, - const std::string& publisher_blob, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr publisher_info) { - if (result != ledger::mojom::Result::LEDGER_OK && - result != ledger::mojom::Result::NOT_FOUND) { - OnMediaActivityError(visit_data, window_id); - return; - } - - if (!publisher_info || result == ledger::mojom::Result::NOT_FOUND) { - std::string publisher_name; - std::string publisher_favicon_url; - - UpdatePublisherData(&publisher_name, - &publisher_favicon_url, - publisher_blob); - - if (publisher_name.empty()) { - publisher_name = media_id; - } - - SavePublisherInfo(0, - media_key, - "", - publisher_name, - visit_data, - window_id, - publisher_favicon_url, - media_id); - } else { - ledger_->ledger_client()->OnPanelPublisherInfo( - result, - std::move(publisher_info), - window_id); - } -} - -void Twitch::SavePublisherInfo(const uint64_t duration, - const std::string& media_key, - const std::string& publisher_url, - const std::string& publisher_name, - const ledger::mojom::VisitData& visit_data, - const uint64_t window_id, - const std::string& fav_icon, - const std::string& channel_id, - const std::string& publisher_key) { - if (channel_id.empty() && publisher_key.empty()) { - BLOG(0, "author id is missing"); - return; - } - - std::string key = publisher_key; - if (key.empty()) { - key = GetPublisherKey(channel_id); - } - - if (key.empty()) { - BLOG(0, "Publisher id is missing"); - return; - } - - ledger::mojom::VisitData new_visit_data; - if (fav_icon.length() > 0) { - new_visit_data.favicon_url = fav_icon; - } - - std::string url = publisher_url; - if (url.empty()) { - url = GetMediaURL(channel_id) + "/videos"; - } - - new_visit_data.provider = TWITCH_MEDIA_TYPE; - new_visit_data.name = publisher_name; - new_visit_data.url = url; - - ledger_->publisher()->SaveVideoVisit( - key, new_visit_data, duration, true, window_id, - [](ledger::mojom::Result, ledger::mojom::PublisherInfoPtr) {}); - - if (!media_key.empty()) { - ledger_->database()->SaveMediaPublisherInfo( - media_key, key, [](const ledger::mojom::Result) {}); - } -} - -} // namespace braveledger_media diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/twitch.h b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/twitch.h deleted file mode 100644 index 43b91b11fa85..000000000000 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/twitch.h +++ /dev/null @@ -1,139 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#ifndef BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_LEGACY_MEDIA_TWITCH_H_ -#define BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_LEGACY_MEDIA_TWITCH_H_ - -#include -#include -#include - -#include "base/containers/flat_map.h" -#include "base/gtest_prod_util.h" -#include "bat/ledger/internal/legacy/media/helper.h" -#include "bat/ledger/ledger.h" - -namespace ledger { -class LedgerImpl; -} - -namespace braveledger_media { - -class Twitch { - public: - explicit Twitch(ledger::LedgerImpl* ledger); - - ~Twitch(); - - void OnMediaActivityError(const ledger::mojom::VisitData& visit_data, - uint64_t window_id); - - void ProcessMedia(const base::flat_map& parts, - const ledger::mojom::VisitData& visit_data); - - void ProcessActivityFromUrl(uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - const std::string& publisher_blob); - - static std::string GetLinkType(const std::string& url, - const std::string& first_party_url, - const std::string& referrer); - - private: - static std::pair GetMediaIdFromParts( - const base::flat_map& parts); - - static std::string GetMediaURL(const std::string& mediaId); - - static std::string GetTwitchStatus( - const ledger::mojom::MediaEventInfo& old_event, - const ledger::mojom::MediaEventInfo& new_event); - - static uint64_t GetTwitchDuration( - const ledger::mojom::MediaEventInfo& old_event, - const ledger::mojom::MediaEventInfo& new_event); - - static std::string GetMediaIdFromUrl(const std::string& url, - const std::string& publisher_blob); - - static std::string GetMediaKeyFromUrl(const std::string& id, - const std::string& url); - - static std::string GetPublisherKey(const std::string& key); - - static void UpdatePublisherData(std::string* publisher_name, - std::string* publisher_favicon_url, - const std::string& publisher_blob); - - static std::string GetPublisherName(const std::string& publisher_blob); - - static std::string GetFaviconUrl(const std::string& publisher_blob, - const std::string& twitchHandle); - - void OnMediaPublisherInfo(const std::string& media_id, - const std::string& media_key, - const ledger::mojom::MediaEventInfo& twitch_info, - const ledger::mojom::VisitData& visit_data, - const uint64_t window_id, - const std::string& user_id, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr publisher_info); - - void FetchDataFromUrl(const std::string& url, - ledger::client::LegacyLoadURLCallback callback); - - void OnEmbedResponse(const uint64_t duration, - const std::string& media_key, - const ledger::mojom::VisitData& visit_data, - const uint64_t window_id, - const std::string& user_id, - const ledger::mojom::UrlResponse& response); - - void OnMediaPublisherActivity(uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - const std::string& media_key, - const std::string& media_id, - const std::string& publisher_blob, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr info); - - void OnPublisherInfo(uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - const std::string& media_key, - const std::string& media_id, - const std::string& publisher_blob, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr publisher_info); - - void SavePublisherInfo(const uint64_t duration, - const std::string& media_key, - const std::string& publisher_url, - const std::string& publisher_name, - const ledger::mojom::VisitData& visit_data, - const uint64_t window_id, - const std::string& fav_icon, - const std::string& channel_id, - const std::string& publisher_key = ""); - - ledger::LedgerImpl* ledger_; // NOT OWNED - base::flat_map twitch_events; - - // For testing purposes - friend class MediaTwitchTest; - FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetMediaIdFromParts); - FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetMediaURL); - FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetTwitchStatus); - FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetTwitchDuration); - FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetMediaIdFromUrl); - FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetMediaKeyFromUrl); - FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetPublisherKey); - FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, UpdatePublisherData); - FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetPublisherName); - FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetFaviconUrl); -}; - -} // namespace braveledger_media - -#endif // BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_LEGACY_MEDIA_TWITCH_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/twitch_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/twitch_unittest.cc deleted file mode 100644 index 668ac1f58dd1..000000000000 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/twitch_unittest.cc +++ /dev/null @@ -1,317 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include - -#include "bat/ledger/internal/legacy/media/twitch.h" -#include "bat/ledger/ledger.h" -#include "testing/gtest/include/gtest/gtest.h" - -// npm run test -- brave_unit_tests --filter=MediaTwitchTest.* - -namespace braveledger_media { - -class MediaTwitchTest : public testing::Test { -}; - -const char profile_html[] = - "
" - "
\"bravesoftware\"" - "
bravesoftware
" - "
Videos
0
"; - -TEST(MediaTwitchTest, GetMediaIdFromParts) { - std::string media_id; - std::string user_id; - // empty - std::pair result = - Twitch::GetMediaIdFromParts({}); - media_id = result.first; - user_id = result.second; - EXPECT_TRUE(user_id.empty()); - ASSERT_TRUE(media_id.empty()); - - // event is not on the list - result = Twitch::GetMediaIdFromParts({ - {"event", "test"}, - {"properties", ""} - }); - media_id = result.first; - user_id = result.second; - EXPECT_TRUE(user_id.empty()); - ASSERT_TRUE(media_id.empty()); - - // properties are missing - result = Twitch::GetMediaIdFromParts({ - {"event", "minute-watched"} - }); - media_id = result.first; - user_id = result.second; - EXPECT_TRUE(user_id.empty()); - ASSERT_TRUE(media_id.empty()); - - // channel is missing - result = Twitch::GetMediaIdFromParts({ - {"event", "minute-watched"}, - {"properties", ""} - }); - media_id = result.first; - user_id = result.second; - EXPECT_TRUE(user_id.empty()); - ASSERT_TRUE(media_id.empty()); - - // channel is provided - result = Twitch::GetMediaIdFromParts({ - {"event", "minute-watched"}, - {"properties", ""}, - {"channel", "bravesoftware"} - }); - media_id = result.first; - user_id = result.second; - EXPECT_EQ(user_id, "bravesoftware"); - ASSERT_EQ(media_id, "bravesoftware"); - - // vod is missing leading v - result = Twitch::GetMediaIdFromParts({ - {"event", "minute-watched"}, - {"properties", ""}, - {"channel", "bravesoftware"}, - {"vod", "123312312"} - }); - media_id = result.first; - user_id = result.second; - EXPECT_EQ(user_id, "bravesoftware"); - ASSERT_EQ(media_id, "bravesoftware"); - - // vod is provided - result = Twitch::GetMediaIdFromParts({ - {"event", "minute-watched"}, - {"properties", ""}, - {"channel", "bravesoftware"}, - {"vod", "v123312312"} - }); - media_id = result.first; - user_id = result.second; - EXPECT_EQ(user_id, "bravesoftware"); - ASSERT_EQ(media_id, "bravesoftware_vod_123312312"); - - // live stream username has '_' - result = Twitch::GetMediaIdFromParts({ - {"event", "minute-watched"}, - {"properties", ""}, - {"channel", "anatomyz_2"} - }); - media_id = result.first; - user_id = result.second; - EXPECT_EQ(user_id, "anatomyz_2"); - ASSERT_EQ(media_id, "anatomyz_2"); - - // vod has '_' - result = Twitch::GetMediaIdFromParts({ - {"event", "minute-watched"}, - {"properties", ""}, - {"channel", "anatomyz_2"}, - {"vod", "v123312312"} - }); - media_id = result.first; - user_id = result.second; - EXPECT_EQ(user_id, "anatomyz_2"); - ASSERT_EQ(media_id, "anatomyz_2_vod_123312312"); -} - -TEST(MediaTwitchTest, GetMediaURL) { - // empty - std::string result = Twitch::GetMediaURL(""); - ASSERT_EQ(result, ""); - - // all ok - result = Twitch::GetMediaURL("bravesoftware"); - ASSERT_EQ(result, "https://www.twitch.tv/bravesoftware"); -} - -TEST(MediaTwitchTest, GetTwitchStatus) { - // empty - ledger::mojom::MediaEventInfo old_event; - ledger::mojom::MediaEventInfo new_event; - std::string result = Twitch::GetTwitchStatus(old_event, new_event); - ASSERT_EQ(result, "playing"); - - // user paused the video - old_event.event = "video_pause"; - old_event.status = "playing"; - new_event.event = "video_pause"; - result = Twitch::GetTwitchStatus(old_event, new_event); - ASSERT_EQ(result, "paused"); - - // user seeked while video was paused - old_event.status = "paused"; - new_event.event = "player_click_vod_seek"; - result = Twitch::GetTwitchStatus(old_event, new_event); - ASSERT_EQ(result, "paused"); - - // user skipped the video that was playing - old_event.status = "playing"; - old_event.event = "video_pause"; - new_event.event = "player_click_vod_seek"; - result = Twitch::GetTwitchStatus(old_event, new_event); - ASSERT_EQ(result, "playing"); - - // user pauses a video, then seeks it and plays it again - old_event.status = "paused"; - old_event.event = "player_click_vod_seek"; - new_event.event = "video_pause"; - result = Twitch::GetTwitchStatus(old_event, new_event); - ASSERT_EQ(result, "playing"); -} - -TEST(MediaTwitchTest, GetMediaIdFromUrl) { - // for live stream - std::string result = Twitch::GetMediaIdFromUrl( - "https://www.twitch.tv/bravesoftware", profile_html); - - ASSERT_EQ(result, "bravesoftware"); - - // longer url - result = Twitch::GetMediaIdFromUrl( - "https://www.twitch.tv/bravesoftware/clips", profile_html); - - ASSERT_EQ(result, "bravesoftware"); - - // video - result = Twitch::GetMediaIdFromUrl( - "https://www.twitch.tv/videos/11111", profile_html); - - ASSERT_EQ(result, "bravesoftware"); -} - -TEST(MediaTwitchTest, GetLinkType) { - const std::string url("https://k8923479-sub.cdn.ttvnw.net/v1/segment/"); - - // url is not correct - std::string result = Twitch::GetLinkType("https://brave.com", - "https://www.twitch.tv", - ""); - ASSERT_EQ(result, ""); - - // first party is off - result = Twitch::GetLinkType(url, "https://www.brave.com", ""); - ASSERT_EQ(result, ""); - - // regular page - result = Twitch::GetLinkType(url, "https://www.twitch.tv/", ""); - ASSERT_EQ(result, "twitch"); - - // mobile page - result = Twitch::GetLinkType(url, "https://m.twitch.tv/", ""); - ASSERT_EQ(result, "twitch"); - - // player page - result = Twitch::GetLinkType(url, - "https://brave.com/", - "https://player.twitch.tv/"); - ASSERT_EQ(result, "twitch"); -} - -TEST(MediaTwitchTest, GetMediaKeyFromUrl) { - // id is empty - std::string result = Twitch::GetMediaKeyFromUrl("", ""); - ASSERT_EQ(result, ""); - - // id is twitch - result = Twitch::GetMediaKeyFromUrl("twitch", ""); - ASSERT_EQ(result, ""); - - // get vod id - result = Twitch::GetMediaKeyFromUrl( - "bravesoftware", - "https://www.twitch.tv/videos/411403500"); - ASSERT_EQ(result, "twitch_bravesoftware_vod_411403500"); - - // regular id - result = Twitch::GetMediaKeyFromUrl("bravesoftware", ""); - ASSERT_EQ(result, "twitch_bravesoftware"); -} - -TEST(MediaTwitchTest, GetPublisherKey) { - // empty - std::string result = Twitch::GetPublisherKey(""); - ASSERT_EQ(result, ""); - - // all ok - result = Twitch::GetPublisherKey("key"); - ASSERT_EQ(result, "twitch#author:key"); -} - -TEST(MediaTwitchTest, GetPublisherName) { - // blob is not correct - std::string result = Twitch::GetPublisherName("dfsfsdfsdfds"); - ASSERT_EQ(result, ""); - - // all ok - result = Twitch::GetPublisherName(profile_html); - ASSERT_EQ(result, "bravesoftware"); -} - -TEST(MediaTwitchTest, GetFaviconUrl) { - // handler is empty - std::string result = Twitch::GetFaviconUrl(profile_html, ""); - ASSERT_EQ(result, ""); - - // blob is not correct - result = Twitch::GetFaviconUrl("dfsfsdfsdfds", "bravesoftware"); - ASSERT_EQ(result, ""); - - // all ok - result = Twitch::GetFaviconUrl(profile_html, "bravesoftware"); - ASSERT_EQ(result, - "https://static-cdn.jtvnw.net/user-default-pictures/" - "0ecbb6c3-fecb-4016-8115-aa467b7c36ed-profile_image-70x70.jpg"); -} - -TEST(MediaTwitchTest, UpdatePublisherData) { - // blob is not correct - std::string name; - std::string favicon_url; - Twitch::UpdatePublisherData( - &name, - &favicon_url, - "dfsfsdfsdfds"); - - ASSERT_EQ(name, ""); - ASSERT_EQ(favicon_url, ""); - - // all ok - Twitch::UpdatePublisherData( - &name, - &favicon_url, - profile_html); - - ASSERT_EQ(name, "bravesoftware"); - ASSERT_EQ(favicon_url, - "https://static-cdn.jtvnw.net/user-default-pictures/" - "0ecbb6c3-fecb-4016-8115-aa467b7c36ed-profile_image-70x70.jpg"); -} - -} // namespace braveledger_media diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/twitter.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/twitter.cc deleted file mode 100644 index a16b838cbfc3..000000000000 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/twitter.cc +++ /dev/null @@ -1,547 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include -#include -#include - -#include "base/containers/contains.h" -#include "base/strings/string_split.h" -#include "base/strings/string_util.h" -#include "base/strings/stringprintf.h" -#include "base/strings/utf_string_conversions.h" -#include "bat/ledger/internal/constants.h" -#include "bat/ledger/internal/ledger_impl.h" -#include "bat/ledger/internal/legacy/media/helper.h" -#include "bat/ledger/internal/legacy/media/twitter.h" -#include "bat/ledger/internal/legacy/static_values.h" -#include "net/base/url_util.h" -#include "net/http/http_status_code.h" -#include "url/gurl.h" - -#define TWITTER_BASE_URL "https://twitter.com/" - -using std::placeholders::_1; -using std::placeholders::_2; -using std::placeholders::_3; - -namespace { - -std::string GetUserIdFromUrl(const std::string& path) { - if (path.empty()) { - return std::string(); - } - - GURL url(TWITTER_BASE_URL + path); - if (!url.is_valid()) { - return std::string(); - } - - for (net::QueryIterator it(url); !it.IsAtEnd(); it.Advance()) { - if (it.GetKey() == "user_id") { - return it.GetUnescapedValue(); - } - } - - return std::string(); -} - -bool IsExcludedPathComponent(const std::string& path) { - const std::vector paths({ - "/", - "/settings", - "/explore", - "/notifications", - "/messages", - "/logout", - "/search", - "/about", - "/tos", - "/privacy", - "/home" - }); - - for (const std::string& str_path : paths) { - if (str_path == path || str_path + "/" == path) { - return true; - } - } - - const std::vector patterns({ - "/i/", - "/account/", - "/compose/", - "/?login", - "/?logout", - "/who_to_follow/", - "/hashtag/", - "/settings/" - }); - - for (const std::string& str_path : patterns) { - if (base::StartsWith(path, - str_path, - base::CompareCase::INSENSITIVE_ASCII)) { - return true; - } - } - - - return false; -} - -bool IsExcludedScreenName(const std::string& path) { - const std::vector screen_names({ - "settings", - "explore", - "notifications", - "messages", - "logout", - "search", - "about", - "tos", - "privacy", - "home", - }); - - GURL url(TWITTER_BASE_URL + path); - if (!url.is_valid()) { - return true; - } - - for (net::QueryIterator it(url); !it.IsAtEnd(); it.Advance()) { - if (it.GetKey() == "screen_name") { - if (base::Contains(screen_names, it.GetUnescapedValue())) { - return true; - } - break; - } - } - - return false; -} - -} // namespace - -namespace braveledger_media { - -Twitter::Twitter(ledger::LedgerImpl* ledger): - ledger_(ledger) { -} - -Twitter::~Twitter() { -} - -// static -std::string Twitter::GetProfileURL(const std::string& screen_name, - const std::string& user_id) { - if (!user_id.empty()) { - return base::StringPrintf("https://twitter.com/intent/user?user_id=%s", - user_id.c_str()); - } - - if (!screen_name.empty()) { - return base::StringPrintf("https://twitter.com/%s/", screen_name.c_str()); - } - - return ""; -} - -// static -std::string Twitter::GetProfileImageURL(const std::string& screen_name) { - if (screen_name.empty()) { - return std::string(); - } - - return base::StringPrintf( - "https://twitter.com/%s/profile_image?size=original", - screen_name.c_str()); -} - -// static -std::string Twitter::GetPublisherKey(const std::string& key) { - if (key.empty()) { - return std::string(); - } - - return (std::string)TWITTER_MEDIA_TYPE + "#channel:" + key; -} - -// static -std::string Twitter::GetMediaKey(const std::string& screen_name) { - if (screen_name.empty()) { - return std::string(); - } - - return (std::string)TWITTER_MEDIA_TYPE + "_" + screen_name; -} - -// static -std::string Twitter::GetUserNameFromUrl(const std::string& path) { - if (path.empty()) { - return std::string(); - } - - GURL url(TWITTER_BASE_URL + path); - if (!url.is_valid()) { - return std::string(); - } - - for (net::QueryIterator it(url); !it.IsAtEnd(); it.Advance()) { - if (it.GetKey() == "screen_name") { - return it.GetUnescapedValue(); - } - } - - std::vector parts = base::SplitString( - path, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); - - if (parts.size() > 0) { - return parts.at(0); - } - - return std::string(); -} - -// static -bool Twitter::IsExcludedPath(const std::string& path) { - if (path.empty()) { - return true; - } - - // Due to implementation differences on desktop and mobile - // platforms, we may receive the screen name as part of a - // query-string or as a path component - if (IsExcludedScreenName(path) || IsExcludedPathComponent(path)) { - return true; - } - - return false; -} - -// static -std::string Twitter::GetUserId(const std::string& response) { - if (response.empty()) { - return std::string(); - } - - std::string id = braveledger_media::ExtractData( - response, - ""); - - if (id.empty()) { - id = braveledger_media::ExtractData( - response, - "
"); - } - - if (id.empty()) { - id = braveledger_media::ExtractData( - response, "https://pbs.twimg.com/profile_banners/", "/"); - } - - return id; -} - -// static -std::string Twitter::GetPublisherName(const std::string& response) { - if (response.empty()) { - return std::string(); - } - - const std::string title = braveledger_media::ExtractData( - response, "", ""); - - if (title.empty()) { - return std::string(); - } - - std::vector parts = base::SplitStringUsingSubstr( - title, " (@", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); - - if (parts.size() > 0) { - return parts.at(0); - } - - return title; -} - -void Twitter::SaveMediaInfo( - const base::flat_map& data, - ledger::PublisherInfoCallback callback) { - auto user_id = data.find("user_id"); - auto screen_name = data.find("screen_name"); - if (user_id == data.end() || screen_name == data.end()) { - callback(ledger::mojom::Result::LEDGER_ERROR, nullptr); - return; - } - - const std::string media_key = GetMediaKey(screen_name->second); - - auto name = data.find("name"); - std::string publisher_name = screen_name->second; - if (name != data.end()) { - publisher_name = name->second; - } - - ledger_->database()->GetMediaPublisherInfo( - media_key, - std::bind(&Twitter::OnMediaPublisherInfo, - this, - 0, - user_id->second, - screen_name->second, - publisher_name, - callback, - _1, - _2)); -} - -// static -std::string Twitter::GetShareURL( - const base::flat_map& args) { - auto comment = args.find("comment"); - auto name = args.find("name"); - auto tweet_id = args.find("tweet_id"); - auto hashtag = args.find("hashtag"); - if (comment == args.end() || name == args.end() || hashtag == args.end()) - return std::string(); - - // Append hashtag to comment ("%20%23" = percent-escaped space and - // number sign) - std::string comment_with_hashtag = - comment->second + "%20%23" + hashtag->second; - - // If a tweet ID was specified, then quote the original tweet along - // with the supplied comment; otherwise, just tweet the comment. - std::string share_url; - if (tweet_id != args.end() && !tweet_id->second.empty()) { - std::string quoted_tweet_url = - base::StringPrintf("https://twitter.com/%s/status/%s", - name->second.c_str(), tweet_id->second.c_str()); - share_url = base::StringPrintf( - "https://twitter.com/intent/tweet?text=%s&url=%s", - comment_with_hashtag.c_str(), quoted_tweet_url.c_str()); - } else { - share_url = base::StringPrintf("https://twitter.com/intent/tweet?text=%s", - comment_with_hashtag.c_str()); - } - return share_url; -} - -void Twitter::OnMediaPublisherInfo( - uint64_t window_id, - const std::string& user_id, - const std::string& screen_name, - const std::string& publisher_name, - ledger::PublisherInfoCallback callback, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr publisher_info) { - if (result != ledger::mojom::Result::LEDGER_OK && - result != ledger::mojom::Result::NOT_FOUND) { - callback(ledger::mojom::Result::LEDGER_ERROR, nullptr); - return; - } - - if (!publisher_info || result == ledger::mojom::Result::NOT_FOUND) { - SavePublisherInfo(0, - user_id, - screen_name, - publisher_name, - window_id, - callback); - } else { - // TODO(nejczdovc): we need to check if user is verified, - // but his image was not saved yet, so that we can fix it - callback(result, std::move(publisher_info)); - } -} - -void Twitter::SavePublisherInfo( - const uint64_t duration, - const std::string& user_id, - const std::string& screen_name, - const std::string& publisher_name, - const uint64_t window_id, - ledger::PublisherInfoCallback callback) { - const std::string publisher_key = GetPublisherKey(user_id); - const std::string url = GetProfileURL(screen_name, user_id); - const std::string favicon_url = GetProfileImageURL(screen_name); - const std::string media_key = GetMediaKey(screen_name); - - if (publisher_key.empty()) { - callback(ledger::mojom::Result::LEDGER_ERROR, nullptr); - BLOG(0, "Publisher key is missing"); - return; - } - - ledger::mojom::VisitDataPtr visit_data = ledger::mojom::VisitData::New(); - visit_data->provider = TWITTER_MEDIA_TYPE; - visit_data->url = url; - visit_data->favicon_url = favicon_url; - visit_data->name = publisher_name; - - ledger_->publisher()->SaveVisit( - publisher_key, - *visit_data, - duration, - true, - window_id, - callback); - - if (!media_key.empty()) { - ledger_->database()->SaveMediaPublisherInfo( - media_key, publisher_key, [](const ledger::mojom::Result) {}); - } -} - -void Twitter::FetchDataFromUrl( - const std::string& url, - ledger::client::LoadURLCallback callback) { - auto request = ledger::mojom::UrlRequest::New(); - request->url = url; - request->skip_log = true; - ledger_->LoadURL(std::move(request), callback); -} - -void Twitter::OnMediaActivityError(const ledger::mojom::VisitData& visit_data, - uint64_t window_id) { - std::string url = TWITTER_TLD; - std::string name = TWITTER_MEDIA_TYPE; - - DCHECK(!url.empty()); - - ledger::mojom::VisitData new_visit_data; - new_visit_data.domain = url; - new_visit_data.url = "https://" + url; - new_visit_data.path = "/"; - new_visit_data.name = name; - - ledger_->publisher()->GetPublisherActivityFromUrl( - window_id, ledger::mojom::VisitData::New(new_visit_data), std::string()); -} - -void Twitter::ProcessActivityFromUrl( - uint64_t window_id, - const ledger::mojom::VisitData& visit_data) { - // not all url's are publisher specific - if (IsExcludedPath(visit_data.path)) { - OnMediaActivityError(visit_data, window_id); - return; - } - - const std::string user_name = GetUserNameFromUrl(visit_data.path); - const std::string media_key = GetMediaKey(user_name); - - if (media_key.empty()) { - OnMediaActivityError(visit_data, window_id); - return; - } - - ledger_->database()->GetMediaPublisherInfo( - media_key, - std::bind(&Twitter::OnMediaPublisherActivity, - this, - _1, - _2, - window_id, - visit_data, - media_key)); -} - -void Twitter::OnMediaPublisherActivity( - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr info, - uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - const std::string& media_key) { - if (result != ledger::mojom::Result::LEDGER_OK && - result != ledger::mojom::Result::NOT_FOUND) { - OnMediaActivityError(visit_data, window_id); - return; - } - - if (!info || result == ledger::mojom::Result::NOT_FOUND) { - const std::string user_name = GetUserNameFromUrl(visit_data.path); - const std::string user_id = GetUserIdFromUrl(visit_data.path); - const std::string url = GetProfileURL(user_name, user_id); - - auto url_callback = std::bind(&Twitter::OnUserPage, - this, - window_id, - visit_data, - _1); - - FetchDataFromUrl(url, url_callback); - } else { - GetPublisherPanelInfo(window_id, - visit_data, - info->id); - } -} - -// Gets publisher panel info where we know that publisher info exists -void Twitter::GetPublisherPanelInfo(uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - const std::string& publisher_key) { - auto filter = ledger_->publisher()->CreateActivityFilter( - publisher_key, ledger::mojom::ExcludeFilter::FILTER_ALL, false, - ledger_->state()->GetReconcileStamp(), true, false); - ledger_->database()->GetPanelPublisherInfo(std::move(filter), - std::bind(&Twitter::OnPublisherPanelInfo, - this, - window_id, - visit_data, - publisher_key, - _1, - _2)); -} - -void Twitter::OnPublisherPanelInfo(uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - const std::string& publisher_key, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr info) { - if (!info || result == ledger::mojom::Result::NOT_FOUND) { - auto url_callback = std::bind(&Twitter::OnUserPage, - this, - window_id, - visit_data, - _1); - FetchDataFromUrl(visit_data.url, url_callback); - } else { - ledger_->ledger_client()->OnPanelPublisherInfo( - result, - std::move(info), - window_id); - } -} - -void Twitter::OnUserPage(uint64_t window_id, - const ledger::mojom::VisitData& visit_data, - const ledger::mojom::UrlResponse& response) { - if (response.status_code != net::HTTP_OK) { - OnMediaActivityError(visit_data, window_id); - return; - } - - std::string user_id = GetUserIdFromUrl(visit_data.path); - if (user_id.empty()) { - user_id = GetUserId(response.body); - } - - const std::string user_name = GetUserNameFromUrl(visit_data.path); - std::string publisher_name = GetPublisherName(response.body); - - if (publisher_name.empty()) { - publisher_name = user_name; - } - - SavePublisherInfo( - 0, user_id, user_name, publisher_name, window_id, - [](ledger::mojom::Result, ledger::mojom::PublisherInfoPtr) {}); -} - -} // namespace braveledger_media diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/vimeo.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/vimeo.cc deleted file mode 100644 index fae7ed1d024e..000000000000 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/vimeo.cc +++ /dev/null @@ -1,652 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include -#include -#include - -#include "base/containers/fixed_flat_set.h" -#include "base/json/json_reader.h" -#include "base/strings/string_util.h" -#include "base/strings/stringprintf.h" -#include "bat/ledger/internal/constants.h" -#include "bat/ledger/internal/ledger_impl.h" -#include "bat/ledger/internal/legacy/bat_helper.h" -#include "bat/ledger/internal/legacy/media/vimeo.h" -#include "bat/ledger/internal/legacy/static_values.h" -#include "net/http/http_status_code.h" - -using std::placeholders::_1; -using std::placeholders::_2; -using std::placeholders::_3; - -namespace braveledger_media { - -Vimeo::Vimeo(ledger::LedgerImpl* ledger): - ledger_(ledger) { -} - -Vimeo::~Vimeo() = default; - -// static -std::string Vimeo::GetLinkType(const std::string& url) { - const std::string api = "https://fresnel.vimeocdn.com/add/player-stats?"; - std::string type; - - if (!url.empty() && url.find(api) != std::string::npos) { - type = VIMEO_MEDIA_TYPE; - } - - return type; -} - -// static -std::string Vimeo::GetVideoUrl(const std::string& video_id) { - if (video_id.empty()) { - return ""; - } - - return "https://vimeo.com/" + video_id; -} - -// static -std::string Vimeo::GetMediaKey(const std::string& video_id, - const std::string& type) { - if (video_id.empty()) { - return ""; - } - - if (type == "vimeo-vod") { - return (std::string)VIMEO_MEDIA_TYPE + "_" + video_id; - } - - return ""; -} - -// static -std::string Vimeo::GetPublisherKey(const std::string& key) { - if (key.empty()) { - return ""; - } - - return (std::string)VIMEO_MEDIA_TYPE + "#channel:" + key; -} - -// static -std::string Vimeo::GetIdFromVideoPage(const std::string& data) { - if (data.empty()) { - return ""; - } - - return braveledger_media::ExtractData(data, - "\"creator_id\":", ","); -} - -// static -std::string Vimeo::GenerateFaviconUrl(const std::string& id) { - if (id.empty()) { - return ""; - } - - return base::StringPrintf("https://i.vimeocdn.com/portrait/%s_300x300.webp", - id.c_str()); -} - -// static -std::string Vimeo::GetNameFromVideoPage(const std::string& data) { - if (data.empty()) { - return ""; - } - - std::string publisher_name; - const std::string publisher_json_name = - braveledger_media::ExtractData(data, "\"display_name\":\"", "\""); - const std::string publisher_json = "{\"brave_publisher\":\"" + - publisher_json_name + "\"}"; - braveledger_bat_helper::getJSONValue( - "brave_publisher", publisher_json, &publisher_name); - return publisher_name; -} - -// static -std::string Vimeo::GetUrlFromVideoPage(const std::string& data) { - if (data.empty()) { - return ""; - } - - const std::string wrapper = braveledger_media::ExtractData(data, - "", ""); - - const std::string name = braveledger_media::ExtractData(wrapper, - ""); - - if (name.empty()) { - return ""; - } - - return base::StringPrintf("https://vimeo.com/%s/videos", - name.c_str()); -} - -// static -bool Vimeo::AllowedEvent(const std::string& event) { - if (event.empty()) { - return false; - } - - static constexpr auto kAllowList = base::MakeFixedFlatSet( - {"video-start-time", "video-minute-watched", "video-paused", - "video-played", "video-seek", "video-seeked"}); - - return kAllowList.contains(event); -} - -// static -uint64_t Vimeo::GetDuration(const ledger::mojom::MediaEventInfo& old_event, - const ledger::mojom::MediaEventInfo& new_event) { - // Remove duplicated events - if (old_event.event == new_event.event && - old_event.time == new_event.time) { - return 0u; - } - - double time = 0.0; - std::stringstream tempNew(new_event.time); - double newTime = 0.0; - tempNew >> newTime; - - // Video started - if (new_event.event == "video-start-time") { - time = newTime; - } else { - std::stringstream tempOld(old_event.time); - double oldTime = 0; - tempOld >> oldTime; - - if (new_event.event == "video-minute-watched" || - new_event.event == "video-paused") { - time = newTime - oldTime; - } - } - - if (time < 0) { - time = 0; - } - - return static_cast(std::round(time)); -} - -// static -bool Vimeo::IsExcludedPath(const std::string& path) { - if (path.empty()) { - return true; - } - - static constexpr auto kExcludedPaths = - base::MakeFixedFlatSet({ - "/", "/log_in", "/upgrade", "/live", - "/watch", "/videoschool", "/upload", "/ondemand", - "/ott", "/site_map", "/blog", "/help", - "/about", "/jobs", "/stats", "/watchlater", - "/purchases", "/settings", "/stock", - }); - - // Removing the trailing forward-slash for searching excluded paths. - base::StringPiece path_piece = - (path.length() > 1 && path.back() == '/') - ? base::StringPiece(path.data(), path.length() - 1) - : base::StringPiece(path); - - if (kExcludedPaths.contains(path_piece)) - return true; - - static constexpr auto kExcludedPatterns = - base::MakeFixedFlatSet({ - "/features/", - "/categories/", - "/blog/", - "/ott/", - "/help/", - "/manage/", - "/settings/", - "/stock/", - }); - - for (const auto& excluded_pattern : kExcludedPatterns) { - if (base::StartsWith(path, excluded_pattern, - base::CompareCase::INSENSITIVE_ASCII)) { - return true; - } - } - - return false; -} - -// static -std::string Vimeo::GetIdFromPublisherPage(const std::string& data) { - if (data.empty()) { - return ""; - } - - return braveledger_media::ExtractData( - data, - "data-deep-link=\"users/", - "\""); -} - -// static -std::string Vimeo::GetNameFromPublisherPage(const std::string& data) { - if (data.empty()) { - return ""; - } - std::string publisher_name = GetNameFromVideoPage(data); - if (publisher_name == "") { - return braveledger_media::ExtractData(data, - "url = url; - request->skip_log = true; - ledger_->LoadURL(std::move(request), callback); -} - -void Vimeo::OnMediaActivityError(uint64_t window_id) { - const std::string url = VIMEO_TLD; - const std::string name = VIMEO_MEDIA_TYPE; - - DCHECK(!url.empty()); - - ledger::mojom::VisitData new_data; - new_data.domain = url; - new_data.url = "https://" + url; - new_data.path = "/"; - new_data.name = name; - - ledger_->publisher()->GetPublisherActivityFromUrl( - window_id, ledger::mojom::VisitData::New(new_data), ""); -} - -void Vimeo::ProcessMedia( - const base::flat_map& parts) { - auto iter = parts.find("video_id"); - std::string media_id; - if (iter != parts.end()) { - media_id = iter->second; - } - - if (media_id.empty()) { - return; - } - - std::string type; - iter = parts.find("type"); - if (iter != parts.end()) { - type = iter->second; - } - - const std::string media_key = GetMediaKey(media_id, type); - - ledger::mojom::MediaEventInfo event_info; - iter = parts.find("event"); - if (iter != parts.end()) { - event_info.event = iter->second; - } - - // We should only record events that are relevant to us - if (!AllowedEvent(event_info.event)) { - return; - } - - iter = parts.find("time"); - if (iter != parts.end()) { - event_info.time = iter->second; - } - - ledger_->database()->GetMediaPublisherInfo(media_key, - std::bind(&Vimeo::OnMediaPublisherInfo, - this, - media_id, - media_key, - event_info, - _1, - _2)); -} - -void Vimeo::ProcessActivityFromUrl(uint64_t window_id, - const ledger::mojom::VisitData& visit_data) { - // not all url's are publisher specific - if (IsExcludedPath(visit_data.path)) { - OnMediaActivityError(window_id); - return; - } - - const std::string url = (std::string)VIMEO_PROVIDER_URL + - "?url=" + - ledger_->ledger_client()->URIEncode(visit_data.url); - - auto callback = std::bind(&Vimeo::OnEmbedResponse, - this, - visit_data, - window_id, - _1); - - FetchDataFromUrl(url, callback); -} - -void Vimeo::OnEmbedResponse(const ledger::mojom::VisitData& visit_data, - const uint64_t window_id, - const ledger::mojom::UrlResponse& response) { - if (response.status_code != net::HTTP_OK) { - auto callback = std::bind(&Vimeo::OnUnknownPage, - this, - visit_data, - window_id, - _1); - - FetchDataFromUrl(visit_data.url, callback); - return; - } - - absl::optional data = base::JSONReader::Read(response.body); - if (!data || !data->is_dict()) { - auto callback = std::bind(&Vimeo::OnUnknownPage, - this, - visit_data, - window_id, - _1); - - FetchDataFromUrl(visit_data.url, callback); - return; - } - - std::string publisher_url; - if (data->FindStringKey("author_url")) { - publisher_url = *data->FindStringKey("author_url"); - } - - int32_t video_id = 0; - if (data->FindIntKey("video_id")) { - video_id = *data->FindIntKey("video_id"); - } - - if (publisher_url.empty() || video_id == 0) { - auto callback = std::bind(&Vimeo::OnUnknownPage, - this, - visit_data, - window_id, - _1); - - FetchDataFromUrl(visit_data.url, callback); - return; - } - - std::string publisher_name; - if (data->FindStringKey("author_name")) { - publisher_name = *data->FindStringKey("author_name"); - } - - const std::string media_key = GetMediaKey(std::to_string(video_id), - "vimeo-vod"); - - auto callback = std::bind(&Vimeo::OnPublisherPage, - this, - media_key, - publisher_url, - publisher_name, - visit_data, - window_id, - _1); - - FetchDataFromUrl(publisher_url, callback); -} - -void Vimeo::OnPublisherPage(const std::string& media_key, - const std::string& publisher_url, - const std::string& publisher_name, - const ledger::mojom::VisitData& visit_data, - const uint64_t window_id, - const ledger::mojom::UrlResponse& response) { - if (response.status_code != net::HTTP_OK) { - OnMediaActivityError(window_id); - return; - } - - const std::string user_id = GetIdFromPublisherPage(response.body); - const std::string publisher_key = GetPublisherKey(user_id); - - GetPublisherPanleInfo(media_key, - window_id, - publisher_url, - publisher_key, - publisher_name, - user_id); -} - -void Vimeo::OnUnknownPage(const ledger::mojom::VisitData& visit_data, - const uint64_t window_id, - const ledger::mojom::UrlResponse& response) { - if (response.status_code != net::HTTP_OK) { - OnMediaActivityError(window_id); - return; - } - - std::string user_id = GetIdFromPublisherPage(response.body); - std::string publisher_name; - std::string media_key; - if (!user_id.empty()) { - // we are on publisher page - publisher_name = GetNameFromPublisherPage(response.body); - } else { - user_id = GetIdFromVideoPage(response.body); - - if (user_id.empty()) { - OnMediaActivityError(window_id); - return; - } - - // we are on video page - publisher_name = GetNameFromVideoPage(response.body); - media_key = GetMediaKey(GetVideoIdFromVideoPage(response.body), - "vimeo-vod"); - } - - if (publisher_name.empty()) { - OnMediaActivityError(window_id); - return; - } - - const std::string publisher_key = GetPublisherKey(user_id); - GetPublisherPanleInfo(media_key, - window_id, - visit_data.url, - publisher_key, - publisher_name, - user_id); -} - -void Vimeo::OnPublisherPanleInfo(const std::string& media_key, - uint64_t window_id, - const std::string& publisher_url, - const std::string& publisher_name, - const std::string& user_id, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr info) { - if (!info || result == ledger::mojom::Result::NOT_FOUND) { - SavePublisherInfo(media_key, - 0, - user_id, - publisher_name, - publisher_url, - window_id); - } else { - ledger_->ledger_client()->OnPanelPublisherInfo( - result, - std::move(info), - window_id); - } -} - -void Vimeo::GetPublisherPanleInfo( - const std::string& media_key, - uint64_t window_id, - const std::string& publisher_url, - const std::string& publisher_key, - const std::string& publisher_name, - const std::string& user_id) { - auto filter = ledger_->publisher()->CreateActivityFilter( - publisher_key, ledger::mojom::ExcludeFilter::FILTER_ALL, false, - ledger_->state()->GetReconcileStamp(), true, false); - ledger_->database()->GetPanelPublisherInfo(std::move(filter), - std::bind(&Vimeo::OnPublisherPanleInfo, - this, - media_key, - window_id, - publisher_url, - publisher_name, - user_id, - _1, - _2)); -} - -void Vimeo::OnMediaPublisherInfo( - const std::string& media_id, - const std::string& media_key, - const ledger::mojom::MediaEventInfo& event_info, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr publisher_info) { - if (result != ledger::mojom::Result::LEDGER_OK && - result != ledger::mojom::Result::NOT_FOUND) { - OnMediaActivityError(); - BLOG(0, "Failed to get publisher info"); - return; - } - - if (!publisher_info && !publisher_info.get()) { - auto callback = std::bind(&Vimeo::OnPublisherVideoPage, - this, - media_key, - event_info, - _1); - - FetchDataFromUrl(GetVideoUrl(media_id), callback); - return; - } - - ledger::mojom::MediaEventInfo old_event; - auto iter = events.find(media_key); - if (iter != events.end()) { - old_event = iter->second; - } - - const uint64_t duration = GetDuration(old_event, event_info); - events[media_key] = event_info; - - SavePublisherInfo("", - duration, - "", - publisher_info->name, - publisher_info->url, - 0, - publisher_info->id, - publisher_info->favicon_url); -} - -void Vimeo::OnPublisherVideoPage(const std::string& media_key, - ledger::mojom::MediaEventInfo event_info, - const ledger::mojom::UrlResponse& response) { - if (response.status_code != net::HTTP_OK) { - OnMediaActivityError(); - return; - } - - const std::string user_id = GetIdFromVideoPage(response.body); - - if (user_id.empty()) { - OnMediaActivityError(); - return; - } - - ledger::mojom::MediaEventInfo old_event; - auto iter = events.find(media_key); - if (iter != events.end()) { - old_event = iter->second; - } - - const uint64_t duration = GetDuration(old_event, event_info); - events[media_key] = event_info; - - SavePublisherInfo(media_key, - duration, - user_id, - GetNameFromVideoPage(response.body), - GetUrlFromVideoPage(response.body), - 0); -} - -void Vimeo::SavePublisherInfo( - const std::string& media_key, - const uint64_t duration, - const std::string& user_id, - const std::string& publisher_name, - const std::string& publisher_url, - const uint64_t window_id, - const std::string& publisher_key, - const std::string& publisher_favicon) { - if (user_id.empty() && publisher_key.empty()) { - OnMediaActivityError(window_id); - BLOG(0, "User id is missing"); - return; - } - - std::string key = publisher_key; - if (key.empty()) { - key = GetPublisherKey(user_id); - } - - if (key.empty()) { - OnMediaActivityError(window_id); - BLOG(0, "Publisher key is missing"); - return; - } - - std::string icon = publisher_favicon; - if (icon.empty()) { - icon = GenerateFaviconUrl(user_id); - } - - ledger::mojom::VisitData visit_data; - visit_data.provider = VIMEO_MEDIA_TYPE; - visit_data.url = publisher_url; - visit_data.favicon_url = icon; - visit_data.name = publisher_name; - - ledger_->publisher()->SaveVideoVisit( - key, visit_data, duration, true, window_id, - [](ledger::mojom::Result, ledger::mojom::PublisherInfoPtr) {}); - - if (!media_key.empty()) { - ledger_->database()->SaveMediaPublisherInfo( - media_key, key, [](const ledger::mojom::Result) {}); - } -} - -} // namespace braveledger_media diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/vimeo.h b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/vimeo.h deleted file mode 100644 index a00cde9f2659..000000000000 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/vimeo.h +++ /dev/null @@ -1,145 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#ifndef BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_LEGACY_MEDIA_VIMEO_H_ -#define BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_LEGACY_MEDIA_VIMEO_H_ - -#include - -#include -#include - -#include "base/containers/flat_map.h" -#include "base/gtest_prod_util.h" -#include "bat/ledger/internal/legacy/media/helper.h" -#include "bat/ledger/ledger.h" - -namespace ledger { -class LedgerImpl; -} - -namespace braveledger_media { - -class Vimeo { - public: - explicit Vimeo(ledger::LedgerImpl* ledger); - - ~Vimeo(); - - void ProcessMedia(const base::flat_map& parts); - - static std::string GetLinkType(const std::string& url); - - void ProcessActivityFromUrl(uint64_t window_id, - const ledger::mojom::VisitData& visit_data); - - private: - static std::string GetVideoUrl(const std::string& video_id); - - static std::string GetMediaKey(const std::string& video_id, - const std::string& type); - - static std::string GetPublisherKey(const std::string& key); - - static std::string GetIdFromVideoPage(const std::string& data); - - static std::string GenerateFaviconUrl(const std::string& id); - - static std::string GetNameFromVideoPage(const std::string& data); - - static std::string GetUrlFromVideoPage(const std::string& data); - - static bool AllowedEvent(const std::string& event); - - static uint64_t GetDuration(const ledger::mojom::MediaEventInfo& old_event, - const ledger::mojom::MediaEventInfo& new_event); - - static bool IsExcludedPath(const std::string& path); - - static std::string GetIdFromPublisherPage(const std::string& data); - - static std::string GetNameFromPublisherPage(const std::string& data); - - static std::string GetVideoIdFromVideoPage(const std::string& data); - - void FetchDataFromUrl(const std::string& url, - ledger::client::LegacyLoadURLCallback callback); - - void OnMediaActivityError(uint64_t window_id = 0); - - void OnEmbedResponse(const ledger::mojom::VisitData& visit_data, - const uint64_t window_id, - const ledger::mojom::UrlResponse& response); - - void OnPublisherPage(const std::string& media_key, - const std::string& publisher_url, - const std::string& publisher_name, - const ledger::mojom::VisitData& visit_data, - const uint64_t window_id, - const ledger::mojom::UrlResponse& response); - - void OnUnknownPage(const ledger::mojom::VisitData& visit_data, - const uint64_t window_id, - const ledger::mojom::UrlResponse& response); - - void OnPublisherPanleInfo(const std::string& media_key, - uint64_t window_id, - const std::string& publisher_url, - const std::string& publisher_name, - const std::string& user_id, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr info); - - void GetPublisherPanleInfo( - const std::string& media_key, - uint64_t window_id, - const std::string& publisher_url, - const std::string& publisher_key, - const std::string& publisher_name, - const std::string& user_id); - - void OnMediaPublisherInfo(const std::string& media_id, - const std::string& media_key, - const ledger::mojom::MediaEventInfo& event_info, - ledger::mojom::Result result, - ledger::mojom::PublisherInfoPtr publisher_info); - - void OnPublisherVideoPage(const std::string& media_key, - ledger::mojom::MediaEventInfo event_info, - const ledger::mojom::UrlResponse& response); - - void SavePublisherInfo( - const std::string& media_key, - const uint64_t duration, - const std::string& user_id, - const std::string& publisher_name, - const std::string& publisher_url, - const uint64_t window_id, - const std::string& publisher_key = "", - const std::string& publisher_favicon = ""); - - ledger::LedgerImpl* ledger_; // NOT OWNED - base::flat_map events; - - // For testing purposes - friend class VimeoTest; - FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetVideoUrl); - FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetMediaKey); - FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetPublisherKey); - FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetIdFromVideoPage); - FRIEND_TEST_ALL_PREFIXES(VimeoTest, GenerateFaviconUrl); - FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetNameFromVideoPage); - FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetUrlFromVideoPage); - FRIEND_TEST_ALL_PREFIXES(VimeoTest, AllowedEvent); - FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetDuration); - FRIEND_TEST_ALL_PREFIXES(VimeoTest, IsExcludedPath); - FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetIdFromPublisherPage); - FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetNameFromPublisherPage); - FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetVideoIdFromVideoPage); -}; - -} // namespace braveledger_media - -#endif // BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_LEGACY_MEDIA_VIMEO_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/vimeo_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/vimeo_unittest.cc deleted file mode 100644 index c50d12f07f0f..000000000000 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/media/vimeo_unittest.cc +++ /dev/null @@ -1,310 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include "bat/ledger/internal/legacy/media/vimeo.h" -#include "bat/ledger/ledger.h" -#include "testing/gtest/include/gtest/gtest.h" - -// npm run test -- brave_unit_tests --filter=VimeoTest.* - -namespace braveledger_media { - -class VimeoTest : public testing::Test { -}; - -const char profile_html[] = - "window._gtm = [{\"clip_id\":265045525,\"page_path\":\"\\/265045525\"," - "\"creator_id\":123234205645,\"creator_user_type\":\"plus\"," - "\"video_categories\":\"Animation,Experimental,2D\",\"privacy\":" - "\"anybody\",\"staff_pick\":\"yes\",\"user_id\":,\"page_type\":\"Video\"," - "\"language\":\"en\",\"user_status\":\"logged_in\",\"user_type\":" - "\"basic\",\"ga_universal_id\":\"\",\"comscore_site_id\":\"\",\"new_user\"" - ":false,\"video_count\":1,\"recent_upload_count\":0,\"storage_used_gb\":"; - -const char page_config[] = - "window.vimeo.clip_page_config = {\"clip\":{\"id\":331165963,\"title\"" - ":\"IMG_2306\",\"description\":null,\"uploaded_on\":\"2019-04-18 " - "03:15:32\",\"uploaded_on_relative\":\"3 weeks ago\",\"uploaded_on_full" - "\":\"Thursday, April 18, 2019 at 3:15 AM EST\",\"is_spatial\":false,\"" - "is_hdr\":false,\"privacy\":{\"is_public\":true,\"type\":\"anybody\",\"" - "description\":\"Public\"},\"duration\":{\"raw\":72,\"formatted\":\"" - "01:12\"},\"is_liked\":false,\"is_unavailable\":false,\"likes_url\":" - "\"/331165963/likes\",\"is_live\":false,\"unlisted_hash\":null}," - "\"owner\":{\"id\":97518779,\"display_name\":\"Nejcé\"," - "\"has_advanced_stats\":false}"; - -const char user_link[] = - "

from" - "Nejc" - "

" - "
"; - -const char publisher_page[] = - "" - "" - "" - "" - "" - "" - "" - "" - "" - "" - "" - "" - "" - "" - "
Watch in our app
Open in app
" - "