Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2009 Google 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 are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "WebDatabase.h"
     33 
     34 #include "Database.h"
     35 #include "DatabaseTask.h"
     36 #include "DatabaseThread.h"
     37 #include "DatabaseTracker.h"
     38 #include "Document.h"
     39 #include "KURL.h"
     40 #include "QuotaTracker.h"
     41 #include "SecurityOrigin.h"
     42 #include "WebDatabaseObserver.h"
     43 #include "WebString.h"
     44 #include <wtf/PassRefPtr.h>
     45 #include <wtf/RefPtr.h>
     46 
     47 using namespace WebCore;
     48 
     49 namespace WebKit {
     50 
     51 static WebDatabaseObserver* databaseObserver = 0;
     52 
     53 class WebDatabasePrivate : public Database {
     54 };
     55 
     56 void WebDatabase::reset()
     57 {
     58     assign(0);
     59 }
     60 
     61 void WebDatabase::assign(const WebDatabase& other)
     62 {
     63     WebDatabasePrivate* d = const_cast<WebDatabasePrivate*>(other.m_private);
     64     if (d)
     65         d->ref();
     66     assign(d);
     67 }
     68 
     69 WebString WebDatabase::name() const
     70 {
     71     ASSERT(m_private);
     72     return m_private->stringIdentifier();
     73 }
     74 
     75 WebString WebDatabase::displayName() const
     76 {
     77     ASSERT(m_private);
     78     return m_private->displayName();
     79 }
     80 
     81 unsigned long WebDatabase::estimatedSize() const
     82 {
     83     ASSERT(m_private);
     84     return m_private->estimatedSize();
     85 }
     86 
     87 WebSecurityOrigin WebDatabase::securityOrigin() const
     88 {
     89     ASSERT(m_private);
     90     return WebSecurityOrigin(m_private->securityOrigin());
     91 }
     92 
     93 void WebDatabase::setObserver(WebDatabaseObserver* observer)
     94 {
     95     databaseObserver = observer;
     96 }
     97 
     98 WebDatabaseObserver* WebDatabase::observer()
     99 {
    100     return databaseObserver;
    101 }
    102 
    103 void WebDatabase::updateDatabaseSize(
    104     const WebString& originIdentifier, const WebString& databaseName,
    105     unsigned long long databaseSize, unsigned long long spaceAvailable)
    106 {
    107     WebCore::QuotaTracker::instance().updateDatabaseSizeAndSpaceAvailableToOrigin(
    108         originIdentifier, databaseName, databaseSize, spaceAvailable);
    109 }
    110 
    111 void WebDatabase::closeDatabaseImmediately(const WebString& originIdentifier, const WebString& databaseName)
    112 {
    113     HashSet<RefPtr<Database> > databaseHandles;
    114     PassRefPtr<SecurityOrigin> originPrp(*WebSecurityOrigin::createFromDatabaseIdentifier(originIdentifier));
    115     RefPtr<SecurityOrigin> origin = originPrp;
    116     DatabaseTracker::tracker().getOpenDatabases(origin.get(), databaseName, &databaseHandles);
    117     for (HashSet<RefPtr<Database> >::iterator it = databaseHandles.begin(); it != databaseHandles.end(); ++it) {
    118         Database* database = it->get();
    119         DatabaseThread* databaseThread = database->scriptExecutionContext()->databaseThread();
    120         if (databaseThread && !databaseThread->terminationRequested()) {
    121             database->stop();
    122             databaseThread->scheduleTask(DatabaseCloseTask::create(database, 0));
    123         }
    124     }
    125 }
    126 
    127 WebDatabase::WebDatabase(const WTF::PassRefPtr<Database>& database)
    128     : m_private(static_cast<WebDatabasePrivate*>(database.releaseRef()))
    129 {
    130 }
    131 
    132 WebDatabase& WebDatabase::operator=(const WTF::PassRefPtr<Database>& database)
    133 {
    134     assign(static_cast<WebDatabasePrivate*>(database.releaseRef()));
    135     return *this;
    136 }
    137 
    138 WebDatabase::operator WTF::PassRefPtr<Database>() const
    139 {
    140     return PassRefPtr<Database>(const_cast<WebDatabasePrivate*>(m_private));
    141 }
    142 
    143 void WebDatabase::assign(WebDatabasePrivate* d)
    144 {
    145     // d is already ref'd for us by the caller
    146     if (m_private)
    147         m_private->deref();
    148     m_private = d;
    149 }
    150 
    151 } // namespace WebKit
    152