1/*
2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "BlobDataFileReference.h"
28
29#include "File.h"
30#include "FileMetadata.h"
31
32namespace WebCore {
33
34BlobDataFileReference::BlobDataFileReference(const String& path)
35    : m_path(path)
36#if ENABLE(FILE_REPLACEMENT)
37    , m_replacementShouldBeGenerated(false)
38#endif
39    , m_size(0)
40    , m_expectedModificationTime(invalidFileTime())
41{
42}
43
44BlobDataFileReference::~BlobDataFileReference()
45{
46#if ENABLE(FILE_REPLACEMENT)
47    if (!m_replacementPath.isNull())
48        deleteFile(m_replacementPath);
49#endif
50}
51
52const String& BlobDataFileReference::path()
53{
54#if ENABLE(FILE_REPLACEMENT)
55    if (m_replacementShouldBeGenerated)
56        generateReplacementFile();
57
58    if (!m_replacementPath.isNull())
59        return m_replacementPath;
60#endif
61
62    return m_path;
63}
64
65unsigned long long BlobDataFileReference::size()
66{
67#if ENABLE(FILE_REPLACEMENT)
68    if (m_replacementShouldBeGenerated)
69        generateReplacementFile();
70#endif
71
72    return m_size;
73}
74
75double BlobDataFileReference::expectedModificationTime()
76{
77#if ENABLE(FILE_REPLACEMENT)
78    // We do not currently track modifications for generated files, because we have a snapshot.
79    // Unfortunately, this is inconsistent with regular file handling - File objects should be invalidated when underlying files change.
80    if (m_replacementShouldBeGenerated || !m_replacementPath.isNull())
81        return invalidFileTime();
82#endif
83
84    return m_expectedModificationTime;
85}
86
87void BlobDataFileReference::startTrackingModifications()
88{
89    // This is not done automatically by the constructor, because BlobDataFileReference is
90    // also used to pass paths around before registration. Only registered blobs need to pay
91    // the cost of tracking file modifications.
92
93    ASSERT(!isValidFileTime(m_expectedModificationTime));
94
95#if ENABLE(FILE_REPLACEMENT)
96    m_replacementShouldBeGenerated = File::shouldReplaceFile(m_path);
97#endif
98
99    // FIXME: Some platforms provide better ways to listen for file system object changes, consider using these.
100    FileMetadata metadata;
101    if (!getFileMetadata(m_path, metadata))
102        return;
103
104    m_expectedModificationTime = metadata.modificationTime;
105
106#if ENABLE(FILE_REPLACEMENT)
107    if (m_replacementShouldBeGenerated)
108        return;
109#endif
110
111    m_size = metadata.length;
112}
113
114void BlobDataFileReference::prepareForFileAccess()
115{
116}
117
118void BlobDataFileReference::revokeFileAccess()
119{
120}
121
122}
123