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