Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: content/browser/download/download_id.h

Issue 7847027: DownloadId (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: merge Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 <iosfwd>
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.
18 class DownloadId {
19 public:
20 static DownloadId Invalid() { return DownloadId(NULL, -1); }
21
22 DownloadId(const DownloadManager* manager, int32 local_id)
23 : manager_(manager),
24 local_id_(local_id) {
25 }
26
27 // Return the per-profile and persistent part of this DownloadId.
28 int32 local() const { return local_id_; }
29
30 // Returns true if this DownloadId has been allocated and could possibly refer
31 // to a DownloadItem that exists.
32 bool IsValid() const { return ((manager_ != NULL) && (local_id_ >= 0)); }
33
34 // The following methods (operator==, hash(), copy, and assign) provide
35 // support for STL containers such as hash_map.
36
37 bool operator==(const DownloadId& that) const {
38 return ((that.local_id_ == local_id_) &&
39 (that.manager_ == manager_));
40 }
41 bool operator<(const DownloadId& that) const {
42 // Even though DownloadManager* < DownloadManager* is not well defined and
43 // GCC does not require it for hash_map, MSVC requires operator< for
44 // hash_map. We don't ifdef it out here because we will probably make a
45 // set<DownloadId> at some point, when GCC will require it.
46 return ((that.local_id_ < local_id_) &&
47 (that.manager_ < manager_));
48 }
49
50 size_t hash() const {
51 // The top half of manager is unlikely to be distinct, and the user is
52 // unlikely to have >64K downloads. If these assumptions are incorrect, then
53 // DownloadFileManager's hash_map might have a few collisions, but it will
54 // use operator== to safely disambiguate.
55 return reinterpret_cast<size_t>(manager_) +
56 (static_cast<size_t>(local_id_) << (4 * sizeof(size_t)));
57 }
58
59 private:
60 // DownloadId is used mostly off the UI thread, so manager's methods can't be
61 // called, but the pointer can be compared.
62 const DownloadManager* manager_;
63
64 int32 local_id_;
65
66 friend std::ostream& operator<<(std::ostream& out,
67 const DownloadId& global_id);
68
69 // Allow copy and assign.
70 };
71
72 // Allow logging DownloadIds. Looks like "0x01234567:42".
73 std::ostream& operator<<(std::ostream& out, const DownloadId& global_id);
74
75 // Allow using DownloadIds as keys in hash_maps.
76 namespace BASE_HASH_NAMESPACE {
77 #if defined(COMPILER_GCC)
78 template<> struct hash<DownloadId> {
79 std::size_t operator()(const DownloadId& id) const {
80 return id.hash();
81 }
82 };
83 #elif defined(COMPILER_MSVC)
84 inline size_t hash_value(const DownloadId& id) {
85 return id.hash();
86 }
87 #endif // COMPILER
88 }
89 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ID_H_
OLDNEW
« no previous file with comments | « content/browser/download/download_file_manager.cc ('k') | content/browser/download/download_id.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698