OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "base/win/metro.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include "base/strings/string_util.h" | |
10 #include "base/win/windows_version.h" | |
11 | |
12 namespace base { | |
13 namespace win { | |
14 | |
15 bool IsChromeMetroSupported() { | |
16 const Version win_version = GetVersion(); | |
17 return win_version >= VERSION_WIN8 && win_version < VERSION_WIN10; | |
18 } | |
19 | |
20 HMODULE GetMetroModule() { | |
21 const HMODULE kUninitialized = reinterpret_cast<HMODULE>(1); | |
22 static HMODULE metro_module = kUninitialized; | |
23 | |
24 if (metro_module == kUninitialized) { | |
25 // Initialize the cache, note that the initialization is idempotent | |
26 // under the assumption that metro_driver is never unloaded, so the | |
27 // race to this assignment is safe. | |
28 metro_module = GetModuleHandleA("metro_driver.dll"); | |
29 if (metro_module != NULL) { | |
30 // This must be a metro process if the metro_driver is loaded. | |
31 DCHECK(IsMetroProcess()); | |
32 } | |
33 } | |
34 | |
35 DCHECK(metro_module != kUninitialized); | |
36 return metro_module; | |
37 } | |
38 | |
39 bool IsMetroProcess() { | |
40 enum ImmersiveState { | |
41 kImmersiveUnknown, | |
42 kImmersiveTrue, | |
43 kImmersiveFalse | |
44 }; | |
45 // The immersive state of a process can never change. | |
46 // Look it up once and cache it here. | |
47 static ImmersiveState state = kImmersiveUnknown; | |
48 | |
49 if (state == kImmersiveUnknown) { | |
50 if (IsProcessImmersive(::GetCurrentProcess())) { | |
51 state = kImmersiveTrue; | |
52 } else { | |
53 state = kImmersiveFalse; | |
54 } | |
55 } | |
56 DCHECK_NE(kImmersiveUnknown, state); | |
57 return state == kImmersiveTrue; | |
58 } | |
59 | |
60 bool IsProcessImmersive(HANDLE process) { | |
61 typedef BOOL (WINAPI* IsImmersiveProcessFunc)(HANDLE process); | |
62 HMODULE user32 = ::GetModuleHandleA("user32.dll"); | |
63 DCHECK(user32 != NULL); | |
64 | |
65 IsImmersiveProcessFunc is_immersive_process = | |
66 reinterpret_cast<IsImmersiveProcessFunc>( | |
67 ::GetProcAddress(user32, "IsImmersiveProcess")); | |
68 | |
69 if (is_immersive_process) | |
70 return is_immersive_process(process) ? true: false; | |
71 return false; | |
72 } | |
73 | |
74 wchar_t* LocalAllocAndCopyString(const string16& src) { | |
75 size_t dest_size = (src.length() + 1) * sizeof(wchar_t); | |
76 wchar_t* dest = reinterpret_cast<wchar_t*>(LocalAlloc(LPTR, dest_size)); | |
77 base::wcslcpy(dest, src.c_str(), dest_size); | |
78 return dest; | |
79 } | |
80 | |
81 // Metro driver exports for getting the launch type, initial url, initial | |
82 // search term, etc. | |
83 extern "C" { | |
84 typedef const wchar_t* (*GetInitialUrl)(); | |
85 typedef const wchar_t* (*GetInitialSearchString)(); | |
86 typedef base::win::MetroLaunchType (*GetLaunchType)( | |
87 base::win::MetroPreviousExecutionState* previous_state); | |
88 } | |
89 | |
90 MetroLaunchType GetMetroLaunchParams(string16* params) { | |
91 HMODULE metro = base::win::GetMetroModule(); | |
92 if (!metro) | |
93 return base::win::METRO_LAUNCH_ERROR; | |
94 | |
95 GetLaunchType get_launch_type = reinterpret_cast<GetLaunchType>( | |
96 ::GetProcAddress(metro, "GetLaunchType")); | |
97 DCHECK(get_launch_type); | |
98 | |
99 base::win::MetroLaunchType launch_type = get_launch_type(NULL); | |
100 | |
101 if ((launch_type == base::win::METRO_PROTOCOL) || | |
102 (launch_type == base::win::METRO_LAUNCH)) { | |
103 GetInitialUrl initial_metro_url = reinterpret_cast<GetInitialUrl>( | |
104 ::GetProcAddress(metro, "GetInitialUrl")); | |
105 DCHECK(initial_metro_url); | |
106 *params = initial_metro_url(); | |
107 } else if (launch_type == base::win::METRO_SEARCH) { | |
108 GetInitialSearchString initial_search_string = | |
109 reinterpret_cast<GetInitialSearchString>( | |
110 ::GetProcAddress(metro, "GetInitialSearchString")); | |
111 DCHECK(initial_search_string); | |
112 *params = initial_search_string(); | |
113 } | |
114 return launch_type; | |
115 } | |
116 | |
117 } // namespace win | |
118 } // namespace base | |
OLD | NEW |