Skip to content

Commit

Permalink
Shields gets getViewPreferences and setViewPreferences API
Browse files Browse the repository at this point in the history
  • Loading branch information
petemill committed Jul 30, 2019
1 parent 9a2b83f commit cde4417
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
38 changes: 38 additions & 0 deletions browser/extensions/api/brave_shields_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
#include <utility>

#include "base/strings/string_number_conversions.h"
#include "brave/common/pref_names.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/extension_tab_util.h"
#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_util.h"

Expand Down Expand Up @@ -55,6 +57,42 @@ ExtensionFunction::ResponseAction BraveShieldsAllowScriptsOnceFunction::Run() {
return RespondNow(NoArguments());
}

BraveShieldsGetViewPreferencesFunction::~BraveShieldsGetViewPreferencesFunction() {
}

ExtensionFunction::ResponseAction BraveShieldsGetViewPreferencesFunction::Run() {
// Combine all the relevant prefs in to a JSON object
Profile* profile = Profile::FromBrowserContext(browser_context());
PrefService* prefs = profile->GetPrefs();

auto result = std::make_unique<base::DictionaryValue>();
result->SetBoolean(
"showAdvancedView",
prefs->GetBoolean(kAdvancedViewControlType));

return RespondNow(OneArgument(std::move(result)));
}

BraveShieldsSetViewPreferencesFunction::~BraveShieldsSetViewPreferencesFunction() {
}

ExtensionFunction::ResponseAction BraveShieldsSetViewPreferencesFunction::Run() {
// Get args
std::unique_ptr<brave_shields::SetViewPreferences::Params> params(
brave_shields::SetViewPreferences::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());

// Set prefs only for properties that are provided in the args
Profile* profile = Profile::FromBrowserContext(browser_context());
PrefService* prefs = profile->GetPrefs();
if (params->preferences.show_advanced_view.get()) {
bool settingValue = *params->preferences.show_advanced_view;
prefs->SetBoolean(kAdvancedViewControlType, settingValue);
}

return RespondNow(NoArguments());
}

ExtensionFunction::ResponseAction
BraveShieldsSetBraveShieldsControlTypeFunction::Run() {
std::unique_ptr<brave_shields::SetBraveShieldsControlType::Params> params(
Expand Down
20 changes: 20 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,26 @@ class BraveShieldsAllowScriptsOnceFunction : public UIThreadExtensionFunction {
ResponseAction Run() override;
};

class BraveShieldsGetViewPreferencesFunction : public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveShields.getViewPreferences", UNKNOWN)

protected:
~BraveShieldsGetViewPreferencesFunction() override;

ResponseAction Run() override;
};

class BraveShieldsSetViewPreferencesFunction : public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveShields.setViewPreferences", UNKNOWN)

protected:
~BraveShieldsSetViewPreferencesFunction() override;

ResponseAction Run() override;
};

class BraveShieldsSetBraveShieldsControlTypeFunction
: public UIThreadExtensionFunction {
public:
Expand Down
46 changes: 46 additions & 0 deletions common/extensions/api/brave_shields.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,39 @@
}
]
},
{
"name": "getViewPreferences",
"type": "function",
"description": "Get all the user selected preferences concerning the display of a shields User Interface",
"parameters": [
{
"type": "function",
"name": "callback",
"parameters": [
{
"name": "preferences",
"$ref": "BraveShieldsViewPreferences"
}
]
}
]
},
{
"name": "setViewPreferences",
"type": "function",
"description": "Set any user selected preferences concerning the display of a shields User Interface",
"parameters": [
{
"name": "preferences",
"$ref": "BraveShieldsViewPreferences"
},
{
"name": "callback",
"type": "function",
"parameters": []
}
]
},
{
"name": "setAdControlType",
"type": "function",
Expand Down Expand Up @@ -266,6 +299,19 @@
}
]
}
],
"types": [
{
"id": "BraveShieldsViewPreferences",
"type": "object",
"properties": {
"showAdvancedView": {
"type": "boolean",
"optional": true,
"description": "Whether to show the advanced shields view in the shields UI"
}
}
}
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,17 @@ export const setAllowScriptOriginsOnce = (origins: Array<string>, tabId: number)
resolve()
})
})

export type GetViewPreferencesData = chrome.braveShields.BraveShieldsViewPreferences
export function getViewPreferences (): Promise<GetViewPreferencesData> {
return new Promise<GetViewPreferencesData>(resolve => {
chrome.braveShields.getViewPreferences(resolve)
})
}

export type SetViewPreferencesData = chrome.braveShields.BraveShieldsSetViewPreferencesData
export function setViewPreferences (preferences: SetViewPreferencesData): Promise<void> {
return new Promise<void>(resolve => {
chrome.braveShields.setViewPreferences(preferences, resolve)
})
}
14 changes: 14 additions & 0 deletions components/definitions/chromel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ declare namespace chrome.braveShields {
const getHTTPSEverywhereControlTypeAsync: any
const setNoScriptControlTypeAsync: any
const getNoScriptControlTypeAsync: any

type BraveShieldsViewPreferences = {
showAdvancedView: boolean
}
type BraveShieldsSetViewPreferencesData = {
showAdvancedView?: boolean
}
const getViewPreferences: (
callback: (preferences: BraveShieldsViewPreferences) => void
) => void
const setViewPreferences: (
preferences: BraveShieldsSetViewPreferencesData,
callback: () => void
) => void
}

declare namespace chrome.braveWallet {
Expand Down

0 comments on commit cde4417

Please sign in to comment.