Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / appcache / appcache_storage_impl.h
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 #ifndef CONTENT_BROWSER_APPCACHE_APPCACHE_STORAGE_IMPL_H_
6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_STORAGE_IMPL_H_
7
8 #include <deque>
9 #include <map>
10 #include <set>
11 #include <utility>
12 #include <vector>
13
14 #include "base/callback.h"
15 #include "base/files/file_path.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/message_loop/message_loop_proxy.h"
18 #include "content/browser/appcache/appcache_database.h"
19 #include "content/browser/appcache/appcache_disk_cache.h"
20 #include "content/browser/appcache/appcache_storage.h"
21 #include "content/common/content_export.h"
22
23 namespace content {
24 class AppCacheStorageImplTest;
25 class ChromeAppCacheServiceTest;
26
27 class AppCacheStorageImpl : public AppCacheStorage {
28  public:
29   explicit AppCacheStorageImpl(AppCacheServiceImpl* service);
30   virtual ~AppCacheStorageImpl();
31
32   void Initialize(const base::FilePath& cache_directory,
33                   base::MessageLoopProxy* db_thread,
34                   base::MessageLoopProxy* cache_thread);
35   void Disable();
36   bool is_disabled() const { return is_disabled_; }
37
38   // AppCacheStorage methods, see the base class for doc comments.
39   virtual void GetAllInfo(Delegate* delegate) OVERRIDE;
40   virtual void LoadCache(int64 id, Delegate* delegate) OVERRIDE;
41   virtual void LoadOrCreateGroup(const GURL& manifest_url,
42                                  Delegate* delegate) OVERRIDE;
43   virtual void StoreGroupAndNewestCache(AppCacheGroup* group,
44                                         AppCache* newest_cache,
45                                         Delegate* delegate) OVERRIDE;
46   virtual void FindResponseForMainRequest(const GURL& url,
47                                           const GURL& preferred_manifest_url,
48                                           Delegate* delegate) OVERRIDE;
49   virtual void FindResponseForSubRequest(
50       AppCache* cache, const GURL& url,
51       AppCacheEntry* found_entry, AppCacheEntry* found_fallback_entry,
52       bool* found_network_namespace) OVERRIDE;
53   virtual void MarkEntryAsForeign(const GURL& entry_url,
54                                   int64 cache_id) OVERRIDE;
55   virtual void MakeGroupObsolete(AppCacheGroup* group,
56                                  Delegate* delegate,
57                                  int response_code) OVERRIDE;
58   virtual AppCacheResponseReader* CreateResponseReader(
59       const GURL& manifest_url, int64 group_id, int64 response_id) OVERRIDE;
60   virtual AppCacheResponseWriter* CreateResponseWriter(
61       const GURL& manifest_url, int64 group_id) OVERRIDE;
62   virtual void DoomResponses(const GURL& manifest_url,
63                              const std::vector<int64>& response_ids) OVERRIDE;
64   virtual void DeleteResponses(const GURL& manifest_url,
65                                const std::vector<int64>& response_ids) OVERRIDE;
66
67  private:
68   // The AppCacheStorageImpl class methods and datamembers may only be
69   // accessed on the IO thread. This class manufactures seperate DatabaseTasks
70   // which access the DB on a seperate background thread.
71   class DatabaseTask;
72   class InitTask;
73   class DisableDatabaseTask;
74   class GetAllInfoTask;
75   class StoreOrLoadTask;
76   class CacheLoadTask;
77   class GroupLoadTask;
78   class StoreGroupAndCacheTask;
79   class FindMainResponseTask;
80   class MarkEntryAsForeignTask;
81   class MakeGroupObsoleteTask;
82   class GetDeletableResponseIdsTask;
83   class InsertDeletableResponseIdsTask;
84   class DeleteDeletableResponseIdsTask;
85   class UpdateGroupLastAccessTimeTask;
86
87   typedef std::deque<DatabaseTask*> DatabaseTaskQueue;
88   typedef std::map<int64, CacheLoadTask*> PendingCacheLoads;
89   typedef std::map<GURL, GroupLoadTask*> PendingGroupLoads;
90   typedef std::deque<std::pair<GURL, int64> > PendingForeignMarkings;
91   typedef std::set<StoreGroupAndCacheTask*> PendingQuotaQueries;
92
93   bool IsInitTaskComplete() {
94     return last_cache_id_ != AppCacheStorage::kUnitializedId;
95   }
96
97   CacheLoadTask* GetPendingCacheLoadTask(int64 cache_id);
98   GroupLoadTask* GetPendingGroupLoadTask(const GURL& manifest_url);
99   void GetPendingForeignMarkingsForCache(
100       int64 cache_id, std::vector<GURL>* urls);
101
102   void ScheduleSimpleTask(const base::Closure& task);
103   void RunOnePendingSimpleTask();
104
105   void DelayedStartDeletingUnusedResponses();
106   void StartDeletingResponses(const std::vector<int64>& response_ids);
107   void ScheduleDeleteOneResponse();
108   void DeleteOneResponse();
109
110   void OnDeletedOneResponse(int rv);
111   void OnDiskCacheInitialized(int rv);
112   void DeleteAndStartOver();
113   void DeleteAndStartOverPart2();
114   void CallScheduleReinitialize();
115
116   // Sometimes we can respond without having to query the database.
117   bool FindResponseForMainRequestInGroup(
118       AppCacheGroup* group,  const GURL& url, Delegate* delegate);
119   void DeliverShortCircuitedFindMainResponse(
120       const GURL& url,
121       const AppCacheEntry& found_entry,
122       scoped_refptr<AppCacheGroup> group,
123       scoped_refptr<AppCache> newest_cache,
124       scoped_refptr<DelegateReference> delegate_ref);
125
126   void CallOnMainResponseFound(
127       DelegateReferenceVector* delegates,
128       const GURL& url, const AppCacheEntry& entry,
129       const GURL& namespace_entry_url, const AppCacheEntry& fallback_entry,
130       int64 cache_id, int64 group_id, const GURL& manifest_url);
131
132   CONTENT_EXPORT AppCacheDiskCache* disk_cache();
133
134   // The directory in which we place files in the file system.
135   base::FilePath cache_directory_;
136   bool is_incognito_;
137
138   // This class operates primarily on the IO thread, but schedules
139   // its DatabaseTasks on the db thread. Separately, the disk_cache uses
140   // the cache_thread.
141   scoped_refptr<base::MessageLoopProxy> db_thread_;
142   scoped_refptr<base::MessageLoopProxy> cache_thread_;
143
144   // Structures to keep track of DatabaseTasks that are in-flight.
145   DatabaseTaskQueue scheduled_database_tasks_;
146   PendingCacheLoads pending_cache_loads_;
147   PendingGroupLoads pending_group_loads_;
148   PendingForeignMarkings pending_foreign_markings_;
149   PendingQuotaQueries pending_quota_queries_;
150
151   // Structures to keep track of lazy response deletion.
152   std::deque<int64> deletable_response_ids_;
153   std::vector<int64> deleted_response_ids_;
154   bool is_response_deletion_scheduled_;
155   bool did_start_deleting_responses_;
156   int64 last_deletable_response_rowid_;
157
158   // Created on the IO thread, but only used on the DB thread.
159   AppCacheDatabase* database_;
160
161   // Set if we discover a fatal error like a corrupt SQL database or
162   // disk cache and cannot continue.
163   bool is_disabled_;
164
165   scoped_ptr<AppCacheDiskCache> disk_cache_;
166
167   // Used to short-circuit certain operations without having to schedule
168   // any tasks on the background database thread.
169   std::deque<base::Closure> pending_simple_tasks_;
170   base::WeakPtrFactory<AppCacheStorageImpl> weak_factory_;
171
172   friend class content::AppCacheStorageImplTest;
173   friend class content::ChromeAppCacheServiceTest;
174 };
175
176 }  // namespace content
177
178 #endif  // CONTENT_BROWSER_APPCACHE_APPCACHE_STORAGE_IMPL_H_