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

Unified Diff: chrome/browser/extensions/api/systeminfo_storage/storage_info_provider_win.cc

Issue 10824232: Add systeminfo.storage API implementation skeleton (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Add StorageInfoProvder impl on Windows Created 8 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/systeminfo_storage/storage_info_provider_win.cc
diff --git a/chrome/browser/extensions/api/systeminfo_storage/storage_info_provider_win.cc b/chrome/browser/extensions/api/systeminfo_storage/storage_info_provider_win.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3300d6135631fa40dff37b284e64030c6be69350
--- /dev/null
+++ b/chrome/browser/extensions/api/systeminfo_storage/storage_info_provider_win.cc
@@ -0,0 +1,91 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/extensions/api/systeminfo_storage/storage_info_provider.h"
+
+#include "base/string_util.h"
+
+#include <windows.h>
+
+namespace extensions {
+using api::experimental_systeminfo_storage::StorageInfo;
+using api::experimental_systeminfo_storage::StorageUnitInfo;
+
+// Logical drive string length is 4. It contains one driver letter character,
+// L":", L"\" and a terminating null character.
+const unsigned long kMaxLogicalDriveString = 4 * 26;
+
+// StorageInfoProvider implementation on Windows platform
+class StorageInfoProviderWin : public StorageInfoProvider {
+ public:
+ StorageInfoProviderWin();
+ virtual ~StorageInfoProviderWin();
+ virtual bool GetStorageInfo(StorageInfo* info) OVERRIDE;
+};
+
+StorageInfoProviderWin::StorageInfoProviderWin() {
+}
+
+StorageInfoProviderWin::~StorageInfoProviderWin() {
+}
+
+bool StorageInfoProviderWin::GetStorageInfo(StorageInfo* info) {
+ if (info == NULL) return false;
+
+ WCHAR logical_drive_strings[kMaxLogicalDriveString];
+
+ // The total length of drive string returned from GetLogicalDriveStrings
+ // must be divided exactly by 4 since each drive is represented by 4 wchars.
+ DWORD string_length = GetLogicalDriveStrings(
+ kMaxLogicalDriveString - 1, logical_drive_strings);
+ DCHECK(string_length % 4 == 0);
+ // No drive found, return false.
+ if (string_length == 0)
+ return false;
+
+ // Iterate the drive string by 4 wchars each step
+ for (unsigned int i = 0; i < string_length; i += 4) {
+ std::string type;
+ DWORD ret = GetDriveType(&logical_drive_strings[i]);
+ switch (ret) {
+ case DRIVE_FIXED:
+ type = systeminfo::kStorageTypeHardDisk;
+ break;
+ case DRIVE_REMOVABLE:
+ type = systeminfo::kStorageTypeRemovable;
+ break;
+ case DRIVE_UNKNOWN:
+ type = systeminfo::kStorageTypeUnknown;
+ break;
+ case DRIVE_CDROM: // CD ROM
+ case DRIVE_REMOTE: // Remote network drive
+ case DRIVE_NO_ROOT_DIR: // Invalid root path
+ case DRIVE_RAMDISK: // RAM disk
+ // TODO(hmin): Do we need to care about these drive types?
+ continue;
+ }
+
+ ULARGE_INTEGER available_bytes;
+ ULARGE_INTEGER free_bytes;
+ BOOL status = GetDiskFreeSpaceEx(&logical_drive_strings[i], NULL,
+ &available_bytes,
+ &free_bytes);
+ if (status) {
+ linked_ptr<StorageUnitInfo> unit(new StorageUnitInfo());
+ unit->id = WideToASCII(string16(&logical_drive_strings[i]));
+ unit->type = type;
+ unit->capacity = static_cast<double>(available_bytes.QuadPart);
+ unit->available_capacity = static_cast<double>(free_bytes.QuadPart);
+ info->units.push_back(unit);
+ }
+ }
+ return true;
+}
+
+// static
+StorageInfoProvider* StorageInfoProvider::Create() {
+ return new StorageInfoProviderWin();
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698