1/*
2 * Copyright 2010, The Android Open Source Project
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 *  * Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 *  * 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 THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "PackageNotifier.h"
28
29#if ENABLE(APPLICATION_INSTALLED)
30
31#include <wtf/Assertions.h>
32#include <wtf/StdLibExtras.h>
33
34namespace WebCore {
35
36PackageNotifier::PackageNotifier()
37        : m_onResultAvailable(0), m_isInitialized(false), m_timer(this, &PackageNotifier::timerFired)  { }
38
39void PackageNotifier::setOnResultAvailable(Callback callback)
40{
41    m_onResultAvailable = callback;
42}
43
44void PackageNotifier::addPackageNames(const HashSet<String>& packageNames)
45{
46    if (!m_isInitialized)
47        m_isInitialized = true;
48
49    typedef HashSet<String>::const_iterator NamesIterator;
50    for (NamesIterator iter = packageNames.begin(); iter != packageNames.end(); ++iter)
51        m_packageNames.add(*iter);
52
53    if (m_onResultAvailable)
54        m_onResultAvailable();
55}
56
57void PackageNotifier::addPackageName(const String& packageName)
58{
59    ASSERT(m_isInitialized);
60    m_packageNames.add(packageName);
61}
62
63void PackageNotifier::removePackageName(const String& packageName)
64{
65    ASSERT(m_isInitialized);
66    m_packageNames.remove(packageName);
67}
68
69void PackageNotifier::requestPackageResult()
70{
71    if (!m_isInitialized || m_timer.isActive())
72        return;
73
74    m_timer.startOneShot(0);
75}
76
77void PackageNotifier::timerFired(Timer<PackageNotifier>*)
78{
79    m_timer.stop();
80    if (m_onResultAvailable)
81        m_onResultAvailable();
82}
83
84bool PackageNotifier::isPackageInstalled(const String& packageName)
85{
86    return m_packageNames.contains(packageName);
87}
88
89PackageNotifier& packageNotifier()
90{
91    AtomicallyInitializedStatic(PackageNotifier*, packageNotifier = new PackageNotifier);
92    return *packageNotifier;
93}
94
95}
96
97#endif // ENABLE(APPLICATION_INSTALLED)
98