Skip to content

Commit

Permalink
Add foreground/background notifications for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
bridiver authored and tmancey committed Apr 1, 2019
1 parent 7712094 commit 5182218
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 3 deletions.
2 changes: 2 additions & 0 deletions components/brave_ads/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion components/brave_ads/browser/background_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<BackgroundHelper>::get();
Expand Down
71 changes: 71 additions & 0 deletions components/brave_ads/browser/background_helper_linux.cc
Original file line number Diff line number Diff line change
@@ -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<BackgroundHelperLinux>::get();
}

BackgroundHelper* BackgroundHelper::GetInstance() {
return BackgroundHelperLinux::GetInstance();
}

} // namespace brave_ads
44 changes: 44 additions & 0 deletions components/brave_ads/browser/background_helper_linux.h
Original file line number Diff line number Diff line change
@@ -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<BackgroundHelperLinux>,
public BrowserListObserver {
public:
BackgroundHelperLinux();
~BackgroundHelperLinux() override;

static BackgroundHelperLinux* GetInstance();

private:
friend struct base::DefaultSingletonTraits<BackgroundHelperLinux>;

// 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_
2 changes: 1 addition & 1 deletion components/brave_ads/browser/buildflags/buildflags.gni
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import("//build/config/features.gni")

declare_args() {
brave_ads_enabled = true
brave_ads_enabled = !is_android
}
2 changes: 1 addition & 1 deletion components/services/bat_ads/manifest.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand Down

0 comments on commit 5182218

Please sign in to comment.