forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1869417 - Trim included CPP in `toolkit/mozapps/defaultagent/prox…
…y`. r=mhughes,nshukla By splitting `ScheduledTask.cpp`, the proxy can include less CPP code. In turn, this makes it easier to depend on Gecko in `toolkit/mozapps/defaultagent`. Differential Revision: https://phabricator.services.mozilla.com/D196119
- Loading branch information
Showing
8 changed files
with
172 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||
/* vim:set ts=2 sw=2 sts=2 et cindent: */ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "ScheduledTaskRemove.h" | ||
|
||
#include <string> | ||
|
||
#include <comutil.h> | ||
#include <taskschd.h> | ||
|
||
#include "EventLog.h" | ||
#include "mozilla/RefPtr.h" | ||
|
||
namespace mozilla::default_agent { | ||
|
||
#define ENSURE(x) \ | ||
if (FAILED(hr = (x))) { \ | ||
LOG_ERROR(hr); \ | ||
return hr; \ | ||
} | ||
|
||
bool EndsWith(const wchar_t* string, const wchar_t* suffix) { | ||
size_t string_len = wcslen(string); | ||
size_t suffix_len = wcslen(suffix); | ||
if (suffix_len > string_len) { | ||
return false; | ||
} | ||
const wchar_t* substring = string + string_len - suffix_len; | ||
return wcscmp(substring, suffix) == 0; | ||
} | ||
|
||
HRESULT RemoveTasks(const wchar_t* uniqueToken, WhichTasks tasksToRemove) { | ||
if (!uniqueToken || wcslen(uniqueToken) == 0) { | ||
return E_INVALIDARG; | ||
} | ||
|
||
RefPtr<ITaskService> scheduler; | ||
HRESULT hr = S_OK; | ||
ENSURE(CoCreateInstance(CLSID_TaskScheduler, nullptr, CLSCTX_INPROC_SERVER, | ||
IID_ITaskService, getter_AddRefs(scheduler))); | ||
|
||
ENSURE(scheduler->Connect(VARIANT{}, VARIANT{}, VARIANT{}, VARIANT{})); | ||
|
||
RefPtr<ITaskFolder> taskFolder; | ||
BStrPtr folderBStr(SysAllocString(kTaskVendor)); | ||
|
||
hr = scheduler->GetFolder(folderBStr.get(), getter_AddRefs(taskFolder)); | ||
if (FAILED(hr)) { | ||
if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) { | ||
// Don't return an error code if our folder doesn't exist, | ||
// because that just means it's been removed already. | ||
return S_OK; | ||
} else { | ||
return hr; | ||
} | ||
} | ||
|
||
RefPtr<IRegisteredTaskCollection> tasksInFolder; | ||
ENSURE(taskFolder->GetTasks(TASK_ENUM_HIDDEN, getter_AddRefs(tasksInFolder))); | ||
|
||
LONG numTasks = 0; | ||
ENSURE(tasksInFolder->get_Count(&numTasks)); | ||
|
||
std::wstring WdbaTaskName(kTaskName); | ||
WdbaTaskName += uniqueToken; | ||
|
||
// This will be set to the last error that we encounter while deleting tasks. | ||
// This allows us to keep attempting to remove the remaining tasks, even if | ||
// we encounter an error, while still preserving what error we encountered so | ||
// we can return it from this function. | ||
HRESULT deleteResult = S_OK; | ||
// Set to true if we intentionally skip any tasks. | ||
bool tasksSkipped = false; | ||
|
||
for (LONG i = 0; i < numTasks; ++i) { | ||
RefPtr<IRegisteredTask> task; | ||
// IRegisteredTaskCollection's are 1-indexed. | ||
hr = tasksInFolder->get_Item(_variant_t(i + 1), getter_AddRefs(task)); | ||
if (FAILED(hr)) { | ||
deleteResult = hr; | ||
continue; | ||
} | ||
|
||
BSTR taskName; | ||
hr = task->get_Name(&taskName); | ||
if (FAILED(hr)) { | ||
deleteResult = hr; | ||
continue; | ||
} | ||
// Automatically free taskName when we are done with it. | ||
BStrPtr uniqueTaskName(taskName); | ||
|
||
if (tasksToRemove == WhichTasks::WdbaTaskOnly) { | ||
if (WdbaTaskName.compare(taskName) != 0) { | ||
tasksSkipped = true; | ||
continue; | ||
} | ||
} else { // tasksToRemove == WhichTasks::AllTasksForInstallation | ||
if (!EndsWith(taskName, uniqueToken)) { | ||
tasksSkipped = true; | ||
continue; | ||
} | ||
} | ||
|
||
hr = taskFolder->DeleteTask(taskName, 0 /* flags */); | ||
if (FAILED(hr)) { | ||
deleteResult = hr; | ||
} | ||
} | ||
|
||
// If we successfully removed all the tasks, delete the folder too. | ||
if (!tasksSkipped && SUCCEEDED(deleteResult)) { | ||
RefPtr<ITaskFolder> rootFolder; | ||
BStrPtr rootFolderBStr = BStrPtr(SysAllocString(L"\\")); | ||
ENSURE( | ||
scheduler->GetFolder(rootFolderBStr.get(), getter_AddRefs(rootFolder))); | ||
ENSURE(rootFolder->DeleteFolder(folderBStr.get(), 0)); | ||
} | ||
|
||
return deleteResult; | ||
} | ||
|
||
} // namespace mozilla::default_agent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||
/* vim:set ts=2 sw=2 sts=2 et cindent: */ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef __DEFAULT_BROWSER_AGENT_SCHEDULED_TASK_REMOVE_H__ | ||
#define __DEFAULT_BROWSER_AGENT_SCHEDULED_TASK_REMOVE_H__ | ||
|
||
#include <windows.h> | ||
#include <wtypes.h> | ||
|
||
#include <oleauto.h> | ||
|
||
#include "mozilla/UniquePtr.h" | ||
|
||
namespace mozilla::default_agent { | ||
|
||
struct SysFreeStringDeleter { | ||
void operator()(BSTR aPtr) { ::SysFreeString(aPtr); } | ||
}; | ||
using BStrPtr = mozilla::UniquePtr<OLECHAR, SysFreeStringDeleter>; | ||
|
||
static const wchar_t* kTaskVendor = L"" MOZ_APP_VENDOR; | ||
// kTaskName should have the unique token appended before being used. | ||
static const wchar_t* kTaskName = | ||
L"" MOZ_APP_DISPLAYNAME " Default Browser Agent "; | ||
|
||
enum class WhichTasks { | ||
WdbaTaskOnly, | ||
AllTasksForInstallation, | ||
}; | ||
HRESULT RemoveTasks(const wchar_t* uniqueToken, WhichTasks tasksToRemove); | ||
|
||
} // namespace mozilla::default_agent | ||
|
||
#endif // __DEFAULT_BROWSER_AGENT_SCHEDULED_TASK_REMOVE_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters