18a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block/*
28a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block * Copyright 2010, The Android Open Source Project
38a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block *
48a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block * Redistribution and use in source and binary forms, with or without
58a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block * modification, are permitted provided that the following conditions
68a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block * are met:
78a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block *  * Redistributions of source code must retain the above copyright
88a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block *    notice, this list of conditions and the following disclaimer.
98a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block *  * Redistributions in binary form must reproduce the above copyright
108a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block *    notice, this list of conditions and the following disclaimer in the
118a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block *    documentation and/or other materials provided with the distribution.
128a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block *
138a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
148a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
158a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
168a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
178a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
188a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
198a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
208a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
218a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
228a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
238a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
248a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block */
258a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block
268a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block#ifndef GeolocationPositionCache_h
278a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block#define GeolocationPositionCache_h
288a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block
29f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch#include "PlatformString.h"
30f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch#include "ScriptExecutionContext.h"
31f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch
32dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch#include <wtf/Forward.h>
33f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch#include <wtf/MessageQueue.h>
348a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block#include <wtf/PassRefPtr.h>
358a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block#include <wtf/RefPtr.h>
368a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block
378a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Blocknamespace WebCore {
388a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block
398a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Blockclass Geoposition;
408a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block
41f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch// Maintains a cached position for Geolocation. Takes care of writing and
42f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch// reading the position to a database on a background thread. The Geolocation
43f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch// spec does not require a cached position to be maintained, so we take a
44f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch// best-effort approach where we do not wait for database reads or writes.
458a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Blockclass GeolocationPositionCache {
46f05b935882198ccf7d81675736e3aeb089c5113aBen Murdochpublic:
47f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    static GeolocationPositionCache* instance();
48f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch
49f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    void addUser();
50f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    void removeUser();
518a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block
52f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    void setDatabasePath(const String&);
538a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block    void setCachedPosition(Geoposition*);
548a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block    Geoposition* cachedPosition();
558a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block
56f05b935882198ccf7d81675736e3aeb089c5113aBen Murdochprivate:
57f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    GeolocationPositionCache();
58f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch
59f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    void startBackgroundThread();
60f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    static void* threadEntryPoint(void* object);
61f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    void threadEntryPointImpl();
62f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch
63f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    void triggerReadFromDatabase();
64f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    static void readFromDatabase(ScriptExecutionContext*, GeolocationPositionCache*);
65f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    void readFromDatabaseImpl();
66f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    void triggerWriteToDatabase();
67f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    static void writeToDatabase(ScriptExecutionContext*, GeolocationPositionCache*);
68f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    void writeToDatabaseImpl();
698a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block
70f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    RefPtr<Geoposition> m_cachedPosition;
71f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    Mutex m_cachedPositionMutex;
72f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    ThreadIdentifier m_threadId;
73f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    MessageQueue<ScriptExecutionContext::Task> m_queue;
74f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    String m_databaseFile;
75f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    Mutex m_databaseFileMutex;
768a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block};
778a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block
788a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block} // namespace WebCore
798a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block
808a03f7cd9e884b3db11bb5485b4d9f5095dc0bcaSteve Block#endif // GeolocationPositionCache_h
81