OLD | NEW |
| (Empty) |
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 "chrome/browser/media/desktop_streams_registry.h" | |
6 | |
7 #include "base/base64.h" | |
8 #include "base/location.h" | |
9 #include "base/macros.h" | |
10 #include "base/time/time.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 #include "crypto/random.h" | |
13 | |
14 namespace { | |
15 | |
16 const int kStreamIdLengthBytes = 16; | |
17 | |
18 const int kApprovedStreamTimeToLiveSeconds = 10; | |
19 | |
20 std::string GenerateRandomStreamId() { | |
21 char buffer[kStreamIdLengthBytes]; | |
22 crypto::RandBytes(buffer, arraysize(buffer)); | |
23 std::string result; | |
24 base::Base64Encode(base::StringPiece(buffer, arraysize(buffer)), | |
25 &result); | |
26 return result; | |
27 } | |
28 | |
29 } // namespace | |
30 | |
31 DesktopStreamsRegistry::DesktopStreamsRegistry() {} | |
32 DesktopStreamsRegistry::~DesktopStreamsRegistry() {} | |
33 | |
34 std::string DesktopStreamsRegistry::RegisterStream( | |
35 int render_process_id, | |
36 int render_frame_id, | |
37 const GURL& origin, | |
38 const content::DesktopMediaID& source, | |
39 const std::string& extension_name) { | |
40 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
41 | |
42 std::string id = GenerateRandomStreamId(); | |
43 DCHECK(approved_streams_.find(id) == approved_streams_.end()); | |
44 ApprovedDesktopMediaStream& stream = approved_streams_[id]; | |
45 stream.render_process_id = render_process_id; | |
46 stream.render_frame_id = render_frame_id; | |
47 stream.origin = origin; | |
48 stream.source = source; | |
49 stream.extension_name = extension_name; | |
50 | |
51 content::BrowserThread::PostDelayedTask( | |
52 content::BrowserThread::UI, FROM_HERE, | |
53 base::Bind(&DesktopStreamsRegistry::CleanupStream, | |
54 base::Unretained(this), id), | |
55 base::TimeDelta::FromSeconds(kApprovedStreamTimeToLiveSeconds)); | |
56 | |
57 return id; | |
58 } | |
59 | |
60 content::DesktopMediaID DesktopStreamsRegistry::RequestMediaForStreamId( | |
61 const std::string& id, | |
62 int render_process_id, | |
63 int render_frame_id, | |
64 const GURL& origin, | |
65 std::string* extension_name) { | |
66 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
67 | |
68 StreamsMap::iterator it = approved_streams_.find(id); | |
69 | |
70 // Verify that if there is a request with the specified ID it was created for | |
71 // the same origin and the same renderer. | |
72 if (it == approved_streams_.end() || | |
73 render_process_id != it->second.render_process_id || | |
74 render_frame_id != it->second.render_frame_id || | |
75 origin != it->second.origin) { | |
76 return content::DesktopMediaID(); | |
77 } | |
78 | |
79 content::DesktopMediaID result = it->second.source; | |
80 *extension_name = it->second.extension_name; | |
81 approved_streams_.erase(it); | |
82 return result; | |
83 } | |
84 | |
85 void DesktopStreamsRegistry::CleanupStream(const std::string& id) { | |
86 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
87 approved_streams_.erase(id); | |
88 } | |
89 | |
90 DesktopStreamsRegistry::ApprovedDesktopMediaStream::ApprovedDesktopMediaStream() | |
91 : render_process_id(-1), render_frame_id(-1) {} | |
OLD | NEW |