Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API to open Shields and Rewards dialog UI (uplift to 0.71.x) #3872

Merged
merged 2 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ declare_args() {

source_set("extensions") {
sources = [
"api/brave_action_api.cc",
"api/brave_action_api.h",
"api/brave_extensions_api_client.cc",
"api/brave_extensions_api_client.h",
"api/brave_rewards_api.cc",
Expand Down
169 changes: 169 additions & 0 deletions browser/extensions/api/brave_action_api.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// 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/browser/extensions/api/brave_action_api.h"

#include <memory>
#include <string>
#include <utility>

#include "base/no_destructor.h"
#include "components/keyed_service/core/dependency_manager.h"
#include "components/keyed_service/core/keyed_service_factory.h"
#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
#include "chrome/browser/extensions/api/tabs/windows_util.h"
#include "chrome/browser/extensions/chrome_extension_function_details.h"
#include "chrome/browser/extensions/window_controller.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "extensions/browser/extension_function.h"

namespace {

class BraveActionAPIDependencyManager : public DependencyManager {
public:
static BraveActionAPIDependencyManager* GetInstance() {
static base::NoDestructor<BraveActionAPIDependencyManager> factory;
return factory.get();
}
BraveActionAPIDependencyManager() {}
~BraveActionAPIDependencyManager() override {}

#ifndef NDEBUG
void DumpContextDependencies(void* context) const override {}
#endif // NDEBUG

private:
DISALLOW_COPY_AND_ASSIGN(BraveActionAPIDependencyManager);
};

class BraveActionAPIFactory : public KeyedServiceFactory {
public:
BraveActionAPIFactory() : KeyedServiceFactory("BraveActionAPI",
BraveActionAPIDependencyManager::GetInstance(), SIMPLE) { }

extensions::BraveActionAPI* GetBraveActionAPI(Browser* context) {
return static_cast<extensions::BraveActionAPI*>(
GetServiceForContext(context, true));
}

private:
// KeyedServiceFactory:
std::unique_ptr<KeyedService> BuildServiceInstanceFor(
void* context) const final {
return base::WrapUnique(new extensions::BraveActionAPI());
}
bool IsOffTheRecord(void* context) const final {
return static_cast<Browser*>(context)
->profile()->IsOffTheRecord();
}
void* GetContextToUse(void* context) const final {
return context;
}
void CreateServiceNow(void* context) final {
KeyedServiceFactory::GetServiceForContext(context, true);
}
DISALLOW_COPY_AND_ASSIGN(BraveActionAPIFactory);
};

static BraveActionAPIFactory* GetFactoryInstance() {
static base::NoDestructor<BraveActionAPIFactory> instance;
return instance.get();
}

} // namespace

namespace extensions {
//
// BraveActionAPI::Observer
//
BraveActionAPI::Observer::Observer() { }

BraveActionAPI::Observer::~Observer() { }

//
// BraveActionAPI
//
// static
BraveActionAPI* BraveActionAPI::Get(Browser* context) {
return GetFactoryInstance()->GetBraveActionAPI(context);
}

// static
bool BraveActionAPI::ShowActionUI(
ExtensionFunction* extension_function,
const std::string& extension_id,
std::unique_ptr<int> window_id_param,
std::unique_ptr<std::string> ui_relative_path_param,
std::string* error) {
// Which browser should we send the action to
Browser* browser = nullptr;
// If the windowId is specified, find it. Otherwise get the active
// window for the profile.
if (!window_id_param.get()) {
browser = ChromeExtensionFunctionDetails(extension_function)
.GetCurrentBrowser();
if (!browser) {
*error = tabs_constants::kNoCurrentWindowError;
return false;
}
} else {
int window_id = *window_id_param;
std::string get_browser_error;
if (!windows_util::GetBrowserFromWindowID(
extension_function,
window_id,
WindowController::GetAllWindowFilter(),
&browser,
&get_browser_error)) {
*error = get_browser_error;
return false;
}
}
return ShowActionUI(browser, extension_id, std::move(ui_relative_path_param),
error);
}

// static
bool BraveActionAPI::ShowActionUI(
Browser* browser,
const std::string& extension_id,
std::unique_ptr<std::string> ui_relative_path_param,
std::string* error) {
bool did_notify = BraveActionAPI::Get(browser)->NotifyObservers(extension_id,
std::move(ui_relative_path_param));
if (!did_notify) {
*error = "No toolbar is registered to observe BraveActionUI "
"calls for this window";
return false;
}
return true;
}

BraveActionAPI::BraveActionAPI() {}

BraveActionAPI::~BraveActionAPI() {
}

void BraveActionAPI::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}

void BraveActionAPI::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}

bool BraveActionAPI::NotifyObservers(const std::string& extension_id,
std::unique_ptr<std::string> ui_relative_path_param) {
bool did_notify = false;
for (auto& observer : observers_) {
observer.OnBraveActionShouldTrigger(extension_id,
std::move(ui_relative_path_param));
did_notify = true;
}
return did_notify;
}
} // namespace extensions
62 changes: 62 additions & 0 deletions browser/extensions/api/brave_action_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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_BROWSER_EXTENSIONS_API_BRAVE_ACTION_API_H_
#define BRAVE_BROWSER_EXTENSIONS_API_BRAVE_ACTION_API_H_

#include <memory>
#include <string>

#include "components/keyed_service/core/keyed_service.h"
#include "extensions/browser/extension_function.h"
#include "extensions/common/extension.h"

class Browser;

namespace extensions {
class BraveActionAPI : public KeyedService {
public:
class Observer {
public:
Observer();
virtual void OnBraveActionShouldTrigger(
const std::string& extension_id,
std::unique_ptr<std::string> ui_relative_path) = 0;

protected:
virtual ~Observer();
};

static BraveActionAPI* Get(Browser* context);
static bool ShowActionUI(
ExtensionFunction* extension_function,
const std::string& extension_id,
std::unique_ptr<int> window_id,
std::unique_ptr<std::string> ui_relative_path,
std::string* error);
static bool ShowActionUI(
Browser* browser,
const std::string& extension_id,
std::unique_ptr<std::string> ui_relative_path,
std::string* error);
BraveActionAPI();
~BraveActionAPI() override;

// Add or remove observers.
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);

protected:
bool NotifyObservers(const std::string& extension_id,
std::unique_ptr<std::string> ui_relative_path_param);

private:
base::ObserverList<Observer>::Unchecked observers_;

DISALLOW_COPY_AND_ASSIGN(BraveActionAPI);
};
} // namespace extensions

#endif // BRAVE_BROWSER_EXTENSIONS_API_BRAVE_ACTION_API_H_
22 changes: 22 additions & 0 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
#include "base/bind.h"
#include "base/strings/string_number_conversions.h"
#include "brave/browser/brave_rewards/tip_dialog.h"
#include "brave/browser/extensions/api/brave_action_api.h"
#include "brave/common/extensions/api/brave_rewards.h"
#include "brave/common/extensions/extension_constants.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/browser/ads_service_factory.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "brave/components/brave_rewards/browser/rewards_service_factory.h"
#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
#include "chrome/browser/extensions/chrome_extension_function_details.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/web_contents.h"
Expand Down Expand Up @@ -53,6 +56,25 @@ ExtensionFunction::ResponseAction BraveRewardsCreateWalletFunction::Run() {
return RespondNow(NoArguments());
}

BraveRewardsOpenBrowserActionUIFunction::
~BraveRewardsOpenBrowserActionUIFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsOpenBrowserActionUIFunction::Run() {
std::unique_ptr<brave_rewards::OpenBrowserActionUI::Params> params(
brave_rewards::OpenBrowserActionUI::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
std::string error;
if (!BraveActionAPI::ShowActionUI(this,
brave_rewards_extension_id,
std::move(params->window_id),
std::move(params->relative_path), &error)) {
return RespondNow(Error(error));
}
return RespondNow(NoArguments());
}

BraveRewardsTipSiteFunction::~BraveRewardsTipSiteFunction() {
}

Expand Down
11 changes: 11 additions & 0 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ class BraveRewardsCreateWalletFunction : public ExtensionFunction {
void OnCreateWallet(int32_t result);
};

class BraveRewardsOpenBrowserActionUIFunction :
public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.openBrowserActionUI", UNKNOWN)

protected:
~BraveRewardsOpenBrowserActionUIFunction() override;

ResponseAction Run() override;
};

class BraveRewardsTipSiteFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.tipSite", UNKNOWN)
Expand Down
21 changes: 21 additions & 0 deletions browser/extensions/api/brave_shields_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
#include <utility>

#include "base/strings/string_number_conversions.h"
#include "brave/browser/extensions/api/brave_action_api.h"
#include "brave/common/extensions/api/brave_shields.h"
#include "brave/common/extensions/extension_constants.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h"
#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
#include "chrome/browser/extensions/chrome_extension_function_details.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/web_contents.h"
Expand Down Expand Up @@ -55,6 +57,25 @@ ExtensionFunction::ResponseAction BraveShieldsAllowScriptsOnceFunction::Run() {
return RespondNow(NoArguments());
}

BraveShieldsOpenBrowserActionUIFunction::
~BraveShieldsOpenBrowserActionUIFunction() {
}

ExtensionFunction::ResponseAction
BraveShieldsOpenBrowserActionUIFunction::Run() {
std::unique_ptr<brave_shields::OpenBrowserActionUI::Params> params(
brave_shields::OpenBrowserActionUI::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
std::string error;
if (!BraveActionAPI::ShowActionUI(this,
brave_extension_id,
std::move(params->window_id),
std::move(params->relative_path), &error)) {
return RespondNow(Error(error));
}
return RespondNow(NoArguments());
}

ExtensionFunction::ResponseAction
BraveShieldsSetBraveShieldsEnabledFunction::Run() {
std::unique_ptr<brave_shields::SetBraveShieldsEnabled::Params> params(
Expand Down
11 changes: 11 additions & 0 deletions browser/extensions/api/brave_shields_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ class BraveShieldsAllowScriptsOnceFunction : public ExtensionFunction {
ResponseAction Run() override;
};

class BraveShieldsOpenBrowserActionUIFunction :
public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveShields.openBrowserActionUI", UNKNOWN)

protected:
~BraveShieldsOpenBrowserActionUIFunction() override;

ResponseAction Run() override;
};

class BraveShieldsSetBraveShieldsEnabledFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveShields.setBraveShieldsEnabled", UNKNOWN)
Expand Down
1 change: 1 addition & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ source_set("ui") {
"//brave/browser/resources/extensions:resources",
"//brave/components/brave_extension:generated_resources",
"//brave/components/brave_extension:static_resources",
"//brave/browser/extensions",
"//chrome/browser/extensions",
"//extensions/browser",
]
Expand Down
7 changes: 7 additions & 0 deletions browser/ui/brave_actions/brave_action_view_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ ui::MenuModel* BraveActionViewController::GetContextMenu() {
return nullptr;
}

bool BraveActionViewController::ExecuteActionUI(
std::string relative_path) {
return TriggerPopupWithUrl(PopupShowAction::SHOW_POPUP,
extension()->GetResourceURL(relative_path),
true);
}

ExtensionActionViewController*
BraveActionViewController::GetPreferredPopupViewController() {
return this;
Expand Down
Loading