15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/data_deleter.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/extension_service.h"
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/extensions/extension_special_storage_policy.h"
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/extensions/extension_util.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/browser_context.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/browser_thread.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/browser/site_instance.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/storage_partition.h"
176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "extensions/browser/api/storage/storage_frontend.h"
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "extensions/browser/extension_prefs.h"
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "extensions/browser/extension_system.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "extensions/common/constants.h"
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/extension.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_context_getter.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using base::WeakPtr;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserContext;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserThread;
27a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)using content::StoragePartition;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace extensions {
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Helper function that deletes data of a given |storage_origin| in a given
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// |partition|.
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void DeleteOrigin(Profile* profile,
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                  StoragePartition* partition,
375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                  const GURL& origin,
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                  const base::Closure& callback) {
39effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DCHECK_CURRENTLY_ON(BrowserThread::UI);
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(profile);
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(partition);
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (origin.SchemeIs(kExtensionScheme)) {
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TODO(ajwong): Cookies are not properly isolated for
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // chrome-extension:// scheme.  (http://crbug.com/158386).
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    //
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // However, no isolated apps actually can write to kExtensionScheme
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // origins. Thus, it is benign to delete from the
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // RequestContextForExtensions because there's nothing stored there. We
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // preserve this code path without checking for isolation because it's
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // simpler than special casing.  This code should go away once we merge
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // the various URLRequestContexts (http://crbug.com/159193).
53a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    partition->ClearDataForOrigin(
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        ~StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE,
55a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)        StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        origin,
575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        profile->GetRequestContextForExtensions(),
585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        callback);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // We don't need to worry about the media request context because that
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // shares the same cookie store as the main request context.
62a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    partition->ClearDataForOrigin(
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        ~StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE,
64a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)        StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        origin,
665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        partition->GetURLRequestContext(),
675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        callback);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void OnNeedsToGarbageCollectIsolatedStorage(WeakPtr<ExtensionService> es,
725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                            const base::Closure& callback) {
735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (es)
745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    ExtensionPrefs::Get(es->profile())->SetNeedsStorageGarbageCollection(true);
755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  callback.Run();
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}  // namespace
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static
815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void DataDeleter::StartDeleting(Profile* profile,
825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                const Extension* extension,
835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                const base::Closure& callback) {
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(profile);
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(extension);
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (AppIsolationInfo::HasIsolatedStorage(extension)) {
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    BrowserContext::AsyncObliterateStoragePartition(
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        profile,
90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        util::GetSiteForExtensionId(extension->id(), profile),
91a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        base::Bind(
92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            &OnNeedsToGarbageCollectIsolatedStorage,
935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            ExtensionSystem::Get(profile)->extension_service()->AsWeakPtr(),
945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            callback));
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else {
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    GURL launch_web_url_origin(
97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        AppLaunchInfo::GetLaunchWebURL(extension).GetOrigin());
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    StoragePartition* partition = BrowserContext::GetStoragePartitionForSite(
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        profile,
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        Extension::GetBaseURLFromExtensionId(extension->id()));
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (extension->is_hosted_app() &&
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        !profile->GetExtensionSpecialStoragePolicy()->
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            IsStorageProtected(launch_web_url_origin)) {
1065f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      DeleteOrigin(profile,
1075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                   partition,
1085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                   launch_web_url_origin,
1095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                   base::Bind(&base::DoNothing));
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
1115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    DeleteOrigin(profile, partition, extension->url(), callback);
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Begin removal of the settings for the current extension.
115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // StorageFrontend may not exist in unit tests.
116a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  StorageFrontend* frontend = StorageFrontend::Get(profile);
117a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (frontend)
118a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    frontend->DeleteStorageSoon(extension->id());
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace extensions
122