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