Skip to content

Commit

Permalink
wsd: docbroker: send browsersetting to client by downloading it from …
Browse files Browse the repository at this point in the history
…wopihost

Signed-off-by: Rashesh <rashesh.padia@collabora.com>
Change-Id: I82b7f3f74ef04a02275a13de3fd4278f0dea0db9
  • Loading branch information
Rash419 committed Dec 18, 2024
1 parent f65978b commit eaed1d8
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
59 changes: 59 additions & 0 deletions wsd/DocumentBroker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,12 @@ DocumentBroker::updateSessionWithWopiInfo(const std::shared_ptr<ClientSession>&
asyncInstallPresets(session, userSettingsUri, jailPresetsPath);
}

std::string browserSettingUri = wopiFileInfo->getBrowserSettingsUri();
if (_sessions.empty() && !browserSettingUri.empty())
{
asyncSendBrowserSetting(session, browserSettingUri);
}

// Pass the ownership to the client session.
session->setWopiFileInfo(std::move(wopiFileInfo));
session->setUserId(userId);
Expand Down Expand Up @@ -1603,6 +1609,59 @@ void DocumentBroker::asyncInstallPresets(const std::shared_ptr<ClientSession> se
_asyncInstallTask->appendCallback([this](bool){ _asyncInstallTask.reset(); });
}

void DocumentBroker::asyncSendBrowserSetting(const std::shared_ptr<ClientSession>& session,
const std::string& browserSettingUri)
{
// Download the json for browser settings
const Poco::URI settingsUri{ browserSettingUri };
std::shared_ptr<http::Session> httpSession(
StorageConnectionManager::getHttpSession(settingsUri));
http::Request request(settingsUri.getPathAndQuery());
request.set("User-Agent", http::getAgentString());

const std::string uriAnonym = COOLWSD::anonymizeUrl(browserSettingUri);
LOG_DBG("Getting settings from [" << uriAnonym << ']');

http::Session::FinishedCallback finishedCallback =
[uriAnonym, session](const std::shared_ptr<http::Session>& configSession)
{
if (SigUtil::getShutdownRequestFlag())
{
LOG_DBG("Shutdown flagged, giving up on in-flight requests");
return;
}

const std::shared_ptr<const http::Response> httpResponse = configSession->response();
LOG_TRC("DocumentBroker::asyncSendBrowserSetting returned "
<< httpResponse->statusLine().statusCode());

const bool failed = (httpResponse->statusLine().statusCode() != http::StatusCode::OK);
if (failed)
{
if (httpResponse->statusLine().statusCode() == http::StatusCode::Forbidden)
LOG_ERR("Access denied to [" << uriAnonym << ']');
else
LOG_ERR("Invalid URI or access denied to [" << uriAnonym << ']');
return;
}

const std::string& body = httpResponse->getBody();
Poco::JSON::Object::Ptr settings;
if (JsonUtil::parseJSON(body, settings))
{
std::ostringstream jsonStream;
settings->stringify(jsonStream);
}
else
{
LOG_ERR("Parse of browserSetting json: " << uriAnonym << " failed");
}
};

httpSession->setFinishedHandler(std::move(finishedCallback));
httpSession->asyncRequest(request, *_poll);
}

struct PresetRequest
{
std::string _uri;
Expand Down
3 changes: 3 additions & 0 deletions wsd/DocumentBroker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,9 @@ class DocumentBroker : public std::enable_shared_from_this<DocumentBroker>
const std::string& userSettingsUri,
const std::string& presetsPath);

void asyncSendBrowserSetting(const std::shared_ptr<ClientSession>& session,
const std::string& browserSettingUri);

/// Start an asynchronous Installation of the user presets, e.g. autotexts etc, as
/// described at userSettingsUri for installation into presetsPath
static std::shared_ptr<PresetsInstallTask> asyncInstallPresets(SocketPoll& poll, const std::string& userSettingsUri,
Expand Down
8 changes: 8 additions & 0 deletions wsd/FileServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,14 @@ bool FileServerRequestHandler::isAdminLoggedIn(const HTTPRequest& request, http:
fileInfo->set("UserSettings", userSettings);
}

{
Poco::JSON::Object::Ptr browserSettings = new Poco::JSON::Object();
std::string uri = COOLWSD::getServerURL() + "/wopi/settings/browserconfig.json";
browserSettings->set("uri", Util::trim(uri));
browserSettings->set("stamp", etagString);
fileInfo->set("BrowserSettings", browserSettings);
}

fileInfo->set("UserCanWrite", (requestDetails.getParam("permission") != "readonly") ? "true": "false");
fileInfo->set("PostMessageOrigin", postMessageOrigin);
fileInfo->set("LastModifiedTime", localFile->getLastModifiedTime());
Expand Down
2 changes: 2 additions & 0 deletions wsd/wopi/WopiStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ WopiStorage::WOPIFileInfo::WOPIFileInfo(const FileInfo& fileInfo, Poco::JSON::Ob
JsonUtil::findJSONValue(object, "ServerPrivateInfo", _serverPrivateInfo);
if (auto settingsJSON = object->getObject("UserSettings"))
JsonUtil::findJSONValue(settingsJSON, "uri", _userSettingsUri);
if (auto browserSettingsJSON = object->getObject("BrowserSettings"))
JsonUtil::findJSONValue(browserSettingsJSON, "uri", _browserSettingsUri);

JsonUtil::findJSONValue(object, "WatermarkText", _watermarkText);
JsonUtil::findJSONValue(object, "UserCanWrite", _userCanWrite);
Expand Down
3 changes: 3 additions & 0 deletions wsd/wopi/WopiStorage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class WopiStorage : public StorageBase
const std::string& getUserPrivateInfo() const { return _userPrivateInfo; }
const std::string& getServerPrivateInfo() const { return _serverPrivateInfo; }
const std::string& getUserSettingsUri() const { return _userSettingsUri; }
const std::string& getBrowserSettingsUri() const { return _browserSettingsUri; }
const std::string& getWatermarkText() const { return _watermarkText; }
const std::string& getTemplateSaveAs() const { return _templateSaveAs; }
const std::string& getTemplateSource() const { return _templateSource; }
Expand Down Expand Up @@ -108,6 +109,8 @@ class WopiStorage : public StorageBase
std::string _serverPrivateInfo;
/// Uri to get settings json for this user, for autotext location, etc.
std::string _userSettingsUri;
/// Uri to get browser settings json for this user
std::string _browserSettingsUri;
/// In case a watermark has to be rendered on each tile.
std::string _watermarkText;
/// In case we want to use this file as a template, it should be first re-saved under this name (using PutRelativeFile).
Expand Down

0 comments on commit eaed1d8

Please sign in to comment.