From caddc83cfe1be3837d060467835e4a705d9d0cb4 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Mon, 11 Apr 2022 22:51:10 +0200 Subject: [PATCH] feat: implement chrome.tabs.reload (#33560) --- docs/api/extensions.md | 1 + shell/browser/extensions/api/tabs/tabs_api.cc | 30 +++++++++++++++++-- shell/browser/extensions/api/tabs/tabs_api.h | 10 +++++++ shell/common/extensions/api/tabs.json | 21 +++++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/docs/api/extensions.md b/docs/api/extensions.md index afaff0ef6348..c8fbb015a1f7 100644 --- a/docs/api/extensions.md +++ b/docs/api/extensions.md @@ -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`. diff --git a/shell/browser/extensions/api/tabs/tabs_api.cc b/shell/browser/extensions/api/tabs/tabs_api.cc index 8fa5783e3370..7bc0a0b5e486 100644 --- a/shell/browser/extensions/api/tabs/tabs_api.cc +++ b/shell/browser/extensions/api/tabs/tabs_api.cc @@ -182,6 +182,30 @@ bool TabsExecuteScriptFunction::ShouldRemoveCSS() const { return false; } +ExtensionFunction::ResponseAction TabsReloadFunction::Run() { + std::unique_ptr 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 params(tabs::Get::Params::Create(args())); EXTENSION_FUNCTION_VALIDATE(params.get()); @@ -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( 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 params( tabs::SetZoomSettings::Params::Create(args())); diff --git a/shell/browser/extensions/api/tabs/tabs_api.h b/shell/browser/extensions/api/tabs/tabs_api.h index f6078610d130..eb3a86612e7f 100644 --- a/shell/browser/extensions/api/tabs/tabs_api.h +++ b/shell/browser/extensions/api/tabs/tabs_api.h @@ -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) }; diff --git a/shell/common/extensions/api/tabs.json b/shell/common/extensions/api/tabs.json index 36276d2f4056..14d83b015a80 100644 --- a/shell/common/extensions/api/tabs.json +++ b/shell/common/extensions/api/tabs.json @@ -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",