blob: c89a3d900cef0069d5cd5f5fb5792777c9a7d43b [file] [log] [blame]
[email protected]aa84a7e2012-03-15 21:29:061// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c10da4b02010-03-25 14:38:322// 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/extensions/extension_data_deleter.h"
6
[email protected]dc0b5a12011-10-14 00:06:137#include "base/bind.h"
[email protected]0d6ec3a72011-09-02 02:09:438#include "base/file_util.h"
[email protected]dc0b5a12011-10-14 00:06:139#include "chrome/browser/extensions/extension_service.h"
[email protected]0d9a2202011-11-09 13:48:4110#include "chrome/browser/extensions/settings/settings_frontend.h"
[email protected]aa84a7e2012-03-15 21:29:0611#include "chrome/browser/profiles/profile.h"
[email protected]0d6ec3a72011-09-02 02:09:4312#include "chrome/common/chrome_constants.h"
[email protected]c10da4b02010-03-25 14:38:3213#include "chrome/common/extensions/extension.h"
[email protected]2a80aee2011-10-07 16:06:0314#include "chrome/common/url_constants.h"
[email protected]35cc399e2012-02-23 18:19:2815#include "content/public/browser/dom_storage_context.h"
16#include "content/public/browser/indexed_db_context.h"
[email protected]314c3e22012-02-21 03:57:4217#include "content/public/browser/resource_context.h"
[email protected]00bbe1662011-12-22 02:25:2118#include "net/base/completion_callback.h"
[email protected]c10da4b02010-03-25 14:38:3219#include "net/base/net_errors.h"
[email protected]aa84a7e2012-03-15 21:29:0620#include "net/cookies/cookie_monster.h"
[email protected]277ec262011-03-30 21:09:4021#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:3422#include "net/url_request/url_request_context_getter.h"
[email protected]314c3e22012-02-21 03:57:4223#include "webkit/appcache/appcache_service.h"
[email protected]04068382010-08-26 22:51:5424#include "webkit/database/database_tracker.h"
[email protected]397281f2011-02-14 05:15:5325#include "webkit/database/database_util.h"
26#include "webkit/fileapi/file_system_context.h"
[email protected]c10da4b02010-03-25 14:38:3227
[email protected]55eb70e762012-02-20 17:38:3928using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:3929using content::BrowserThread;
[email protected]35cc399e2012-02-23 18:19:2830using content::DOMStorageContext;
31using content::IndexedDBContext;
[email protected]314c3e22012-02-21 03:57:4232using content::ResourceContext;
[email protected]631bb742011-11-02 11:29:3933
[email protected]123f2742011-12-02 04:26:5234// static
[email protected]dc0b5a12011-10-14 00:06:1335void ExtensionDataDeleter::StartDeleting(
36 Profile* profile,
37 const std::string& extension_id,
38 const GURL& storage_origin,
39 bool is_storage_isolated) {
40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]c10da4b02010-03-25 14:38:3241 DCHECK(profile);
[email protected]dc0b5a12011-10-14 00:06:1342 scoped_refptr<ExtensionDataDeleter> deleter =
43 new ExtensionDataDeleter(
44 profile, extension_id, storage_origin, is_storage_isolated);
45
46 BrowserThread::PostTask(
47 BrowserThread::IO, FROM_HERE,
48 base::Bind(
49 &ExtensionDataDeleter::DeleteCookiesOnIOThread, deleter));
50
[email protected]5a9e47672012-03-15 04:08:5151 BrowserContext::GetDOMStorageContext(profile)->DeleteForOrigin(
52 deleter->origin_id_);
[email protected]dc0b5a12011-10-14 00:06:1353
54 BrowserThread::PostTask(
[email protected]e1dd5622011-12-20 12:28:5855 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE,
[email protected]dc0b5a12011-10-14 00:06:1356 base::Bind(
[email protected]35cc399e2012-02-23 18:19:2857 &ExtensionDataDeleter::DeleteIndexedDBOnWebkitThread, deleter,
[email protected]c1fff072012-02-24 23:38:1258 make_scoped_refptr(BrowserContext::GetIndexedDBContext(profile))));
[email protected]dc0b5a12011-10-14 00:06:1359
60 BrowserThread::PostTask(
61 BrowserThread::FILE, FROM_HERE,
62 base::Bind(
63 &ExtensionDataDeleter::DeleteDatabaseOnFileThread, deleter));
64
65 BrowserThread::PostTask(
66 BrowserThread::FILE, FROM_HERE,
67 base::Bind(
68 &ExtensionDataDeleter::DeleteFileSystemOnFileThread, deleter));
69
70 BrowserThread::PostTask(
71 BrowserThread::IO, FROM_HERE,
72 base::Bind(
[email protected]314c3e22012-02-21 03:57:4273 &ExtensionDataDeleter::DeleteAppcachesOnIOThread, deleter,
74 profile->GetResourceContext()));
[email protected]dc0b5a12011-10-14 00:06:1375
[email protected]0d9a2202011-11-09 13:48:4176 profile->GetExtensionService()->settings_frontend()->
[email protected]27cc7332011-11-01 01:56:4377 DeleteStorageSoon(extension_id);
[email protected]dc0b5a12011-10-14 00:06:1378}
79
80ExtensionDataDeleter::ExtensionDataDeleter(
81 Profile* profile,
82 const std::string& extension_id,
83 const GURL& storage_origin,
84 bool is_storage_isolated)
85 : extension_id_(extension_id) {
[email protected]55eb70e762012-02-20 17:38:3986 database_tracker_ = BrowserContext::GetDatabaseTracker(profile);
[email protected]0d6ec3a72011-09-02 02:09:4387 // Pick the right request context depending on whether it's an extension,
88 // isolated app, or regular app.
89 if (storage_origin.SchemeIs(chrome::kExtensionScheme)) {
90 extension_request_context_ = profile->GetRequestContextForExtensions();
91 } else if (is_storage_isolated) {
92 extension_request_context_ =
93 profile->GetRequestContextForIsolatedApp(extension_id);
94 isolated_app_path_ = profile->GetPath().
95 Append(chrome::kIsolatedAppStateDirname).AppendASCII(extension_id);
96 } else {
97 extension_request_context_ = profile->GetRequestContext();
98 }
[email protected]55eb70e762012-02-20 17:38:3999 file_system_context_ = BrowserContext::GetFileSystemContext(profile);
[email protected]0d6ec3a72011-09-02 02:09:43100 storage_origin_ = storage_origin;
[email protected]c10da4b02010-03-25 14:38:32101 origin_id_ =
[email protected]0d6ec3a72011-09-02 02:09:43102 webkit_database::DatabaseUtil::GetOriginIdentifier(storage_origin_);
[email protected]c10da4b02010-03-25 14:38:32103}
104
[email protected]04068382010-08-26 22:51:54105ExtensionDataDeleter::~ExtensionDataDeleter() {
106}
107
[email protected]9eaa18e2010-06-29 20:51:01108void ExtensionDataDeleter::DeleteCookiesOnIOThread() {
[email protected]ca4b5fa32010-10-09 12:42:18109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]c10da4b02010-03-25 14:38:32110 net::CookieMonster* cookie_monster =
[email protected]277ec262011-03-30 21:09:40111 extension_request_context_->GetURLRequestContext()->cookie_store()->
[email protected]0d6ec3a72011-09-02 02:09:43112 GetCookieMonster();
[email protected]c10da4b02010-03-25 14:38:32113 if (cookie_monster)
[email protected]b9e48094d2011-07-20 14:27:13114 cookie_monster->DeleteAllForHostAsync(
[email protected]0d6ec3a72011-09-02 02:09:43115 storage_origin_, net::CookieMonster::DeleteCallback());
[email protected]c10da4b02010-03-25 14:38:32116}
117
118void ExtensionDataDeleter::DeleteDatabaseOnFileThread() {
[email protected]ca4b5fa32010-10-09 12:42:18119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]00bbe1662011-12-22 02:25:21120 int rv = database_tracker_->DeleteDataForOrigin(
121 origin_id_, net::CompletionCallback());
[email protected]c10da4b02010-03-25 14:38:32122 DCHECK(rv == net::OK || rv == net::ERR_IO_PENDING);
123}
124
[email protected]35cc399e2012-02-23 18:19:28125void ExtensionDataDeleter::DeleteIndexedDBOnWebkitThread(
126 scoped_refptr<IndexedDBContext> indexed_db_context) {
[email protected]e1dd5622011-12-20 12:28:58127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED));
[email protected]35cc399e2012-02-23 18:19:28128 indexed_db_context->DeleteForOrigin(storage_origin_);
[email protected]e1dcf922010-11-22 19:12:12129}
[email protected]09a316022010-12-03 03:21:37130
131void ExtensionDataDeleter::DeleteFileSystemOnFileThread() {
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]0d6ec3a72011-09-02 02:09:43133 file_system_context_->DeleteDataForOriginOnFileThread(storage_origin_);
134
135 // TODO(creis): The following call fails because the request context is still
136 // around, and holding open file handles in this directory.
137 // See http://crbug.com/85127
138 if (!isolated_app_path_.empty())
139 file_util::Delete(isolated_app_path_, true);
140}
141
[email protected]314c3e22012-02-21 03:57:42142void ExtensionDataDeleter::DeleteAppcachesOnIOThread(ResourceContext* context) {
[email protected]0d6ec3a72011-09-02 02:09:43143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]314c3e22012-02-21 03:57:42144 ResourceContext::GetAppCacheService(context)->DeleteAppCachesForOrigin(
[email protected]123f2742011-12-02 04:26:52145 storage_origin_, net::CompletionCallback());
[email protected]09a316022010-12-03 03:21:37146}