Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: chrome/browser/media_galleries/win/mtp_device_operations_util.cc

Issue 2894483002: Migrate from ScopedComPtr::CreateInstance() to CoCreateInstance in chrome/... (Closed)
Patch Set: Fix Header Include Location Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/media_galleries/win/mtp_device_operations_util.h" 5 #include "chrome/browser/media_galleries/win/mtp_device_operations_util.h"
6 6
7 #include <objbase.h>
7 #include <portabledevice.h> 8 #include <portabledevice.h>
8 #include <stdint.h> 9 #include <stdint.h>
9 10
10 #include <algorithm> 11 #include <algorithm>
11 #include <limits> 12 #include <limits>
12 #include <memory> 13 #include <memory>
13 14
14 #include "base/files/file_path.h" 15 #include "base/files/file_path.h"
15 #include "base/files/file_util.h" 16 #include "base/files/file_util.h"
16 #include "base/logging.h" 17 #include "base/logging.h"
17 #include "base/numerics/safe_conversions.h" 18 #include "base/numerics/safe_conversions.h"
18 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
19 #include "base/threading/thread_restrictions.h" 20 #include "base/threading/thread_restrictions.h"
20 #include "base/time/time.h" 21 #include "base/time/time.h"
21 #include "base/win/scoped_co_mem.h" 22 #include "base/win/scoped_co_mem.h"
22 #include "base/win/scoped_propvariant.h" 23 #include "base/win/scoped_propvariant.h"
23 #include "chrome/common/chrome_constants.h" 24 #include "chrome/common/chrome_constants.h"
24 25
25 namespace media_transfer_protocol { 26 namespace media_transfer_protocol {
26 27
27 namespace { 28 namespace {
28 29
29 // On success, returns true and updates |client_info| with a reference to an 30 // On success, returns true and updates |client_info| with a reference to an
30 // IPortableDeviceValues interface that holds information about the 31 // IPortableDeviceValues interface that holds information about the
31 // application that communicates with the device. 32 // application that communicates with the device.
32 bool GetClientInformation( 33 bool GetClientInformation(
33 base::win::ScopedComPtr<IPortableDeviceValues>* client_info) { 34 base::win::ScopedComPtr<IPortableDeviceValues>* client_info) {
34 base::ThreadRestrictions::AssertIOAllowed(); 35 base::ThreadRestrictions::AssertIOAllowed();
35 DCHECK(client_info); 36 DCHECK(client_info);
36 HRESULT hr = client_info->CreateInstance(__uuidof(PortableDeviceValues), 37 HRESULT hr = ::CoCreateInstance(__uuidof(PortableDeviceValues), NULL,
37 NULL, CLSCTX_INPROC_SERVER); 38 CLSCTX_INPROC_SERVER,
39 IID_PPV_ARGS(client_info->GetAddressOf()));
38 if (FAILED(hr)) { 40 if (FAILED(hr)) {
39 DPLOG(ERROR) << "Failed to create an instance of IPortableDeviceValues"; 41 DPLOG(ERROR) << "Failed to create an instance of IPortableDeviceValues";
40 return false; 42 return false;
41 } 43 }
42 44
43 (*client_info)->SetStringValue(WPD_CLIENT_NAME, 45 (*client_info)->SetStringValue(WPD_CLIENT_NAME,
44 chrome::kBrowserProcessExecutableName); 46 chrome::kBrowserProcessExecutableName);
45 (*client_info)->SetUnsignedIntegerValue(WPD_CLIENT_MAJOR_VERSION, 0); 47 (*client_info)->SetUnsignedIntegerValue(WPD_CLIENT_MAJOR_VERSION, 0);
46 (*client_info)->SetUnsignedIntegerValue(WPD_CLIENT_MINOR_VERSION, 0); 48 (*client_info)->SetUnsignedIntegerValue(WPD_CLIENT_MINOR_VERSION, 0);
47 (*client_info)->SetUnsignedIntegerValue(WPD_CLIENT_REVISION, 0); 49 (*client_info)->SetUnsignedIntegerValue(WPD_CLIENT_REVISION, 0);
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 GetDeviceContent(device); 186 GetDeviceContent(device);
185 if (!content.Get()) 187 if (!content.Get())
186 return false; 188 return false;
187 189
188 base::win::ScopedComPtr<IPortableDeviceProperties> properties; 190 base::win::ScopedComPtr<IPortableDeviceProperties> properties;
189 HRESULT hr = content->Properties(properties.GetAddressOf()); 191 HRESULT hr = content->Properties(properties.GetAddressOf());
190 if (FAILED(hr)) 192 if (FAILED(hr))
191 return false; 193 return false;
192 194
193 base::win::ScopedComPtr<IPortableDeviceKeyCollection> properties_to_read; 195 base::win::ScopedComPtr<IPortableDeviceKeyCollection> properties_to_read;
194 hr = properties_to_read.CreateInstance(__uuidof(PortableDeviceKeyCollection), 196 hr = ::CoCreateInstance(__uuidof(PortableDeviceKeyCollection), NULL,
195 NULL, 197 CLSCTX_INPROC_SERVER,
196 CLSCTX_INPROC_SERVER); 198 IID_PPV_ARGS(&properties_to_read));
197 if (FAILED(hr)) 199 if (FAILED(hr))
198 return false; 200 return false;
199 201
200 if (FAILED(properties_to_read->Add(WPD_OBJECT_CONTENT_TYPE)) || 202 if (FAILED(properties_to_read->Add(WPD_OBJECT_CONTENT_TYPE)) ||
201 FAILED(properties_to_read->Add(WPD_OBJECT_FORMAT)) || 203 FAILED(properties_to_read->Add(WPD_OBJECT_FORMAT)) ||
202 FAILED(properties_to_read->Add(WPD_OBJECT_ORIGINAL_FILE_NAME)) || 204 FAILED(properties_to_read->Add(WPD_OBJECT_ORIGINAL_FILE_NAME)) ||
203 FAILED(properties_to_read->Add(WPD_OBJECT_NAME)) || 205 FAILED(properties_to_read->Add(WPD_OBJECT_NAME)) ||
204 FAILED(properties_to_read->Add(WPD_OBJECT_DATE_MODIFIED)) || 206 FAILED(properties_to_read->Add(WPD_OBJECT_DATE_MODIFIED)) ||
205 FAILED(properties_to_read->Add(WPD_OBJECT_DATE_CREATED)) || 207 FAILED(properties_to_read->Add(WPD_OBJECT_DATE_CREATED)) ||
206 FAILED(properties_to_read->Add(WPD_OBJECT_SIZE))) 208 FAILED(properties_to_read->Add(WPD_OBJECT_SIZE)))
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 } // namespace 305 } // namespace
304 306
305 base::win::ScopedComPtr<IPortableDevice> OpenDevice( 307 base::win::ScopedComPtr<IPortableDevice> OpenDevice(
306 const base::string16& pnp_device_id) { 308 const base::string16& pnp_device_id) {
307 base::ThreadRestrictions::AssertIOAllowed(); 309 base::ThreadRestrictions::AssertIOAllowed();
308 DCHECK(!pnp_device_id.empty()); 310 DCHECK(!pnp_device_id.empty());
309 base::win::ScopedComPtr<IPortableDeviceValues> client_info; 311 base::win::ScopedComPtr<IPortableDeviceValues> client_info;
310 if (!GetClientInformation(&client_info)) 312 if (!GetClientInformation(&client_info))
311 return base::win::ScopedComPtr<IPortableDevice>(); 313 return base::win::ScopedComPtr<IPortableDevice>();
312 base::win::ScopedComPtr<IPortableDevice> device; 314 base::win::ScopedComPtr<IPortableDevice> device;
313 HRESULT hr = device.CreateInstance(__uuidof(PortableDevice), NULL, 315 HRESULT hr = ::CoCreateInstance(__uuidof(PortableDevice), NULL,
314 CLSCTX_INPROC_SERVER); 316 CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&device));
315 if (FAILED(hr)) 317 if (FAILED(hr))
316 return base::win::ScopedComPtr<IPortableDevice>(); 318 return base::win::ScopedComPtr<IPortableDevice>();
317 319
318 hr = device->Open(pnp_device_id.c_str(), client_info.Get()); 320 hr = device->Open(pnp_device_id.c_str(), client_info.Get());
319 if (SUCCEEDED(hr)) 321 if (SUCCEEDED(hr))
320 return device; 322 return device;
321 if (hr == E_ACCESSDENIED) 323 if (hr == E_ACCESSDENIED)
322 DPLOG(ERROR) << "Access denied to open the device"; 324 DPLOG(ERROR) << "Access denied to open the device";
323 return base::win::ScopedComPtr<IPortableDevice>(); 325 return base::win::ScopedComPtr<IPortableDevice>();
324 } 326 }
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 object_entries.empty()) 410 object_entries.empty())
409 return base::string16(); 411 return base::string16();
410 // TODO(thestig): This DCHECK can fail. Multiple MTP objects can have 412 // TODO(thestig): This DCHECK can fail. Multiple MTP objects can have
411 // the same name. Handle the situation gracefully. Refer to crbug.com/169930 413 // the same name. Handle the situation gracefully. Refer to crbug.com/169930
412 // for more details. 414 // for more details.
413 DCHECK_EQ(1U, object_entries.size()); 415 DCHECK_EQ(1U, object_entries.size());
414 return object_entries[0].object_id; 416 return object_entries[0].object_id;
415 } 417 }
416 418
417 } // namespace media_transfer_protocol 419 } // namespace media_transfer_protocol
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698