Chromium Code Reviews| Index: chrome/browser/download/download_id.h |
| diff --git a/chrome/browser/download/download_id.h b/chrome/browser/download/download_id.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..04d0fade5beda6ae864fe8f12549eedc28ded4e2 |
| --- /dev/null |
| +++ b/chrome/browser/download/download_id.h |
| @@ -0,0 +1,84 @@ |
| +// Copyright (c) 2011 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. |
| + |
| +#ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ID_H_ |
| +#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ID_H_ |
| +#pragma once |
| + |
| +#include <iostream> |
| + |
| +#include "base/hash_tables.h" |
| + |
| +class DownloadManager; |
| + |
| +// DownloadId combines per-profile Download ids with an indication of which |
| +// profile in order to be globally unique. DownloadIds are not persistent across |
| +// sessions, but their local() field is. |
| +class DownloadId { |
| + public: |
| + explicit DownloadId(const DownloadManager* manager = NULL, |
| + int32 local_id = -1) |
| + : manager_(manager), |
| + local_id_(local_id) { |
| + } |
| + |
| + // Return the per-profile and persistent part of this DownloadId. |
| + int32 local() const { return local_id_; } |
| + |
| + // Returns true if this DownloadId has been allocated and could possibly refer |
| + // to a DownloadItem that exists. |
| + bool IsValid() const { return ((manager_ != NULL) && (local_id_ >= 0)); } |
| + |
| + // Returns true if this DownloadId was allocated by test_man. |
| + bool BelongsTo(const DownloadManager* test_man) const { |
|
Randy Smith (Not in Mondays)
2011/08/03 21:10:29
What's the intended use for this routine? It does
benjhayden
2011/08/04 17:15:00
I was imagining sprinkling DCHECK(global_id.Belong
Randy Smith (Not in Mondays)
2011/08/04 19:52:27
I'm fine with that. The perfectionist in me sorta
|
| + return test_man == manager_; |
| + } |
| + |
| + // The following methods (operator==, operator<, hash(), copy, and assign) |
| + // provide support for STL containers such as hash_map and set. |
| + |
| + // Returns true if this refers to the same DownloadItem as that. |
| + bool operator==(const DownloadId& that) const { |
| + return ((that.local_id_ == local_id_) && |
| + (that.manager_ == manager_)); |
| + } |
| + bool operator<(const DownloadId& that) const { |
| + return ((manager_ < that.manager_) || |
| + (local_id_ < that.local_id_)); |
| + } |
| + |
| + size_t hash() const { |
| + // The top half of manager is unlikely to be distinct, and the user is |
| + // unlikely to have >64K downloads. If these assumptions are incorrect, then |
| + // DownloadFileManager's hash_map might have a few collisions, but it will |
| + // use operator== to safely disambiguate. |
| + return reinterpret_cast<size_t>(manager_) + |
| + (static_cast<size_t>(local_id_) << (4 * sizeof(size_t))); |
| + } |
| + |
| + private: |
| + // DownloadId is used mostly off the UI thread, so manager's methods can't be |
| + // called, but the pointer can be compared. |
| + const DownloadManager* manager_; |
| + |
| + int32 local_id_; |
| + |
| + friend std::ostream& operator<<(std::ostream& out, |
| + const DownloadId& global_id); |
| + |
| + // Allow copy and assign. |
| +}; |
| + |
| +// Allow logging DownloadIds. Looks like "0x01234567:42". |
| +std::ostream& operator<<(std::ostream& out, const DownloadId& global_id); |
| + |
| +// Allow using DownloadIds as keys in hash_maps. |
| +namespace BASE_HASH_NAMESPACE { |
| +template<> struct hash<DownloadId> { |
| + std::size_t operator()(const DownloadId& did) const { |
| + return did.hash(); |
| + } |
| +}; |
| +} |
| +#endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ID_H_ |