1// Copyright 2013 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#ifndef CONTENT_RENDERER_MEDIA_WEBRTC_IDENTITY_SERVICE_H_
6#define CONTENT_RENDERER_MEDIA_WEBRTC_IDENTITY_SERVICE_H_
7
8#include <deque>
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/callback.h"
13#include "content/common/content_export.h"
14#include "content/public/renderer/render_process_observer.h"
15#include "url/gurl.h"
16
17namespace content {
18
19// This class handles WebRTC DTLS identity requests by sending IPC messages to
20// the browser process. Only one request is sent to the browser at a time; other
21// requests are queued and have to wait for the outstanding request to complete.
22class CONTENT_EXPORT WebRTCIdentityService : public RenderProcessObserver {
23 public:
24  typedef base::Callback<
25      void(const std::string& certificate, const std::string& private_key)>
26      SuccessCallback;
27
28  typedef base::Callback<void(int error)> FailureCallback;
29
30  WebRTCIdentityService();
31  virtual ~WebRTCIdentityService();
32
33  // Sends an identity request.
34  //
35  // |origin| is the origin of the caller;
36  // |identity_name| and |common_name| have the same meaning as in
37  // webrtc::DTLSIdentityServiceInterface::RequestIdentity;
38  // |success_callback| is the callback if the identity is successfully
39  // returned;
40  // |failure_callback| is the callback if the identity request fails.
41  //
42  // The request id is returned. It's unique within the renderer and can be used
43  // to cancel the request.
44  int RequestIdentity(const GURL& origin,
45                      const std::string& identity_name,
46                      const std::string& common_name,
47                      const SuccessCallback& success_callback,
48                      const FailureCallback& failure_callback);
49
50  // Cancels a previous request and the callbacks will not be called.
51  // If the |request_id| is not associated with the
52  // outstanding request or any queued request, this method does nothing.
53  //
54  // |request_id| is the request id returned from RequestIdentity.
55  void CancelRequest(int request_id);
56
57 protected:
58  // For unittest to override.
59  virtual bool Send(IPC::Message* message);
60  // RenderProcessObserver implementation. Protected for testing.
61  virtual bool OnControlMessageReceived(const IPC::Message& message) OVERRIDE;
62
63 private:
64  struct RequestInfo {
65    RequestInfo(int request_id,
66                const GURL& origin,
67                const std::string& identity_name,
68                const std::string& common_name,
69                const SuccessCallback& success_callback,
70                const FailureCallback& failure_callback);
71    ~RequestInfo();
72
73    int request_id;
74    GURL origin;
75    std::string identity_name;
76    std::string common_name;
77    SuccessCallback success_callback;
78    FailureCallback failure_callback;
79  };
80
81  // IPC message handlers.
82  void OnIdentityReady(int request_id,
83                       const std::string& certificate,
84                       const std::string& private_key);
85  void OnRequestFailed(int request_id, int error);
86
87  void SendRequest(const RequestInfo& request_info);
88  void OnOutstandingRequestReturned();
89
90  std::deque<RequestInfo> pending_requests_;
91  int next_request_id_;
92
93  DISALLOW_COPY_AND_ASSIGN(WebRTCIdentityService);
94};
95
96}  // namespace content
97
98#endif  // CONTENT_RENDERER_MEDIA_WEBRTC_IDENTITY_SERVICE_H_
99