webrtc_identity_service_host.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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#include "content/browser/renderer_host/media/webrtc_identity_service_host.h"
6
7#include "base/bind.h"
8#include "base/callback_helpers.h"
9#include "content/browser/media/webrtc_identity_store.h"
10#include "content/common/media/webrtc_identity_messages.h"
11#include "net/base/net_errors.h"
12
13namespace content {
14
15WebRTCIdentityServiceHost::WebRTCIdentityServiceHost(
16    WebRTCIdentityStore* identity_store)
17    : identity_store_(identity_store) {}
18
19WebRTCIdentityServiceHost::~WebRTCIdentityServiceHost() {
20  if (cancel_callback_.is_null())
21    return;
22  cancel_callback_.Run();
23}
24
25bool WebRTCIdentityServiceHost::OnMessageReceived(const IPC::Message& message,
26                                                  bool* message_was_ok) {
27  bool handled = true;
28  IPC_BEGIN_MESSAGE_MAP_EX(WebRTCIdentityServiceHost, message, *message_was_ok)
29    IPC_MESSAGE_HANDLER(WebRTCIdentityMsg_RequestIdentity, OnRequestIdentity)
30    IPC_MESSAGE_HANDLER(WebRTCIdentityMsg_CancelRequest, OnCancelRequest)
31    IPC_MESSAGE_UNHANDLED(handled = false)
32  IPC_END_MESSAGE_MAP_EX()
33  return handled;
34}
35
36void WebRTCIdentityServiceHost::OnRequestIdentity(
37    int request_id,
38    const GURL& origin,
39    const std::string& identity_name,
40    const std::string& common_name) {
41  if (!cancel_callback_.is_null()) {
42    DLOG(WARNING)
43        << "The request is rejected because there is already a pending request";
44    SendErrorMessage(request_id, net::ERR_INSUFFICIENT_RESOURCES);
45    return;
46  }
47  cancel_callback_ = identity_store_->RequestIdentity(
48      origin,
49      identity_name,
50      common_name,
51      base::Bind(&WebRTCIdentityServiceHost::OnComplete,
52                 base::Unretained(this),
53                 request_id));
54  if (cancel_callback_.is_null()) {
55    SendErrorMessage(request_id, net::ERR_UNEXPECTED);
56  }
57}
58
59void WebRTCIdentityServiceHost::OnCancelRequest(int request_id) {
60  base::ResetAndReturn(&cancel_callback_);
61}
62
63void WebRTCIdentityServiceHost::OnComplete(int request_id,
64                                           int error,
65                                           const std::string& certificate,
66                                           const std::string& private_key) {
67  cancel_callback_.Reset();
68  if (error == net::OK) {
69    Send(new WebRTCIdentityHostMsg_IdentityReady(
70        request_id, certificate, private_key));
71  } else {
72    SendErrorMessage(request_id, error);
73  }
74}
75
76void WebRTCIdentityServiceHost::SendErrorMessage(int request_id, int error) {
77  Send(new WebRTCIdentityHostMsg_RequestFailed(request_id, error));
78}
79
80}  // namespace content
81