| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ID_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ID_H_ |
| 6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ID_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ID_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <iosfwd> | 9 #include <iosfwd> |
| 10 | 10 |
| 11 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
| 12 #include "content/common/content_export.h" | 12 #include "content/common/content_export.h" |
| 13 | 13 |
| 14 class DownloadManager; | |
| 15 | |
| 16 // DownloadId combines per-profile Download ids with an indication of which | 14 // DownloadId combines per-profile Download ids with an indication of which |
| 17 // profile in order to be globally unique. DownloadIds are not persistent across | 15 // profile in order to be globally unique. DownloadIds are not persistent across |
| 18 // sessions, but their local() field is. | 16 // sessions, but their local() field is. |
| 19 class DownloadId { | 17 class DownloadId { |
| 20 public: | 18 public: |
| 21 static DownloadId Invalid() { return DownloadId(NULL, -1); } | 19 static DownloadId Invalid() { return DownloadId(NULL, -1); } |
| 22 | 20 |
| 23 DownloadId(const DownloadManager* manager, int32 local_id) | 21 // Domain separates spaces of local ids. |
| 24 : manager_(manager), | 22 typedef const void* Domain; |
| 23 |
| 24 DownloadId(Domain domain, int32 local_id) |
| 25 : domain_(domain), |
| 25 local_id_(local_id) { | 26 local_id_(local_id) { |
| 26 } | 27 } |
| 27 | 28 |
| 28 // Return the per-profile and persistent part of this DownloadId. | 29 // Return the per-profile and persistent part of this DownloadId. |
| 29 int32 local() const { return local_id_; } | 30 int32 local() const { return local_id_; } |
| 30 | 31 |
| 31 // Returns true if this DownloadId has been allocated and could possibly refer | 32 // Returns true if this DownloadId has been allocated and could possibly refer |
| 32 // to a DownloadItem that exists. | 33 // to a DownloadItem that exists. |
| 33 bool IsValid() const { return ((manager_ != NULL) && (local_id_ >= 0)); } | 34 bool IsValid() const { return ((domain_ != NULL) && (local_id_ >= 0)); } |
| 34 | 35 |
| 35 // The following methods (operator==, hash(), copy, and assign) provide | 36 // The following methods (operator==, hash(), copy, and assign) provide |
| 36 // support for STL containers such as hash_map. | 37 // support for STL containers such as hash_map. |
| 37 | 38 |
| 38 bool operator==(const DownloadId& that) const { | 39 bool operator==(const DownloadId& that) const { |
| 39 return ((that.local_id_ == local_id_) && | 40 return ((that.local_id_ == local_id_) && |
| 40 (that.manager_ == manager_)); | 41 (that.domain_ == domain_)); |
| 41 } | 42 } |
| 42 bool operator<(const DownloadId& that) const { | 43 bool operator<(const DownloadId& that) const { |
| 43 // Even though DownloadManager* < DownloadManager* is not well defined and | 44 // Even though Domain::operator< is not well defined and GCC does not |
| 44 // GCC does not require it for hash_map, MSVC requires operator< for | 45 // require it for hash_map, MSVC requires operator< for hash_map. We don't |
| 45 // hash_map. We don't ifdef it out here because we will probably make a | 46 // ifdef it out here because we will probably make a set<DownloadId> at some |
| 46 // set<DownloadId> at some point, when GCC will require it. | 47 // point, when GCC will require it. |
| 47 return ((manager_ < that.manager_) || | 48 return ((domain_ < that.domain_) || |
| 48 ((manager_ == that.manager_) && (local_id_ < that.local_id_))); | 49 ((domain_ == that.domain_) && (local_id_ < that.local_id_))); |
| 49 } | 50 } |
| 50 | 51 |
| 51 size_t hash() const { | 52 size_t hash() const { |
| 52 // The top half of manager is unlikely to be distinct, and the user is | 53 // The top half of manager is unlikely to be distinct, and the user is |
| 53 // unlikely to have >64K downloads. If these assumptions are incorrect, then | 54 // unlikely to have >64K downloads. If these assumptions are incorrect, then |
| 54 // DownloadFileManager's hash_map might have a few collisions, but it will | 55 // DownloadFileManager's hash_map might have a few collisions, but it will |
| 55 // use operator== to safely disambiguate. | 56 // use operator== to safely disambiguate. |
| 56 return reinterpret_cast<size_t>(manager_) + | 57 return reinterpret_cast<size_t>(domain_) + |
| 57 (static_cast<size_t>(local_id_) << (4 * sizeof(size_t))); | 58 (static_cast<size_t>(local_id_) << (4 * sizeof(size_t))); |
| 58 } | 59 } |
| 59 | 60 |
| 61 std::string DebugString() const; |
| 62 |
| 60 private: | 63 private: |
| 61 // DownloadId is used mostly off the UI thread, so manager's methods can't be | 64 Domain domain_; |
| 62 // called, but the pointer can be compared. | |
| 63 const DownloadManager* manager_; | |
| 64 | 65 |
| 65 int32 local_id_; | 66 int32 local_id_; |
| 66 | 67 |
| 67 friend CONTENT_EXPORT std::ostream& operator<<(std::ostream& out, | |
| 68 const DownloadId& global_id); | |
| 69 | |
| 70 // Allow copy and assign. | 68 // Allow copy and assign. |
| 71 }; | 69 }; |
| 72 | 70 |
| 73 // Allow logging DownloadIds. Looks like "0x01234567:42". | 71 // Allow logging DownloadIds. Looks like "0x01234567:42". |
| 74 CONTENT_EXPORT std::ostream& operator<<(std::ostream& out, | 72 CONTENT_EXPORT std::ostream& operator<<(std::ostream& out, |
| 75 const DownloadId& global_id); | 73 const DownloadId& global_id); |
| 76 | 74 |
| 77 // Allow using DownloadIds as keys in hash_maps. | 75 // Allow using DownloadIds as keys in hash_maps. |
| 78 namespace BASE_HASH_NAMESPACE { | 76 namespace BASE_HASH_NAMESPACE { |
| 79 #if defined(COMPILER_GCC) | 77 #if defined(COMPILER_GCC) |
| 80 template<> struct hash<DownloadId> { | 78 template<> struct hash<DownloadId> { |
| 81 std::size_t operator()(const DownloadId& id) const { | 79 std::size_t operator()(const DownloadId& id) const { |
| 82 return id.hash(); | 80 return id.hash(); |
| 83 } | 81 } |
| 84 }; | 82 }; |
| 85 #elif defined(COMPILER_MSVC) | 83 #elif defined(COMPILER_MSVC) |
| 86 inline size_t hash_value(const DownloadId& id) { | 84 inline size_t hash_value(const DownloadId& id) { |
| 87 return id.hash(); | 85 return id.hash(); |
| 88 } | 86 } |
| 89 #endif // COMPILER | 87 #endif // COMPILER |
| 90 } | 88 } |
| 91 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ID_H_ | 89 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ID_H_ |
| OLD | NEW |