• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CEF_LIBCEF_COMMON_NET_UPLOAD_ELEMENT_H_
6 #define CEF_LIBCEF_COMMON_NET_UPLOAD_ELEMENT_H_
7 
8 #include <stdint.h>
9 
10 #include <limits>
11 #include <vector>
12 
13 #include "base/files/file_path.h"
14 #include "base/macros.h"
15 #include "base/time/time.h"
16 #include "net/base/net_export.h"
17 
18 namespace net {
19 
20 // A class representing an element contained by UploadData.
21 class UploadElement {
22  public:
23   enum Type {
24     TYPE_BYTES,
25     TYPE_FILE,
26   };
27 
28   UploadElement();
29   ~UploadElement();
30 
type()31   Type type() const { return type_; }
32 
bytes()33   const char* bytes() const { return bytes_start_ ? bytes_start_ : &buf_[0]; }
bytes_length()34   uint64_t bytes_length() const { return buf_.size() + bytes_length_; }
file_path()35   const base::FilePath& file_path() const { return file_path_; }
file_range_offset()36   uint64_t file_range_offset() const { return file_range_offset_; }
file_range_length()37   uint64_t file_range_length() const { return file_range_length_; }
38   // If NULL time is returned, we do not do the check.
expected_file_modification_time()39   const base::Time& expected_file_modification_time() const {
40     return expected_file_modification_time_;
41   }
42 
SetToBytes(const char * bytes,int bytes_len)43   void SetToBytes(const char* bytes, int bytes_len) {
44     type_ = TYPE_BYTES;
45     buf_.assign(bytes, bytes + bytes_len);
46   }
47 
48   // This does not copy the given data and the caller should make sure
49   // the data is secured somewhere else (e.g. by attaching the data
50   // using SetUserData).
SetToSharedBytes(const char * bytes,int bytes_len)51   void SetToSharedBytes(const char* bytes, int bytes_len) {
52     type_ = TYPE_BYTES;
53     bytes_start_ = bytes;
54     bytes_length_ = bytes_len;
55   }
56 
SetToFilePath(const base::FilePath & path)57   void SetToFilePath(const base::FilePath& path) {
58     SetToFilePathRange(path, 0, std::numeric_limits<uint64_t>::max(),
59                        base::Time());
60   }
61 
62   // If expected_modification_time is NULL, we do not check for the file
63   // change. Also note that the granularity for comparison is time_t, not
64   // the full precision.
SetToFilePathRange(const base::FilePath & path,uint64_t offset,uint64_t length,const base::Time & expected_modification_time)65   void SetToFilePathRange(const base::FilePath& path,
66                           uint64_t offset,
67                           uint64_t length,
68                           const base::Time& expected_modification_time) {
69     type_ = TYPE_FILE;
70     file_path_ = path;
71     file_range_offset_ = offset;
72     file_range_length_ = length;
73     expected_file_modification_time_ = expected_modification_time;
74   }
75 
76  private:
77   Type type_;
78   std::vector<char> buf_;
79   const char* bytes_start_;
80   uint64_t bytes_length_;
81   base::FilePath file_path_;
82   uint64_t file_range_offset_;
83   uint64_t file_range_length_;
84   base::Time expected_file_modification_time_;
85 
86   DISALLOW_COPY_AND_ASSIGN(UploadElement);
87 };
88 
89 #if defined(UNIT_TEST)
90 inline bool operator==(const UploadElement& a, const UploadElement& b) {
91   if (a.type() != b.type())
92     return false;
93   if (a.type() == UploadElement::TYPE_BYTES)
94     return a.bytes_length() == b.bytes_length() &&
95            memcmp(a.bytes(), b.bytes(), b.bytes_length()) == 0;
96   if (a.type() == UploadElement::TYPE_FILE) {
97     return a.file_path() == b.file_path() &&
98            a.file_range_offset() == b.file_range_offset() &&
99            a.file_range_length() == b.file_range_length() &&
100            a.expected_file_modification_time() ==
101                b.expected_file_modification_time();
102   }
103   return false;
104 }
105 
106 inline bool operator!=(const UploadElement& a, const UploadElement& b) {
107   return !(a == b);
108 }
109 #endif  // defined(UNIT_TEST)
110 
111 }  // namespace net
112 
113 #endif  // CEF_LIBCEF_COMMON_NET_UPLOAD_ELEMENT_H_
114