Skip to content

Commit

Permalink
Backed out changeset 5156f24f97bf (bug 1879152) for causing bc failur…
Browse files Browse the repository at this point in the history
…es @ en-US/toolkit/contentanalysis/contentanalysis CLOSED TREE
  • Loading branch information
Sandor Molnar committed Mar 7, 2024
1 parent 1660676 commit b54623a
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 155 deletions.
88 changes: 3 additions & 85 deletions browser/components/contentanalysis/content/ContentAnalysis.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,6 @@ class MapByTopBrowsingContext {
* @returns {MapByTopBrowsingContext} this
*/
setEntry(aBrowsingContext, aValue) {
if (!aValue.request) {
console.error(
"MapByTopBrowsingContext.setEntry() called with a value without a request!"
);
}
let topEntry = this.#map.get(aBrowsingContext.top);
if (!topEntry) {
topEntry = new Map();
Expand All @@ -169,16 +164,6 @@ class MapByTopBrowsingContext {
topEntry.set(aBrowsingContext, aValue);
return this;
}

getAllRequests() {
let requests = [];
this.#map.forEach(topEntry => {
for (let entry of topEntry.values()) {
requests.push(entry.request);
}
});
return requests;
}
}

export const ContentAnalysis = {
Expand Down Expand Up @@ -211,7 +196,7 @@ export const ContentAnalysis = {

ChromeUtils.defineLazyGetter(this, "l10n", function () {
return new Localization(
["branding/brand.ftl", "toolkit/contentanalysis/contentanalysis.ftl"],
["toolkit/contentanalysis/contentanalysis.ftl"],
true
);
});
Expand All @@ -232,54 +217,11 @@ export const ContentAnalysis = {
Services.obs.addObserver(this, "dlp-request-made");
Services.obs.addObserver(this, "dlp-response");
Services.obs.addObserver(this, "quit-application");
Services.obs.addObserver(this, "quit-application-requested");
},

// nsIObserver
async observe(aSubj, aTopic, aData) {
switch (aTopic) {
case "quit-application-requested": {
let pendingRequests =
this.dlpBusyViewsByTopBrowsingContext.getAllRequests();
if (pendingRequests.length) {
let messageBody = this.l10n.formatValueSync(
"contentanalysis-inprogress-quit-message"
);
messageBody = messageBody + "\n\n";
for (const pendingRequest of pendingRequests) {
let name = this._getResourceNameFromNameOrOperationType(
this._getResourceNameOrOperationTypeFromRequest(
pendingRequest,
true
)
);
messageBody = messageBody + name + "\n";
}
let buttonSelected = Services.prompt.confirmEx(
null,
this.l10n.formatValueSync("contentanalysis-inprogress-quit-title"),
messageBody,
Ci.nsIPromptService.BUTTON_POS_0 *
Ci.nsIPromptService.BUTTON_TITLE_IS_STRING +
Ci.nsIPromptService.BUTTON_POS_1 *
Ci.nsIPromptService.BUTTON_TITLE_CANCEL +
Ci.nsIPromptService.BUTTON_POS_0_DEFAULT,
this.l10n.formatValueSync(
"contentanalysis-inprogress-quit-yesbutton"
),
null,
null,
null,
{ value: 0 }
);
if (buttonSelected === 0) {
lazy.gContentAnalysis.cancelAllRequests();
} else {
aSubj.data = true;
}
}
break;
}
case "quit-application": {
this.uninitialize();
break;
Expand Down Expand Up @@ -317,7 +259,7 @@ export const ContentAnalysis = {
);
}
let resourceNameOrOperationType =
this._getResourceNameOrOperationTypeFromRequest(request, false);
this._getResourceNameOrOperationTypeFromRequest(request);
this.requestTokenToRequestInfo.set(request.requestToken, {
browsingContext,
resourceNameOrOperationType,
Expand All @@ -331,10 +273,8 @@ export const ContentAnalysis = {
resourceNameOrOperationType,
browsingContext
),
request,
});
}, slowTimeoutMs),
request,
});
}
break;
Expand Down Expand Up @@ -395,7 +335,6 @@ export const ContentAnalysis = {
args.requestToken,
args.resourceNameOrOperationType
),
request: args.request,
});
}
},
Expand Down Expand Up @@ -492,32 +431,11 @@ export const ContentAnalysis = {
return nameOrOperationType.name;
},

/**
* Gets a name or operation type from a request
*
* @param {object} aRequest The nsIContentAnalysisRequest
* @param {boolean} aStandalone Whether the message is going to be used on its own
* line. This is used to add more context to the message
* if a file is being uploaded rather than just the name
* of the file.
* @returns {object} An object with either a name property that can be used as-is, or
* an operationType property.
*/
_getResourceNameOrOperationTypeFromRequest(aRequest, aStandalone) {
_getResourceNameOrOperationTypeFromRequest(aRequest) {
if (
aRequest.operationTypeForDisplay ==
Ci.nsIContentAnalysisRequest.eCustomDisplayString
) {
if (aStandalone) {
return {
name: this.l10n.formatValueSync(
"contentanalysis-customdisplaystring-description",
{
filename: aRequest.operationDisplayString,
}
),
};
}
return { name: aRequest.operationDisplayString };
}
return { operationType: aRequest.operationTypeForDisplay };
Expand Down
59 changes: 5 additions & 54 deletions toolkit/components/contentanalysis/ContentAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "mozilla/Logging.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/Services.h"
#include "mozilla/StaticMutex.h"
#include "mozilla/StaticPrefs_browser.h"
#include "nsAppRunner.h"
#include "nsComponentManagerUtils.h"
Expand Down Expand Up @@ -284,15 +283,6 @@ nsresult ContentAnalysisRequest::GetFileDigest(const nsAString& aFilePath,
return NS_OK;
}

// Generate an ID that will be shared by all DLP requests.
// Used to cancel all requests on Firefox shutdown.
void ContentAnalysis::GenerateUserActionId() {
nsID id = nsID::GenerateUUID();
mUserActionId = nsPrintfCString("Firefox %s", id.ToString().get());
}

nsCString ContentAnalysis::GetUserActionId() { return mUserActionId; }

static nsresult ConvertToProtobuf(
nsIClientDownloadResource* aIn,
content_analysis::sdk::ClientDownloadRequest_Resource* aOut) {
Expand All @@ -312,8 +302,7 @@ static nsresult ConvertToProtobuf(
}

static nsresult ConvertToProtobuf(
nsIContentAnalysisRequest* aIn, nsCString&& aUserActionId,
int64_t aRequestCount,
nsIContentAnalysisRequest* aIn,
content_analysis::sdk::ContentAnalysisRequest* aOut) {
uint32_t timeout = StaticPrefs::browser_contentanalysis_agent_timeout();
aOut->set_expires_at(time(nullptr) + timeout);
Expand All @@ -330,9 +319,6 @@ static nsresult ConvertToProtobuf(
NS_ENSURE_SUCCESS(rv, rv);
aOut->set_request_token(requestToken.get(), requestToken.Length());

aOut->set_user_action_id(aUserActionId.get());
aOut->set_user_action_requests_count(aRequestCount);

const std::string tag = "dlp"; // TODO:
*aOut->add_tags() = tag;

Expand Down Expand Up @@ -713,9 +699,7 @@ ContentAnalysis::ContentAnalysis()
new ClientPromise::Private("ContentAnalysis::ContentAnalysis")),
mClientCreationAttempted(false),
mCallbackMap("ContentAnalysis::mCallbackMap"),
mWarnResponseDataMap("ContentAnalysis::mWarnResponseDataMap") {
GenerateUserActionId();
}
mWarnResponseDataMap("ContentAnalysis::mWarnResponseDataMap") {}

ContentAnalysis::~ContentAnalysis() {
// Accessing mClientCreationAttempted so need to be on the main thread
Expand Down Expand Up @@ -784,7 +768,7 @@ ContentAnalysis::GetMightBeActive(bool* aMightBeActive) {
nsresult ContentAnalysis::CancelWithError(nsCString aRequestToken,
nsresult aResult) {
return NS_DispatchToMainThread(NS_NewCancelableRunnableFunction(
"ContentAnalysis::CancelWithError",
"ContentAnalysis::RunAnalyzeRequestTask::HandleResponse",
[aResult, aRequestToken = std::move(aRequestToken)] {
RefPtr<ContentAnalysis> owner = GetContentAnalysisFromService();
if (!owner) {
Expand Down Expand Up @@ -832,7 +816,6 @@ RefPtr<ContentAnalysis> ContentAnalysis::GetContentAnalysisFromService() {

nsresult ContentAnalysis::RunAnalyzeRequestTask(
const RefPtr<nsIContentAnalysisRequest>& aRequest, bool aAutoAcknowledge,
int64_t aRequestCount,
const RefPtr<nsIContentAnalysisCallback>& aCallback) {
nsresult rv = NS_ERROR_FAILURE;
auto callbackCopy = aCallback;
Expand All @@ -844,8 +827,7 @@ nsresult ContentAnalysis::RunAnalyzeRequestTask(
});

content_analysis::sdk::ContentAnalysisRequest pbRequest;
rv =
ConvertToProtobuf(aRequest, GetUserActionId(), aRequestCount, &pbRequest);
rv = ConvertToProtobuf(aRequest, &pbRequest);
NS_ENSURE_SUCCESS(rv, rv);

nsCString requestToken;
Expand Down Expand Up @@ -1065,11 +1047,7 @@ ContentAnalysis::AnalyzeContentRequestCallback(
mozilla::services::GetObserverService();
obsServ->NotifyObservers(aRequest, "dlp-request-made", nullptr);

MOZ_ASSERT(NS_IsMainThread());
// since we're on the main thread, don't need to synchronize this
int64_t requestCount = ++mRequestCount;
return RunAnalyzeRequestTask(aRequest, aAutoAcknowledge, requestCount,
aCallback);
return RunAnalyzeRequestTask(aRequest, aAutoAcknowledge, aCallback);
}

NS_IMETHODIMP
Expand Down Expand Up @@ -1097,33 +1075,6 @@ ContentAnalysis::CancelContentAnalysisRequest(const nsACString& aRequestToken) {
return NS_OK;
}

NS_IMETHODIMP
ContentAnalysis::CancelAllRequests() {
mCaClientPromise->Then(
GetCurrentSerialEventTarget(), __func__,
[&](std::shared_ptr<content_analysis::sdk::Client> client) {
auto owner = GetContentAnalysisFromService();
if (!owner) {
// May be shutting down
return;
}
if (!client) {
LOGE("CancelAllRequests got a null client");
return;
}
content_analysis::sdk::ContentAnalysisCancelRequests requests;
requests.set_user_action_id(owner->GetUserActionId().get());
int err = client->CancelRequests(requests);
if (err != 0) {
LOGE("CancelAllRequests got error %d", err);
} else {
LOGD("CancelAllRequests did cancelling of requests");
}
},
[&](nsresult rv) { LOGE("CancelAllRequests failed to get the client"); });
return NS_OK;
}

NS_IMETHODIMP
ContentAnalysis::RespondToWarnDialog(const nsACString& aRequestToken,
bool aAllowContent) {
Expand Down
5 changes: 0 additions & 5 deletions toolkit/components/contentanalysis/ContentAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class ContentAnalysis final : public nsIContentAnalysis {
NS_DECL_NSICONTENTANALYSIS

ContentAnalysis();
nsCString GetUserActionId();

private:
~ContentAnalysis();
Expand All @@ -104,13 +103,11 @@ class ContentAnalysis final : public nsIContentAnalysis {
bool aIsPerUser);
nsresult RunAnalyzeRequestTask(
const RefPtr<nsIContentAnalysisRequest>& aRequest, bool aAutoAcknowledge,
int64_t aRequestCount,
const RefPtr<nsIContentAnalysisCallback>& aCallback);
nsresult RunAcknowledgeTask(
nsIContentAnalysisAcknowledgement* aAcknowledgement,
const nsACString& aRequestToken);
nsresult CancelWithError(nsCString aRequestToken, nsresult aResult);
void GenerateUserActionId();
static RefPtr<ContentAnalysis> GetContentAnalysisFromService();
static void DoAnalyzeRequest(
nsCString aRequestToken,
Expand All @@ -120,8 +117,6 @@ class ContentAnalysis final : public nsIContentAnalysis {
using ClientPromise =
MozPromise<std::shared_ptr<content_analysis::sdk::Client>, nsresult,
false>;
nsCString mUserActionId;
int64_t mRequestCount = 0;
RefPtr<ClientPromise::Private> mCaClientPromise;
// Only accessed from the main thread
bool mClientCreationAttempted;
Expand Down
5 changes: 0 additions & 5 deletions toolkit/components/contentanalysis/nsIContentAnalysis.idl
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,4 @@ interface nsIContentAnalysis : nsISupports
* whether the user wants to allow the request to go through.
*/
void respondToWarnDialog(in ACString aRequestToken, in bool aAllowContent);

/**
* Cancels all outstanding DLP requests. Used on shutdown.
*/
void cancelAllRequests();
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ contentanalysis-slow-agent-dialog-body-clipboard = { $agent } is reviewing what
contentanalysis-slow-agent-dialog-body-dropped-text = { $agent } is reviewing the text you dropped against your organization’s data policies. This may take a moment.
contentanalysis-operationtype-clipboard = clipboard
contentanalysis-operationtype-dropped-text = dropped text
# $filename - The filename associated with the request, such as "aFile.txt"
contentanalysis-customdisplaystring-description = upload of "{ $filename }"
contentanalysis-warndialogtitle = This content may be unsafe
Expand All @@ -44,7 +42,3 @@ contentanalysis-block-message = Your organization uses data-loss prevention soft
# Variables:
# $content - Description of the content being blocked, such as "clipboard" or "aFile.txt"
contentanalysis-error-message = An error occurred in communicating with the data-loss prevention software. Transfer denied for resource: { $content }.
contentanalysis-inprogress-quit-title = Quit { -brand-shorter-name }?
contentanalysis-inprogress-quit-message = Several actions are in progress. If you quit { -brand-shorter-name }, these actions will not be completed.
contentanalysis-inprogress-quit-yesbutton = Yes, quit

0 comments on commit b54623a

Please sign in to comment.