Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / streams / stream_context.cc
blob357e68cae5888e8e463ebd80fb998804a962e5f8
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.
5 #include "content/browser/streams/stream_context.h"
7 #include "base/bind.h"
8 #include "content/browser/streams/stream_registry.h"
9 #include "content/public/browser/browser_context.h"
10 #include "content/public/browser/browser_thread.h"
12 using base::UserDataAdapter;
14 namespace {
15 const char* kStreamContextKeyName = "content_stream_context";
18 namespace content {
20 StreamContext::StreamContext() {}
22 StreamContext* StreamContext::GetFor(BrowserContext* context) {
23 if (!context->GetUserData(kStreamContextKeyName)) {
24 scoped_refptr<StreamContext> stream = new StreamContext();
25 context->SetUserData(kStreamContextKeyName,
26 new UserDataAdapter<StreamContext>(stream.get()));
27 // Check first to avoid memory leak in unittests.
28 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
29 BrowserThread::PostTask(
30 BrowserThread::IO, FROM_HERE,
31 base::Bind(&StreamContext::InitializeOnIOThread, stream));
35 return UserDataAdapter<StreamContext>::Get(context, kStreamContextKeyName);
38 void StreamContext::InitializeOnIOThread() {
39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
40 registry_.reset(new StreamRegistry());
43 StreamContext::~StreamContext() {}
45 void StreamContext::DeleteOnCorrectThread() const {
46 // In many tests, there isn't a valid IO thread. In that case, just delete on
47 // the current thread.
48 // TODO(zork): Remove this custom deleter, and fix the leaks in all the
49 // tests.
50 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) &&
51 !BrowserThread::CurrentlyOn(BrowserThread::IO)) {
52 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this);
53 return;
55 delete this;
58 } // namespace content