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.
5 #include "chrome/browser/media/desktop_streams_registry.h"
7 #include "base/base64.h"
8 #include "base/location.h"
9 #include "base/time/time.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "crypto/random.h"
15 const int kStreamIdLengthBytes
= 16;
17 const int kApprovedStreamTimeToLiveSeconds
= 10;
19 std::string
GenerateRandomStreamId() {
20 char buffer
[kStreamIdLengthBytes
];
21 crypto::RandBytes(buffer
, arraysize(buffer
));
23 base::Base64Encode(base::StringPiece(buffer
, arraysize(buffer
)),
30 DesktopStreamsRegistry::DesktopStreamsRegistry() {}
31 DesktopStreamsRegistry::~DesktopStreamsRegistry() {}
33 std::string
DesktopStreamsRegistry::RegisterStream(
34 int render_process_id
,
37 const content::DesktopMediaID
& source
) {
38 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
40 std::string id
= GenerateRandomStreamId();
41 ApprovedDesktopMediaStream
& stream
= approved_streams_
[id
];
42 stream
.render_process_id
= render_process_id
;
43 stream
.render_view_id
= render_view_id
;
44 stream
.origin
= origin
;
45 stream
.source
= source
;
47 content::BrowserThread::PostDelayedTask(
48 content::BrowserThread::UI
, FROM_HERE
,
49 base::Bind(&DesktopStreamsRegistry::CleanupStream
,
50 base::Unretained(this), id
),
51 base::TimeDelta::FromSeconds(kApprovedStreamTimeToLiveSeconds
));
56 content::DesktopMediaID
DesktopStreamsRegistry::RequestMediaForStreamId(
57 const std::string
& id
,
58 int render_process_id
,
61 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
63 StreamsMap::iterator it
= approved_streams_
.find(id
);
65 // Verify that if there is a request with the specified ID it was created for
66 // the same origin and the same renderer.
67 if (it
== approved_streams_
.end() ||
68 render_process_id
!= it
->second
.render_process_id
||
69 render_view_id
!= it
->second
.render_view_id
||
70 origin
!= it
->second
.origin
) {
71 return content::DesktopMediaID();
74 content::DesktopMediaID result
= it
->second
.source
;
75 approved_streams_
.erase(it
);
79 void DesktopStreamsRegistry::CleanupStream(const std::string
& id
) {
80 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
81 approved_streams_
.erase(id
);