1 /*
2  *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/desktop_capture/win/selected_window_context.h"
12 
13 namespace webrtc {
14 
SelectedWindowContext(HWND selected_window,DesktopRect selected_window_rect,WindowCaptureHelperWin * window_capture_helper)15 SelectedWindowContext::SelectedWindowContext(
16     HWND selected_window,
17     DesktopRect selected_window_rect,
18     WindowCaptureHelperWin* window_capture_helper)
19     : selected_window_(selected_window),
20       selected_window_rect_(selected_window_rect),
21       window_capture_helper_(window_capture_helper) {
22   selected_window_thread_id_ =
23       GetWindowThreadProcessId(selected_window, &selected_window_process_id_);
24 }
25 
IsSelectedWindowValid() const26 bool SelectedWindowContext::IsSelectedWindowValid() const {
27   return selected_window_thread_id_ != 0;
28 }
29 
IsWindowOwnedBySelectedWindow(HWND hwnd) const30 bool SelectedWindowContext::IsWindowOwnedBySelectedWindow(HWND hwnd) const {
31   // This check works for drop-down menus & dialog pop-up windows. It doesn't
32   // work for context menus or tooltips, which are handled differently below.
33   if (GetAncestor(hwnd, GA_ROOTOWNER) == selected_window_) {
34     return true;
35   }
36 
37   // Some pop-up windows aren't owned (e.g. context menus, tooltips); treat
38   // windows that belong to the same thread as owned.
39   DWORD enumerated_window_process_id = 0;
40   DWORD enumerated_window_thread_id =
41       GetWindowThreadProcessId(hwnd, &enumerated_window_process_id);
42   return enumerated_window_thread_id != 0 &&
43          enumerated_window_process_id == selected_window_process_id_ &&
44          enumerated_window_thread_id == selected_window_thread_id_;
45 }
46 
IsWindowOverlappingSelectedWindow(HWND hwnd) const47 bool SelectedWindowContext::IsWindowOverlappingSelectedWindow(HWND hwnd) const {
48   return window_capture_helper_->AreWindowsOverlapping(hwnd, selected_window_,
49                                                        selected_window_rect_);
50 }
51 
selected_window() const52 HWND SelectedWindowContext::selected_window() const {
53   return selected_window_;
54 }
55 
window_capture_helper() const56 WindowCaptureHelperWin* SelectedWindowContext::window_capture_helper() const {
57   return window_capture_helper_;
58 }
59 
60 }  // namespace webrtc
61