OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/appcache/chrome_appcache_service.h" | 5 #include "content/browser/appcache/chrome_appcache_service.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "content/browser/content_browser_client.h" | 9 #include "content/browser/content_browser_client.h" |
10 #include "content/common/notification_service.h" | 10 #include "content/common/notification_service.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "webkit/appcache/appcache_thread.h" | 12 #include "webkit/appcache/appcache_thread.h" |
13 #include "webkit/quota/quota_manager.h" | 13 #include "webkit/quota/quota_manager.h" |
14 | 14 |
15 static bool has_initialized_thread_ids; | 15 static bool has_initialized_thread_ids; |
16 | 16 |
17 namespace { | |
18 | |
19 // Used to defer deleting of local storage until the destructor has finished. | |
20 void DeleteLocalStateOnIOThread(FilePath cache_path) { | |
21 // Post the actual deletion to the DB thread to ensure it happens after the | |
22 // database file has been closed. | |
23 BrowserThread::PostTask( | |
24 BrowserThread::DB, FROM_HERE, | |
25 NewRunnableFunction<bool(*)(const FilePath&, bool), FilePath, bool>( | |
26 &file_util::Delete, cache_path, true)); | |
27 } | |
28 | |
29 } // namespace | |
30 | |
31 // ---------------------------------------------------------------------------- | 17 // ---------------------------------------------------------------------------- |
32 | 18 |
33 ChromeAppCacheService::ChromeAppCacheService( | 19 ChromeAppCacheService::ChromeAppCacheService( |
34 quota::QuotaManagerProxy* quota_manager_proxy) | 20 quota::QuotaManagerProxy* quota_manager_proxy) |
35 : AppCacheService(quota_manager_proxy), | 21 : AppCacheService(quota_manager_proxy), |
36 resource_context_(NULL), clear_local_state_on_exit_(false) { | 22 resource_context_(NULL), |
23 ALLOW_THIS_IN_INITIALIZER_LIST(appcache_got_info_callback_( | |
24 this, &ChromeAppCacheService::OnGotAppCacheInfo)), | |
25 ALLOW_THIS_IN_INITIALIZER_LIST(origin_deleted_callback_( | |
26 this, &ChromeAppCacheService::OnOriginDeleted)), | |
27 origins_to_be_deleted_count_(0), | |
28 appcaches_cleared_event_(new base::WaitableEvent(true, false)), | |
29 appcaches_cleared_callback_(NULL), | |
30 waiting_for_clear_appcaches_(false) { | |
37 } | 31 } |
38 | 32 |
39 void ChromeAppCacheService::InitializeOnIOThread( | 33 void ChromeAppCacheService::InitializeOnIOThread( |
40 const FilePath& cache_path, | 34 const FilePath& cache_path, |
41 const content::ResourceContext* resource_context, | 35 const content::ResourceContext* resource_context, |
42 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy, | 36 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy) { |
43 bool clear_local_state_on_exit) { | |
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
45 | 38 |
46 if (!has_initialized_thread_ids) { | 39 if (!has_initialized_thread_ids) { |
47 has_initialized_thread_ids = true; | 40 has_initialized_thread_ids = true; |
48 appcache::AppCacheThread::Init(BrowserThread::DB, BrowserThread::IO); | 41 appcache::AppCacheThread::Init(BrowserThread::DB, BrowserThread::IO); |
49 } | 42 } |
50 | 43 |
51 cache_path_ = cache_path; | 44 cache_path_ = cache_path; |
52 resource_context_ = resource_context; | 45 resource_context_ = resource_context; |
53 registrar_.Add( | 46 registrar_.Add( |
54 this, content::NOTIFICATION_PURGE_MEMORY, | 47 this, content::NOTIFICATION_PURGE_MEMORY, |
55 NotificationService::AllSources()); | 48 NotificationService::AllSources()); |
56 SetClearLocalStateOnExit(clear_local_state_on_exit); | |
57 | 49 |
58 // Init our base class. | 50 // Init our base class. |
59 Initialize(cache_path_, | 51 Initialize(cache_path_, |
60 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)); | 52 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)); |
61 set_appcache_policy(this); | 53 set_appcache_policy(this); |
62 set_special_storage_policy(special_storage_policy); | 54 set_special_storage_policy(special_storage_policy); |
63 } | 55 } |
64 | 56 |
65 ChromeAppCacheService::~ChromeAppCacheService() { | 57 ChromeAppCacheService::~ChromeAppCacheService() { |
66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
67 | |
68 if (clear_local_state_on_exit_ && !cache_path_.empty()) { | |
69 BrowserThread::PostTask( | |
70 BrowserThread::IO, FROM_HERE, | |
71 NewRunnableFunction(DeleteLocalStateOnIOThread, cache_path_)); | |
72 } | |
73 } | 58 } |
74 | 59 |
75 void ChromeAppCacheService::SetClearLocalStateOnExit(bool clear_local_state) { | 60 base::WaitableEvent* ChromeAppCacheService::ClearAppCache( |
76 // TODO(michaeln): How is 'protected' status granted to apps in this case? | 61 net::CompletionCallback* callback) |
62 { | |
77 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 63 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
78 BrowserThread::PostTask( | 64 BrowserThread::PostTask( |
79 BrowserThread::IO, FROM_HERE, | 65 BrowserThread::IO, FROM_HERE, |
80 NewRunnableMethod(this, | 66 NewRunnableMethod(this, |
81 &ChromeAppCacheService::SetClearLocalStateOnExit, | 67 &ChromeAppCacheService::ClearAppCache, |
82 clear_local_state)); | 68 callback)); |
83 return; | 69 return appcaches_cleared_event_.get(); |
84 } | 70 } |
85 clear_local_state_on_exit_ = clear_local_state; | 71 |
72 // Only one appcache deletion should be in progress at any given time. | |
73 DCHECK(waiting_for_clear_appcaches_ == false); | |
74 waiting_for_clear_appcaches_ = true; | |
75 appcaches_cleared_callback_ = callback; | |
76 appcaches_cleared_event_->Reset(); | |
77 | |
78 // Keep the ChromeAppCacheService instance alive until the deletion has | |
79 // finished. | |
80 AddRef(); | |
81 | |
82 appcache_info_ = new appcache::AppCacheInfoCollection; | |
83 GetAllAppCacheInfo( | |
84 appcache_info_, &appcache_got_info_callback_); | |
85 // Continues in OnGotAppCacheInfo. | |
86 | |
87 return appcaches_cleared_event_.get(); | |
88 } | |
89 | |
90 void ChromeAppCacheService::OnGotAppCacheInfo(int rv) { | |
91 using appcache::AppCacheInfoVector; | |
92 typedef std::map<GURL, AppCacheInfoVector> InfoByOrigin; | |
93 | |
94 origins_to_be_deleted_count_ = 0; | |
95 for (InfoByOrigin::const_iterator origin = | |
96 appcache_info_->infos_by_origin.begin(); | |
97 origin != appcache_info_->infos_by_origin.end(); ++origin) { | |
98 | |
99 if (special_storage_policy_->IsStorageProtected(origin->first)) | |
100 continue; | |
101 | |
102 ++origins_to_be_deleted_count_; | |
103 DeleteAppCachesForOrigin(origin->first, &origin_deleted_callback_); | |
104 } | |
105 | |
106 if (origins_to_be_deleted_count_ == 0) | |
107 OnClearedAppCache(); | |
108 // Else continues in OnOriginDeleted. | |
109 } | |
110 | |
111 void ChromeAppCacheService::OnOriginDeleted(int rv) { | |
112 --origins_to_be_deleted_count_; | |
113 if (origins_to_be_deleted_count_ == 0) | |
114 OnClearedAppCache(); | |
115 } | |
116 | |
117 void ChromeAppCacheService::OnClearedAppCache() { | |
michaeln
2011/07/12 18:45:38
Fyi, at the time this callback is invoked, the res
| |
118 appcaches_cleared_event_->Signal(); | |
119 if (appcaches_cleared_callback_) { | |
120 appcaches_cleared_callback_->Run(0); | |
121 } | |
122 waiting_for_clear_appcaches_ = false; | |
123 Release(); | |
86 } | 124 } |
87 | 125 |
88 bool ChromeAppCacheService::CanLoadAppCache(const GURL& manifest_url) { | 126 bool ChromeAppCacheService::CanLoadAppCache(const GURL& manifest_url) { |
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
90 // We don't prompt for read access. | 128 // We don't prompt for read access. |
91 return content::GetContentClient()->browser()->AllowAppCache( | 129 return content::GetContentClient()->browser()->AllowAppCache( |
92 manifest_url, *resource_context_); | 130 manifest_url, *resource_context_); |
93 } | 131 } |
94 | 132 |
95 int ChromeAppCacheService::CanCreateAppCache( | 133 int ChromeAppCacheService::CanCreateAppCache( |
(...skipping 28 matching lines...) Expand all Loading... | |
124 const tracked_objects::Location& from_here, | 162 const tracked_objects::Location& from_here, |
125 Task* task) { | 163 Task* task) { |
126 return BrowserThread::PostTask(ToBrowserThreadID(id), from_here, task); | 164 return BrowserThread::PostTask(ToBrowserThreadID(id), from_here, task); |
127 } | 165 } |
128 | 166 |
129 bool AppCacheThread::CurrentlyOn(int id) { | 167 bool AppCacheThread::CurrentlyOn(int id) { |
130 return BrowserThread::CurrentlyOn(ToBrowserThreadID(id)); | 168 return BrowserThread::CurrentlyOn(ToBrowserThreadID(id)); |
131 } | 169 } |
132 | 170 |
133 } // namespace appcache | 171 } // namespace appcache |
OLD | NEW |