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.
5 #include "base/win/metro.h"
7 #include "base/message_loop.h"
8 #include "base/string_util.h"
9 #include "base/win/scoped_comptr.h"
10 #include "base/win/windows_version.h"
16 bool g_should_tsf_aware_required
= false;
19 HMODULE
GetMetroModule() {
20 const HMODULE kUninitialized
= reinterpret_cast<HMODULE
>(1);
21 static HMODULE metro_module
= kUninitialized
;
23 if (metro_module
== kUninitialized
) {
24 // Initialize the cache, note that the initialization is idempotent
25 // under the assumption that metro_driver is never unloaded, so the
26 // race to this assignment is safe.
27 metro_module
= GetModuleHandleA("metro_driver.dll");
28 if (metro_module
!= NULL
) {
29 // This must be a metro process if the metro_driver is loaded.
30 DCHECK(IsMetroProcess());
34 DCHECK(metro_module
!= kUninitialized
);
38 bool IsMetroProcess() {
44 // The immersive state of a process can never change.
45 // Look it up once and cache it here.
46 static ImmersiveState state
= kImmersiveUnknown
;
48 if (state
== kImmersiveUnknown
) {
49 if (IsProcessImmersive(::GetCurrentProcess())) {
50 state
= kImmersiveTrue
;
52 state
= kImmersiveFalse
;
55 DCHECK_NE(kImmersiveUnknown
, state
);
56 return state
== kImmersiveTrue
;
59 bool IsProcessImmersive(HANDLE process
) {
60 typedef BOOL (WINAPI
* IsImmersiveProcessFunc
)(HANDLE process
);
61 HMODULE user32
= ::GetModuleHandleA("user32.dll");
62 DCHECK(user32
!= NULL
);
64 IsImmersiveProcessFunc is_immersive_process
=
65 reinterpret_cast<IsImmersiveProcessFunc
>(
66 ::GetProcAddress(user32
, "IsImmersiveProcess"));
68 if (is_immersive_process
)
69 return is_immersive_process(process
) ? true: false;
73 bool IsTsfAwareRequired() {
74 // Although this function is equal to IsMetroProcess at this moment,
75 // Chrome for Win7 and Vista may support TSF in the future.
76 return g_should_tsf_aware_required
|| IsMetroProcess();
79 void SetForceToUseTsf() {
80 g_should_tsf_aware_required
= true;
82 // Since Windows 8 Metro mode disables CUAS (Cicero Unaware Application
83 // Support) via ImmDisableLegacyIME API, Chrome must be fully TSF-aware on
84 // Metro mode. For debugging purposes, explicitly call ImmDisableLegacyIME so
85 // that one can test TSF functionality even on Windows 8 desktop mode. Note
86 // that CUAS cannot be disabled on Windows Vista/7 where ImmDisableLegacyIME
88 typedef BOOL (* ImmDisableLegacyIMEFunc
)();
89 HMODULE imm32
= ::GetModuleHandleA("imm32.dll");
93 ImmDisableLegacyIMEFunc imm_disable_legacy_ime
=
94 reinterpret_cast<ImmDisableLegacyIMEFunc
>(
95 ::GetProcAddress(imm32
, "ImmDisableLegacyIME"));
97 if (imm_disable_legacy_ime
== NULL
) {
98 // Unsupported API, just do nothing.
102 if (!imm_disable_legacy_ime()) {
103 DVLOG(1) << "Failed to disable legacy IME.";
107 wchar_t* LocalAllocAndCopyString(const string16
& src
) {
108 size_t dest_size
= (src
.length() + 1) * sizeof(wchar_t);
109 wchar_t* dest
= reinterpret_cast<wchar_t*>(LocalAlloc(LPTR
, dest_size
));
110 base::wcslcpy(dest
, src
.c_str(), dest_size
);
114 bool IsTouchEnabled() {
115 int value
= GetSystemMetrics(SM_DIGITIZER
);
116 return (value
& (NID_READY
| NID_INTEGRATED_TOUCH
)) ==
117 (NID_READY
| NID_INTEGRATED_TOUCH
);
120 bool IsParentalControlActivityLoggingOn() {
121 // Query this info on Windows Vista and above.
122 if (base::win::GetVersion() < base::win::VERSION_VISTA
)
125 static bool parental_control_logging_required
= false;
126 static bool parental_control_status_determined
= false;
128 if (parental_control_status_determined
)
129 return parental_control_logging_required
;
131 parental_control_status_determined
= true;
133 ScopedComPtr
<IWindowsParentalControlsCore
> parent_controls
;
134 HRESULT hr
= parent_controls
.CreateInstance(
135 __uuidof(WindowsParentalControls
));
139 ScopedComPtr
<IWPCSettings
> settings
;
140 hr
= parent_controls
->GetUserSettings(NULL
, settings
.Receive());
144 unsigned long restrictions
= 0;
145 settings
->GetRestrictions(&restrictions
);
147 parental_control_logging_required
=
148 (restrictions
& WPCFLAG_LOGGING_REQUIRED
) == WPCFLAG_LOGGING_REQUIRED
;
149 return parental_control_logging_required
;
152 // Metro driver exports for getting the launch type, initial url, initial
155 typedef const wchar_t* (*GetInitialUrl
)();
156 typedef const wchar_t* (*GetInitialSearchString
)();
157 typedef base::win::MetroLaunchType (*GetLaunchType
)(
158 base::win::MetroPreviousExecutionState
* previous_state
);
161 MetroLaunchType
GetMetroLaunchParams(string16
* params
) {
162 HMODULE metro
= base::win::GetMetroModule();
164 return base::win::METRO_LAUNCH_ERROR
;
166 GetLaunchType get_launch_type
= reinterpret_cast<GetLaunchType
>(
167 ::GetProcAddress(metro
, "GetLaunchType"));
168 DCHECK(get_launch_type
);
170 base::win::MetroLaunchType launch_type
= get_launch_type(NULL
);
172 if ((launch_type
== base::win::METRO_PROTOCOL
) ||
173 (launch_type
== base::win::METRO_LAUNCH
)) {
174 GetInitialUrl initial_metro_url
= reinterpret_cast<GetInitialUrl
>(
175 ::GetProcAddress(metro
, "GetInitialUrl"));
176 DCHECK(initial_metro_url
);
177 *params
= initial_metro_url();
178 } else if (launch_type
== base::win::METRO_SEARCH
) {
179 GetInitialSearchString initial_search_string
=
180 reinterpret_cast<GetInitialSearchString
>(
181 ::GetProcAddress(metro
, "GetInitialSearchString"));
182 DCHECK(initial_search_string
);
183 *params
= initial_search_string();