Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: content/browser/appcache/chrome_appcache_service.cc

Issue 7210006: AppCaches which belong to hosted apps are not protected from deletion (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Small changes cont. Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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, NotificationType::PURGE_MEMORY, NotificationService::AllSources()); 47 this, NotificationType::PURGE_MEMORY, NotificationService::AllSources());
55 SetClearLocalStateOnExit(clear_local_state_on_exit);
56 48
57 // Init our base class. 49 // Init our base class.
58 Initialize(cache_path_, 50 Initialize(cache_path_,
59 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)); 51 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
60 set_appcache_policy(this); 52 set_appcache_policy(this);
61 set_special_storage_policy(special_storage_policy); 53 set_special_storage_policy(special_storage_policy);
62 } 54 }
63 55
64 ChromeAppCacheService::~ChromeAppCacheService() { 56 ChromeAppCacheService::~ChromeAppCacheService() {
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
66
67 if (clear_local_state_on_exit_ && !cache_path_.empty()) {
68 BrowserThread::PostTask(
69 BrowserThread::IO, FROM_HERE,
70 NewRunnableFunction(DeleteLocalStateOnIOThread, cache_path_));
71 }
72 } 57 }
73 58
74 void ChromeAppCacheService::SetClearLocalStateOnExit(bool clear_local_state) { 59 base::WaitableEvent* ChromeAppCacheService::ClearAppCache(
75 // TODO(michaeln): How is 'protected' status granted to apps in this case? 60 net::CompletionCallback* callback)
61 {
76 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { 62 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
77 BrowserThread::PostTask( 63 BrowserThread::PostTask(
78 BrowserThread::IO, FROM_HERE, 64 BrowserThread::IO, FROM_HERE,
79 NewRunnableMethod(this, 65 NewRunnableMethod(this,
80 &ChromeAppCacheService::SetClearLocalStateOnExit, 66 &ChromeAppCacheService::ClearAppCache,
81 clear_local_state)); 67 callback));
82 return; 68 return appcaches_cleared_event_.get();
83 } 69 }
84 clear_local_state_on_exit_ = clear_local_state; 70
71 // Only one appcache deletion should be in progress at any given time.
72 DCHECK(waiting_for_clear_appcaches_ == false);
73 waiting_for_clear_appcaches_ = true;
74 appcaches_cleared_callback_ = callback;
75 appcaches_cleared_event_->Reset();
76
77 // Keep the ChromeAppCacheService instance alive until the deletion has
78 // finished.
79 AddRef();
80
81 appcache_info_ = new appcache::AppCacheInfoCollection;
82 GetAllAppCacheInfo(
83 appcache_info_, &appcache_got_info_callback_);
84 // Continues in OnGotAppCacheInfo.
85
86 return appcaches_cleared_event_.get();
87 }
88
89 void ChromeAppCacheService::OnGotAppCacheInfo(int rv) {
90 using appcache::AppCacheInfoVector;
91 typedef std::map<GURL, AppCacheInfoVector> InfoByOrigin;
92
93 origins_to_be_deleted_count_ = 0;
94 for (InfoByOrigin::const_iterator origin =
95 appcache_info_->infos_by_origin.begin();
96 origin != appcache_info_->infos_by_origin.end(); ++origin) {
97
98 if (special_storage_policy_->IsStorageProtected(origin->first))
99 continue;
100
101 ++origins_to_be_deleted_count_;
102 DeleteAppCachesForOrigin(origin->first, &origin_deleted_callback_);
103 }
104
105 if (origins_to_be_deleted_count_ == 0)
106 OnClearedAppCache();
107 // Else continues in OnOriginDeleted.
108 }
109
110 void ChromeAppCacheService::OnOriginDeleted(int rv) {
111 --origins_to_be_deleted_count_;
112 if (origins_to_be_deleted_count_ == 0)
113 OnClearedAppCache();
114 }
115
116 void ChromeAppCacheService::OnClearedAppCache() {
117 appcaches_cleared_event_->Signal();
118 if (appcaches_cleared_callback_) {
119 appcaches_cleared_callback_->Run(0);
120 }
121 waiting_for_clear_appcaches_ = false;
122 Release();
85 } 123 }
86 124
87 bool ChromeAppCacheService::CanLoadAppCache(const GURL& manifest_url) { 125 bool ChromeAppCacheService::CanLoadAppCache(const GURL& manifest_url) {
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
89 // We don't prompt for read access. 127 // We don't prompt for read access.
90 return content::GetContentClient()->browser()->AllowAppCache( 128 return content::GetContentClient()->browser()->AllowAppCache(
91 manifest_url, *resource_context_); 129 manifest_url, *resource_context_);
92 } 130 }
93 131
94 int ChromeAppCacheService::CanCreateAppCache( 132 int ChromeAppCacheService::CanCreateAppCache(
(...skipping 28 matching lines...) Expand all
123 const tracked_objects::Location& from_here, 161 const tracked_objects::Location& from_here,
124 Task* task) { 162 Task* task) {
125 return BrowserThread::PostTask(ToBrowserThreadID(id), from_here, task); 163 return BrowserThread::PostTask(ToBrowserThreadID(id), from_here, task);
126 } 164 }
127 165
128 bool AppCacheThread::CurrentlyOn(int id) { 166 bool AppCacheThread::CurrentlyOn(int id) {
129 return BrowserThread::CurrentlyOn(ToBrowserThreadID(id)); 167 return BrowserThread::CurrentlyOn(ToBrowserThreadID(id));
130 } 168 }
131 169
132 } // namespace appcache 170 } // namespace appcache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698