blob: 7e6c1be55e513b94c3f8a0ce977a1c262a13b946 [file] [log] [blame]
[email protected]277ec262011-03-30 21:09:401// Copyright (c) 2011 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]8ecad5e2010-12-02 21:18:339#include "chrome/browser/profiles/profile.h"
[email protected]dc0b5a12011-10-14 00:06:1310#include "chrome/browser/extensions/extension_service.h"
[email protected]0d9a2202011-11-09 13:48:4111#include "chrome/browser/extensions/settings/settings_frontend.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]567812d2011-02-24 17:40:5015#include "content/browser/in_process_webkit/webkit_context.h"
[email protected]314c3e22012-02-21 03:57:4216#include "content/public/browser/resource_context.h"
[email protected]00bbe1662011-12-22 02:25:2117#include "net/base/completion_callback.h"
[email protected]c10da4b02010-03-25 14:38:3218#include "net/base/cookie_monster.h"
19#include "net/base/net_errors.h"
[email protected]277ec262011-03-30 21:09:4020#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:3421#include "net/url_request/url_request_context_getter.h"
[email protected]314c3e22012-02-21 03:57:4222#include "webkit/appcache/appcache_service.h"
[email protected]04068382010-08-26 22:51:5423#include "webkit/database/database_tracker.h"
[email protected]397281f2011-02-14 05:15:5324#include "webkit/database/database_util.h"
25#include "webkit/fileapi/file_system_context.h"
[email protected]c10da4b02010-03-25 14:38:3226
[email protected]55eb70e762012-02-20 17:38:3927using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:3928using content::BrowserThread;
[email protected]314c3e22012-02-21 03:57:4229using content::ResourceContext;
[email protected]631bb742011-11-02 11:29:3930
[email protected]123f2742011-12-02 04:26:5231// static
[email protected]dc0b5a12011-10-14 00:06:1332void ExtensionDataDeleter::StartDeleting(
33 Profile* profile,
34 const std::string& extension_id,
35 const GURL& storage_origin,
36 bool is_storage_isolated) {
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]c10da4b02010-03-25 14:38:3238 DCHECK(profile);
[email protected]dc0b5a12011-10-14 00:06:1339 scoped_refptr<ExtensionDataDeleter> deleter =
40 new ExtensionDataDeleter(
41 profile, extension_id, storage_origin, is_storage_isolated);
42
43 BrowserThread::PostTask(
44 BrowserThread::IO, FROM_HERE,
45 base::Bind(
46 &ExtensionDataDeleter::DeleteCookiesOnIOThread, deleter));
47
48 BrowserThread::PostTask(
[email protected]e1dd5622011-12-20 12:28:5849 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE,
[email protected]dc0b5a12011-10-14 00:06:1350 base::Bind(
51 &ExtensionDataDeleter::DeleteLocalStorageOnWebkitThread, deleter));
52
53 BrowserThread::PostTask(
[email protected]e1dd5622011-12-20 12:28:5854 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE,
[email protected]dc0b5a12011-10-14 00:06:1355 base::Bind(
56 &ExtensionDataDeleter::DeleteIndexedDBOnWebkitThread, deleter));
57
58 BrowserThread::PostTask(
59 BrowserThread::FILE, FROM_HERE,
60 base::Bind(
61 &ExtensionDataDeleter::DeleteDatabaseOnFileThread, deleter));
62
63 BrowserThread::PostTask(
64 BrowserThread::FILE, FROM_HERE,
65 base::Bind(
66 &ExtensionDataDeleter::DeleteFileSystemOnFileThread, deleter));
67
68 BrowserThread::PostTask(
69 BrowserThread::IO, FROM_HERE,
70 base::Bind(
[email protected]314c3e22012-02-21 03:57:4271 &ExtensionDataDeleter::DeleteAppcachesOnIOThread, deleter,
72 profile->GetResourceContext()));
[email protected]dc0b5a12011-10-14 00:06:1373
[email protected]0d9a2202011-11-09 13:48:4174 profile->GetExtensionService()->settings_frontend()->
[email protected]27cc7332011-11-01 01:56:4375 DeleteStorageSoon(extension_id);
[email protected]dc0b5a12011-10-14 00:06:1376}
77
78ExtensionDataDeleter::ExtensionDataDeleter(
79 Profile* profile,
80 const std::string& extension_id,
81 const GURL& storage_origin,
82 bool is_storage_isolated)
83 : extension_id_(extension_id) {
[email protected]55eb70e762012-02-20 17:38:3984 webkit_context_ = BrowserContext::GetWebKitContext(profile);
85 database_tracker_ = BrowserContext::GetDatabaseTracker(profile);
[email protected]0d6ec3a72011-09-02 02:09:4386 // Pick the right request context depending on whether it's an extension,
87 // isolated app, or regular app.
88 if (storage_origin.SchemeIs(chrome::kExtensionScheme)) {
89 extension_request_context_ = profile->GetRequestContextForExtensions();
90 } else if (is_storage_isolated) {
91 extension_request_context_ =
92 profile->GetRequestContextForIsolatedApp(extension_id);
93 isolated_app_path_ = profile->GetPath().
94 Append(chrome::kIsolatedAppStateDirname).AppendASCII(extension_id);
95 } else {
96 extension_request_context_ = profile->GetRequestContext();
97 }
[email protected]55eb70e762012-02-20 17:38:3998 file_system_context_ = BrowserContext::GetFileSystemContext(profile);
[email protected]0d6ec3a72011-09-02 02:09:4399 storage_origin_ = storage_origin;
[email protected]c10da4b02010-03-25 14:38:32100 origin_id_ =
[email protected]0d6ec3a72011-09-02 02:09:43101 webkit_database::DatabaseUtil::GetOriginIdentifier(storage_origin_);
[email protected]c10da4b02010-03-25 14:38:32102}
103
[email protected]04068382010-08-26 22:51:54104ExtensionDataDeleter::~ExtensionDataDeleter() {
105}
106
[email protected]9eaa18e2010-06-29 20:51:01107void ExtensionDataDeleter::DeleteCookiesOnIOThread() {
[email protected]ca4b5fa32010-10-09 12:42:18108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]c10da4b02010-03-25 14:38:32109 net::CookieMonster* cookie_monster =
[email protected]277ec262011-03-30 21:09:40110 extension_request_context_->GetURLRequestContext()->cookie_store()->
[email protected]0d6ec3a72011-09-02 02:09:43111 GetCookieMonster();
[email protected]c10da4b02010-03-25 14:38:32112 if (cookie_monster)
[email protected]b9e48094d2011-07-20 14:27:13113 cookie_monster->DeleteAllForHostAsync(
[email protected]0d6ec3a72011-09-02 02:09:43114 storage_origin_, net::CookieMonster::DeleteCallback());
[email protected]c10da4b02010-03-25 14:38:32115}
116
117void ExtensionDataDeleter::DeleteDatabaseOnFileThread() {
[email protected]ca4b5fa32010-10-09 12:42:18118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]00bbe1662011-12-22 02:25:21119 int rv = database_tracker_->DeleteDataForOrigin(
120 origin_id_, net::CompletionCallback());
[email protected]c10da4b02010-03-25 14:38:32121 DCHECK(rv == net::OK || rv == net::ERR_IO_PENDING);
122}
123
124void ExtensionDataDeleter::DeleteLocalStorageOnWebkitThread() {
[email protected]e1dd5622011-12-20 12:28:58125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED));
[email protected]c10da4b02010-03-25 14:38:32126 webkit_context_->dom_storage_context()->DeleteLocalStorageForOrigin(
127 origin_id_);
128}
[email protected]e1dcf922010-11-22 19:12:12129
130void ExtensionDataDeleter::DeleteIndexedDBOnWebkitThread() {
[email protected]e1dd5622011-12-20 12:28:58131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED));
[email protected]682394a2011-08-05 23:53:15132 webkit_context_->indexed_db_context()->DeleteIndexedDBForOrigin(
[email protected]0d6ec3a72011-09-02 02:09:43133 storage_origin_);
[email protected]e1dcf922010-11-22 19:12:12134}
[email protected]09a316022010-12-03 03:21:37135
136void ExtensionDataDeleter::DeleteFileSystemOnFileThread() {
137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]0d6ec3a72011-09-02 02:09:43138 file_system_context_->DeleteDataForOriginOnFileThread(storage_origin_);
139
140 // TODO(creis): The following call fails because the request context is still
141 // around, and holding open file handles in this directory.
142 // See http://crbug.com/85127
143 if (!isolated_app_path_.empty())
144 file_util::Delete(isolated_app_path_, true);
145}
146
[email protected]314c3e22012-02-21 03:57:42147void ExtensionDataDeleter::DeleteAppcachesOnIOThread(ResourceContext* context) {
[email protected]0d6ec3a72011-09-02 02:09:43148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]314c3e22012-02-21 03:57:42149 ResourceContext::GetAppCacheService(context)->DeleteAppCachesForOrigin(
[email protected]123f2742011-12-02 04:26:52150 storage_origin_, net::CompletionCallback());
[email protected]09a316022010-12-03 03:21:37151}