Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/engine/Main.cpp

Issue 6505394822184960: Issue 1109 - Support notifications (Closed)
Patch Set: Created May 11, 2015, 10:01 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #pragma comment(linker,"\"/manifestdependency:type='win32' \
19 name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
20 processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
21
Oleksandr 2015/06/24 01:22:34 We have the same in PluginStdAfx.h. Maybe it makes
sergei 2015/06/24 17:31:35 I don't think it's time to unite them. Even more,
18 #include <AdblockPlus.h> 22 #include <AdblockPlus.h>
19 #include <functional> 23 #include <functional>
20 #include <vector> 24 #include <vector>
21 #include <thread> 25 #include <thread>
22 #include <Windows.h> 26 #include <Windows.h>
23 27
24 #include "../shared/AutoHandle.h" 28 #include "../shared/AutoHandle.h"
25 #include "../shared/Communication.h" 29 #include "../shared/Communication.h"
26 #include "../shared/Dictionary.h" 30 #include "../shared/Dictionary.h"
27 #include "../shared/Utils.h" 31 #include "../shared/Utils.h"
28 #include "../shared/Version.h" 32 #include "../shared/Version.h"
29 #include "../shared/CriticalSection.h" 33 #include "../shared/CriticalSection.h"
30 #include "../shared/IE_version.h" 34 #include "../shared/IE_version.h"
31 #include "AdblockPlus.h" 35 #include "AdblockPlus.h"
32 #include "Debug.h" 36 #include "Debug.h"
33 #include "Updater.h" 37 #include "Updater.h"
38 #include "NotificationWindow.h"
34 39
35 namespace 40 namespace
36 { 41 {
42 class MyModule : public ATL::CAtlExeModuleT<MyModule> {
43 public:
44 } _AtlModule;
45
37 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine; 46 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine;
38 std::auto_ptr<Updater> updater; 47 std::auto_ptr<Updater> updater;
39 int activeConnections = 0; 48 int activeConnections = 0;
40 CriticalSection activeConnectionsLock; 49 CriticalSection activeConnectionsLock;
41 HWND callbackWindow; 50 HWND callbackWindow;
42 51
43 std::string GetWhitelistingFilter(const std::string& urlArg, 52 std::string GetWhitelistingFilter(const std::string& urlArg,
44 const std::vector<std::string>& frameHierarchy, AdblockPlus::FilterEngine::C ontentType type) 53 const std::vector<std::string>& frameHierarchy, AdblockPlus::FilterEngine::C ontentType type)
45 { 54 {
46 auto GetWhitelistingFilter = [&type](const std::string& url, const std::stri ng& parent)->std::string 55 auto GetWhitelistingFilter = [&type](const std::string& url, const std::stri ng& parent)->std::string
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 { 458 {
450 if (params.size() < 1) 459 if (params.size() < 1)
451 { 460 {
452 Debug("updateAvailable event missing URL"); 461 Debug("updateAvailable event missing URL");
453 return; 462 return;
454 } 463 }
455 updateAvailable = true; 464 updateAvailable = true;
456 465
457 updater->Update(params[0]->AsString()); 466 updater->Update(params[0]->AsString());
458 } 467 }
459 }
460 468
461 std::auto_ptr<AdblockPlus::FilterEngine> CreateFilterEngine(const std::wstring& locale) 469 std::auto_ptr<AdblockPlus::FilterEngine> CreateFilterEngine(const std::wstring& locale)
462 { 470 {
463 AdblockPlus::AppInfo appInfo; 471 AdblockPlus::AppInfo appInfo;
464 appInfo.version = ToUtf8String(IEPLUGIN_VERSION); 472 appInfo.version = ToUtf8String(IEPLUGIN_VERSION);
465 appInfo.name = "adblockplusie"; 473 appInfo.name = "adblockplusie";
466 #ifdef _WIN64 474 #ifdef _WIN64
467 appInfo.application = "msie64"; 475 appInfo.application = "msie64";
468 #else 476 #else
469 appInfo.application = "msie32"; 477 appInfo.application = "msie32";
470 #endif 478 #endif
471 appInfo.applicationVersion = ToUtf8String(AdblockPlus::IE::InstalledVersionStr ing()); 479 appInfo.applicationVersion = ToUtf8String(AdblockPlus::IE::InstalledVersionStr ing());
472 appInfo.locale = ToUtf8String(locale); 480 appInfo.locale = ToUtf8String(locale);
473 #ifdef ADBLOCK_PLUS_TEST_MODE 481 #ifdef ADBLOCK_PLUS_TEST_MODE
474 appInfo.developmentBuild = true; 482 appInfo.developmentBuild = true;
475 #else 483 #else
476 appInfo.developmentBuild = false; 484 appInfo.developmentBuild = false;
477 #endif 485 #endif
478 486
479 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New(appInfo); 487 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New(appInfo);
480 jsEngine->SetEventCallback("updateAvailable", &OnUpdateAvailable); 488 jsEngine->SetEventCallback("updateAvailable", &OnUpdateAvailable);
481 489
482 std::string dataPath = ToUtf8String(GetAppDataPath()); 490 std::string dataPath = ToUtf8String(GetAppDataPath());
483 dynamic_cast<AdblockPlus::DefaultFileSystem*>(jsEngine->GetFileSystem().get()) ->SetBasePath(dataPath); 491 dynamic_cast<AdblockPlus::DefaultFileSystem*>(jsEngine->GetFileSystem().get()) ->SetBasePath(dataPath);
484 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine(new AdblockPlus::FilterE ngine(jsEngine)); 492 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine(new AdblockPlus::FilterE ngine(jsEngine));
485 return filterEngine; 493 return filterEngine;
486 } 494 }
487 495
488 int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) 496 class ScopedTimer
497 {
498 public:
499 explicit ScopedTimer(HWND hwnd, UINT_PTR timerId, UINT elapse, TIMERPROC timer Proc)
500 : m_hwnd(hwnd), m_timerId(timerId)
501 {
502 m_timerId = SetTimer(m_hwnd, m_timerId, elapse, timerProc);
503 }
504 ~ScopedTimer()
505 {
506 Close();
507 }
508
509 UINT_PTR GetID()
510 {
511 return m_timerId;
512 }
513
514 operator bool() const
515 {
516 return m_timerId != 0;
517 }
518
519 void Close()
520 {
521 if (m_timerId)
522 {
523 KillTimer(m_hwnd, m_timerId);
524 m_timerId = 0;
525 }
526 }
527 private:
528 ScopedTimer(const ScopedTimer&);
529 ScopedTimer& operator=(const ScopedTimer&);
530 private:
531 HWND m_hwnd;
532 UINT_PTR m_timerId;
533 };
534
535 struct ScopedAtlAxInitializer
536 {
537 ScopedAtlAxInitializer()
538 {
539 ATL::AtlAxWinInit();
540 }
541 ~ScopedAtlAxInitializer()
542 {
543 ATL::AtlAxWinTerm();
544 }
545 };
546
547 void ApplicationMessageLoop(HINSTANCE hInstance)
548 {
549 const std::wstring className = L"ABPEngineMessageWindow";
550 WNDCLASS wc;
551 wc.style = 0;
552 wc.lpfnWndProc = DefWindowProcW;
553 wc.cbClsExtra = 0;
554 wc.cbWndExtra = 0;
555 wc.hInstance = hInstance;
556 wc.hIcon = nullptr;
557 wc.hCursor = nullptr;
558 wc.hbrBackground = nullptr;
559 wc.lpszMenuName = nullptr;
560 wc.lpszClassName = className.c_str();
561 ATOM atom = RegisterClass(&wc);
562 if (!atom)
563 {
564 DebugLastError("Cannot register class for message only window");
565 return;
566 }
567 HWND msgHWnd = CreateWindowW(className.c_str(),
568 nullptr, // window name
569 0, // style
570 0, 0, 0, 0, // geometry (x, y, w, h)
571 HWND_MESSAGE, // parent
572 nullptr, // menu handle
573 hInstance,
574 0); // windows creation data.
575 if (!msgHWnd)
576 {
577 DebugLastError("Cannot create message only window");
578 return;
579 }
580 MSG msg = {};
581 ScopedTimer notificationTimer(msgHWnd, /*timer ID*/1, 3000/*msec*/, nullptr);
582 std::unique_ptr<NotificationWindow> notificationWindow;
583 while (GetMessage(&msg, /*hwnd*/nullptr, /*msgFilterMin*/0, /*msgFilterMax*/0) )
584 {
585 if (msg.hwnd == msgHWnd)
586 {
587 if (msg.message == WM_TIMER)
588 {
589 if (msg.wParam == notificationTimer.GetID())
590 {
591 notificationTimer.Close();
592 auto notification = filterEngine->GetNextNotificationToShow();
593 if (notification)
594 {
595 notificationWindow.reset(new NotificationWindow(*notification, GetEx eDir()));
596 notificationWindow->destroyed = [&notificationWindow]
597 {
598 notificationWindow.reset();
599 };
600 notificationWindow->linkClicked = [](const std::wstring& url)
601 {
602 ShellExecute(nullptr, L"open", url.c_str(), nullptr, nullptr, SW_S HOWNORMAL);
Oleksandr 2015/06/24 01:22:34 This will open a default browser, not necessarily
sergei 2015/06/24 17:31:35 I was trying to launch internet explorer using CoC
603 };
604 notificationWindow->Create(/*parent window*/nullptr);
605 if (notificationWindow->operator HWND() != nullptr)
606 {
607 notificationWindow->ShowWindow(SW_SHOWNOACTIVATE);
608 notificationWindow->UpdateWindow();
609 }
610 }
611 }
612 }
613 }
614 TranslateMessage(&msg);
615 DispatchMessage(&msg);
616 }
617 }
618
619 } // namespace {
620
621 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
489 { 622 {
490 AutoHandle mutex(CreateMutexW(0, false, L"AdblockPlusEngine")); 623 AutoHandle mutex(CreateMutexW(0, false, L"AdblockPlusEngine"));
491 if (!mutex) 624 if (!mutex)
492 { 625 {
493 DebugLastError("CreateMutex failed"); 626 DebugLastError("CreateMutex failed");
494 return 1; 627 return 1;
495 } 628 }
496 629
497 if (GetLastError() == ERROR_ALREADY_EXISTS) 630 if (GetLastError() == ERROR_ALREADY_EXISTS)
498 { 631 {
499 DebugLastError("Named pipe exists, another engine instance appears to be run ning"); 632 DebugLastError("Named pipe exists, another engine instance appears to be run ning");
500 return 1; 633 return 1;
501 } 634 }
502 635
503 int argc; 636 int argc;
504 LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc); 637 LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
505 std::wstring locale(argc >= 2 ? argv[1] : L""); 638 std::wstring locale(argc >= 2 ? argv[1] : L"");
506 LocalFree(argv); 639 LocalFree(argv);
507 Dictionary::Create(locale); 640 Dictionary::Create(locale);
508 filterEngine = CreateFilterEngine(locale); 641 filterEngine = CreateFilterEngine(locale);
509 updater.reset(new Updater(filterEngine->GetJsEngine())); 642 updater.reset(new Updater(filterEngine->GetJsEngine()));
510 643
511 for (;;) 644 std::thread communicationThread([]
512 { 645 {
513 try 646 for (;;)
514 { 647 {
515 auto pipe = std::make_shared<Communication::Pipe>(Communication::pipeName, Communication::Pipe::MODE_CREATE); 648 try
649 {
650 auto pipe = std::make_shared<Communication::Pipe>(Communication::pipeNam e, Communication::Pipe::MODE_CREATE);
651
652 // TODO: we should wait for the finishing of the thread before exiting f rom this function.
653 // It works now in most cases because the browser waits for the response in the pipe, and the
654 // thread has time to finish while this response is being processed and the browser is
655 // disposing all its stuff.
656 std::thread([pipe]()
657 {
658 ClientThread(pipe.get());
659 }).detach();
660 }
661 catch(const std::system_error& ex)
662 {
663 DebugException(ex);
664 return 1;
665 }
666 catch (const std::runtime_error& e)
667 {
668 DebugException(e);
669 return 1;
670 }
671 }
672 });
516 673
517 // TODO: we should wait for the finishing of the thread before exiting fro m this function. 674 ScopedAtlAxInitializer atlAxInit;
518 // It works now in most cases because the browser waits for the response i n the pipe, and the 675 ApplicationMessageLoop(hInstance);
519 // thread has time to finish while this response is being processed and th e browser is 676 if (communicationThread.joinable())
520 // disposing all its stuff. 677 {
521 std::thread([pipe]() 678 communicationThread.join();
522 {
523 ClientThread(pipe.get());
524 }).detach();
525 }
526 catch(const std::system_error& ex)
527 {
528 DebugException(ex);
529 return 1;
530 }
531 catch (const std::runtime_error& e)
532 {
533 DebugException(e);
534 return 1;
535 }
536 } 679 }
537 680
681
538 return 0; 682 return 0;
539 } 683 }
OLDNEW

Powered by Google App Engine
This is Rietveld