Home | History | Annotate | Download | only in metro_driver
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "stdafx.h"
      6 #include "win8/metro_driver/metro_driver.h"
      7 
      8 #include <roerrorapi.h>
      9 #include <shobjidl.h>
     10 
     11 #include "base/at_exit.h"
     12 #include "base/command_line.h"
     13 #include "base/logging.h"
     14 #include "base/logging_win.h"
     15 #include "base/win/scoped_comptr.h"
     16 #include "win8/metro_driver/winrt_utils.h"
     17 
     18 #if !defined(USE_AURA)
     19 #include "win8/metro_driver/chrome_app_view.h"
     20 #endif
     21 
     22 // TODO(siggi): Move this to GYP.
     23 #pragma comment(lib, "runtimeobject.lib")
     24 
     25 namespace {
     26 
     27 LONG WINAPI ErrorReportingHandler(EXCEPTION_POINTERS* ex_info) {
     28   // See roerrorapi.h for a description of the
     29   // exception codes and parameters.
     30   DWORD code = ex_info->ExceptionRecord->ExceptionCode;
     31   ULONG_PTR* info = ex_info->ExceptionRecord->ExceptionInformation;
     32   if (code == EXCEPTION_RO_ORIGINATEERROR) {
     33     string16 msg(reinterpret_cast<wchar_t*>(info[2]), info[1]);
     34     LOG(ERROR) << "VEH: Metro error 0x" << std::hex << info[0] << ": " << msg;
     35   } else if (code == EXCEPTION_RO_TRANSFORMERROR) {
     36     string16 msg(reinterpret_cast<wchar_t*>(info[3]), info[2]);
     37     LOG(ERROR) << "VEH: Metro old error 0x" << std::hex << info[0]
     38                << " new error 0x" << info[1] << ": " << msg;
     39   }
     40 
     41   return EXCEPTION_CONTINUE_SEARCH;
     42 }
     43 
     44 // TODO(robertshield): This GUID is hard-coded in a bunch of places that
     45 //     don't allow explicit includes. Find a single place for it to live.
     46 // {7FE69228-633E-4f06-80C1-527FEA23E3A7}
     47 const GUID kChromeTraceProviderName = {
     48     0x7fe69228, 0x633e, 0x4f06,
     49         { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } };
     50 
     51 }
     52 
     53 #if !defined(COMPONENT_BUILD)
     54 // Required for base initialization.
     55 // TODO(siggi): This should be handled better, as this way our at exit
     56 //     registrations will run under the loader's lock. However,
     57 //     once metro_driver is merged into Chrome.dll, this will go away anyhow.
     58 base::AtExitManager at_exit;
     59 #endif
     60 
     61 extern "C" __declspec(dllexport)
     62 int InitMetro(LPTHREAD_START_ROUTINE thread_proc, void* context) {
     63   // Initialize the command line.
     64   CommandLine::Init(0, NULL);
     65   logging::LoggingSettings settings;
     66   settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
     67   logging::InitLogging(settings);
     68 
     69 #if defined(NDEBUG)
     70   logging::SetMinLogLevel(logging::LOG_ERROR);
     71 #else
     72   logging::SetMinLogLevel(logging::LOG_VERBOSE);
     73   // Set the error reporting flags to always raise an exception,
     74   // which is then processed by our vectored exception handling
     75   // above to log the error message.
     76   winfoundtn::Diagnostics::SetErrorReportingFlags(
     77       winfoundtn::Diagnostics::UseSetErrorInfo |
     78       winfoundtn::Diagnostics::ForceExceptions);
     79 
     80   HANDLE registration =
     81       ::AddVectoredExceptionHandler(TRUE, ErrorReportingHandler);
     82 #endif
     83 
     84   // Enable trace control and transport through event tracing for Windows.
     85   logging::LogEventProvider::Initialize(kChromeTraceProviderName);
     86 
     87   DVLOG(1) << "InitMetro";
     88 
     89   mswrw::RoInitializeWrapper ro_initializer(RO_INIT_MULTITHREADED);
     90   CheckHR(ro_initializer, "RoInitialize failed");
     91 
     92   mswr::ComPtr<winapp::Core::ICoreApplication> core_app;
     93   HRESULT hr = winrt_utils::CreateActivationFactory(
     94       RuntimeClass_Windows_ApplicationModel_Core_CoreApplication,
     95       core_app.GetAddressOf());
     96   CheckHR(hr, "Failed to create app factory");
     97   if (FAILED(hr))
     98     return 1;
     99 
    100   auto view_factory = mswr::Make<ChromeAppViewFactory>(
    101       core_app.Get(), thread_proc, context);
    102   hr = core_app->Run(view_factory.Get());
    103   DVLOG(1) << "exiting InitMetro, hr=" << hr;
    104 
    105 #if !defined(NDEBUG)
    106   ::RemoveVectoredExceptionHandler(registration);
    107 #endif
    108 
    109   return hr;
    110 }
    111 
    112 // Activates the application known by |app_id|.  Returns, among other things,
    113 // E_APPLICATION_NOT_REGISTERED if |app_id| identifies Chrome and Chrome is not
    114 // the default browser.
    115 extern "C" __declspec(dllexport)
    116 HRESULT ActivateApplication(const wchar_t* app_id) {
    117   base::win::ScopedComPtr<IApplicationActivationManager> activator;
    118   HRESULT hr = activator.CreateInstance(CLSID_ApplicationActivationManager);
    119   if (SUCCEEDED(hr)) {
    120     DWORD pid = 0;
    121     hr = activator->ActivateApplication(app_id, L"", AO_NONE, &pid);
    122   }
    123   return hr;
    124 }
    125