Commit 69d992ca authored by Joel Hockey's avatar Joel Hockey Committed by Commit Bot

Add terminalPrivate.getCroshSettings to allow migration

Terminal System app will call
chrome.terminalPrivate.getCroshSettings to get the current
settings used by crosh in order to migrate any settings.

This migration code will be removed after M83.

Terminal System app will add some local storage to
indicate that migration has been done, so this function
should only be called once ever by Terminal System App.

This code reads from the crosh extension sync storage where
settings are currently stored.  It is a close copy of
extensions/browser/api/storage/storage_api code.
https://cs.chromium.org/chromium/src/extensions/browser/api/storage/storage_api.cc?l=71-83&rcl=115bc01b385b57e8f96c4fa0b117e887a26cd55e

TBR=benwells@chromium.org

Bug: 1019021
Change-Id: I5287e804ac2c2a8c1eaf2fe7f14c8012f88b1c7a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1899626
Commit-Queue: Joel Hockey <joelhockey@chromium.org>
Reviewed-by: default avatarJulian Watson <juwa@google.com>
Cr-Commit-Position: refs/heads/master@{#712899}
parent ba215143
......@@ -18,7 +18,10 @@ namespace {
const char kCroshExtensionEntryPoint[] = "/html/crosh.html";
const Extension* GetTerminalExtension(Profile* profile) {
} // namespace
const Extension* TerminalExtensionHelper::GetTerminalExtension(
Profile* profile) {
// Search order for terminal extensions.
// We prefer hterm-dev, then hterm, then the builtin crosh extension.
static const char* const kPossibleAppIds[] = {
......@@ -39,11 +42,9 @@ const Extension* GetTerminalExtension(Profile* profile) {
return extension;
}
return NULL;
return nullptr;
}
} // namespace
GURL TerminalExtensionHelper::GetCroshExtensionURL(Profile* profile) {
GURL url;
const extensions::Extension* extension = GetTerminalExtension(profile);
......
......@@ -13,8 +13,16 @@ class Profile;
namespace extensions {
class Extension;
class TerminalExtensionHelper {
public:
// Returns the crosh extension. It is the first found out of:
// 1. nassh dev : okddffdblfhhnmhodogpojmfkjmhinfp
// 2. nassh : pnhechapfaindjhompbnflcldabbghjo
// 3. crosh builtin: nkoccljplnhpfnfiajclkommnmllphnl
static const Extension* GetTerminalExtension(Profile* profile);
// Returns Hterm extension's entry point for Crosh. If no HTerm extension is
// installed, returns empty url.
static GURL GetCroshExtensionURL(Profile* profile);
......
......@@ -28,10 +28,13 @@
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/api/storage/settings_namespace.h"
#include "extensions/browser/api/storage/storage_frontend.h"
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/common/extension.h"
namespace terminal_private = extensions::api::terminal_private;
namespace OnTerminalResize =
......@@ -118,10 +121,7 @@ int GetTabOrWindowSessionId(content::BrowserContext* browser_context,
namespace extensions {
TerminalPrivateOpenTerminalProcessFunction::
TerminalPrivateOpenTerminalProcessFunction() {}
TerminalPrivateOpenTerminalProcessFunction::
~TerminalPrivateOpenTerminalProcessFunction() {}
~TerminalPrivateOpenTerminalProcessFunction() = default;
ExtensionFunction::ResponseAction
TerminalPrivateOpenTerminalProcessFunction::Run() {
......@@ -243,7 +243,7 @@ void TerminalPrivateOpenTerminalProcessFunction::OpenOnRegistryTaskRunner(
base::BindOnce(callback, success, terminal_id));
}
TerminalPrivateSendInputFunction::~TerminalPrivateSendInputFunction() {}
TerminalPrivateSendInputFunction::~TerminalPrivateSendInputFunction() = default;
void TerminalPrivateOpenTerminalProcessFunction::RespondOnUIThread(
bool success,
......@@ -285,7 +285,7 @@ void TerminalPrivateSendInputFunction::RespondOnUIThread(bool success) {
}
TerminalPrivateCloseTerminalProcessFunction::
~TerminalPrivateCloseTerminalProcessFunction() {}
~TerminalPrivateCloseTerminalProcessFunction() = default;
ExtensionFunction::ResponseAction
TerminalPrivateCloseTerminalProcessFunction::Run() {
......@@ -320,7 +320,7 @@ void TerminalPrivateCloseTerminalProcessFunction::RespondOnUIThread(
}
TerminalPrivateOnTerminalResizeFunction::
~TerminalPrivateOnTerminalResizeFunction() {}
~TerminalPrivateOnTerminalResizeFunction() = default;
ExtensionFunction::ResponseAction
TerminalPrivateOnTerminalResizeFunction::Run() {
......@@ -356,7 +356,7 @@ void TerminalPrivateOnTerminalResizeFunction::RespondOnUIThread(bool success) {
Respond(OneArgument(std::make_unique<base::Value>(success)));
}
TerminalPrivateAckOutputFunction::~TerminalPrivateAckOutputFunction() {}
TerminalPrivateAckOutputFunction::~TerminalPrivateAckOutputFunction() = default;
ExtensionFunction::ResponseAction TerminalPrivateAckOutputFunction::Run() {
std::unique_ptr<AckOutput::Params> params(AckOutput::Params::Create(*args_));
......@@ -388,4 +388,32 @@ void TerminalPrivateAckOutputFunction::AckOutputOnRegistryTaskRunner(
chromeos::ProcessProxyRegistry::Get()->AckOutput(terminal_id);
}
TerminalPrivateGetCroshSettingsFunction::
~TerminalPrivateGetCroshSettingsFunction() = default;
ExtensionFunction::ResponseAction
TerminalPrivateGetCroshSettingsFunction::Run() {
const Extension* crosh_extension =
TerminalExtensionHelper::GetTerminalExtension(
Profile::FromBrowserContext(browser_context()));
StorageFrontend* frontend = StorageFrontend::Get(browser_context());
frontend->RunWithStorage(
crosh_extension, settings_namespace::SYNC,
base::Bind(&TerminalPrivateGetCroshSettingsFunction::AsyncRunWithStorage,
this));
return RespondLater();
}
void TerminalPrivateGetCroshSettingsFunction::AsyncRunWithStorage(
ValueStore* storage) {
ValueStore::ReadResult result = storage->Get();
ExtensionFunction::ResponseValue response =
result.status().ok() ? OneArgument(result.PassSettings())
: Error(result.status().message);
base::PostTask(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(&TerminalPrivateGetCroshSettingsFunction::Respond, this,
std::move(response)));
}
} // namespace extensions
......@@ -9,6 +9,7 @@
#include <vector>
#include "extensions/browser/extension_function.h"
#include "extensions/browser/value_store/value_store.h"
namespace extensions {
......@@ -18,8 +19,6 @@ class TerminalPrivateOpenTerminalProcessFunction : public ExtensionFunction {
DECLARE_EXTENSION_FUNCTION("terminalPrivate.openTerminalProcess",
TERMINALPRIVATE_OPENTERMINALPROCESS)
TerminalPrivateOpenTerminalProcessFunction();
protected:
~TerminalPrivateOpenTerminalProcessFunction() override;
......@@ -109,6 +108,22 @@ class TerminalPrivateAckOutputFunction : public ExtensionFunction {
void AckOutputOnRegistryTaskRunner(const std::string& terminal_id);
};
// TODO(crbug.com/1019021): Remove this function after M-83.
// Be sure to first remove the callsite in the terminal system app.
class TerminalPrivateGetCroshSettingsFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("terminalPrivate.getCroshSettings",
TERMINALPRIVATE_GETCROSHSETTINGS)
protected:
~TerminalPrivateGetCroshSettingsFunction() override;
ExtensionFunction::ResponseAction Run() override;
private:
void AsyncRunWithStorage(ValueStore* storage);
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_TERMINAL_TERMINAL_PRIVATE_API_H_
......@@ -155,6 +155,30 @@
"description": "The id of the process to which |onProcessOutput| was dispatched."
}
]
},
{
"name": "getCroshSettings",
"type": "function",
"description": "Returns settings used by the crosh extension. This function is called by the terminal system app the first time it is run to migrate any previous settings.",
"parameters": [
{
"name": "callback",
"type": "function",
"description": "Callback that will be called with settings.",
"parameters": [
{
"name": "settings",
"type": "object",
"additionalProperties":
{
"type": "any" ,
"preserveNull": true
},
"preserveNull": true
}
]
}
]
}
],
"events": [
......
......@@ -1464,6 +1464,7 @@ enum HistogramValue {
AUTOTESTPRIVATE_CREATENEWDESK = 1401,
AUTOTESTPRIVATE_ACTIVATEDESKATINDEX = 1402,
AUTOTESTPRIVATE_REMOVEACTIVEDESK = 1403,
TERMINALPRIVATE_GETCROSHSETTINGS = 1404,
// Last entry: Add new entries above, then run:
// python tools/metrics/histograms/update_extension_histograms.py
ENUM_BOUNDARY
......
......@@ -21250,6 +21250,7 @@ Called by update_net_error_codes.py.-->
<int value="1401" label="AUTOTESTPRIVATE_CREATENEWDESK"/>
<int value="1402" label="AUTOTESTPRIVATE_ACTIVATEDESKATINDEX"/>
<int value="1403" label="AUTOTESTPRIVATE_REMOVEACTIVEDESK"/>
<int value="1404" label="TERMINALPRIVATE_GETCROSHSETTINGS"/>
</enum>
<enum name="ExtensionIconState">
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment