OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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/renderer/media/dtls_identity_service.h" |
| 6 |
| 7 #include "content/common/media/dtls_identity_messages.h" |
| 8 #include "content/public/renderer/render_thread.h" |
| 9 |
| 10 namespace content { |
| 11 |
| 12 DTLSIdentityService::DTLSIdentityService(const GURL& origin) |
| 13 : origin_(origin), pending_observer_(NULL), pending_request_id_(0) { |
| 14 RenderThread::Get()->AddObserver(this); |
| 15 } |
| 16 |
| 17 DTLSIdentityService::~DTLSIdentityService() { |
| 18 RenderThread::Get()->RemoveObserver(this); |
| 19 if (pending_observer_) { |
| 20 RenderThread::Get()->Send( |
| 21 new DTLSIdentityMsg_CancelRequest(pending_request_id_)); |
| 22 } |
| 23 } |
| 24 |
| 25 bool DTLSIdentityService::RequestIdentity( |
| 26 const std::string& identity_name, |
| 27 const std::string& common_name, |
| 28 webrtc::DTLSIdentityRequestObserver* observer) { |
| 29 static int s_next_request_id = 0; |
| 30 DCHECK(observer); |
| 31 if (pending_observer_) |
| 32 return false; |
| 33 |
| 34 pending_observer_ = observer; |
| 35 pending_request_id_ = s_next_request_id++; |
| 36 RenderThread::Get()->Send( |
| 37 new DTLSIdentityMsg_RequestIdentity( |
| 38 pending_request_id_, origin_, identity_name, common_name)); |
| 39 return true; |
| 40 } |
| 41 |
| 42 bool DTLSIdentityService::OnControlMessageReceived( |
| 43 const IPC::Message& message) { |
| 44 if (!pending_observer_) |
| 45 return false; |
| 46 |
| 47 int old_pending_request_id = pending_request_id_; |
| 48 bool handled = true; |
| 49 IPC_BEGIN_MESSAGE_MAP(DTLSIdentityService, message) |
| 50 IPC_MESSAGE_HANDLER(DTLSIdentityHostMsg_IdentityReady, OnIdentityReady) |
| 51 IPC_MESSAGE_HANDLER(DTLSIdentityHostMsg_RequestFailed, OnRequestFailed) |
| 52 IPC_MESSAGE_UNHANDLED(handled = false) |
| 53 IPC_END_MESSAGE_MAP() |
| 54 |
| 55 if (pending_request_id_ == old_pending_request_id) |
| 56 handled = false; |
| 57 |
| 58 return handled; |
| 59 } |
| 60 |
| 61 void DTLSIdentityService::OnIdentityReady(int request_id, |
| 62 const std::string& certificate, |
| 63 const std::string& private_key) { |
| 64 if (request_id != pending_request_id_) |
| 65 return; |
| 66 pending_observer_->OnSuccess(certificate, private_key); |
| 67 pending_observer_ = NULL; |
| 68 pending_request_id_ = 0; |
| 69 } |
| 70 |
| 71 void DTLSIdentityService::OnRequestFailed(int request_id, int error) { |
| 72 if (request_id != pending_request_id_) |
| 73 return; |
| 74 pending_observer_->OnFailure(error); |
| 75 pending_observer_ = NULL; |
| 76 pending_request_id_ = 0; |
| 77 } |
| 78 |
| 79 } // namespace content |
OLD | NEW |