sbWMDMUtils.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
27 //------------------------------------------------------------------------------
28 //
29 // Songbird WMDM utilities services.
30 //
31 //------------------------------------------------------------------------------
32 
38 //------------------------------------------------------------------------------
39 //
40 // Songbird WMDM utilities imported services.
41 //
42 //------------------------------------------------------------------------------
43 
44 // Self import.
45 #include "sbWMDMUtils.h"
46 
47 // Local imports.
48 #include "sbWindowsDeviceUtils.h"
49 
50 // Songbird imports.
51 #include <WMDRMCertificate.h>
52 
53 // Mozilla imports.
54 #include <nsAutoPtr.h>
55 
56 // Windows imports.
57 #include <SCClient.h>
58 
59 
60 //------------------------------------------------------------------------------
61 //
62 // Songbird WMDM utilities services.
63 //
64 //------------------------------------------------------------------------------
65 
66 //-------------------------------------
67 //
68 // sbWMDMGetDeviceFromDeviceInstanceID
69 //
70 
71 nsresult
72 sbWMDMGetDeviceFromDeviceInstanceID(nsAString& aDeviceInstanceID,
73  IWMDMDevice** aDevice)
74 {
75  // Validate arguments.
76  NS_ENSURE_ARG_POINTER(aDevice);
77 
78  // Function variables.
79  HRESULT hr;
80  nsresult rv;
81 
82  // Get the WMDM manager.
83  nsRefPtr<IWMDeviceManager> deviceManager;
84  nsRefPtr<IWMDeviceManager2> deviceManager2;
85  rv = sbWMDMGetDeviceManager(getter_AddRefs(deviceManager));
86  NS_ENSURE_SUCCESS(rv, rv);
87  hr = deviceManager->QueryInterface(__uuidof(IWMDeviceManager2),
88  getter_AddRefs(deviceManager2));
89  NS_ENSURE_TRUE(SUCCEEDED(hr), NS_ERROR_FAILURE);
90 
91  // Get an enumerator for all WMDM devices.
92  nsRefPtr<IWMDMEnumDevice> deviceEnum;
93  hr = deviceManager2->EnumDevices2(getter_AddRefs(deviceEnum));
94  NS_ENSURE_TRUE(SUCCEEDED(hr), NS_ERROR_FAILURE);
95 
96  // Search for a WMDM device that matches the device instance ID.
97  nsRefPtr<IWMDMDevice> matchingDevice;
98  while (1) {
99  // Get the next device.
100  nsRefPtr<IWMDMDevice> device;
101  ULONG deviceCount;
102  hr = deviceEnum->Next(1, getter_AddRefs(device), &deviceCount);
103  NS_ENSURE_TRUE(SUCCEEDED(hr), NS_ERROR_FAILURE);
104  if (!deviceCount)
105  break;
106 
107  // Get the device canonical name.
108  WCHAR canonicalName[MAX_PATH] = {0};
109  nsRefPtr<IWMDMDevice2> device2;
110  hr = device->QueryInterface(__uuidof(IWMDMDevice2),
111  getter_AddRefs(device2));
112  if (!SUCCEEDED(hr))
113  continue;
114  hr = device2->GetCanonicalName(canonicalName, ARRAYSIZE(canonicalName));
115  if (!SUCCEEDED(hr))
116  continue;
117 
118  // Get the device interface name from the canonical name. Cut off the
119  // "$<index>" string from the end.
120  // See http://msdn.microsoft.com/en-us/library/ff801405%28v=VS.85%29.aspx
121  nsAutoString deviceInterfaceName(canonicalName);
122  PRInt32 index = deviceInterfaceName.RFindChar('$');
123  if (index >= 0)
124  deviceInterfaceName.SetLength(index);
125 
126  // Get the device instance ID from the device interface name.
127  nsAutoString deviceInstanceID;
128  rv = sbWinGetDeviceInstanceIDFromDeviceInterfaceName(deviceInterfaceName,
129  deviceInstanceID);
130  if (NS_FAILED(rv))
131  continue;
132 
133  // Check for a match.
134  if (deviceInstanceID.Equals(aDeviceInstanceID)) {
135  matchingDevice = device;
136  break;
137  }
138  }
139  if (!matchingDevice)
140  return NS_ERROR_NOT_AVAILABLE;
141 
142  // Return results.
143  matchingDevice.forget(aDevice);
144 
145  return NS_OK;
146 }
147 
148 
149 //-------------------------------------
150 //
151 // sbWMDMGetDeviceManager
152 //
153 
154 nsresult
155 sbWMDMGetDeviceManager(IWMDeviceManager** aDeviceManager)
156 {
157  // Validate arguments.
158  NS_ENSURE_ARG_POINTER(aDeviceManager);
159 
160  // Function variables.
161  HRESULT hr;
162 
163  // Get the WMDM device manager authentication interface.
164  nsRefPtr<IComponentAuthenticate> wmdmDeviceManagerAuth;
165  hr = ::CoCreateInstance(__uuidof(::MediaDevMgr),
166  NULL,
167  CLSCTX_INPROC_SERVER,
168  __uuidof(IComponentAuthenticate),
169  getter_AddRefs(wmdmDeviceManagerAuth));
170  NS_ENSURE_TRUE(SUCCEEDED(hr), NS_ERROR_FAILURE);
171 
172  // Create a secure channel with the WMDRM certificate.
173  CSecureChannelClient* secureChannelClient = new ::CSecureChannelClient;
174  NS_ENSURE_TRUE(secureChannelClient, NS_ERROR_OUT_OF_MEMORY);
175  hr = secureChannelClient->SetCertificate(SAC_CERT_V1,
176  wmDRMCertificate,
177  sizeof(wmDRMCertificate),
178  wmDRMPrivateKey,
179  sizeof(wmDRMPrivateKey));
180  NS_ENSURE_TRUE(SUCCEEDED(hr), NS_ERROR_FAILURE);
181 
182  // Authenticate with the WMDM device manager.
183  secureChannelClient->SetInterface(wmdmDeviceManagerAuth);
184  hr = secureChannelClient->Authenticate(SAC_PROTOCOL_V1);
185  NS_ENSURE_TRUE(SUCCEEDED(hr), NS_ERROR_FAILURE);
186 
187  // Get the WMDM device manager interface.
188  nsRefPtr<IWMDeviceManager> deviceManager;
189  hr = wmdmDeviceManagerAuth->QueryInterface(__uuidof(IWMDeviceManager),
190  getter_AddRefs(deviceManager));
191  NS_ENSURE_TRUE(SUCCEEDED(hr), NS_ERROR_FAILURE);
192 
193  // Return results.
194  deviceManager.forget(aDeviceManager);
195 
196  return NS_OK;
197 }
198 
return NS_OK
Songbird Windows Device Utilities Definitions.
Songbird WMDM Utilities Definitions.
nsresult sbWinGetDeviceInstanceIDFromDeviceInterfaceName(nsAString &aDeviceInterfaceName, nsAString &aDeviceInstanceID)
nsresult sbWMDMGetDeviceFromDeviceInstanceID(nsAString &aDeviceInstanceID, IWMDMDevice **aDevice)
Definition: sbWMDMUtils.cpp:72
nsresult sbWMDMGetDeviceManager(IWMDeviceManager **aDeviceManager)