1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/at_exit.h"
8 #include "base/command_line.h"
9 #include "base/files/file_path.h"
10 #include "base/metrics/histogram_functions.h"
11 #include "base/metrics/persistent_histogram_storage.h"
12 #include "base/process/memory.h"
13 #include "base/timer/elapsed_timer.h"
14 #include "base/win/process_startup_helper.h"
15 #include "base/win/scoped_winrt_initializer.h"
16 #include "chrome/install_static/product_install_details.h"
17 #include "chrome/install_static/user_data_dir.h"
18 #include "chrome/notification_helper/com_server_module.h"
19 #include "chrome/notification_helper/notification_helper_constants.h"
20 #include "chrome/notification_helper/notification_helper_crash_reporter_client.h"
21 #include "chrome/notification_helper/notification_helper_util.h"
22 #include "chrome/notification_helper/trace_util.h"
24 extern "C" int WINAPI wWinMain(HINSTANCE instance,
25 HINSTANCE prev_instance,
26 wchar_t* command_line,
28 // Persist histograms so they can be uploaded later.
29 base::PersistentHistogramStorage persistent_histogram_storage(
30 notification_helper::kNotificationHelperHistogramAllocatorName,
31 base::PersistentHistogramStorage::StorageDirManagement::kCreate);
33 // Initialize the CommandLine singleton from the environment.
34 base::CommandLine::Init(0, nullptr);
36 // This process is designed to be launched by COM only, which appends the
37 // "-Embedding" flag to the command line. If this flag is not found, the
38 // process should exit immediately.
39 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms683844.aspx
40 if (!base::CommandLine::ForCurrentProcess()->HasSwitch("embedding")) {
41 // Histogram storage is enabled at the very top of this wWinMain. Disable it
42 // in this case as there is no directory in which to write them nor a
43 // browser to subsequently upload them.
44 persistent_histogram_storage.Disable();
48 // The exit manager is in charge of calling the dtors of singletons.
49 base::AtExitManager exit_manager;
51 install_static::InitializeProductDetailsForPrimaryModule();
53 // Use crashpad embedded in chrome.exe as the crash handler.
54 base::FilePath chrome_exe_path = notification_helper::GetChromeExePath();
55 if (!chrome_exe_path.empty()) {
56 NotificationHelperCrashReporterClient::
57 InitializeCrashReportingForProcessWithHandler(chrome_exe_path);
60 // The histogram storage folder should be under folder "User Data".
61 std::wstring user_data_dir;
62 install_static::GetUserDataDirectory(&user_data_dir, nullptr);
64 persistent_histogram_storage.set_storage_base_dir(
65 base::FilePath(std::move(user_data_dir)));
67 // Make sure the process exits cleanly on unexpected errors.
68 base::EnableTerminationOnHeapCorruption();
69 base::EnableTerminationOnOutOfMemory();
70 base::win::RegisterInvalidParamHandler();
71 base::win::SetupCRT(*base::CommandLine::ForCurrentProcess());
73 base::win::ScopedWinrtInitializer winrt_initializer;
74 if (!winrt_initializer.Succeeded()) {
75 Trace(L"Failed initializing WinRT\n");
79 base::ElapsedTimer run_timer;
81 notification_helper::ComServerModule com_server_module;
82 com_server_module.Run();
84 base::UmaHistogramMediumTimes(
85 "Notifications.NotificationHelper.ServerRuntime", run_timer.Elapsed());