Skip to content

Commit

Permalink
feat: implement chrome.tabs.reload to fix PDF Viewer (#33711)
Browse files Browse the repository at this point in the history
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
  • Loading branch information
trop[bot] and codebytere committed Apr 27, 2022
1 parent 178688f commit b6d6f86
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/api/extensions.md
Expand Up @@ -99,6 +99,7 @@ Only `chrome.storage.local` is supported; `chrome.storage.sync` and
The following methods of `chrome.tabs` are supported:

- `chrome.tabs.sendMessage`
- `chrome.tabs.reload`
- `chrome.tabs.executeScript`
- `chrome.tabs.update` (partial support)
- supported properties: `url`, `muted`.
Expand Down
30 changes: 27 additions & 3 deletions shell/browser/extensions/api/tabs/tabs_api.cc
Expand Up @@ -182,6 +182,30 @@ bool TabsExecuteScriptFunction::ShouldRemoveCSS() const {
return false;
}

ExtensionFunction::ResponseAction TabsReloadFunction::Run() {
std::unique_ptr<tabs::Reload::Params> params(
tabs::Reload::Params::Create(args()));
EXTENSION_FUNCTION_VALIDATE(params.get());

bool bypass_cache = false;
if (params->reload_properties.get() &&
params->reload_properties->bypass_cache.get()) {
bypass_cache = *params->reload_properties->bypass_cache;
}

int tab_id = params->tab_id ? *params->tab_id : -1;
auto* contents = electron::api::WebContents::FromID(tab_id);
if (!contents)
return RespondNow(Error("No such tab"));

contents->web_contents()->GetController().Reload(
bypass_cache ? content::ReloadType::BYPASSING_CACHE
: content::ReloadType::NORMAL,
true);

return RespondNow(NoArguments());
}

ExtensionFunction::ResponseAction TabsGetFunction::Run() {
std::unique_ptr<tabs::Get::Params> params(tabs::Get::Params::Create(args()));
EXTENSION_FUNCTION_VALIDATE(params.get());
Expand Down Expand Up @@ -262,17 +286,17 @@ ExtensionFunction::ResponseAction TabsGetZoomSettingsFunction::Run() {
auto* zoom_controller = contents->GetZoomController();
WebContentsZoomController::ZoomMode zoom_mode =
contents->GetZoomController()->zoom_mode();
api::tabs::ZoomSettings zoom_settings;
tabs::ZoomSettings zoom_settings;
ZoomModeToZoomSettings(zoom_mode, &zoom_settings);
zoom_settings.default_zoom_factor = std::make_unique<double>(
blink::PageZoomLevelToZoomFactor(zoom_controller->GetDefaultZoomLevel()));

return RespondNow(
ArgumentList(api::tabs::GetZoomSettings::Results::Create(zoom_settings)));
ArgumentList(tabs::GetZoomSettings::Results::Create(zoom_settings)));
}

ExtensionFunction::ResponseAction TabsSetZoomSettingsFunction::Run() {
using api::tabs::ZoomSettings;
using tabs::ZoomSettings;

std::unique_ptr<tabs::SetZoomSettings::Params> params(
tabs::SetZoomSettings::Params::Create(args()));
Expand Down
10 changes: 10 additions & 0 deletions shell/browser/extensions/api/tabs/tabs_api.h
Expand Up @@ -46,9 +46,19 @@ class TabsExecuteScriptFunction : public ExecuteCodeInTabFunction {
DECLARE_EXTENSION_FUNCTION("tabs.executeScript", TABS_EXECUTESCRIPT)
};

class TabsReloadFunction : public ExtensionFunction {
~TabsReloadFunction() override {}

ResponseAction Run() override;

DECLARE_EXTENSION_FUNCTION("tabs.reload", TABS_RELOAD)
};

class TabsGetFunction : public ExtensionFunction {
~TabsGetFunction() override {}

ResponseAction Run() override;

DECLARE_EXTENSION_FUNCTION("tabs.get", TABS_GET)
};

Expand Down
21 changes: 21 additions & 0 deletions shell/common/extensions/api/tabs.json
Expand Up @@ -120,6 +120,27 @@
}
],
"functions": [
{
"name": "reload",
"type": "function",
"description": "Reload a tab.",
"parameters": [
{"type": "integer", "name": "tabId", "minimum": 0, "optional": true, "description": "The ID of the tab to reload; defaults to the selected tab of the current window."},
{
"type": "object",
"name": "reloadProperties",
"optional": true,
"properties": {
"bypassCache": {
"type": "boolean",
"optional": true,
"description": "Whether using any local cache. Default is false."
}
}
},
{"type": "function", "name": "callback", "optional": true, "parameters": []}
]
},
{
"name": "get",
"type": "function",
Expand Down

0 comments on commit b6d6f86

Please sign in to comment.