OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ID_H_ | |
6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ID_H_ | |
7 #pragma once | |
8 | |
9 #include <iostream> | |
10 | |
11 #include "base/hash_tables.h" | |
12 | |
13 class DownloadManager; | |
14 | |
15 // DownloadId combines per-profile Download ids with an indication of which | |
16 // profile in order to be globally unique. DownloadIds are not persistent across | |
17 // sessions, but their local() field is. | |
Randy Smith (Not in Mondays)
2011/07/28 21:03:16
Huh. This brings up a really interesting question
benjhayden
2011/08/03 17:44:46
I was going to show extensions the local id, becau
Randy Smith (Not in Mondays)
2011/08/03 21:10:29
Sounds good--I'll feel better if we have someone w
benjhayden
2011/08/04 17:15:00
ProfileImpl seems to suggest that there is exactly
| |
18 class DownloadId { | |
19 public: | |
20 explicit DownloadId(const DownloadManager* manager = NULL, | |
21 int32 local_id = -1) | |
22 : manager_(manager), | |
23 local_id_(local_id) { | |
24 } | |
25 | |
26 // Return the per-profile and persistent part of this DownloadId. | |
27 int32 local() const { return local_id_; } | |
28 | |
29 // Returns true if this DownloadId has been allocated and could possibly refer | |
30 // to a DownloadItem that exists. | |
31 bool IsValid() const { return ((manager_ != NULL) && (local_id_ >= 0)); } | |
32 | |
33 // Returns true if this DownloadId was allocated by test_man. | |
34 bool BelongsTo(const DownloadManager* test_man) const { | |
35 return test_man == manager_; | |
36 } | |
37 | |
38 // The following methods (operator==, operator<, hash(), copy, and assign) | |
39 // provide support for STL containers such as hash_map and set. | |
40 | |
41 // Returns true if this refers to the same DownloadItem as that. | |
42 bool operator==(const DownloadId& that) const { | |
43 return ((that.local_id_ == local_id_) && | |
44 (that.manager_ == manager_)); | |
45 } | |
46 bool operator<(const DownloadId& that) const { | |
47 return ((manager_ < that.manager_) || | |
48 (local_id_ < that.local_id_)); | |
49 } | |
50 | |
51 size_t hash() const { | |
52 // 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 // DownloadFileManager's hash_map might have a few collisions, but it will | |
55 // use operator== to safely disambiguate. | |
56 return reinterpret_cast<size_t>(manager_) + | |
57 (static_cast<size_t>(local_id_) << (4 * sizeof(size_t))); | |
58 } | |
59 | |
60 private: | |
61 // DownloadId is used mostly off the UI thread, so manager's methods can't be | |
62 // called, but the pointer can be compared. | |
63 const DownloadManager* manager_; | |
64 | |
65 int32 local_id_; | |
66 | |
67 friend std::ostream& operator<<(std::ostream& out, | |
68 const DownloadId& global_id); | |
69 | |
70 // Allow copy and assign. | |
71 }; | |
72 | |
73 // Allow logging DownloadIds. Looks like "0x01234567:42". | |
74 std::ostream& operator<<(std::ostream& out, const DownloadId& global_id); | |
75 | |
76 // Allow using DownloadIds as keys in hash_maps. | |
77 namespace BASE_HASH_NAMESPACE { | |
78 template<> struct hash<DownloadId> { | |
79 std::size_t operator()(const DownloadId& did) const { | |
80 return did.hash(); | |
81 } | |
82 }; | |
83 } | |
84 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ID_H_ | |
OLD | NEW |