joshualitt | cdad12f | 2016-02-08 07:08:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "UrlDataManager.h" |
| 9 | |
| 10 | bool operator==(const SkData& a, const SkData& b) { |
| 11 | return a.equals(&b); |
| 12 | } |
| 13 | |
| 14 | UrlDataManager::UrlDataManager(SkString rootUrl) : fRootUrl(rootUrl), fDataId(0) {} |
| 15 | |
| 16 | SkString UrlDataManager::addData(SkData* data, const char* contentType) { |
| 17 | UrlData* urlData = fCache.find(*data); |
| 18 | if (fCache.find(*data)) { |
| 19 | SkASSERT(data->equals(urlData->fData.get())); |
| 20 | return urlData->fUrl; |
| 21 | } |
| 22 | |
| 23 | urlData = new UrlData; |
| 24 | urlData->fData.reset(SkRef(data)); |
| 25 | urlData->fContentType.set(contentType); |
| 26 | urlData->fUrl.appendf("%s/%d", fRootUrl.c_str(), fDataId++); |
| 27 | |
| 28 | fCache.add(urlData); |
| 29 | |
| 30 | SkASSERT(!fUrlLookup.find(urlData->fUrl)); |
| 31 | fUrlLookup.add(urlData); |
| 32 | return urlData->fUrl; |
| 33 | } |
| 34 | |
| 35 | void UrlDataManager::reset() { |
| 36 | SkTDynamicHash<UrlData, SkData, LookupTrait>::Iter iter(&fCache); |
| 37 | while (!iter.done()) { |
| 38 | UrlData* urlData = &(*iter); |
| 39 | urlData->unref(); |
| 40 | ++iter; |
| 41 | } |
| 42 | |
| 43 | fCache.rewind(); |
| 44 | } |