-
Notifications
You must be signed in to change notification settings - Fork 879
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add foreground/background notifications for linux
- Loading branch information
Showing
6 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters