Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / webkit / browser / appcache / appcache_service.cc
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 #include "webkit/browser/appcache/appcache_service.h"
6
7 #include <functional>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/stl_util.h"
14 #include "net/base/completion_callback.h"
15 #include "net/base/io_buffer.h"
16 #include "webkit/browser/appcache/appcache.h"
17 #include "webkit/browser/appcache/appcache_backend_impl.h"
18 #include "webkit/browser/appcache/appcache_entry.h"
19 #include "webkit/browser/appcache/appcache_executable_handler.h"
20 #include "webkit/browser/appcache/appcache_histograms.h"
21 #include "webkit/browser/appcache/appcache_policy.h"
22 #include "webkit/browser/appcache/appcache_quota_client.h"
23 #include "webkit/browser/appcache/appcache_response.h"
24 #include "webkit/browser/appcache/appcache_storage_impl.h"
25 #include "webkit/browser/quota/special_storage_policy.h"
26
27 namespace appcache {
28
29 namespace {
30
31 void DeferredCallback(const net::CompletionCallback& callback, int rv) {
32   callback.Run(rv);
33 }
34
35 }  // namespace
36
37 AppCacheInfoCollection::AppCacheInfoCollection() {}
38
39 AppCacheInfoCollection::~AppCacheInfoCollection() {}
40
41 // AsyncHelper -------
42
43 class AppCacheService::AsyncHelper
44     : public AppCacheStorage::Delegate {
45  public:
46   AsyncHelper(AppCacheService* service,
47               const net::CompletionCallback& callback)
48       : service_(service), callback_(callback) {
49     service_->pending_helpers_.insert(this);
50   }
51
52   virtual ~AsyncHelper() {
53     if (service_)
54       service_->pending_helpers_.erase(this);
55   }
56
57   virtual void Start() = 0;
58   virtual void Cancel();
59
60  protected:
61   void CallCallback(int rv) {
62     if (!callback_.is_null()) {
63       // Defer to guarantee async completion.
64       base::MessageLoop::current()->PostTask(
65           FROM_HERE, base::Bind(&DeferredCallback, callback_, rv));
66     }
67     callback_.Reset();
68   }
69
70   AppCacheService* service_;
71   net::CompletionCallback callback_;
72 };
73
74 void AppCacheService::AsyncHelper::Cancel() {
75   if (!callback_.is_null()) {
76     callback_.Run(net::ERR_ABORTED);
77     callback_.Reset();
78   }
79   service_->storage()->CancelDelegateCallbacks(this);
80   service_ = NULL;
81 }
82
83 // CanHandleOfflineHelper -------
84
85 class AppCacheService::CanHandleOfflineHelper : AsyncHelper {
86  public:
87   CanHandleOfflineHelper(
88       AppCacheService* service, const GURL& url,
89       const GURL& first_party, const net::CompletionCallback& callback)
90       : AsyncHelper(service, callback),
91         url_(url),
92         first_party_(first_party) {
93   }
94
95   virtual void Start() OVERRIDE {
96     AppCachePolicy* policy = service_->appcache_policy();
97     if (policy && !policy->CanLoadAppCache(url_, first_party_)) {
98       CallCallback(net::ERR_FAILED);
99       delete this;
100       return;
101     }
102
103     service_->storage()->FindResponseForMainRequest(url_, GURL(), this);
104   }
105
106  private:
107   // AppCacheStorage::Delegate implementation.
108   virtual void OnMainResponseFound(
109       const GURL& url, const AppCacheEntry& entry,
110       const GURL& fallback_url, const AppCacheEntry& fallback_entry,
111       int64 cache_id, int64 group_id, const GURL& mainfest_url) OVERRIDE;
112
113   GURL url_;
114   GURL first_party_;
115
116   DISALLOW_COPY_AND_ASSIGN(CanHandleOfflineHelper);
117 };
118
119 void AppCacheService::CanHandleOfflineHelper::OnMainResponseFound(
120       const GURL& url, const AppCacheEntry& entry,
121       const GURL& fallback_url, const AppCacheEntry& fallback_entry,
122       int64 cache_id, int64 group_id, const GURL& manifest_url) {
123   bool can = (entry.has_response_id() || fallback_entry.has_response_id());
124   CallCallback(can ? net::OK : net::ERR_FAILED);
125   delete this;
126 }
127
128 // DeleteHelper -------
129
130 class AppCacheService::DeleteHelper : public AsyncHelper {
131  public:
132   DeleteHelper(
133       AppCacheService* service, const GURL& manifest_url,
134       const net::CompletionCallback& callback)
135       : AsyncHelper(service, callback), manifest_url_(manifest_url) {
136   }
137
138   virtual void Start() OVERRIDE {
139     service_->storage()->LoadOrCreateGroup(manifest_url_, this);
140   }
141
142  private:
143   // AppCacheStorage::Delegate implementation.
144   virtual void OnGroupLoaded(
145       appcache::AppCacheGroup* group, const GURL& manifest_url) OVERRIDE;
146   virtual void OnGroupMadeObsolete(
147       appcache::AppCacheGroup* group, bool success) OVERRIDE;
148
149   GURL manifest_url_;
150   DISALLOW_COPY_AND_ASSIGN(DeleteHelper);
151 };
152
153 void AppCacheService::DeleteHelper::OnGroupLoaded(
154       appcache::AppCacheGroup* group, const GURL& manifest_url) {
155   if (group) {
156     group->set_being_deleted(true);
157     group->CancelUpdate();
158     service_->storage()->MakeGroupObsolete(group, this);
159   } else {
160     CallCallback(net::ERR_FAILED);
161     delete this;
162   }
163 }
164
165 void AppCacheService::DeleteHelper::OnGroupMadeObsolete(
166       appcache::AppCacheGroup* group, bool success) {
167   CallCallback(success ? net::OK : net::ERR_FAILED);
168   delete this;
169 }
170
171 // DeleteOriginHelper -------
172
173 class AppCacheService::DeleteOriginHelper : public AsyncHelper {
174  public:
175   DeleteOriginHelper(
176       AppCacheService* service, const GURL& origin,
177       const net::CompletionCallback& callback)
178       : AsyncHelper(service, callback), origin_(origin),
179         num_caches_to_delete_(0), successes_(0), failures_(0) {
180   }
181
182   virtual void Start() OVERRIDE {
183     // We start by listing all caches, continues in OnAllInfo().
184     service_->storage()->GetAllInfo(this);
185   }
186
187  private:
188   // AppCacheStorage::Delegate implementation.
189   virtual void OnAllInfo(AppCacheInfoCollection* collection) OVERRIDE;
190   virtual void OnGroupLoaded(
191       appcache::AppCacheGroup* group, const GURL& manifest_url) OVERRIDE;
192   virtual void OnGroupMadeObsolete(
193       appcache::AppCacheGroup* group, bool success) OVERRIDE;
194
195   void CacheCompleted(bool success);
196
197   GURL origin_;
198   int num_caches_to_delete_;
199   int successes_;
200   int failures_;
201
202   DISALLOW_COPY_AND_ASSIGN(DeleteOriginHelper);
203 };
204
205 void AppCacheService::DeleteOriginHelper::OnAllInfo(
206     AppCacheInfoCollection* collection) {
207   if (!collection) {
208     // Failed to get a listing.
209     CallCallback(net::ERR_FAILED);
210     delete this;
211     return;
212   }
213
214   std::map<GURL, AppCacheInfoVector>::iterator found =
215       collection->infos_by_origin.find(origin_);
216   if (found == collection->infos_by_origin.end() || found->second.empty()) {
217     // No caches for this origin.
218     CallCallback(net::OK);
219     delete this;
220     return;
221   }
222
223   // We have some caches to delete.
224   const AppCacheInfoVector& caches_to_delete = found->second;
225   successes_ = 0;
226   failures_ = 0;
227   num_caches_to_delete_ = static_cast<int>(caches_to_delete.size());
228   for (AppCacheInfoVector::const_iterator iter = caches_to_delete.begin();
229        iter != caches_to_delete.end(); ++iter) {
230     service_->storage()->LoadOrCreateGroup(iter->manifest_url, this);
231   }
232 }
233
234 void AppCacheService::DeleteOriginHelper::OnGroupLoaded(
235       appcache::AppCacheGroup* group, const GURL& manifest_url) {
236   if (group) {
237     group->set_being_deleted(true);
238     group->CancelUpdate();
239     service_->storage()->MakeGroupObsolete(group, this);
240   } else {
241     CacheCompleted(false);
242   }
243 }
244
245 void AppCacheService::DeleteOriginHelper::OnGroupMadeObsolete(
246       appcache::AppCacheGroup* group, bool success) {
247   CacheCompleted(success);
248 }
249
250 void AppCacheService::DeleteOriginHelper::CacheCompleted(bool success) {
251   if (success)
252     ++successes_;
253   else
254     ++failures_;
255   if ((successes_ + failures_) < num_caches_to_delete_)
256     return;
257
258   CallCallback(!failures_ ? net::OK : net::ERR_FAILED);
259   delete this;
260 }
261
262
263 // GetInfoHelper -------
264
265 class AppCacheService::GetInfoHelper : AsyncHelper {
266  public:
267   GetInfoHelper(
268       AppCacheService* service, AppCacheInfoCollection* collection,
269       const net::CompletionCallback& callback)
270       : AsyncHelper(service, callback), collection_(collection) {
271   }
272
273   virtual void Start() OVERRIDE {
274     service_->storage()->GetAllInfo(this);
275   }
276
277  private:
278   // AppCacheStorage::Delegate implementation.
279   virtual void OnAllInfo(AppCacheInfoCollection* collection) OVERRIDE;
280
281   scoped_refptr<AppCacheInfoCollection> collection_;
282
283   DISALLOW_COPY_AND_ASSIGN(GetInfoHelper);
284 };
285
286 void AppCacheService::GetInfoHelper::OnAllInfo(
287       AppCacheInfoCollection* collection) {
288   if (collection)
289     collection->infos_by_origin.swap(collection_->infos_by_origin);
290   CallCallback(collection ? net::OK : net::ERR_FAILED);
291   delete this;
292 }
293
294 // CheckResponseHelper -------
295
296 class AppCacheService::CheckResponseHelper : AsyncHelper {
297  public:
298   CheckResponseHelper(
299       AppCacheService* service, const GURL& manifest_url, int64 cache_id,
300       int64 response_id)
301       : AsyncHelper(service, net::CompletionCallback()),
302         manifest_url_(manifest_url),
303         cache_id_(cache_id),
304         response_id_(response_id),
305         kIOBufferSize(32 * 1024),
306         expected_total_size_(0),
307         amount_headers_read_(0),
308         amount_data_read_(0) {
309   }
310
311   virtual void Start() OVERRIDE {
312     service_->storage()->LoadOrCreateGroup(manifest_url_, this);
313   }
314
315   virtual void Cancel() OVERRIDE {
316     AppCacheHistograms::CountCheckResponseResult(
317         AppCacheHistograms::CHECK_CANCELED);
318     response_reader_.reset();
319     AsyncHelper::Cancel();
320   }
321
322  private:
323   virtual void OnGroupLoaded(AppCacheGroup* group,
324                              const GURL& manifest_url) OVERRIDE;
325   void OnReadInfoComplete(int result);
326   void OnReadDataComplete(int result);
327
328   // Inputs describing what to check.
329   GURL manifest_url_;
330   int64 cache_id_;
331   int64 response_id_;
332
333   // Internals used to perform the checks.
334   const int kIOBufferSize;
335   scoped_refptr<AppCache> cache_;
336   scoped_ptr<AppCacheResponseReader> response_reader_;
337   scoped_refptr<HttpResponseInfoIOBuffer> info_buffer_;
338   scoped_refptr<net::IOBuffer> data_buffer_;
339   int64 expected_total_size_;
340   int amount_headers_read_;
341   int amount_data_read_;
342   DISALLOW_COPY_AND_ASSIGN(CheckResponseHelper);
343 };
344
345 void AppCacheService::CheckResponseHelper::OnGroupLoaded(
346     AppCacheGroup* group, const GURL& manifest_url) {
347   DCHECK_EQ(manifest_url_, manifest_url);
348   if (!group || !group->newest_complete_cache() || group->is_being_deleted() ||
349       group->is_obsolete()) {
350     AppCacheHistograms::CountCheckResponseResult(
351         AppCacheHistograms::MANIFEST_OUT_OF_DATE);
352     delete this;
353     return;
354   }
355
356   cache_ = group->newest_complete_cache();
357   const AppCacheEntry* entry = cache_->GetEntryWithResponseId(response_id_);
358   if (!entry) {
359     if (cache_->cache_id() == cache_id_) {
360       AppCacheHistograms::CountCheckResponseResult(
361           AppCacheHistograms::ENTRY_NOT_FOUND);
362       service_->DeleteAppCacheGroup(manifest_url_, net::CompletionCallback());
363     } else {
364       AppCacheHistograms::CountCheckResponseResult(
365           AppCacheHistograms::RESPONSE_OUT_OF_DATE);
366     }
367     delete this;
368     return;
369   }
370
371   // Verify that we can read the response info and data.
372   expected_total_size_ = entry->response_size();
373   response_reader_.reset(service_->storage()->CreateResponseReader(
374       manifest_url_, group->group_id(), response_id_));
375   info_buffer_ = new HttpResponseInfoIOBuffer();
376   response_reader_->ReadInfo(
377       info_buffer_.get(),
378       base::Bind(&CheckResponseHelper::OnReadInfoComplete,
379                  base::Unretained(this)));
380 }
381
382 void AppCacheService::CheckResponseHelper::OnReadInfoComplete(int result) {
383   if (result < 0) {
384     AppCacheHistograms::CountCheckResponseResult(
385         AppCacheHistograms::READ_HEADERS_ERROR);
386     service_->DeleteAppCacheGroup(manifest_url_, net::CompletionCallback());
387     delete this;
388     return;
389   }
390   amount_headers_read_ = result;
391
392   // Start reading the data.
393   data_buffer_ = new net::IOBuffer(kIOBufferSize);
394   response_reader_->ReadData(
395       data_buffer_.get(),
396       kIOBufferSize,
397       base::Bind(&CheckResponseHelper::OnReadDataComplete,
398                  base::Unretained(this)));
399 }
400
401 void AppCacheService::CheckResponseHelper::OnReadDataComplete(int result) {
402   if (result > 0) {
403     // Keep reading until we've read thru everything or failed to read.
404     amount_data_read_ += result;
405     response_reader_->ReadData(
406         data_buffer_.get(),
407         kIOBufferSize,
408         base::Bind(&CheckResponseHelper::OnReadDataComplete,
409                    base::Unretained(this)));
410     return;
411   }
412
413   AppCacheHistograms::CheckResponseResultType check_result;
414   if (result < 0)
415     check_result = AppCacheHistograms::READ_DATA_ERROR;
416   else if (info_buffer_->response_data_size != amount_data_read_ ||
417            expected_total_size_ != amount_data_read_ + amount_headers_read_)
418     check_result = AppCacheHistograms::UNEXPECTED_DATA_SIZE;
419   else
420     check_result = AppCacheHistograms::RESPONSE_OK;
421   AppCacheHistograms::CountCheckResponseResult(check_result);
422
423   if (check_result != AppCacheHistograms::RESPONSE_OK)
424     service_->DeleteAppCacheGroup(manifest_url_, net::CompletionCallback());
425   delete this;
426 }
427
428 // AppCacheStorageReference ------
429
430 AppCacheStorageReference::AppCacheStorageReference(
431     scoped_ptr<AppCacheStorage> storage)
432     : storage_(storage.Pass()) {}
433 AppCacheStorageReference::~AppCacheStorageReference() {}
434
435 // AppCacheService -------
436
437 AppCacheService::AppCacheService(quota::QuotaManagerProxy* quota_manager_proxy)
438     : appcache_policy_(NULL), quota_client_(NULL), handler_factory_(NULL),
439       quota_manager_proxy_(quota_manager_proxy),
440       request_context_(NULL),
441       force_keep_session_state_(false) {
442   if (quota_manager_proxy_.get()) {
443     quota_client_ = new AppCacheQuotaClient(this);
444     quota_manager_proxy_->RegisterClient(quota_client_);
445   }
446 }
447
448 AppCacheService::~AppCacheService() {
449   DCHECK(backends_.empty());
450   std::for_each(pending_helpers_.begin(),
451                 pending_helpers_.end(),
452                 std::mem_fun(&AsyncHelper::Cancel));
453   STLDeleteElements(&pending_helpers_);
454   if (quota_client_)
455     quota_client_->NotifyAppCacheDestroyed();
456
457   // Destroy storage_ first; ~AppCacheStorageImpl accesses other data members
458   // (special_storage_policy_).
459   storage_.reset();
460 }
461
462 void AppCacheService::Initialize(const base::FilePath& cache_directory,
463                                  base::MessageLoopProxy* db_thread,
464                                  base::MessageLoopProxy* cache_thread) {
465   DCHECK(!storage_.get());
466   cache_directory_ = cache_directory;
467   db_thread_ = db_thread;
468   cache_thread_ = cache_thread;
469   AppCacheStorageImpl* storage = new AppCacheStorageImpl(this);
470   storage->Initialize(cache_directory, db_thread, cache_thread);
471   storage_.reset(storage);
472 }
473
474 void AppCacheService::ScheduleReinitialize() {
475   if (reinit_timer_.IsRunning())
476     return;
477
478   // Reinitialization only happens when corruption has been noticed.
479   // We don't want to thrash the disk but we also don't want to
480   // leave the appcache disabled for an indefinite period of time. Some
481   // users never shutdown the browser.
482
483   const base::TimeDelta kZeroDelta;
484   const base::TimeDelta kOneHour(base::TimeDelta::FromHours(1));
485   const base::TimeDelta k30Seconds(base::TimeDelta::FromSeconds(30));
486
487   // If the system managed to stay up for long enough, reset the
488   // delay so a new failure won't incur a long wait to get going again.
489   base::TimeDelta up_time = base::Time::Now() - last_reinit_time_;
490   if (next_reinit_delay_ != kZeroDelta && up_time > kOneHour)
491     next_reinit_delay_ = kZeroDelta;
492
493   reinit_timer_.Start(FROM_HERE, next_reinit_delay_,
494                       this, &AppCacheService::Reinitialize);
495
496   // Adjust the delay for next time.
497   base::TimeDelta increment = std::max(k30Seconds, next_reinit_delay_);
498   next_reinit_delay_ = std::min(next_reinit_delay_ + increment,  kOneHour);
499 }
500
501 void AppCacheService::Reinitialize() {
502   AppCacheHistograms::CountReinitAttempt(!last_reinit_time_.is_null());
503   last_reinit_time_ = base::Time::Now();
504
505   // Inform observers of about this and give them a chance to
506   // defer deletion of the old storage object.
507   scoped_refptr<AppCacheStorageReference>
508       old_storage_ref(new AppCacheStorageReference(storage_.Pass()));
509   FOR_EACH_OBSERVER(Observer, observers_,
510                     OnServiceReinitialized(old_storage_ref.get()));
511
512   Initialize(cache_directory_, db_thread_, cache_thread_);
513 }
514
515 void AppCacheService::CanHandleMainResourceOffline(
516     const GURL& url,
517     const GURL& first_party,
518     const net::CompletionCallback& callback) {
519   CanHandleOfflineHelper* helper =
520       new CanHandleOfflineHelper(this, url, first_party, callback);
521   helper->Start();
522 }
523
524 void AppCacheService::GetAllAppCacheInfo(
525     AppCacheInfoCollection* collection,
526     const net::CompletionCallback& callback) {
527   DCHECK(collection);
528   GetInfoHelper* helper = new GetInfoHelper(this, collection, callback);
529   helper->Start();
530 }
531
532 void AppCacheService::DeleteAppCacheGroup(
533     const GURL& manifest_url,
534     const net::CompletionCallback& callback) {
535   DeleteHelper* helper = new DeleteHelper(this, manifest_url, callback);
536   helper->Start();
537 }
538
539 void AppCacheService::DeleteAppCachesForOrigin(
540     const GURL& origin,  const net::CompletionCallback& callback) {
541   DeleteOriginHelper* helper = new DeleteOriginHelper(this, origin, callback);
542   helper->Start();
543 }
544
545 void AppCacheService::CheckAppCacheResponse(const GURL& manifest_url,
546                                             int64 cache_id,
547                                             int64 response_id) {
548   CheckResponseHelper* helper = new CheckResponseHelper(
549       this, manifest_url, cache_id, response_id);
550   helper->Start();
551 }
552
553 void AppCacheService::set_special_storage_policy(
554     quota::SpecialStoragePolicy* policy) {
555   special_storage_policy_ = policy;
556 }
557
558 void AppCacheService::RegisterBackend(
559     AppCacheBackendImpl* backend_impl) {
560   DCHECK(backends_.find(backend_impl->process_id()) == backends_.end());
561   backends_.insert(
562       BackendMap::value_type(backend_impl->process_id(), backend_impl));
563 }
564
565 void AppCacheService::UnregisterBackend(
566     AppCacheBackendImpl* backend_impl) {
567   backends_.erase(backend_impl->process_id());
568 }
569
570 }  // namespace appcache