Chromium Code Reviews| 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/browser/renderer_host/media/webrtc_identity_service_host.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "content/common/media/webrtc_identity_messages.h" | |
| 9 #include "content/public/browser/render_process_host.h" | |
| 10 #include "net/base/net_errors.h" | |
| 11 | |
| 12 namespace content { | |
| 13 | |
| 14 const int WebRTCIdentityServiceHost::MAX_REQUESTS_PER_SECOND = 5; | |
| 15 | |
| 16 WebRTCIdentityServiceHost::WebRTCIdentityServiceHost( | |
| 17 WebRTCIdentityStore* identity_store) | |
| 18 : identity_store_(identity_store) {} | |
| 19 | |
| 20 WebRTCIdentityServiceHost::~WebRTCIdentityServiceHost() { | |
| 21 RequestCancelCallbackMap::iterator it; | |
| 22 for (it = pending_request_cancel_callback_map_.begin(); | |
| 23 it != pending_request_cancel_callback_map_.end(); | |
| 24 ++it) { | |
| 25 it->second.Run(); | |
| 26 } | |
| 27 } | |
| 28 | |
| 29 bool WebRTCIdentityServiceHost::OnMessageReceived(const IPC::Message& message, | |
| 30 bool* message_was_ok) { | |
| 31 bool handled = true; | |
| 32 IPC_BEGIN_MESSAGE_MAP_EX(WebRTCIdentityServiceHost, message, *message_was_ok) | |
| 33 IPC_MESSAGE_HANDLER(WebRTCIdentityMsg_RequestIdentity, OnRequestIdentity) | |
| 34 IPC_MESSAGE_HANDLER(WebRTCIdentityMsg_CancelRequest, OnCancelRequest) | |
| 35 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 36 IPC_END_MESSAGE_MAP_EX() | |
| 37 return handled; | |
| 38 } | |
| 39 | |
| 40 void WebRTCIdentityServiceHost::OnRequestIdentity( | |
| 41 int request_id, | |
| 42 const GURL& origin, | |
| 43 const std::string& identity_name, | |
| 44 const std::string& common_name) { | |
| 45 if (pending_request_cancel_callback_map_.end() != | |
| 46 pending_request_cancel_callback_map_.find(request_id)) | |
| 47 return; | |
| 48 | |
| 49 if (!IsRequestAllowed()) { | |
| 50 DLOG(WARNING) << "The request is rejected due to too many requests."; | |
| 51 OnComplete(request_id, net::ERR_INSUFFICIENT_RESOURCES, std::string(), | |
| 52 std::string()); | |
| 53 return; | |
| 54 } | |
| 55 | |
| 56 base::Closure cancel_callback; | |
| 57 bool success = identity_store_->RequestIdentity( | |
| 58 origin, | |
| 59 identity_name, | |
| 60 common_name, | |
| 61 base::Bind(&WebRTCIdentityServiceHost::OnComplete, | |
| 62 base::Unretained(this), | |
| 63 request_id), | |
| 64 &cancel_callback); | |
| 65 if (success) { | |
| 66 pending_request_cancel_callback_map_[request_id] = cancel_callback; | |
| 67 UpdateRequestTime(); | |
| 68 } else { | |
| 69 OnComplete(request_id, net::ERR_UNEXPECTED, std::string(), std::string()); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 void WebRTCIdentityServiceHost::OnCancelRequest(int request_id) { | |
| 74 if (pending_request_cancel_callback_map_.find(request_id) == | |
| 75 pending_request_cancel_callback_map_.end()) | |
| 76 return; | |
| 77 pending_request_cancel_callback_map_[request_id].Run(); | |
| 78 pending_request_cancel_callback_map_.erase(request_id); | |
| 79 } | |
| 80 | |
| 81 void WebRTCIdentityServiceHost::OnComplete(int request_id, | |
| 82 int error, | |
| 83 const std::string& certificate, | |
| 84 const std::string& private_key) { | |
| 85 if (pending_request_cancel_callback_map_.find(request_id) != | |
| 86 pending_request_cancel_callback_map_.end()) | |
| 87 pending_request_cancel_callback_map_.erase(request_id); | |
| 88 | |
| 89 if (error == net::OK) { | |
| 90 Send(new WebRTCIdentityHostMsg_IdentityReady( | |
| 91 request_id, certificate, private_key)); | |
| 92 } else { | |
| 93 Send(new WebRTCIdentityHostMsg_RequestFailed(request_id, error)); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 bool WebRTCIdentityServiceHost::IsRequestAllowed() { | |
| 98 if (recent_request_time_.size() < MAX_REQUESTS_PER_SECOND) | |
| 99 return true; | |
| 100 | |
| 101 base::Time now = base::Time::NowFromSystemTime(); | |
|
Ryan Sleevi
2013/06/27 00:14:55
If you continue to want a time, do not use base::T
| |
| 102 base::Time last_n_request_time = recent_request_time_.front(); | |
| 103 return (now - last_n_request_time) > base::TimeDelta::FromSeconds(1); | |
| 104 } | |
| 105 | |
| 106 void WebRTCIdentityServiceHost::UpdateRequestTime() { | |
| 107 if (recent_request_time_.size() == MAX_REQUESTS_PER_SECOND) | |
| 108 recent_request_time_.pop(); | |
| 109 recent_request_time_.push(base::Time::NowFromSystemTime()); | |
| 110 } | |
| 111 | |
| 112 } // namespace content | |
| OLD | NEW |