Don't preload rarely seen large images
[chromium-blink-merge.git] / components / proximity_auth / connection.h
blob41414f6273d8582ee6860dbf1b0af19a05695dae
1 // Copyright 2014 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.
5 #ifndef COMPONENTS_PROXIMITY_AUTH_CONNECTION_H
6 #define COMPONENTS_PROXIMITY_AUTH_CONNECTION_H
8 #include "base/macros.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/observer_list.h"
12 #include "components/proximity_auth/remote_device.h"
14 namespace proximity_auth {
16 class ConnectionObserver;
17 class WireMessage;
19 // Base class representing a connection with a remote device, which is a
20 // persistent bidirectional channel for sending and receiving wire messages.
21 class Connection {
22 public:
23 enum Status {
24 DISCONNECTED,
25 IN_PROGRESS,
26 CONNECTED,
29 // Constructs a connection to the given |remote_device|.
30 explicit Connection(const RemoteDevice& remote_device);
31 virtual ~Connection();
33 // Returns true iff the connection's status is CONNECTED.
34 bool IsConnected() const;
36 // Returns true iff the connection is currently sending a message.
37 bool is_sending_message() const { return is_sending_message_; }
39 // Sends a message to the remote device.
40 // |OnSendCompleted()| will be called for all observers upon completion with
41 // either success or failure.
42 void SendMessage(scoped_ptr<WireMessage> message);
44 void AddObserver(ConnectionObserver* observer);
45 void RemoveObserver(ConnectionObserver* observer);
47 const RemoteDevice& remote_device() const { return remote_device_; }
49 // Abstract methods that subclasses should implement:
51 // Attempts to connect to the remote device if not already connected.
52 virtual void Connect() = 0;
54 // Disconnects from the remote device.
55 virtual void Disconnect() = 0;
57 protected:
58 // Sets the connection's status to |status|. If this is different from the
59 // previous status, notifies observers of the change in status.
60 // Virtual for testing.
61 virtual void SetStatus(Status status);
63 Status status() const { return status_; }
65 // Called after attempting to send bytes over the connection, whether the
66 // message was successfully sent or not.
67 // Virtual for testing.
68 virtual void OnDidSendMessage(const WireMessage& message, bool success);
70 // Called when bytes are read from the connection. There should not be a send
71 // in progress when this function is called.
72 // Virtual for testing.
73 virtual void OnBytesReceived(const std::string& bytes);
75 // Sends bytes over the connection. The implementing class should call
76 // OnSendCompleted() once the send succeeds or fails. At most one send will be
77 // in progress.
78 virtual void SendMessageImpl(scoped_ptr<WireMessage> message) = 0;
80 // Deserializes the |recieved_bytes_| and returns the resulting WireMessage,
81 // or NULL if the message is malformed. Sets |is_incomplete_message| to true
82 // if the |serialized_message| does not have enough data to parse the header,
83 // or if the message length encoded in the message header exceeds the size of
84 // the |serialized_message|. Exposed for testing.
85 virtual scoped_ptr<WireMessage> DeserializeWireMessage(
86 bool* is_incomplete_message);
88 // Exposed so it is possible to override DeserializeWireMessage in
89 // BluetoothLowEnergyConnection class, while mantaining the same
90 // functionality.
91 // TODO(sacomoto): remove this when FakeWireMessage is not needed anymore.
92 const std::string& received_bytes() { return received_bytes_; }
94 private:
95 // The remote device corresponding to this connection.
96 const RemoteDevice remote_device_;
98 // The current status of the connection.
99 Status status_;
101 // The registered observers of the connection.
102 base::ObserverList<ConnectionObserver> observers_;
104 // A temporary buffer storing bytes received before a received message can be
105 // fully constructed.
106 std::string received_bytes_;
108 // Whether a message is currently in the process of being sent.
109 bool is_sending_message_;
111 DISALLOW_COPY_AND_ASSIGN(Connection);
114 } // namespace proximity_auth
116 #endif // COMPONENTS_PROXIMITY_AUTH_CONNECTION_H