Home | History | Annotate | Download | only in dom_storage
      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 #ifndef CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_IMPL_H_
      6 #define CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_IMPL_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <vector>
     11 
     12 #include "base/atomic_sequence_num.h"
     13 #include "base/basictypes.h"
     14 #include "base/files/file_path.h"
     15 #include "base/gtest_prod_util.h"
     16 #include "base/memory/ref_counted.h"
     17 #include "base/observer_list.h"
     18 #include "base/time/time.h"
     19 #include "content/browser/dom_storage/dom_storage_namespace.h"
     20 #include "content/common/content_export.h"
     21 #include "content/public/browser/session_storage_namespace.h"
     22 #include "url/gurl.h"
     23 
     24 namespace base {
     25 class FilePath;
     26 class NullableString16;
     27 class Time;
     28 }
     29 
     30 namespace quota {
     31 class SpecialStoragePolicy;
     32 }
     33 
     34 namespace content {
     35 
     36 class DOMStorageArea;
     37 class DOMStorageSession;
     38 class DOMStorageTaskRunner;
     39 class SessionStorageDatabase;
     40 struct LocalStorageUsageInfo;
     41 struct SessionStorageUsageInfo;
     42 
     43 // The Context is the root of an object containment hierachy for
     44 // Namespaces and Areas related to the owning profile.
     45 // One instance is allocated in the main process for each profile,
     46 // instance methods should be called serially in the background as
     47 // determined by the task_runner. Specifcally not on chrome's non-blocking
     48 // IO thread since these methods can result in blocking file io.
     49 //
     50 // In general terms, the DOMStorage object relationships are...
     51 //   Contexts (per-profile) own Namespaces which own Areas which share Maps.
     52 //   Hosts(per-renderer) refer to Namespaces and Areas open in its renderer.
     53 //   Sessions (per-tab) cause the creation and deletion of session Namespaces.
     54 //
     55 // Session Namespaces are cloned by initially making a shallow copy of
     56 // all contained Areas, the shallow copies refer to the same refcounted Map,
     57 // and does a deep copy-on-write if needed.
     58 //
     59 // Classes intended to be used by an embedder are DOMStorageContextImpl,
     60 // DOMStorageHost, and DOMStorageSession. The other classes are for
     61 // internal consumption.
     62 class CONTENT_EXPORT DOMStorageContextImpl
     63     : public base::RefCountedThreadSafe<DOMStorageContextImpl> {
     64  public:
     65   // An interface for observing Local and Session Storage events on the
     66   // background thread.
     67   class EventObserver {
     68    public:
     69     // |old_value| may be null on initial insert.
     70     virtual void OnDOMStorageItemSet(
     71         const DOMStorageArea* area,
     72         const base::string16& key,
     73         const base::string16& new_value,
     74         const base::NullableString16& old_value,
     75         const GURL& page_url) = 0;
     76     virtual void OnDOMStorageItemRemoved(
     77         const DOMStorageArea* area,
     78         const base::string16& key,
     79         const base::string16& old_value,
     80         const GURL& page_url) = 0;
     81     virtual void OnDOMStorageAreaCleared(
     82         const DOMStorageArea* area,
     83         const GURL& page_url) = 0;
     84     // Indicates that cached values of the DOM Storage provided must be
     85     // cleared and retrieved again.
     86     virtual void OnDOMSessionStorageReset(int64 namespace_id) = 0;
     87 
     88    protected:
     89     virtual ~EventObserver() {}
     90   };
     91 
     92   // |localstorage_directory| and |sessionstorage_directory| may be empty
     93   // for incognito browser contexts.
     94   DOMStorageContextImpl(
     95       const base::FilePath& localstorage_directory,
     96       const base::FilePath& sessionstorage_directory,
     97       quota::SpecialStoragePolicy* special_storage_policy,
     98       DOMStorageTaskRunner* task_runner);
     99 
    100   // Returns the directory path for localStorage, or an empty directory, if
    101   // there is no backing on disk.
    102   const base::FilePath& localstorage_directory() {
    103     return localstorage_directory_;
    104   }
    105 
    106   // Returns the directory path for sessionStorage, or an empty directory, if
    107   // there is no backing on disk.
    108   const base::FilePath& sessionstorage_directory() {
    109     return sessionstorage_directory_;
    110   }
    111 
    112   DOMStorageTaskRunner* task_runner() const { return task_runner_.get(); }
    113   DOMStorageNamespace* GetStorageNamespace(int64 namespace_id);
    114 
    115   void GetLocalStorageUsage(std::vector<LocalStorageUsageInfo>* infos,
    116                             bool include_file_info);
    117   void GetSessionStorageUsage(std::vector<SessionStorageUsageInfo>* infos);
    118   void DeleteLocalStorage(const GURL& origin);
    119   void DeleteSessionStorage(const SessionStorageUsageInfo& usage_info);
    120 
    121   // Used by content settings to alter the behavior around
    122   // what data to keep and what data to discard at shutdown.
    123   // The policy is not so straight forward to describe, see
    124   // the implementation for details.
    125   void SetForceKeepSessionState() {
    126     force_keep_session_state_ = true;
    127   }
    128 
    129   // Called when the owning BrowserContext is ending.
    130   // Schedules the commit of any unsaved changes and will delete
    131   // and keep data on disk per the content settings and special storage
    132   // policies. Contained areas and namespaces will stop functioning after
    133   // this method has been called.
    134   void Shutdown();
    135 
    136   // Methods to add, remove, and notify EventObservers.
    137   void AddEventObserver(EventObserver* observer);
    138   void RemoveEventObserver(EventObserver* observer);
    139 
    140   void NotifyItemSet(
    141       const DOMStorageArea* area,
    142       const base::string16& key,
    143       const base::string16& new_value,
    144       const base::NullableString16& old_value,
    145       const GURL& page_url);
    146   void NotifyItemRemoved(
    147       const DOMStorageArea* area,
    148       const base::string16& key,
    149       const base::string16& old_value,
    150       const GURL& page_url);
    151   void NotifyAreaCleared(
    152       const DOMStorageArea* area,
    153       const GURL& page_url);
    154   void NotifyAliasSessionMerged(
    155       int64 namespace_id,
    156       DOMStorageNamespace* old_alias_master_namespace);
    157 
    158   // May be called on any thread.
    159   int64 AllocateSessionId() {
    160     return session_id_sequence_.GetNext();
    161   }
    162 
    163   std::string AllocatePersistentSessionId();
    164 
    165   // Must be called on the background thread.
    166   void CreateSessionNamespace(int64 namespace_id,
    167                               const std::string& persistent_namespace_id);
    168   void DeleteSessionNamespace(int64 namespace_id, bool should_persist_data);
    169   void CloneSessionNamespace(int64 existing_id, int64 new_id,
    170                              const std::string& new_persistent_id);
    171   void CreateAliasSessionNamespace(int64 existing_id, int64 new_id,
    172                                    const std::string& persistent_id);
    173 
    174   // Starts backing sessionStorage on disk. This function must be called right
    175   // after DOMStorageContextImpl is created, before it's used.
    176   void SetSaveSessionStorageOnDisk();
    177 
    178   // Deletes all namespaces which don't have an associated DOMStorageNamespace
    179   // alive. This function is used for deleting possible leftover data after an
    180   // unclean exit.
    181   void StartScavengingUnusedSessionStorage();
    182 
    183   void AddTransactionLogProcessId(int64 namespace_id, int process_id);
    184   void RemoveTransactionLogProcessId(int64 namespace_id, int process_id);
    185 
    186   SessionStorageNamespace::MergeResult MergeSessionStorage(
    187       int64 namespace1_id, bool actually_merge, int process_id,
    188       int64 namespace2_id);
    189 
    190  private:
    191   friend class DOMStorageContextImplTest;
    192   FRIEND_TEST_ALL_PREFIXES(DOMStorageContextImplTest, Basics);
    193   friend class base::RefCountedThreadSafe<DOMStorageContextImpl>;
    194   typedef std::map<int64, scoped_refptr<DOMStorageNamespace> >
    195       StorageNamespaceMap;
    196 
    197   virtual ~DOMStorageContextImpl();
    198 
    199   void ClearSessionOnlyOrigins();
    200 
    201   void MaybeShutdownSessionNamespace(DOMStorageNamespace* ns);
    202 
    203   // For scavenging unused sessionStorages.
    204   void FindUnusedNamespaces();
    205   void FindUnusedNamespacesInCommitSequence(
    206       const std::set<std::string>& namespace_ids_in_use,
    207       const std::set<std::string>& protected_persistent_session_ids);
    208   void DeleteNextUnusedNamespace();
    209   void DeleteNextUnusedNamespaceInCommitSequence();
    210 
    211   // Collection of namespaces keyed by id.
    212   StorageNamespaceMap namespaces_;
    213 
    214   // Where localstorage data is stored, maybe empty for the incognito use case.
    215   base::FilePath localstorage_directory_;
    216 
    217   // Where sessionstorage data is stored, maybe empty for the incognito use
    218   // case. Always empty until the file-backed session storage feature is
    219   // implemented.
    220   base::FilePath sessionstorage_directory_;
    221 
    222   // Used to schedule sequenced background tasks.
    223   scoped_refptr<DOMStorageTaskRunner> task_runner_;
    224 
    225   // List of objects observing local storage events.
    226   ObserverList<EventObserver> event_observers_;
    227 
    228   // We use a 32 bit identifier for per tab storage sessions.
    229   // At a tab per second, this range is large enough for 68 years.
    230   base::AtomicSequenceNumber session_id_sequence_;
    231 
    232   bool is_shutdown_;
    233   bool force_keep_session_state_;
    234   scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_;
    235   scoped_refptr<SessionStorageDatabase> session_storage_database_;
    236 
    237   // For cleaning up unused namespaces gradually.
    238   bool scavenging_started_;
    239   std::vector<std::string> deletable_persistent_namespace_ids_;
    240 
    241   // Persistent namespace IDs to protect from gradual deletion (they will
    242   // be needed for session restore).
    243   std::set<std::string> protected_persistent_session_ids_;
    244 
    245   // Mapping between persistent namespace IDs and namespace IDs for
    246   // sessionStorage.
    247   std::map<std::string, int64> persistent_namespace_id_to_namespace_id_;
    248 };
    249 
    250 }  // namespace content
    251 
    252 #endif  // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_IMPL_H_
    253