1 // Copyright (c) 2012 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.
5 #ifndef CONTENT_PUBLIC_BROWSER_DOWNLOAD_ID_H_
6 #define CONTENT_PUBLIC_BROWSER_DOWNLOAD_ID_H_
11 #include "base/containers/hash_tables.h"
12 #include "base/strings/stringprintf.h"
13 #include "content/common/content_export.h"
17 // DownloadId combines per-profile Download ids with an indication of which
18 // profile in order to be globally unique. DownloadIds are not persistent across
19 // sessions, but their local() field is.
22 static DownloadId
Invalid() { return DownloadId(); }
24 // Domain separates spaces of local ids.
25 typedef const void* Domain
;
27 DownloadId() : domain_(NULL
), local_id_(-1) {}
29 DownloadId(Domain domain
, int32 local_id
)
34 // Return the per-profile and persistent part of this DownloadId.
35 int32
local() const { return local_id_
; }
37 // Returns true if this DownloadId has been allocated and could possibly refer
38 // to a DownloadItem that exists.
39 bool IsValid() const { return ((domain_
!= NULL
) && (local_id_
>= 0)); }
41 // The following methods (operator==, hash(), copy, and assign) provide
42 // support for STL containers such as hash_map.
44 bool operator==(const DownloadId
& that
) const {
45 return ((that
.local_id_
== local_id_
) &&
46 (that
.domain_
== domain_
));
48 bool operator<(const DownloadId
& that
) const {
49 // Even though Domain::operator< is not well defined and
50 // GCC does not require it for hash_map, MSVC requires operator< for
51 // hash_map. We don't ifdef it out here because we will probably make a
52 // set<DownloadId> at some point, when GCC will require it.
53 return ((domain_
< that
.domain_
) ||
54 ((domain_
== that
.domain_
) && (local_id_
< that
.local_id_
)));
58 // The top half of domain_ is unlikely to be distinct, and the user is
59 // unlikely to have >64K downloads. If these assumptions are incorrect, then
60 // hash_maps containing DownloadIds might have a few collisions, but can
61 // use operator== to safely disambiguate.
62 return reinterpret_cast<size_t>(domain_
) +
63 (static_cast<size_t>(local_id_
) << (4 * sizeof(size_t)));
66 std::string
DebugString() const {
67 return base::StringPrintf("%p:%d", domain_
, local_id_
);
75 // Allow copy and assign.
78 } // namespace content
80 // Allow logging DownloadIds. Looks like "0x01234567:42".
81 CONTENT_EXPORT
std::ostream
& operator<<(std::ostream
& out
,
82 const content::DownloadId
& global_id
);
83 inline std::ostream
& operator<<(std::ostream
& out
,
84 const content::DownloadId
& global_id
) {
85 return out
<< global_id
.DebugString();
88 // Allow using DownloadIds as keys in hash_maps.
89 namespace BASE_HASH_NAMESPACE
{
90 #if defined(COMPILER_GCC)
91 template<> struct hash
<content::DownloadId
> {
92 std::size_t operator()(const content::DownloadId
& id
) const {
96 #elif defined(COMPILER_MSVC)
97 inline size_t hash_value(const content::DownloadId
& id
) {
102 #endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_ID_H_