1/*
2 * Copyright (C) 2007 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 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "IconRecord.h"
31
32#include "BitmapImage.h"
33#include "IconDatabase.h"
34#include "Logging.h"
35#include "SQLiteStatement.h"
36#include "SQLiteTransaction.h"
37#include <wtf/text/CString.h>
38
39#include <limits.h>
40
41namespace WebCore {
42
43IconRecord::IconRecord(const String& url)
44    : m_iconURL(url)
45    , m_stamp(0)
46    , m_dataSet(false)
47{
48
49}
50
51IconRecord::~IconRecord()
52{
53    LOG(IconDatabase, "Destroying IconRecord for icon url %s", m_iconURL.ascii().data());
54}
55
56Image* IconRecord::image(const IntSize&)
57{
58    // FIXME rdar://4680377 - For size right now, we are returning our one and only image and the Bridge
59    // is resizing it in place.  We need to actually store all the original representations here and return a native
60    // one, or resize the best one to the requested size and cache that result.
61
62    return m_image.get();
63}
64
65void IconRecord::setImageData(PassRefPtr<SharedBuffer> data)
66{
67    // It's okay to delete the raw image here. Any existing clients using this icon will be
68    // managing an image that was created with a copy of this raw image data.
69    m_image = BitmapImage::create();
70
71    // Copy the provided data into the buffer of the new Image object.
72    if (!m_image->setData(data, true)) {
73        LOG(IconDatabase, "Manual image data for iconURL '%s' FAILED - it was probably invalid image data", m_iconURL.ascii().data());
74        m_image.clear();
75    }
76
77    m_dataSet = true;
78}
79
80void IconRecord::loadImageFromResource(const char* resource)
81{
82    if (!resource)
83        return;
84
85    m_image = Image::loadPlatformResource(resource);
86    m_dataSet = true;
87}
88
89ImageDataStatus IconRecord::imageDataStatus()
90{
91    if (!m_dataSet)
92        return ImageDataStatusUnknown;
93    if (!m_image)
94        return ImageDataStatusMissing;
95    return ImageDataStatusPresent;
96}
97
98IconSnapshot IconRecord::snapshot(bool forDeletion) const
99{
100    if (forDeletion)
101        return IconSnapshot(m_iconURL, 0, 0);
102
103    return IconSnapshot(m_iconURL, m_stamp, m_image ? m_image->data() : 0);
104}
105
106} // namespace WebCore
107