main_message_loop_multithreaded_win.cc 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
// Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.

#include "../browser/main_message_loop_multithreaded_win.h"

#include "include/base/cef_bind.h"
#include "include/base/cef_logging.h"
#include "include/cef_app.h"
#include "../browser/resource.h"
#include "shared/browser/util_win.h"

namespace client {

namespace {

const wchar_t kWndClass[] = L"Client_MessageWindow";
const wchar_t kTaskMessageName[] = L"Client_CustomTask";

}  // namespace

MainMessageLoopMultithreadedWin::MainMessageLoopMultithreadedWin()
    : thread_id_(base::PlatformThread::CurrentId()),
      task_message_id_(RegisterWindowMessage(kTaskMessageName)),
      dialog_hwnd_(NULL),
      message_hwnd_(NULL) {}

MainMessageLoopMultithreadedWin::~MainMessageLoopMultithreadedWin() {
  DCHECK(RunsTasksOnCurrentThread());
  DCHECK(!message_hwnd_);
  DCHECK(queued_tasks_.empty());
}

int MainMessageLoopMultithreadedWin::Run() {
  DCHECK(RunsTasksOnCurrentThread());

  HINSTANCE hInstance = ::GetModuleHandle(NULL);

  {
    base::AutoLock lock_scope(lock_);

    // Create the hidden window for message processing.
    message_hwnd_ = CreateMessageWindow(hInstance);
    CHECK(message_hwnd_);

    // Store a pointer to |this| in the window's user data.
    SetUserDataPtr(message_hwnd_, this);

    // Execute any tasks that are currently queued.
    while (!queued_tasks_.empty()) {
      PostTaskInternal(queued_tasks_.front());
      queued_tasks_.pop();
    }
  }

  HACCEL hAccelTable =
      LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CEFCLIENT));

  MSG msg;

  // Run the application message loop.
  while (GetMessage(&msg, NULL, 0, 0)) {
    // Allow processing of dialog messages.
    if (dialog_hwnd_ && IsDialogMessage(dialog_hwnd_, &msg))
      continue;

    if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
  }

  {
    base::AutoLock lock_scope(lock_);

    // Destroy the message window.
    DestroyWindow(message_hwnd_);
    message_hwnd_ = NULL;
  }

  return static_cast<int>(msg.wParam);
}

void MainMessageLoopMultithreadedWin::Quit() {
  // Execute PostQuitMessage(0) on the main thread.
  PostClosure(base::Bind(::PostQuitMessage, 0));
}

void MainMessageLoopMultithreadedWin::PostTask(CefRefPtr<CefTask> task) {
  base::AutoLock lock_scope(lock_);
  PostTaskInternal(task);
}

bool MainMessageLoopMultithreadedWin::RunsTasksOnCurrentThread() const {
  return (thread_id_ == base::PlatformThread::CurrentId());
}

void MainMessageLoopMultithreadedWin::SetCurrentModelessDialog(
    HWND hWndDialog) {
  DCHECK(RunsTasksOnCurrentThread());

#if DCHECK_IS_ON()
  if (hWndDialog) {
    // A new dialog reference should not be set while one is currently set.
    DCHECK(!dialog_hwnd_);
  }
#endif
  dialog_hwnd_ = hWndDialog;
}

// static
HWND MainMessageLoopMultithreadedWin::CreateMessageWindow(HINSTANCE hInstance) {
  WNDCLASSEX wc = {0};
  wc.cbSize = sizeof(wc);
  wc.lpfnWndProc = MessageWndProc;
  wc.hInstance = hInstance;
  wc.lpszClassName = kWndClass;
  RegisterClassEx(&wc);

  return CreateWindow(kWndClass, 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, hInstance,
                      0);
}

// static
LRESULT CALLBACK
MainMessageLoopMultithreadedWin::MessageWndProc(HWND hWnd,
                                                UINT message,
                                                WPARAM wParam,
                                                LPARAM lParam) {
  MainMessageLoopMultithreadedWin* self =
      GetUserDataPtr<MainMessageLoopMultithreadedWin*>(hWnd);

  if (self && message == self->task_message_id_) {
    // Execute the task.
    CefTask* task = reinterpret_cast<CefTask*>(wParam);
    task->Execute();

    // Release the reference added in PostTaskInternal. This will likely result
    // in |task| being deleted.
    task->Release();
  } else {
    switch (message) {
      case WM_NCDESTROY:
        // Clear the reference to |self|.
        SetUserDataPtr(hWnd, NULL);
        break;
    }
  }

  return DefWindowProc(hWnd, message, wParam, lParam);
}

void MainMessageLoopMultithreadedWin::PostTaskInternal(
    CefRefPtr<CefTask> task) {
  lock_.AssertAcquired();

  if (!message_hwnd_) {
    // Queue the task until the message loop starts running.
    queued_tasks_.push(task);
    return;
  }

  // Add a reference that will be released in MessageWndProc.
  task->AddRef();

  // Post the task for execution by the message window.
  PostMessage(message_hwnd_, task_message_id_,
              reinterpret_cast<WPARAM>(task.get()), 0);
}

}  // namespace client