1 // Copyright (c) 2012 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/extensions/data_deleter.h"
6
7 #include "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/extensions/extension_special_storage_policy.h"
9 #include "chrome/browser/extensions/extension_util.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
12 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
13 #include "content/public/browser/browser_context.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/site_instance.h"
16 #include "content/public/browser/storage_partition.h"
17 #include "extensions/browser/extension_prefs.h"
18 #include "extensions/browser/extension_system.h"
19 #include "extensions/common/constants.h"
20 #include "extensions/common/extension.h"
21 #include "net/url_request/url_request_context_getter.h"
22
23 #if defined(ENABLE_EXTENSIONS)
24 #include "extensions/browser/api/storage/storage_frontend.h"
25 #endif
26
27 using base::WeakPtr;
28 using content::BrowserContext;
29 using content::BrowserThread;
30 using content::StoragePartition;
31
32 namespace extensions {
33
34 namespace {
35
36 // Helper function that deletes data of a given |storage_origin| in a given
37 // |partition|.
DeleteOrigin(Profile * profile,StoragePartition * partition,const GURL & origin)38 void DeleteOrigin(Profile* profile,
39 StoragePartition* partition,
40 const GURL& origin) {
41 DCHECK_CURRENTLY_ON(BrowserThread::UI);
42 DCHECK(profile);
43 DCHECK(partition);
44
45 if (origin.SchemeIs(kExtensionScheme)) {
46 // TODO(ajwong): Cookies are not properly isolated for
47 // chrome-extension:// scheme. (http://crbug.com/158386).
48 //
49 // However, no isolated apps actually can write to kExtensionScheme
50 // origins. Thus, it is benign to delete from the
51 // RequestContextForExtensions because there's nothing stored there. We
52 // preserve this code path without checking for isolation because it's
53 // simpler than special casing. This code should go away once we merge
54 // the various URLRequestContexts (http://crbug.com/159193).
55 partition->ClearDataForOrigin(
56 StoragePartition::REMOVE_DATA_MASK_ALL &
57 (~StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE),
58 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
59 origin,
60 profile->GetRequestContextForExtensions());
61 } else {
62 // We don't need to worry about the media request context because that
63 // shares the same cookie store as the main request context.
64 partition->ClearDataForOrigin(
65 StoragePartition::REMOVE_DATA_MASK_ALL &
66 (~StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE),
67 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
68 origin,
69 partition->GetURLRequestContext());
70 }
71 }
72
OnNeedsToGarbageCollectIsolatedStorage(WeakPtr<ExtensionService> es)73 void OnNeedsToGarbageCollectIsolatedStorage(WeakPtr<ExtensionService> es) {
74 if (!es)
75 return;
76 ExtensionPrefs::Get(es->profile())->SetNeedsStorageGarbageCollection(true);
77 }
78
79 } // namespace
80
81 // static
StartDeleting(Profile * profile,const Extension * extension)82 void DataDeleter::StartDeleting(Profile* profile, const Extension* extension) {
83 DCHECK(profile);
84 DCHECK(extension);
85
86 if (AppIsolationInfo::HasIsolatedStorage(extension)) {
87 BrowserContext::AsyncObliterateStoragePartition(
88 profile,
89 util::GetSiteForExtensionId(extension->id(), profile),
90 base::Bind(
91 &OnNeedsToGarbageCollectIsolatedStorage,
92 ExtensionSystem::Get(profile)->extension_service()->AsWeakPtr()));
93 } else {
94 GURL launch_web_url_origin(
95 AppLaunchInfo::GetLaunchWebURL(extension).GetOrigin());
96
97 StoragePartition* partition = BrowserContext::GetStoragePartitionForSite(
98 profile,
99 Extension::GetBaseURLFromExtensionId(extension->id()));
100
101 if (extension->is_hosted_app() &&
102 !profile->GetExtensionSpecialStoragePolicy()->
103 IsStorageProtected(launch_web_url_origin)) {
104 DeleteOrigin(profile, partition, launch_web_url_origin);
105 }
106 DeleteOrigin(profile, partition, extension->url());
107 }
108
109 #if defined(ENABLE_EXTENSIONS)
110 // Begin removal of the settings for the current extension.
111 // StorageFrontend may not exist in unit tests.
112 StorageFrontend* frontend = StorageFrontend::Get(profile);
113 if (frontend)
114 frontend->DeleteStorageSoon(extension->id());
115 #endif // defined(ENABLE_EXTENSIONS)
116 }
117
118 } // namespace extensions
119