summaryrefslogtreecommitdiffstats
path: root/content/browser/system_message_window_win.cc
blob: 1aca1c1c184d960a3822aecac657442c5364f9ff (plain)
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
// Copyright (c) 2012 The Chromium 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 "content/browser/system_message_window_win.h"

#include <windows.h>
#include <dbt.h>
#include <string>

#include "base/file_path.h"
#include "base/sys_string_conversions.h"
#include "base/system_monitor/system_monitor.h"
#include "base/win/wrapped_window_proc.h"

static const wchar_t* const WindowClassName = L"Chrome_SystemMessageWindow";

namespace {

LRESULT GetVolumeName(LPCWSTR drive,
                      LPWSTR volume_name,
                      unsigned int volume_name_len) {
  return GetVolumeInformation(drive, volume_name, volume_name_len, NULL, NULL,
      NULL, NULL, 0);
}

// Returns 0 if the devicetype is not volume.
DWORD GetVolumeBitMaskFromBroadcastHeader(DWORD data) {
  PDEV_BROADCAST_HDR dev_broadcast_hdr =
      reinterpret_cast<PDEV_BROADCAST_HDR>(data);
  if (dev_broadcast_hdr->dbch_devicetype == DBT_DEVTYP_VOLUME) {
    PDEV_BROADCAST_VOLUME dev_broadcast_volume =
        reinterpret_cast<PDEV_BROADCAST_VOLUME>(dev_broadcast_hdr);
    return dev_broadcast_volume->dbcv_unitmask;
  }
  return 0;
}

}  // namespace


SystemMessageWindowWin::SystemMessageWindowWin()
    : volume_name_func_(&GetVolumeName) {
  Init();
}

SystemMessageWindowWin::SystemMessageWindowWin(VolumeNameFunc volume_name_func)
    : volume_name_func_(volume_name_func) {
  Init();
}

void SystemMessageWindowWin::Init() {
  HINSTANCE hinst = GetModuleHandle(NULL);

  WNDCLASSEX wc = {0};
  wc.cbSize = sizeof(wc);
  wc.lpfnWndProc =
      base::win::WrappedWindowProc<&SystemMessageWindowWin::WndProcThunk>;
  wc.hInstance = hinst;
  wc.lpszClassName = WindowClassName;
  ATOM clazz = RegisterClassEx(&wc);
  DCHECK(clazz);

  window_ = CreateWindow(WindowClassName,
                         0, 0, 0, 0, 0, 0, 0, 0, hinst, 0);
  SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
}

SystemMessageWindowWin::~SystemMessageWindowWin() {
  if (window_) {
    DestroyWindow(window_);
    UnregisterClass(WindowClassName, GetModuleHandle(NULL));
  }
}

LRESULT SystemMessageWindowWin::OnDeviceChange(UINT event_type, DWORD data) {
  base::SystemMonitor* monitor = base::SystemMonitor::Get();
  switch (event_type) {
    case DBT_DEVNODES_CHANGED:
      monitor->ProcessDevicesChanged();
      break;
    case DBT_DEVICEARRIVAL: {
      DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data);
      for (int i = 0; unitmask; ++i, unitmask >>= 1) {
        if (unitmask & 0x01) {
          FilePath::StringType drive(L"_:\\");
          drive[0] = L'A' + i;
          WCHAR volume_name[MAX_PATH + 1];
          if ((*volume_name_func_)(drive.c_str(), volume_name, MAX_PATH + 1)) {
            monitor->ProcessMediaDeviceAttached(
                i, base::SysWideToUTF8(volume_name), FilePath(drive));
          }
        }
      }
      break;
    }
    case DBT_DEVICEREMOVECOMPLETE: {
      DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data);
      for (int i = 0; unitmask; ++i, unitmask >>= 1) {
        if (unitmask & 0x01) {
          monitor->ProcessMediaDeviceDetached(i);
        }
      }
      break;
    }
  }
  return TRUE;
}

LRESULT CALLBACK SystemMessageWindowWin::WndProc(HWND hwnd, UINT message,
                                                 WPARAM wparam, LPARAM lparam) {
  switch (message) {
    case WM_DEVICECHANGE:
      return OnDeviceChange(static_cast<UINT>(wparam),
                            static_cast<DWORD>(lparam));
    default:
      break;
  }

  return ::DefWindowProc(hwnd, message, wparam, lparam);
}