summaryrefslogtreecommitdiffstats
path: root/content/child/quota_dispatcher.h
blob: d42d02d8d91845ed40e1b08000af646d9c7fa26c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_CHILD_QUOTA_DISPATCHER_H_
#define CONTENT_CHILD_QUOTA_DISPATCHER_H_

#include <map>
#include <set>

#include "base/basictypes.h"
#include "base/id_map.h"
#include "base/memory/ref_counted.h"
#include "webkit/child/worker_task_runner.h"
#include "webkit/common/quota/quota_types.h"

class GURL;

namespace IPC {
class Message;
}

namespace blink {
class WebStorageQuotaCallbacks;
}

namespace content {

class ThreadSafeSender;
class QuotaMessageFilter;

// Dispatches and sends quota related messages sent to/from a child
// process from/to the main browser process.  There is one instance
// per each thread.  Thread-specific instance can be obtained by
// ThreadSpecificInstance().
class QuotaDispatcher : public webkit_glue::WorkerTaskRunner::Observer {
 public:
  class Callback {
   public:
    virtual ~Callback() {}
    virtual void DidQueryStorageUsageAndQuota(int64 usage, int64 quota) = 0;
    virtual void DidGrantStorageQuota(int64 granted_quota) = 0;
    virtual void DidFail(quota::QuotaStatusCode status) = 0;
  };

  QuotaDispatcher(ThreadSafeSender* thread_safe_sender,
                  QuotaMessageFilter* quota_message_filter);
  virtual ~QuotaDispatcher();

  // |thread_safe_sender| and |quota_message_filter| are used if
  // calling this leads to construction.
  static QuotaDispatcher* ThreadSpecificInstance(
      ThreadSafeSender* thread_safe_sender,
      QuotaMessageFilter* quota_message_filter);

  // webkit_glue::WorkerTaskRunner::Observer implementation.
  virtual void OnWorkerRunLoopStopped() OVERRIDE;

  void OnMessageReceived(const IPC::Message& msg);

  void QueryStorageUsageAndQuota(const GURL& gurl,
                                 quota::StorageType type,
                                 Callback* callback);
  void RequestStorageQuota(int render_view_id,
                           const GURL& gurl,
                           quota::StorageType type,
                           int64 requested_size,
                           Callback* callback);

  // Creates a new Callback instance for WebStorageQuotaCallbacks.
  static Callback* CreateWebStorageQuotaCallbacksWrapper(
      blink::WebStorageQuotaCallbacks* callbacks);

 private:
  // Message handlers.
  void DidQueryStorageUsageAndQuota(int request_id,
                                    int64 current_usage,
                                    int64 current_quota);
  void DidGrantStorageQuota(int request_id,
                            int64 granted_quota);
  void DidFail(int request_id,
               quota::QuotaStatusCode error);

  IDMap<Callback, IDMapOwnPointer> pending_quota_callbacks_;

  scoped_refptr<ThreadSafeSender> thread_safe_sender_;
  scoped_refptr<QuotaMessageFilter> quota_message_filter_;

  DISALLOW_COPY_AND_ASSIGN(QuotaDispatcher);
};

}  // namespace content

#endif  // CONTENT_CHILD_QUOTA_DISPATCHER_H_