1
0

Mo changes

This commit is contained in:
Scott E. Graves
2017-03-23 19:09:10 -05:00
parent edfa2cb7b5
commit d09893ba96
7 changed files with 71 additions and 103 deletions

View File

@@ -62,6 +62,7 @@ public:
private:
static void ExecuteSetter(CefRefPtr<CefV8Context> context, CefRefPtr<CefV8Value> obj, const SString& method, const SString& value);
static void ExecuteSetter(CefRefPtr<CefV8Context> context, CefRefPtr<CefV8Value> obj, const SString& method, CefRefPtr<CefV8Value> value);
void ShutdownServices();
void SiaApiRefreshCallback(CefRefPtr<CefV8Context> context, const Api::CSiaCurl& siaCurl, Api::CSiaDriveConfig* siaDriveConfig);
private:

View File

@@ -45,29 +45,21 @@ public:
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
// CefLoadHandler methods:
virtual void OnLoadError(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl) OVERRIDE;
// Request that all existing browser windows close.
void CloseAllBrowsers(bool forceClose);
bool IsClosing() const { return _isClosing; }
private:
// Platform-specific implementation.
void PlatformTitleChange(CefRefPtr<CefBrowser> browser, const CefString& title);
// True if the application is using the Views framework.
private:
const bool _useViews;
// List of existing browser windows. Only accessed on the CEF UI thread.
typedef std::list<CefRefPtr<CefBrowser> > BrowserList;
BrowserList _browserList;
bool _isClosing;
// Include the default reference counting implementation.
IMPLEMENT_REFCOUNTING(CSiaDriveHandler);
};
NS_END(1)

View File

@@ -9,7 +9,6 @@
using namespace Sia;
using namespace Sia::Api;
// Entry point function for all processes.
int APIENTRY wWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
@@ -30,8 +29,9 @@ int APIENTRY wWinMain(HINSTANCE hInstance,
CefSettings settings;
settings.no_sandbox = true;
settings.remote_debugging_port = 8080;
#ifdef _DEBUG
settings.single_process = true;
#endif
CefInitialize(mainArgs, settings, app, nullptr);
CefRunMessageLoop();

View File

@@ -154,12 +154,9 @@ public:
return false;
}
// Provide the reference counting implementation for this class.
IMPLEMENT_REFCOUNTING(FunctionHandler);
};
// When using the Views framework this object provides the delegate
// implementation for the CefWindow that hosts the Views-based browser.
class SimpleWindowDelegate :
public CefWindowDelegate
{
@@ -171,11 +168,9 @@ public:
void OnWindowCreated(CefRefPtr<CefWindow> window) OVERRIDE
{
// Add the browser view and show the window.
window->AddChildView(_browserView);
window->Show();
// Give keyboard focus to the browser view.
_browserView->RequestFocus();
}
@@ -186,7 +181,6 @@ public:
bool CanClose(CefRefPtr<CefWindow> window) OVERRIDE
{
// Allow the window to close if the browser says it's OK.
CefRefPtr<CefBrowser> browser = _browserView->GetBrowser();
return (browser) ? browser->GetHost()->TryCloseBrowser() : true;
}
@@ -261,59 +255,58 @@ void CSiaDriveApp::OnContextInitialized()
CefRefPtr<CefCommandLine> commandLine = CefCommandLine::GetGlobalCommandLine();
#if defined(OS_WIN) || defined(OS_LINUX)
// Create the browser using the Views framework if "--use-views" is specified
// via the command-line. Otherwise, create the browser using the native
// platform framework. The Views framework is currently only supported on
// Windows and Linux.
const bool useViews = commandLine->HasSwitch("use-views");
#else
const bool useViews = false;
#endif
// SimpleHandler implements browser-level callbacks.
CefRefPtr<CSiaDriveHandler> handler(new CSiaDriveHandler(useViews));
// Specify CEF browser settings here.
CefBrowserSettings browserSettings;
SString url = "file:///./htdocs/index.html";
if (useViews)
{
// Create the BrowserView.
CefRefPtr<CefBrowserView> browserView = CefBrowserView::CreateBrowserView(handler, url.str(), browserSettings, nullptr, nullptr);
// Create the Window. It will show itself after creation.
CefWindow::CreateTopLevelWindow(new SimpleWindowDelegate(browserView));
}
else
{
// Information used when creating the native window.
CefWindowInfo windowInfo;
#ifdef _WIN32
// On Windows we need to specify certain flags that will be passed to
// CreateWindowEx().
windowInfo.SetAsPopup(nullptr, "SiaDrive");
#endif
windowInfo.width = 800;
windowInfo.height = 675;
// Create the first browser window.
CefBrowserHost::CreateBrowser(windowInfo, handler, url.str(), browserSettings, nullptr);
}
}
void CSiaDriveApp::OnContextReleased(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context)
void CSiaDriveApp::ShutdownServices()
{
if (CefCurrentlyOn(TID_RENDERER))
{
if (_refreshThread)
{
_refreshThread->StopAutoThread();
_refreshThread.reset(nullptr);
}
if (_siaDrive)
{
_siaDrive->Unmount();
_siaDrive.reset(nullptr);
}
}
else
{
CefPostTask(TID_RENDERER, base::Bind(&CSiaDriveApp::ShutdownServices, this));
}
}
void CSiaDriveApp::OnContextReleased(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context)
{
ShutdownServices();
}
void CSiaDriveApp::SiaApiRefreshCallback(CefRefPtr<CefV8Context> context, const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig)

View File

@@ -23,7 +23,6 @@ CSiaDriveHandler::~CSiaDriveHandler()
g_instance = nullptr;
}
// static
CSiaDriveHandler* CSiaDriveHandler::GetInstance()
{
return g_instance;
@@ -35,7 +34,6 @@ void CSiaDriveHandler::OnTitleChange(CefRefPtr<CefBrowser> browser, const CefStr
if (_useViews)
{
// Set the title of the window using the Views framework.
CefRefPtr<CefBrowserView> browserView = CefBrowserView::GetForBrowser(browser);
if (browserView)
{
@@ -46,7 +44,6 @@ void CSiaDriveHandler::OnTitleChange(CefRefPtr<CefBrowser> browser, const CefStr
}
else
{
// Set the title of the window using platform APIs.
PlatformTitleChange(browser, title);
}
}
@@ -55,7 +52,6 @@ void CSiaDriveHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser)
{
CEF_REQUIRE_UI_THREAD();
// Add to the list of existing browsers.
_browserList.push_back(browser);
}
@@ -63,9 +59,6 @@ bool CSiaDriveHandler::DoClose(CefRefPtr<CefBrowser> browser)
{
CEF_REQUIRE_UI_THREAD();
// Closing the main window requires special handling. See the DoClose()
// documentation in the CEF header for a detailed destription of this
// process.
if (_browserList.size() == 1)
{
// Set a flag to indicate that the window close should be allowed.
@@ -74,14 +67,13 @@ bool CSiaDriveHandler::DoClose(CefRefPtr<CefBrowser> browser)
// Allow the close. For windowed browsers this will result in the OS close
// event being sent.
return false;
return true;
}
void CSiaDriveHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser)
{
CEF_REQUIRE_UI_THREAD();
// Remove from the list of existing browsers.
BrowserList::iterator bit = _browserList.begin();
for (; bit != _browserList.end(); ++bit)
{
@@ -94,7 +86,6 @@ void CSiaDriveHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser)
if (_browserList.empty())
{
// All browser windows have closed. Quit the application message loop.
CefQuitMessageLoop();
}
}
@@ -107,34 +98,29 @@ void CSiaDriveHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
{
CEF_REQUIRE_UI_THREAD();
// Don't display an error for downloaded files.
if (errorCode == ERR_ABORTED)
return;
// Display a load error message.
if (errorCode != ERR_ABORTED)
{
std::stringstream ss;
ss << "<html><body bgcolor=\"white\">"
"<h2>Failed to load URL " << std::string(failedUrl) <<
" with error " << std::string(errorText) << " (" << errorCode <<
").</h2></body></html>";
frame->LoadString(ss.str(), failedUrl);
}
}
void CSiaDriveHandler::CloseAllBrowsers(bool forceClose)
{
if (!CefCurrentlyOn(TID_UI))
if (CefCurrentlyOn(TID_UI))
{
// Execute on the UI thread.
CefPostTask(TID_UI, base::Bind(&CSiaDriveHandler::CloseAllBrowsers, this, forceClose));
return;
}
if (_browserList.empty())
return;
BrowserList::const_iterator it = _browserList.begin();
for (; it != _browserList.end(); ++it)
(*it)->GetHost()->CloseBrowser(forceClose);
}
else if (!_browserList.empty())
{
CefPostTask(TID_UI, base::Bind(&CSiaDriveHandler::CloseAllBrowsers, this, forceClose));
}
}
#ifdef _WIN32

View File

@@ -132,7 +132,7 @@ bool FilePath::IsUNC() const
bool FilePath::CreateDirectory() const
{
#ifdef _WIN32
return ::CreateDirectory(&_path[0], nullptr) ? true : false;
return (::SHCreateDirectory(nullptr, &_path[0]) == ERROR_SUCCESS);
#else
a
#endif
@@ -159,6 +159,8 @@ bool FilePath::DeleteFile() const
bool FilePath::MoveFile(const FilePath& filePath)
{
#ifdef _WIN32
FilePath folder(filePath);
folder.RemoveFileName().CreateDirectory();
return ::MoveFile(&_path[0], &filePath[0]) ? true : false;
#else
a

View File

@@ -49,17 +49,19 @@ private:
static bool AddFileToCache(const SString& siaPath, const SString& cacheLocation)
{
bool ret = false;
SString tempPath;
tempPath.Resize(MAX_PATH + 1);
if (::GetTempPath(MAX_PATH + 1, &tempPath[0]))
{
// Check cache size is large enough to hold new file
ret = ApiSuccess(_siaApi->GetRenter()->DownloadFile(siaPath, tempPath));
FilePath tempFilePath = FilePath::GetTempDirectory();
tempFilePath.Append(GenerateSha256(siaPath) + ".siatmp");
// TODO Check cache size is large enough to hold new file
ret = ApiSuccess(_siaApi->GetRenter()->DownloadFile(siaPath, tempFilePath));
if (ret)
{
FilePath src(tempPath, L"");
FilePath src(tempFilePath);
FilePath dest(GetCacheLocation(), siaPath);
ret = src.MoveFile(dest);
if (!ret)
{
src.DeleteFile();
}
}
@@ -68,6 +70,7 @@ private:
static void QueueUploadIfChanged(const ULONG64& id, const std::uint64_t& size)
{
return;
if (!_openFileMap[id].ReadOnly)
{
if (size > 0)
@@ -175,7 +178,7 @@ private:
{
if (creationDisposition == CREATE_NEW)
{
if (!::CreateDirectory(&cacheFilePath[0], &securityAttrib))
if (!cacheFilePath.CreateDirectory())
{
DWORD error = GetLastError();
ret = DokanNtStatusFromWin32(error);
@@ -183,10 +186,9 @@ private:
}
else if (creationDisposition == OPEN_ALWAYS)
{
if (!::CreateDirectory(&cacheFilePath[0], &securityAttrib))
if (cacheFilePath.CreateDirectory())
{
DWORD error = GetLastError();
if (error != ERROR_ALREADY_EXISTS)
{
ret = DokanNtStatusFromWin32(error);
@@ -355,17 +357,6 @@ private:
ofi.ReadOnly = false;
std::lock_guard<std::mutex> l(_dokanMutex);
_openFileMap.insert({ DokanFileInfo->Context, ofi });
/*if (creationDisposition == OPEN_ALWAYS ||
creationDisposition == CREATE_ALWAYS) {
error = GetLastError();
if (error == ERROR_ALREADY_EXISTS) {
DbgPrint(L"\tOpen an already existing file\n");
// Open succeed but we need to inform the driver
// that the file open and not created by returning STATUS_OBJECT_NAME_COLLISION
return STATUS_OBJECT_NAME_COLLISION;
}
}*/
}
}
}
@@ -549,6 +540,7 @@ private:
static NTSTATUS DOKAN_CALLBACK Sia_Mounted(PDOKAN_FILE_INFO DokanFileInfo)
{
// May spend a little wait time here while files are cleaned-up and re-added to queue
_uploadManager.reset(new CUploadManager(CSiaCurl(_siaApi->GetHostConfig()), _siaDriveConfig));
StartFileListThread();
return STATUS_SUCCESS;
@@ -791,8 +783,10 @@ private:
if (DokanFileInfo->Context)
{
::CloseHandle(reinterpret_cast<HANDLE>(DokanFileInfo->Context));
{
std::lock_guard<std::mutex> l(_dokanMutex);
_openFileMap.erase(DokanFileInfo->Context);
}
DokanFileInfo->Context = 0;
}
else {
@@ -823,11 +817,10 @@ private:
}
}
static NTSTATUS DOKAN_CALLBACK Sia_FlushFileBuffers(LPCWSTR FileName, PDOKAN_FILE_INFO DokanFileInfo)
{
HANDLE handle = reinterpret_cast<HANDLE>(DokanFileInfo->Context);
if (!handle || handle == INVALID_HANDLE_VALUE)
if (!handle || (handle == INVALID_HANDLE_VALUE))
{
return STATUS_SUCCESS;
}
@@ -877,14 +870,15 @@ public:
ZeroMemory(&_dokanOptions, sizeof(DOKAN_OPTIONS));
_dokanOptions.Version = DOKAN_VERSION;
_dokanOptions.ThreadCount = 0; // use default
#ifdef _DEBUG
_dokanOptions.Options = DOKAN_OPTION_DEBUG;
#endif
}
static void Mount(const wchar_t& driveLetter, const SString& cacheLocation)
{
if (_siaApi && !_mountThread)
{
// May spend a little wait time here while files are cleaned-up and re-added to queue
_cacheLocation = cacheLocation;
wchar_t tmp[] = { driveLetter, ':', '\\', 0 };
_mountPoint = tmp;