summaryrefslogtreecommitdiff
path: root/chromium/content/browser/blob_storage/blob_dispatcher_host.h
blob: bf1a03cf8fb81272f9d656e68d0eceb723c0d4da (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2016 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_BROWSER_BLOB_STORAGE_BLOB_DISPATCHER_HOST_H_
#define CONTENT_BROWSER_BLOB_STORAGE_BLOB_DISPATCHER_HOST_H_

#include <set>
#include <string>
#include <vector>

#include "base/files/file.h"
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/shared_memory_handle.h"
#include "content/common/content_export.h"
#include "content/public/browser/browser_message_filter.h"
#include "storage/browser/blob/blob_async_builder_host.h"
#include "storage/browser/blob/blob_transport_result.h"
#include "storage/common/blob_storage/blob_storage_constants.h"

class GURL;

namespace IPC {
class Sender;
}

namespace storage {
class DataElement;
class BlobDataBuilder;
struct BlobItemBytesRequest;
struct BlobItemBytesResponse;
class BlobStorageContext;
}

namespace content {
class ChromeBlobStorageContext;

// This class's responsibility is to listen for and dispatch blob storage
// messages and handle logistics of blob storage for a single child process.
// When the child process terminates all blob references attributable to
// that process go away upon destruction of the instance.
// This lives in the browser process, is single threaded (IO thread), and there
// is one per child process.
class CONTENT_EXPORT BlobDispatcherHost : public BrowserMessageFilter {
 public:
  explicit BlobDispatcherHost(ChromeBlobStorageContext* blob_storage_context);

  // BrowserMessageFilter implementation.
  void OnChannelClosing() override;
  bool OnMessageReceived(const IPC::Message& message) override;

 protected:
  ~BlobDispatcherHost() override;

  // For testing use only.
  void SetMemoryConstantsForTesting(size_t max_ipc_memory_size,
                                    size_t max_shared_memory_size,
                                    uint64_t max_file_size) {
    async_builder_.SetMemoryConstantsForTesting(
        max_ipc_memory_size, max_shared_memory_size, max_file_size);
  }

 private:
  friend class base::RefCountedThreadSafe<BlobDispatcherHost>;
  friend class BlobDispatcherHostTest;
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, EmptyUUIDs);
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, MultipleTransfers);
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, SharedMemoryTransfer);
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, OnCancelBuildingBlob);
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest,
                           BlobReferenceWhileConstructing);
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest,
                           BlobReferenceWhileShortcutConstructing);
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest,
                           BlobReferenceWhileConstructingCancelled);
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, DecrementRefAfterRegister);
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, DecrementRefAfterOnStart);
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest,
                           DecrementRefAfterOnStartWithHandle);
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest,
                           HostDisconnectAfterRegisterWithHandle);
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, HostDisconnectAfterOnStart);
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest,
                           HostDisconnectAfterOnMemoryResponse);
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest,
                           CreateBlobWithBrokenReference);
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest,
                           DeferenceBlobOnDifferentHost);
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, BuildingReferenceChain);
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest,
                           BuildingReferenceChainWithCancel);
  FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest,
                           BuildingReferenceChainWithSourceDeath);

  typedef std::map<std::string, int> BlobReferenceMap;

  void OnRegisterBlobUUID(const std::string& uuid,
                          const std::string& content_type,
                          const std::string& content_disposition,
                          const std::set<std::string>& referenced_blob_uuids);
  void OnStartBuildingBlob(
      const std::string& uuid,
      const std::vector<storage::DataElement>& descriptions);
  void OnMemoryItemResponse(
      const std::string& uuid,
      const std::vector<storage::BlobItemBytesResponse>& response);
  void OnCancelBuildingBlob(const std::string& uuid,
                            const storage::IPCBlobCreationCancelCode code);

  void OnIncrementBlobRefCount(const std::string& uuid);
  void OnDecrementBlobRefCount(const std::string& uuid);
  void OnRegisterPublicBlobURL(const GURL& public_url, const std::string& uuid);
  void OnRevokePublicBlobURL(const GURL& public_url);

  storage::BlobStorageContext* context();

  void SendMemoryRequest(
      const std::string& uuid,
      scoped_ptr<std::vector<storage::BlobItemBytesRequest>> requests,
      scoped_ptr<std::vector<base::SharedMemoryHandle>> memory_handles,
      scoped_ptr<std::vector<base::File>> files);

  // Send the appropriate IPC response to the renderer for the given result.
  void SendIPCResponse(const std::string& uuid,
                       storage::BlobTransportResult result);

  bool IsInUseInHost(const std::string& uuid);
  bool IsUrlRegisteredInHost(const GURL& blob_url);

  // Unregisters all blobs and urls that were registered in this host.
  void ClearHostFromBlobStorageContext();

  // Collection of blob ids and a count of how many usages
  // of that id are attributable to this consumer.
  BlobReferenceMap blobs_inuse_map_;

  // The set of public blob urls coined by this consumer.
  std::set<GURL> public_blob_urls_;

  scoped_refptr<ChromeBlobStorageContext> blob_storage_context_;
  storage::BlobAsyncBuilderHost async_builder_;

  DISALLOW_COPY_AND_ASSIGN(BlobDispatcherHost);
};
}  // namespace content
#endif  // CONTENT_BROWSER_BLOB_STORAGE_BLOB_DISPATCHER_HOST_H_