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