| OLD | NEW |
| (Empty) |
| 1 // Copyright 2009-2010 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 #ifndef OMAHA_GOOPDATE_PACKAGE_CACHE_H_ | |
| 17 #define OMAHA_GOOPDATE_PACKAGE_CACHE_H_ | |
| 18 | |
| 19 #include <windows.h> | |
| 20 #include <atlstr.h> | |
| 21 #include <vector> | |
| 22 #include "base/basictypes.h" | |
| 23 #include "base/synchronized.h" | |
| 24 | |
| 25 namespace omaha { | |
| 26 | |
| 27 class PackageCache { | |
| 28 public: | |
| 29 | |
| 30 // Defines the key that uniquely identifies the packages in the cache. | |
| 31 // The key uses a default version value in the case the application does | |
| 32 // not provide a version string. | |
| 33 class Key { | |
| 34 public: | |
| 35 Key(const CString& app_id, | |
| 36 const CString& version, | |
| 37 const CString& package_name) | |
| 38 : app_id_(app_id), | |
| 39 version_(version.IsEmpty() ? _T("0.0.0.0") : version), | |
| 40 package_name_(package_name) {} | |
| 41 | |
| 42 CString app_id() const { return app_id_; } | |
| 43 CString version() const { return version_; } | |
| 44 CString package_name() const { return package_name_; } | |
| 45 | |
| 46 CString ToString() const { | |
| 47 CString result; | |
| 48 result.Format(_T("appid=%s; version=%s; package_name=%s"), | |
| 49 app_id_, version_, package_name_); | |
| 50 return result; | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 const CString app_id_; | |
| 55 const CString version_; | |
| 56 const CString package_name_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(Key); | |
| 59 }; | |
| 60 | |
| 61 PackageCache(); | |
| 62 ~PackageCache(); | |
| 63 | |
| 64 HRESULT Initialize(const CString& cache_root); | |
| 65 | |
| 66 HRESULT Put(const Key& key, | |
| 67 const CString& source_file, | |
| 68 const CString& hash); | |
| 69 | |
| 70 HRESULT Get(const Key& key, | |
| 71 const CString& destination_file, | |
| 72 const CString& hash) const; | |
| 73 | |
| 74 bool IsCached(const Key& key, const CString& hash) const; | |
| 75 | |
| 76 HRESULT Purge(const Key& key); | |
| 77 | |
| 78 HRESULT PurgeVersion(const CString& app_id, const CString& version); | |
| 79 | |
| 80 HRESULT PurgeApp(const CString& app_id); | |
| 81 | |
| 82 // Purges version directories lower than a 'version' of the form 1.2.3.4. If | |
| 83 // the version format is not recognized, returns E_INVALIDARG. | |
| 84 HRESULT PurgeAppLowerVersions(const CString& app_id, const CString& version); | |
| 85 | |
| 86 HRESULT PurgeAll(); | |
| 87 | |
| 88 // Purges expired packages and keeps total cache size below the limit by | |
| 89 // purging oldest ones. | |
| 90 HRESULT PurgeOldPackagesIfNecessary() const; | |
| 91 | |
| 92 // Returns the total size of all files in the cache. Returns 0 if the size | |
| 93 // cannot be determined or the cache is empty. | |
| 94 uint64 Size() const; | |
| 95 | |
| 96 CString cache_root() const; | |
| 97 | |
| 98 static HRESULT AuthenticateFile(const CString& filename, | |
| 99 const CString& hash); | |
| 100 | |
| 101 private: | |
| 102 friend class PackageCacheTest; | |
| 103 | |
| 104 HRESULT BuildCacheFileNameForKey(const Key& key, CString* filename) const; | |
| 105 HRESULT BuildCacheFileName(const CString& app_id, | |
| 106 const CString& version, | |
| 107 const CString& package_name, | |
| 108 CString* filename) const; | |
| 109 | |
| 110 // Deletes the cache entries that match the app_id, version, and package_name. | |
| 111 // If the parameters are empty, the function deletes the packages of versions | |
| 112 // of apps, respectively. | |
| 113 HRESULT Delete(const CString& app_id, | |
| 114 const CString& version, | |
| 115 const CString& package_name); | |
| 116 | |
| 117 // Returns the cache expiration time. All files in the cache before that time | |
| 118 // are considered as expired and should be purged. | |
| 119 FILETIME GetCacheExpirationTime() const; | |
| 120 | |
| 121 // The cache duration, specified as a count of days. (This is converted to | |
| 122 // an absolute time by GetCacheExpirationTime().) | |
| 123 int cache_time_limit_days_; | |
| 124 | |
| 125 // The maximum allowed cache size, in bytes. If the cache grows over this | |
| 126 // size, files will be purged using a least-recently-added metric. | |
| 127 uint64 cache_size_limit_bytes_; | |
| 128 | |
| 129 CString cache_root_; | |
| 130 | |
| 131 LLock cache_lock_; | |
| 132 | |
| 133 DISALLOW_COPY_AND_ASSIGN(PackageCache); | |
| 134 }; | |
| 135 | |
| 136 } // namespace omaha | |
| 137 | |
| 138 #endif // OMAHA_GOOPDATE_PACKAGE_CACHE_H_ | |
| 139 | |
| OLD | NEW |