Skip to content

Commit

Permalink
add logs to the files explorer integration DLL
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu Gallien <[email protected]>
  • Loading branch information
mgallien committed Mar 19, 2024
1 parent 5b705d9 commit 331e89a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
20 changes: 17 additions & 3 deletions shell_integration/windows/NCContextMenu/NCClientInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@
#include <sstream>
#include <iterator>
#include <unordered_set>
#include <locale>
#include <codecvt>

using namespace std;

#define PIPE_TIMEOUT 5*1000 //ms

NCClientInterface::ContextMenuInfo NCClientInterface::FetchInfo(const std::wstring &files)
NCClientInterface::ContextMenuInfo NCClientInterface::FetchInfo(const std::wstring &files, std::ofstream &logger)

Check warning on line 36 in shell_integration/windows/NCContextMenu/NCClientInterface.cpp

View workflow job for this annotation

GitHub Actions / build

shell_integration/windows/NCContextMenu/NCClientInterface.cpp:36:55 [modernize-use-trailing-return-type]

use a trailing return type for this function

Check warning on line 36 in shell_integration/windows/NCContextMenu/NCClientInterface.cpp

View workflow job for this annotation

GitHub Actions / build

shell_integration/windows/NCContextMenu/NCClientInterface.cpp:36:55 [readability-function-cognitive-complexity]

function 'FetchInfo' has cognitive complexity of 29 (threshold 25)
{
auto pipename = CommunicationSocket::DefaultPipePath();

CommunicationSocket socket;
if (!WaitNamedPipe(pipename.data(), PIPE_TIMEOUT)) {
logger << "error with WaitNamedPipe" << std::endl;
return {};
}
if (!socket.Connect(pipename)) {
logger << "error with Connect" << std::endl;
return {};
}
socket.SendMsg(L"GET_STRINGS:CONTEXT_MENU_TITLE\n");
Expand All @@ -51,19 +55,25 @@ NCClientInterface::ContextMenuInfo NCClientInterface::FetchInfo(const std::wstri
constexpr auto noReplyTimeout = 20;
constexpr auto replyTimeout = 200;
bool receivedReplayFromDesktopClient = false;
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;

Check warning on line 58 in shell_integration/windows/NCContextMenu/NCClientInterface.cpp

View workflow job for this annotation

GitHub Actions / build

shell_integration/windows/NCContextMenu/NCClientInterface.cpp:58:54 [cppcoreguidelines-init-variables]

variable 'converter' is not initialized
while ((!receivedReplayFromDesktopClient && sleptCount < noReplyTimeout) || (receivedReplayFromDesktopClient && sleptCount < replyTimeout)) {
logger << "trying to read a line" << std::endl;
if (socket.ReadLine(&response)) {
logger << "received: " << converter.to_bytes(response) << std::endl;
if (StringUtil::begins_with(response, wstring(L"REGISTER_PATH:"))) {
logger << "received: REGISTER_PATH" << std::endl;
wstring responsePath = response.substr(14); // length of REGISTER_PATH
info.watchedDirectories.push_back(responsePath);
}
else if (StringUtil::begins_with(response, wstring(L"STRING:"))) {
logger << "received: STRING" << std::endl;
wstring stringName, stringValue;

Check warning on line 70 in shell_integration/windows/NCContextMenu/NCClientInterface.cpp

View workflow job for this annotation

GitHub Actions / build

shell_integration/windows/NCContextMenu/NCClientInterface.cpp:70:17 [readability-isolate-declaration]

multiple declarations in a single statement reduces readability
if (!StringUtil::extractChunks(response, stringName, stringValue))
continue;
if (stringName == L"CONTEXT_MENU_TITLE")
info.contextMenuTitle = move(stringValue);
} else if (StringUtil::begins_with(response, wstring(L"MENU_ITEM:"))) {
logger << "received: MENU_ITEM" << std::endl;
wstring commandName, flags, title;

Check warning on line 77 in shell_integration/windows/NCContextMenu/NCClientInterface.cpp

View workflow job for this annotation

GitHub Actions / build

shell_integration/windows/NCContextMenu/NCClientInterface.cpp:77:17 [readability-isolate-declaration]

multiple declarations in a single statement reduces readability
if (!StringUtil::extractChunks(response, commandName, flags, title))
continue;
Expand All @@ -72,14 +82,18 @@ NCClientInterface::ContextMenuInfo NCClientInterface::FetchInfo(const std::wstri
receivedReplayFromDesktopClient = true;
continue;
} else if (StringUtil::begins_with(response, wstring(L"GET_MENU_ITEMS:END"))) {
logger << "received: GET_MENU_ITEMS:END" << std::endl;
break; // Stop once we completely received the last sent request
} else {
logger << "received: another reply" << std::endl;
}
}
else {
} else {
logger << "received nothing" << std::endl;
Sleep(50);
++sleptCount;
}
}

return info;
}

Expand Down
3 changes: 2 additions & 1 deletion shell_integration/windows/NCContextMenu/NCClientInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <mutex>
#include <atomic>
#include <condition_variable>
#include <fstream>

class CommunicationSocket;

Expand All @@ -52,7 +53,7 @@ class NCClientInterface
};
std::vector<MenuItem> menuItems;
};
static ContextMenuInfo FetchInfo(const std::wstring &files);
static ContextMenuInfo FetchInfo(const std::wstring &files, std::ofstream &logger);
static void SendRequest(const wchar_t *verb, const std::wstring &path);
};

Expand Down
18 changes: 17 additions & 1 deletion shell_integration/windows/NCContextMenu/NCContextMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,22 @@
#include <StringUtil.h>
#include <strsafe.h>

#include <iostream>
#include <fstream>

extern long g_cDllRef;

Check warning on line 27 in shell_integration/windows/NCContextMenu/NCContextMenu.cpp

View workflow job for this annotation

GitHub Actions / build

shell_integration/windows/NCContextMenu/NCContextMenu.cpp:27:13 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'g_cDllRef' is non-const and globally accessible, consider making it const

NCContextMenu::NCContextMenu(void)
: m_cRef(1)
{
InterlockedIncrement(&g_cDllRef);
m_logger.open("c:\\test.log");
m_logger << "hello world" << std::endl;
}

NCContextMenu::~NCContextMenu(void)
{
m_logger << "NCContextMenu::~NCContextMenu" << std::endl;
InterlockedDecrement(&g_cDllRef);
}

Expand All @@ -39,6 +45,7 @@ NCContextMenu::~NCContextMenu(void)
// Query to the interface the component supported.
IFACEMETHODIMP NCContextMenu::QueryInterface(REFIID riid, void **ppv)
{
m_logger << "NCContextMenu::QueryInterface" << std::endl;
static const QITAB qit[] =
{
QITABENT(NCContextMenu, IContextMenu),
Expand All @@ -51,12 +58,14 @@ IFACEMETHODIMP NCContextMenu::QueryInterface(REFIID riid, void **ppv)
// Increase the reference count for an interface on an object.
IFACEMETHODIMP_(ULONG) NCContextMenu::AddRef()
{
m_logger << "NCContextMenu::AddRef" << std::endl;
return InterlockedIncrement(&m_cRef);
}

// Decrease the reference count for an interface on an object.
IFACEMETHODIMP_(ULONG) NCContextMenu::Release()
{
m_logger << "NCContextMenu::Release" << std::endl;
ULONG cRef = InterlockedDecrement(&m_cRef);
if (0 == cRef) {
delete this;
Expand All @@ -74,6 +83,7 @@ IFACEMETHODIMP_(ULONG) NCContextMenu::Release()
IFACEMETHODIMP NCContextMenu::Initialize(
LPCITEMIDLIST, LPDATAOBJECT pDataObj, HKEY)
{
m_logger << "NCContextMenu::Initialize" << std::endl;
m_selectedFiles.clear();

if (!pDataObj) {
Expand Down Expand Up @@ -129,17 +139,20 @@ void InsertSeperator(HMENU hMenu, UINT indexMenu)

IFACEMETHODIMP NCContextMenu::QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags)
{
m_logger << "NCContextMenu::QueryContextMenu" << std::endl;
// If uFlags include CMF_DEFAULTONLY then we should not do anything.
if (CMF_DEFAULTONLY & uFlags)
{
return MAKE_HRESULT(SEVERITY_SUCCESS, 0, USHORT(0));
}

m_info = NCClientInterface::FetchInfo(m_selectedFiles);
m_info = NCClientInterface::FetchInfo(m_selectedFiles, m_logger);
if (m_info.menuItems.empty()) {
m_logger << "NCContextMenu::QueryContextMenu " << "empty info" << std::endl;
return MAKE_HRESULT(SEVERITY_SUCCESS, 0, USHORT(0));
}

m_logger << "NCContextMenu::QueryContextMenu" << "insert separator" << std::endl;
InsertSeperator(hMenu, indexMenu++);

HMENU hSubmenu = CreateMenu();
Expand All @@ -153,6 +166,7 @@ IFACEMETHODIMP NCContextMenu::QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT
if (!InsertMenuItem(hMenu, indexMenu++, TRUE, &mii))
return HRESULT_FROM_WIN32(GetLastError());
}
m_logger << "NCContextMenu::QueryContextMenu" << "insert separator" << std::endl;
InsertSeperator(hMenu, indexMenu++);

UINT indexSubMenu = 0;
Expand All @@ -179,6 +193,7 @@ IFACEMETHODIMP NCContextMenu::QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT

IFACEMETHODIMP NCContextMenu::InvokeCommand(LPCMINVOKECOMMANDINFO pici)
{
m_logger << "NCContextMenu::InvokeCommand" << std::endl;
std::wstring command;

CMINVOKECOMMANDINFOEX *piciEx = nullptr;
Expand Down Expand Up @@ -222,6 +237,7 @@ IFACEMETHODIMP NCContextMenu::InvokeCommand(LPCMINVOKECOMMANDINFO pici)
IFACEMETHODIMP NCContextMenu::GetCommandString(UINT_PTR idCommand,
UINT uFlags, UINT *pwReserved, LPSTR pszName, UINT cchMax)
{
m_logger << "NCContextMenu::GetCommandString" << std::endl;
if (idCommand < m_info.menuItems.size() && uFlags == GCS_VERBW) {
return StringCchCopyW(reinterpret_cast<PWSTR>(pszName), cchMax,
m_info.menuItems[idCommand].command.data());
Expand Down
8 changes: 7 additions & 1 deletion shell_integration/windows/NCContextMenu/NCContextMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
#define NCCONTEXTMENU_H

#pragma once
#include "NCClientInterface.h"

#include <shlobj.h> // For IShellExtInit and IContextMenu

#include <string>
#include "NCClientInterface.h"
#include <fstream>
#include <iostream>

class NCContextMenu : public IShellExtInit, public IContextMenu
{
Expand Down Expand Up @@ -48,6 +52,8 @@ class NCContextMenu : public IShellExtInit, public IContextMenu
// The name of the selected files (separated by '\x1e')
std::wstring m_selectedFiles;
NCClientInterface::ContextMenuInfo m_info;

std::ofstream m_logger;
};

#endif //NCCONTEXTMENU_H

0 comments on commit 331e89a

Please sign in to comment.