summaryrefslogtreecommitdiffstats
path: root/chrome_frame/chrome_frame_helper_util.cc
blob: d246881cc055ea51e75139d57f752e65d7654666 (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
// Copyright (c) 2010 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 "chrome_frame/chrome_frame_helper_util.h"

#include <shlwapi.h>

const wchar_t kGetBrowserMessage[] = L"GetAutomationObject";

bool UtilIsWebBrowserWindow(HWND window_to_check) {
  bool is_browser_window = false;

  if (!IsWindow(window_to_check)) {
    return is_browser_window;
  }

  static wchar_t* known_ie_window_classes[] = {
    L"IEFrame",
    L"TabWindowClass"
  };

  for (int i = 0; i < ARRAYSIZE(known_ie_window_classes); i++) {
    if (IsWindowOfClass(window_to_check, known_ie_window_classes[i])) {
     is_browser_window = true;
     break;
    }
  }

  return is_browser_window;
}

HRESULT UtilGetWebBrowserObjectFromWindow(HWND window,
                                          REFIID iid,
                                          void** web_browser_object) {
  if (NULL == web_browser_object) {
    return E_POINTER;
  }

  // Check whether this window is really a web browser window.
  if (UtilIsWebBrowserWindow(window)) {
    // IWebBroswer2 interface pointer can be retrieved from the browser
    // window by simply sending a registered message "GetAutomationObject"
    // Note that since we are sending a message to parent window make sure that
    // it is in the same thread.
    if (GetWindowThreadProcessId(window, NULL) != GetCurrentThreadId()) {
      return E_UNEXPECTED;
    }

    static const ULONG get_browser_message =
        RegisterWindowMessageW(kGetBrowserMessage);

    *web_browser_object =
        reinterpret_cast<void*>(SendMessage(window,
                                            get_browser_message,
                                            reinterpret_cast<WPARAM>(&iid),
                                            NULL));
    if (NULL != *web_browser_object) {
      return S_OK;
    }
  } else {
    return E_INVALIDARG;
  }
  return E_NOINTERFACE;
}


bool IsWindowOfClass(HWND window_to_check, const wchar_t* window_class) {
  bool window_matches = false;
  const int buf_size = MAX_PATH;
  wchar_t buffer[buf_size] = {0};
  DWORD dwRetSize = GetClassNameW(window_to_check, buffer, buf_size);
  // If the window name is any longer than this, it isn't the one we want.
  if (dwRetSize < (buf_size - 1)) {
    if (0 == lstrcmpiW(window_class, buffer)) {
     window_matches = true;
    }
  }
  return window_matches;
}


bool IsNamedProcess(const wchar_t* process_name) {
  wchar_t file_path[2048] = {0};
  GetModuleFileName(NULL, file_path, 2047);
  wchar_t* file_name = PathFindFileName(file_path);
  return (0 == lstrcmpiW(file_name, process_name));
}