diff --git a/components/brave_ads/browser/BUILD.gn b/components/brave_ads/browser/BUILD.gn index 0b08175fb1ca..34dfd172d52c 100644 --- a/components/brave_ads/browser/BUILD.gn +++ b/components/brave_ads/browser/BUILD.gn @@ -40,6 +40,8 @@ source_set("browser") { "ads_service_impl.h", "background_helper.cc", "background_helper.h", + "background_helper_linux.cc", + "background_helper_linux.h", "background_helper_mac.mm", "background_helper_mac.h", "background_helper_win.cc", diff --git a/components/brave_ads/browser/background_helper.cc b/components/brave_ads/browser/background_helper.cc index adbc5999c33b..50c6b2e5ef7b 100644 --- a/components/brave_ads/browser/background_helper.cc +++ b/components/brave_ads/browser/background_helper.cc @@ -32,7 +32,7 @@ void BackgroundHelper::RemoveObserver(Observer* observer) { observers_.RemoveObserver(observer); } -#if !defined(OS_MACOSX) && !defined(OS_WIN) +#if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_LINUX) BackgroundHelper* BackgroundHelper::GetInstance() { // just return a dummy background helper for all other platforms return base::Singleton::get(); diff --git a/components/brave_ads/browser/background_helper_linux.cc b/components/brave_ads/browser/background_helper_linux.cc new file mode 100644 index 000000000000..06d3489b54df --- /dev/null +++ b/components/brave_ads/browser/background_helper_linux.cc @@ -0,0 +1,71 @@ +/* Copyright 2018 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_ads/browser/background_helper_linux.h" + +#include "base/bind.h" +#include "base/threading/sequenced_task_runner_handle.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/browser_list.h" +#include "chrome/browser/ui/browser_window.h" +#include "ui/aura/window.h" +#include "ui/aura/window_tree_host.h" +#include "ui/base/x/x11_util.h" + +namespace brave_ads { + + +BackgroundHelperLinux::BackgroundHelperLinux() { + BrowserList::AddObserver(this); + OnBrowserSetLastActive(BrowserList::GetInstance()->GetLastActive()); +} + +BackgroundHelperLinux::~BackgroundHelperLinux() { + BrowserList::RemoveObserver(this); +} + +bool BackgroundHelperLinux::IsForeground() const { + XID xid = 0; + ui::GetXIDProperty(ui::GetX11RootWindow(), "_NET_ACTIVE_WINDOW", &xid); + + for (auto* browser : *BrowserList::GetInstance()) { + auto window = + browser->window()->GetNativeWindow()->GetHost()->GetAcceleratedWidget(); + if (xid == window) + return true; + } + + return false; +} + +void BackgroundHelperLinux::CheckState() { + if (IsForeground()) { + TriggerOnForeground(); + } else { + TriggerOnBackground(); + } +} + +void BackgroundHelperLinux::OnBrowserSetLastActive(Browser* browser) { + base::SequencedTaskRunnerHandle::Get()->PostTask( + FROM_HERE, + base::BindOnce(&BackgroundHelperLinux::CheckState, AsWeakPtr())); +} + +void BackgroundHelperLinux::OnBrowserNoLongerActive(Browser* browser) { + base::SequencedTaskRunnerHandle::Get()->PostTask( + FROM_HERE, + base::BindOnce(&BackgroundHelperLinux::CheckState, AsWeakPtr())); +} + +BackgroundHelperLinux* BackgroundHelperLinux::GetInstance() { + return base::Singleton::get(); +} + +BackgroundHelper* BackgroundHelper::GetInstance() { + return BackgroundHelperLinux::GetInstance(); +} + +} // namespace brave_ads diff --git a/components/brave_ads/browser/background_helper_linux.h b/components/brave_ads/browser/background_helper_linux.h new file mode 100644 index 000000000000..809c13c903c4 --- /dev/null +++ b/components/brave_ads/browser/background_helper_linux.h @@ -0,0 +1,44 @@ +/* Copyright 2018 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_COMPONENTS_BRAVE_ADS_BROWSER_BACKGROUND_HELPER_LINUX_H_ +#define BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_BACKGROUND_HELPER_LINUX_H_ + +#include "base/macros.h" +#include "base/memory/singleton.h" +#include "base/memory/weak_ptr.h" +#include "brave/components/brave_ads/browser/background_helper.h" +#include "chrome/browser/ui/browser_list_observer.h" + +namespace brave_ads { + +class BackgroundHelperLinux : + public BackgroundHelper, + public base::SupportsWeakPtr, + public BrowserListObserver { + public: + BackgroundHelperLinux(); + ~BackgroundHelperLinux() override; + + static BackgroundHelperLinux* GetInstance(); + + private: + friend struct base::DefaultSingletonTraits; + + // BackgroundHelper impl + bool IsForeground() const override; + + // BrowserListObserver overrides + void OnBrowserSetLastActive(Browser* browser) override; + void OnBrowserNoLongerActive(Browser* browser) override; + + void CheckState(); + + DISALLOW_COPY_AND_ASSIGN(BackgroundHelperLinux); +}; + +} // namespace brave_ads + +#endif // BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_BACKGROUND_HELPER_LINUX_H_ diff --git a/components/brave_ads/browser/buildflags/buildflags.gni b/components/brave_ads/browser/buildflags/buildflags.gni index fa3e76f19b0d..a0113746b844 100644 --- a/components/brave_ads/browser/buildflags/buildflags.gni +++ b/components/brave_ads/browser/buildflags/buildflags.gni @@ -1,5 +1,5 @@ import("//build/config/features.gni") declare_args() { - brave_ads_enabled = !is_linux + brave_ads_enabled = !is_android } diff --git a/components/services/bat_ads/manifest.json b/components/services/bat_ads/manifest.json index 6ad126409414..757ed4356703 100644 --- a/components/services/bat_ads/manifest.json +++ b/components/services/bat_ads/manifest.json @@ -1,7 +1,7 @@ { "name": "bat_ads", "display_name": "Bat Ads Service", - "sandbox_type": "utility", + "sandbox_type": "none", "options" : { "instance_sharing" : "shared_instance_across_users" },