Home | History | Annotate | Download | only in net
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/net/quota_policy_channel_id_store.h"
      6 
      7 #include <list>
      8 
      9 #include "base/basictypes.h"
     10 #include "base/bind.h"
     11 #include "base/files/file_path.h"
     12 #include "base/files/file_util.h"
     13 #include "base/logging.h"
     14 #include "base/metrics/histogram.h"
     15 #include "base/strings/string_util.h"
     16 #include "base/threading/thread.h"
     17 #include "base/threading/thread_restrictions.h"
     18 #include "net/cookies/cookie_util.h"
     19 #include "net/extras/sqlite/sqlite_channel_id_store.h"
     20 #include "storage/browser/quota/special_storage_policy.h"
     21 #include "url/gurl.h"
     22 
     23 QuotaPolicyChannelIDStore::QuotaPolicyChannelIDStore(
     24     const base::FilePath& path,
     25     const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
     26     const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy)
     27     : special_storage_policy_(special_storage_policy),
     28       persistent_store_(
     29           new net::SQLiteChannelIDStore(path, background_task_runner)) {
     30   DCHECK(background_task_runner.get());
     31 }
     32 
     33 QuotaPolicyChannelIDStore::~QuotaPolicyChannelIDStore() {
     34   if (!special_storage_policy_.get() ||
     35       !special_storage_policy_->HasSessionOnlyOrigins()) {
     36     return;
     37   }
     38   std::list<std::string> session_only_server_identifiers;
     39   for (std::set<std::string>::iterator it = server_identifiers_.begin();
     40        it != server_identifiers_.end();
     41        ++it) {
     42     GURL url(net::cookie_util::CookieOriginToURL(*it, true));
     43     if (special_storage_policy_->IsStorageSessionOnly(url))
     44       session_only_server_identifiers.push_back(*it);
     45   }
     46   persistent_store_->DeleteAllInList(session_only_server_identifiers);
     47 }
     48 
     49 void QuotaPolicyChannelIDStore::Load(const LoadedCallback& loaded_callback) {
     50   persistent_store_->Load(
     51       base::Bind(&QuotaPolicyChannelIDStore::OnLoad, this, loaded_callback));
     52 }
     53 
     54 void QuotaPolicyChannelIDStore::AddChannelID(
     55     const net::DefaultChannelIDStore::ChannelID& channel_id) {
     56   server_identifiers_.insert(channel_id.server_identifier());
     57   persistent_store_->AddChannelID(channel_id);
     58 }
     59 
     60 void QuotaPolicyChannelIDStore::DeleteChannelID(
     61     const net::DefaultChannelIDStore::ChannelID& channel_id) {
     62   server_identifiers_.erase(channel_id.server_identifier());
     63   persistent_store_->DeleteChannelID(channel_id);
     64 }
     65 
     66 void QuotaPolicyChannelIDStore::SetForceKeepSessionState() {
     67   special_storage_policy_ = NULL;
     68 }
     69 
     70 void QuotaPolicyChannelIDStore::OnLoad(
     71     const LoadedCallback& loaded_callback,
     72     scoped_ptr<ChannelIDVector> channel_ids) {
     73   for (ChannelIDVector::const_iterator channel_id = channel_ids->begin();
     74        channel_id != channel_ids->end();
     75        ++channel_id) {
     76     server_identifiers_.insert((*channel_id)->server_identifier());
     77   }
     78   loaded_callback.Run(channel_ids.Pass());
     79 }
     80