Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2011 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/extension_special_storage_policy.h"
      6 
      7 #include "base/logging.h"
      8 #include "chrome/common/extensions/extension.h"
      9 #include "chrome/common/url_constants.h"
     10 
     11 ExtensionSpecialStoragePolicy::ExtensionSpecialStoragePolicy() {}
     12 
     13 ExtensionSpecialStoragePolicy::~ExtensionSpecialStoragePolicy() {}
     14 
     15 bool ExtensionSpecialStoragePolicy::IsStorageProtected(const GURL& origin) {
     16   if (origin.SchemeIs(chrome::kExtensionScheme))
     17     return true;
     18   base::AutoLock locker(lock_);
     19   return protected_apps_.Contains(origin);
     20 }
     21 
     22 bool ExtensionSpecialStoragePolicy::IsStorageUnlimited(const GURL& origin) {
     23   base::AutoLock locker(lock_);
     24   return unlimited_extensions_.Contains(origin);
     25 }
     26 
     27 bool ExtensionSpecialStoragePolicy::IsFileHandler(
     28     const std::string& extension_id) {
     29   base::AutoLock locker(lock_);
     30   return file_handler_extensions_.ContainsExtension(extension_id);
     31 }
     32 
     33 void ExtensionSpecialStoragePolicy::GrantRightsForExtension(
     34     const Extension* extension) {
     35   DCHECK(extension);
     36   if (!extension->is_hosted_app() &&
     37       !extension->HasApiPermission(Extension::kUnlimitedStoragePermission) &&
     38       !extension->HasApiPermission(Extension::kFileBrowserHandlerPermission)) {
     39     return;
     40   }
     41   base::AutoLock locker(lock_);
     42   if (extension->is_hosted_app())
     43     protected_apps_.Add(extension);
     44   if (extension->HasApiPermission(Extension::kUnlimitedStoragePermission))
     45     unlimited_extensions_.Add(extension);
     46   if (extension->HasApiPermission(Extension::kFileBrowserHandlerPermission))
     47     file_handler_extensions_.Add(extension);
     48 }
     49 
     50 void ExtensionSpecialStoragePolicy::RevokeRightsForExtension(
     51     const Extension* extension) {
     52   DCHECK(extension);
     53   if (!extension->is_hosted_app() &&
     54       !extension->HasApiPermission(Extension::kUnlimitedStoragePermission) &&
     55       !extension->HasApiPermission(Extension::kFileBrowserHandlerPermission)) {
     56     return;
     57   }
     58   base::AutoLock locker(lock_);
     59   if (extension->is_hosted_app())
     60     protected_apps_.Remove(extension);
     61   if (extension->HasApiPermission(Extension::kUnlimitedStoragePermission))
     62     unlimited_extensions_.Remove(extension);
     63   if (extension->HasApiPermission(Extension::kFileBrowserHandlerPermission))
     64     file_handler_extensions_.Remove(extension);
     65 }
     66 
     67 void ExtensionSpecialStoragePolicy::RevokeRightsForAllExtensions() {
     68   base::AutoLock locker(lock_);
     69   protected_apps_.Clear();
     70   unlimited_extensions_.Clear();
     71   file_handler_extensions_.Clear();
     72 }
     73 
     74 //-----------------------------------------------------------------------------
     75 // SpecialCollection helper class
     76 //-----------------------------------------------------------------------------
     77 
     78 ExtensionSpecialStoragePolicy::SpecialCollection::SpecialCollection() {}
     79 
     80 ExtensionSpecialStoragePolicy::SpecialCollection::~SpecialCollection() {}
     81 
     82 bool ExtensionSpecialStoragePolicy::SpecialCollection::Contains(
     83     const GURL& origin) {
     84   CachedResults::const_iterator found = cached_results_.find(origin);
     85   if (found != cached_results_.end())
     86     return found->second;
     87 
     88   for (Extensions::const_iterator iter = extensions_.begin();
     89        iter != extensions_.end(); ++iter) {
     90     if (iter->second->OverlapsWithOrigin(origin)) {
     91       cached_results_[origin] = true;
     92       return true;
     93     }
     94   }
     95   cached_results_[origin] = false;
     96   return false;
     97 }
     98 
     99 bool ExtensionSpecialStoragePolicy::SpecialCollection::ContainsExtension(
    100     const std::string& extension_id) {
    101   return extensions_.find(extension_id) != extensions_.end();
    102 }
    103 
    104 void ExtensionSpecialStoragePolicy::SpecialCollection::Add(
    105     const Extension* extension) {
    106   cached_results_.clear();
    107   extensions_[extension->id()] = extension;
    108 }
    109 
    110 void ExtensionSpecialStoragePolicy::SpecialCollection::Remove(
    111     const Extension* extension) {
    112   cached_results_.clear();
    113   extensions_.erase(extension->id());
    114 }
    115 
    116 void ExtensionSpecialStoragePolicy::SpecialCollection::Clear() {
    117   cached_results_.clear();
    118   extensions_.clear();
    119 }
    120