Skip to content

Commit

Permalink
baseline for upgrade desktop agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Ruiz committed Apr 23, 2019
1 parent 77a0370 commit 5b868bc
Show file tree
Hide file tree
Showing 15 changed files with 376 additions and 31 deletions.
9 changes: 7 additions & 2 deletions src/DesktopApp/Browser/BrowserScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ namespace desktop { namespace ui{
{
m_cefBrowser = browser;

m_downloadPath = theApp.onBrowserCreated(m_cefBrowser);
m_downloadBasePath = theApp.onBrowserCreated(m_cefBrowser);

m_subscriber.subscribe([this](const desktop::core::utils::patterns::Event& rawEvt)
{
Expand All @@ -131,9 +131,14 @@ namespace desktop { namespace ui{

}

void BrowserScreen::setDownloadFolder(const std::string& downloadFolder)
{
m_downloadFolder = downloadFolder;
}

std::string BrowserScreen::getDownloadPath(const std::string& file_name) const
{
return m_downloadPath + file_name;
return m_downloadBasePath + m_downloadFolder + file_name;
}

LRESULT BrowserScreen::OnKeyBoardHook(WPARAM /*wParam*/, LPARAM lParam)
Expand Down
5 changes: 4 additions & 1 deletion src/DesktopApp/Browser/BrowserScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ namespace desktop { namespace ui{
void onBrowserCreated(CefRefPtr<CefBrowser> browser) override;
void onBrowserClosed(CefRefPtr<CefBrowser> browser) override;
void onTitleChange(const std::string& title) override;

void setDownloadFolder(const std::string& file_name);
std::string getDownloadPath(const std::string& file_name) const override;

afx_msg LRESULT OnKeyBoardHook(WPARAM wParam, LPARAM lParam);
Expand All @@ -67,7 +69,8 @@ namespace desktop { namespace ui{
CefRefPtr<BrowserClientHandler> m_cefHandler;
CefRefPtr<CefBrowser> m_cefBrowser;
std::string m_path;
std::string m_downloadPath;
std::string m_downloadFolder;
std::string m_downloadBasePath;

cup::Subscriber m_subscriber;
};
Expand Down
7 changes: 5 additions & 2 deletions src/DesktopApp/DesktopApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
#include "Agents\NotificationAgent.h"
#include "DesktopCore\DesktopCore.h"
#include "DesktopCore\Upgrade\Agents\UpgradeViewerAgent.h"
#include "DesktopCore\Upgrade\Agents\UpgradeDesktopAgent.h"
#include "DesktopCore\Blink\Agents\SyncVideoAgent.h"
#include "DesktopCore\Blink\Agents\SyncThumbnailAgent.h"
#include "DesktopCore\Blink\Agents\SaveTokenAgent.h"
#include "DesktopCore\Blink\Agents\ActivityAgent.h"
#include "DesktopCore\System\Services\ApplicationDataService.h"
#include "DesktopCore\System\Services\CrashReportService.h"
#include "Services\DownloadFileService.h"
#include "Services\DownloadViewerService.h"
#include "Services\DownloadDesktopService.h"
#include "Services\ActivityNotificationService.h"

#include <boost/optional.hpp>
Expand Down Expand Up @@ -115,7 +117,8 @@ std::string DesktopApp::onBrowserCreated(CefRefPtr<CefBrowser> browser)
{
m_core->initialize();

m_core->addAgent(std::make_unique<desktop::core::agent::UpgradeViewerAgent>(std::make_unique<desktop::ui::service::DownloadFileService>(browser)));
m_core->addAgent(std::make_unique<desktop::core::agent::UpgradeViewerAgent>(std::make_unique<desktop::ui::service::DownloadViewerService>(browser)));
m_core->addAgent(std::make_unique<desktop::core::agent::UpgradeDesktopAgent>(std::make_unique<desktop::ui::service::DownloadDesktopService>(browser)));
m_core->addAgent(std::make_unique<desktop::core::agent::SyncVideoAgent>());
m_core->addAgent(std::make_unique<desktop::core::agent::SyncThumbnailAgent>());
m_core->addAgent(std::make_unique<desktop::core::agent::SaveTokenAgent>());
Expand Down
6 changes: 4 additions & 2 deletions src/DesktopApp/DesktopApp.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ xcopy $(SolutionDir)lib\crashsender\$(Configuration)\* $(SolutionDir)\bin\$(Conf
<ClInclude Include="Events.h" />
<ClInclude Include="Resource.h" />
<ClInclude Include="Services\ActivityNotificationService.h" />
<ClInclude Include="Services\DownloadFileService.h" />
<ClInclude Include="Services\DownloadDesktopService.h" />
<ClInclude Include="Services\DownloadViewerService.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
<ClInclude Include="DesktopAppDlg.h" />
Expand All @@ -420,7 +421,8 @@ xcopy $(SolutionDir)lib\crashsender\$(Configuration)\* $(SolutionDir)\bin\$(Conf
<ClCompile Include="Browser\Resources\InternalResourceManager.cpp" />
<ClCompile Include="Browser\Resources\RequestResourceProvider.cpp" />
<ClCompile Include="Services\ActivityNotificationService.cpp" />
<ClCompile Include="Services\DownloadFileService.cpp" />
<ClCompile Include="Services\DownloadDesktopService.cpp" />
<ClCompile Include="Services\DownloadViewerService.cpp" />
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
Expand Down
16 changes: 11 additions & 5 deletions src/DesktopApp/DesktopApp.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,16 @@
<ClInclude Include="Resource.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
<ClInclude Include="Services\DownloadFileService.h">
<Filter>Services</Filter>
</ClInclude>
<ClInclude Include="Events.h" />
<ClInclude Include="Services\ActivityNotificationService.h">
<Filter>Services</Filter>
</ClInclude>
<ClInclude Include="Services\DownloadViewerService.h">
<Filter>Services</Filter>
</ClInclude>
<ClInclude Include="Services\DownloadDesktopService.h">
<Filter>Services</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Agents\NotificationAgent.cpp">
Expand Down Expand Up @@ -118,10 +121,13 @@
<ClCompile Include="DesktopApp.cpp" />
<ClCompile Include="DesktopAppDlg.cpp" />
<ClCompile Include="stdafx.cpp" />
<ClCompile Include="Services\DownloadFileService.cpp">
<ClCompile Include="Services\ActivityNotificationService.cpp">
<Filter>Services</Filter>
</ClCompile>
<ClCompile Include="Services\ActivityNotificationService.cpp">
<ClCompile Include="Services\DownloadViewerService.cpp">
<Filter>Services</Filter>
</ClCompile>
<ClCompile Include="Services\DownloadDesktopService.cpp">
<Filter>Services</Filter>
</ClCompile>
</ItemGroup>
Expand Down
83 changes: 83 additions & 0 deletions src/DesktopApp/Services/DownloadDesktopService.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "stdafx.h"

#include "DownloadDesktopService.h"

#include "Agents/NotificationAgent.h"
#include "Events.h"
#include "DesktopApp.h"

#include "DesktopCore\Upgrade\Events.h"
#include "Toast\ToastEventHandler.h"
#include "Toast\ToastCommandLineInfo.h"
#include "Toast\ToastFactory.h"

#include "DesktopCore\Utils\Patterns\PublisherSubscriber\Broker.h"

#include <boost\filesystem\operations.hpp>

namespace desktop { namespace ui { namespace service {

DownloadDesktopService::DownloadDesktopService(CefRefPtr<CefBrowser> browser, std::unique_ptr<core::service::EncodeStringService> encodeService,
std::unique_ptr<core::service::ApplicationDataService> applicationService)
: m_browser(browser)
, m_encodeService(std::move(encodeService))
, m_applicationService(std::move(applicationService))
{
m_subscriber.subscribe([this](const desktop::core::utils::patterns::Event& rawEvt)
{
auto evt = static_cast<const events::DownloadStatusEvent&>(rawEvt);

if (evt.bIsComplete)
{
m_path = evt.m_path;

m_cv.notify_one();
}

}, events::DOWNLOAD_STATUS_EVENT);

m_subscriber.subscribe([this](const core::utils::patterns::Event& rawEvt)
{
auto evt = static_cast<const core::events::DownloadUpgradeEvent&>(rawEvt);

auto version = m_encodeService->utf8toUtf16(evt.m_version);

toast::ToastFactory factory;

auto callback = evt.m_callback;

auto notification = std::make_unique<core::model::Notification>([callback]()
{
return (theApp.m_toastAction == L"accept") ? callback() : true;
}, []() {return true; }, []() {return true; });

m_handler = std::make_shared<toast::ToastEventHandler>(std::move(notification));
m_toast = factory.getYesNo(L"Desktop upgrade", L"Version " + version + L" available", L"Download");

agent::NotificationAgent::ShowNotificationEvent notificationEvt(m_toast, m_handler);
core::utils::patterns::Broker::get().publish(notificationEvt);

}, core::events::DOWNLOAD_UPGRADE_EVENT);

m_subscriber.subscribe([this](const desktop::core::utils::patterns::Event& rawEvt)
{
//m_browser->GetMainFrame()->LoadURL(boost::filesystem::canonical(m_applicationService->getViewerFolder() + "/index.html").string());
}, desktop::core::events::UPGRADE_DESKTOP_COMPLETED_EVENT);
}

DownloadDesktopService::~DownloadDesktopService() = default;

std::string DownloadDesktopService::download(const std::string& host, const std::string& url, std::map<std::string, std::string> requestHeaders, const std::string &/*folder*/) const
{
auto pos = url.find(host) + host.size();

std::string script = "window.location = 'https://" + host + url.substr(pos) + "';";

m_browser->GetMainFrame()->ExecuteJavaScript(script, m_browser->GetMainFrame()->GetURL(), 0);

std::unique_lock<std::mutex> lock(m_mutex);
m_cv.wait(lock);

return m_path;
}
}}}
64 changes: 64 additions & 0 deletions src/DesktopApp/Services/DownloadDesktopService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#pragma once

#include "DesktopCore\Utils\Patterns\PublisherSubscriber\Subscriber.h"

#include "DesktopCore\System\Services\EncodeStringService.h"
#include "DesktopCore\System\Services\ApplicationDataService.h"
#include "DesktopCore\Network\Services\IDownloadFileService.h"

#pragma warning(push)
#pragma warning(disable : 4100)
#pragma warning(disable : 4481)
#include <cef/cef_app.h>
#pragma warning(pop)

#include <mutex>
#include <memory>
#include <condition_variable>

namespace ToastPP
{
class CToast;
}

namespace desktop {

namespace core { namespace service {
class HTTPClientService;
}}

namespace ui {

namespace toast
{
class ToastEventHandler;
}

namespace service {

namespace cup = core::utils::patterns;

class DownloadDesktopService : public core::service::IDownloadFileService
{
public:
DownloadDesktopService(CefRefPtr<CefBrowser> browser,
std::unique_ptr<core::service::EncodeStringService> encodeService = std::make_unique<core::service::EncodeStringService>(),
std::unique_ptr<core::service::ApplicationDataService> applicationService = std::make_unique<core::service::ApplicationDataService>());
~DownloadDesktopService();
std::string download(const std::string& host, const std::string& url, std::map<std::string, std::string> requestHeaders, const std::string &folder) const override;
private:
CefRefPtr<CefBrowser> m_browser;
cup::Subscriber m_subscriber;

std::string m_path;

std::unique_ptr<core::service::EncodeStringService> m_encodeService;
std::unique_ptr<core::service::ApplicationDataService> m_applicationService;

mutable std::condition_variable m_cv;
mutable std::mutex m_mutex;

std::shared_ptr<toast::ToastEventHandler> m_handler;
std::shared_ptr<ToastPP::CToast> m_toast;
};
}}}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "stdafx.h"

#include "DownloadFileService.h"
#include "DownloadViewerService.h"

#include "Agents/NotificationAgent.h"
#include "Events.h"
Expand All @@ -17,7 +17,7 @@

namespace desktop { namespace ui { namespace service {

DownloadFileService::DownloadFileService(CefRefPtr<CefBrowser> browser, std::unique_ptr<core::service::EncodeStringService> encodeService,
DownloadViewerService::DownloadViewerService(CefRefPtr<CefBrowser> browser, std::unique_ptr<core::service::EncodeStringService> encodeService,
std::unique_ptr<core::service::ApplicationDataService> applicationService)
: m_browser(browser)
, m_encodeService(std::move(encodeService))
Expand Down Expand Up @@ -54,7 +54,7 @@ namespace desktop { namespace ui { namespace service {
}, []() {return true; }, []() {return true; });

m_handler = std::make_shared<toast::ToastEventHandler>(std::move(notification));
m_toast = factory.getYesNo(L"Upgrade", L"Version " + version + L" available", L"Download");
m_toast = factory.getYesNo(L"Viewer upgrade", L"Version " + version + L" available", L"Download");

agent::NotificationAgent::ShowNotificationEvent notificationEvt(m_toast, m_handler);
core::utils::patterns::Broker::get().publish(notificationEvt);
Expand All @@ -64,7 +64,7 @@ namespace desktop { namespace ui { namespace service {
auto notification = std::make_unique<core::model::Notification>([](){return true;}, [](){return true;}, [](){return true;});

m_handler = std::make_shared<toast::ToastEventHandler>(std::move(notification));
m_toast = factory.getBasic(L"Upgrading...", L"Version " + version + L"");
m_toast = factory.getBasic(L"Upgrading Viewer...", L"Version " + version + L"");

agent::NotificationAgent::ShowNotificationEvent notificationEvt(m_toast, m_handler);
core::utils::patterns::Broker::get().publish(notificationEvt);
Expand All @@ -76,12 +76,12 @@ namespace desktop { namespace ui { namespace service {
m_subscriber.subscribe([this](const desktop::core::utils::patterns::Event& rawEvt)
{
m_browser->GetMainFrame()->LoadURL(boost::filesystem::canonical(m_applicationService->getViewerFolder() + "/index.html").string());
}, desktop::core::events::UPGRADE_COMPLETED_EVENT);
}, desktop::core::events::UPGRADE_VIEWER_COMPLETED_EVENT);
}

DownloadFileService::~DownloadFileService() = default;
DownloadViewerService::~DownloadViewerService() = default;

std::string DownloadFileService::download(const std::string& host, const std::string& url, std::map<std::string, std::string> requestHeaders, const std::string &/*folder*/) const
std::string DownloadViewerService::download(const std::string& host, const std::string& url, std::map<std::string, std::string> requestHeaders, const std::string &/*folder*/) const
{
auto pos = url.find(host) + host.size();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ namespace desktop {

namespace cup = core::utils::patterns;

class DownloadFileService : public core::service::IDownloadFileService
class DownloadViewerService : public core::service::IDownloadFileService
{
public:
DownloadFileService(CefRefPtr<CefBrowser> browser,
DownloadViewerService(CefRefPtr<CefBrowser> browser,
std::unique_ptr<core::service::EncodeStringService> encodeService = std::make_unique<core::service::EncodeStringService>(),
std::unique_ptr<core::service::ApplicationDataService> applicationService = std::make_unique<core::service::ApplicationDataService>());
~DownloadFileService();
~DownloadViewerService();
std::string download(const std::string& host, const std::string& url, std::map<std::string, std::string> requestHeaders, const std::string &folder) const override;
private:
CefRefPtr<CefBrowser> m_browser;
Expand Down
2 changes: 2 additions & 0 deletions src/DesktopCore/DesktopCore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
<ClCompile Include="System\Services\ReplaceFolderService.cpp" />
<ClCompile Include="System\Services\TimestampFolderService.cpp" />
<ClCompile Include="System\Services\TimeZoneService.cpp" />
<ClCompile Include="Upgrade\Agents\UpgradeDesktopAgent.cpp" />
<ClCompile Include="Upgrade\Agents\UpgradeViewerAgent.cpp" />
<ClCompile Include="Utils\Patterns\PublisherSubscriber\Broker.cpp" />
<ClCompile Include="Utils\Patterns\PublisherSubscriber\Subscriber.cpp" />
Expand Down Expand Up @@ -178,6 +179,7 @@
<ClInclude Include="System\Services\ReplaceFolderService.h" />
<ClInclude Include="System\Services\TimestampFolderService.h" />
<ClInclude Include="System\Services\TimeZoneService.h" />
<ClInclude Include="Upgrade\Agents\UpgradeDesktopAgent.h" />
<ClInclude Include="Upgrade\Agents\UpgradeViewerAgent.h" />
<ClInclude Include="Upgrade\Events.h" />
<ClInclude Include="Utils\Patterns\PublisherSubscriber\Broker.h" />
Expand Down
6 changes: 6 additions & 0 deletions src/DesktopCore/DesktopCore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
<ClCompile Include="System\Services\TimeZoneService.cpp">
<Filter>System\Services</Filter>
</ClCompile>
<ClCompile Include="Upgrade\Agents\UpgradeDesktopAgent.cpp">
<Filter>Upgrade\Agents</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Upgrade\Agents\UpgradeViewerAgent.h">
Expand Down Expand Up @@ -201,5 +204,8 @@
<ClInclude Include="System\Services\TimeZoneService.h">
<Filter>System\Services</Filter>
</ClInclude>
<ClInclude Include="Upgrade\Agents\UpgradeDesktopAgent.h">
<Filter>Upgrade\Agents</Filter>
</ClInclude>
</ItemGroup>
</Project>
Loading

0 comments on commit 5b868bc

Please sign in to comment.