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/message_loop.h"
8 #include "base/strings/string_util.h"
9 #include "base/win/scoped_comptr.h"
10 #include "base/win/windows_version.h"
15 HMODULE
GetMetroModule() {
16 const HMODULE kUninitialized
= reinterpret_cast<HMODULE
>(1);
17 static HMODULE metro_module
= kUninitialized
;
19 if (metro_module
== kUninitialized
) {
20 // Initialize the cache, note that the initialization is idempotent
21 // under the assumption that metro_driver is never unloaded, so the
22 // race to this assignment is safe.
23 metro_module
= GetModuleHandleA("metro_driver.dll");
24 if (metro_module
!= NULL
) {
25 // This must be a metro process if the metro_driver is loaded.
26 DCHECK(IsMetroProcess());
30 DCHECK(metro_module
!= kUninitialized
);
34 bool IsMetroProcess() {
40 // The immersive state of a process can never change.
41 // Look it up once and cache it here.
42 static ImmersiveState state
= kImmersiveUnknown
;
44 if (state
== kImmersiveUnknown
) {
45 if (IsProcessImmersive(::GetCurrentProcess())) {
46 state
= kImmersiveTrue
;
48 state
= kImmersiveFalse
;
51 DCHECK_NE(kImmersiveUnknown
, state
);
52 return state
== kImmersiveTrue
;
55 bool IsProcessImmersive(HANDLE process
) {
56 typedef BOOL (WINAPI
* IsImmersiveProcessFunc
)(HANDLE process
);
57 HMODULE user32
= ::GetModuleHandleA("user32.dll");
58 DCHECK(user32
!= NULL
);
60 IsImmersiveProcessFunc is_immersive_process
=
61 reinterpret_cast<IsImmersiveProcessFunc
>(
62 ::GetProcAddress(user32
, "IsImmersiveProcess"));
64 if (is_immersive_process
)
65 return is_immersive_process(process
) ? true: false;
69 wchar_t* LocalAllocAndCopyString(const string16
& src
) {
70 size_t dest_size
= (src
.length() + 1) * sizeof(wchar_t);
71 wchar_t* dest
= reinterpret_cast<wchar_t*>(LocalAlloc(LPTR
, dest_size
));
72 base::wcslcpy(dest
, src
.c_str(), dest_size
);
76 bool IsParentalControlActivityLoggingOn() {
77 // Query this info on Windows 7 and above.
78 if (base::win::GetVersion() < base::win::VERSION_WIN7
)
81 static bool parental_control_logging_required
= false;
82 static bool parental_control_status_determined
= false;
84 if (parental_control_status_determined
)
85 return parental_control_logging_required
;
87 parental_control_status_determined
= true;
89 ScopedComPtr
<IWindowsParentalControlsCore
> parent_controls
;
90 HRESULT hr
= parent_controls
.CreateInstance(
91 __uuidof(WindowsParentalControls
));
95 ScopedComPtr
<IWPCSettings
> settings
;
96 hr
= parent_controls
->GetUserSettings(NULL
, settings
.Receive());
100 unsigned long restrictions
= 0;
101 settings
->GetRestrictions(&restrictions
);
103 parental_control_logging_required
=
104 (restrictions
& WPCFLAG_LOGGING_REQUIRED
) == WPCFLAG_LOGGING_REQUIRED
;
105 return parental_control_logging_required
;
108 // Metro driver exports for getting the launch type, initial url, initial
111 typedef const wchar_t* (*GetInitialUrl
)();
112 typedef const wchar_t* (*GetInitialSearchString
)();
113 typedef base::win::MetroLaunchType (*GetLaunchType
)(
114 base::win::MetroPreviousExecutionState
* previous_state
);
117 MetroLaunchType
GetMetroLaunchParams(string16
* params
) {
118 HMODULE metro
= base::win::GetMetroModule();
120 return base::win::METRO_LAUNCH_ERROR
;
122 GetLaunchType get_launch_type
= reinterpret_cast<GetLaunchType
>(
123 ::GetProcAddress(metro
, "GetLaunchType"));
124 DCHECK(get_launch_type
);
126 base::win::MetroLaunchType launch_type
= get_launch_type(NULL
);
128 if ((launch_type
== base::win::METRO_PROTOCOL
) ||
129 (launch_type
== base::win::METRO_LAUNCH
)) {
130 GetInitialUrl initial_metro_url
= reinterpret_cast<GetInitialUrl
>(
131 ::GetProcAddress(metro
, "GetInitialUrl"));
132 DCHECK(initial_metro_url
);
133 *params
= initial_metro_url();
134 } else if (launch_type
== base::win::METRO_SEARCH
) {
135 GetInitialSearchString initial_search_string
=
136 reinterpret_cast<GetInitialSearchString
>(
137 ::GetProcAddress(metro
, "GetInitialSearchString"));
138 DCHECK(initial_search_string
);
139 *params
= initial_search_string();