summaryrefslogtreecommitdiffstats
path: root/chromium/content/browser/streams/stream_context.cc
blob: 44ca0f4a1cdcb2d296a7795ac287904d164cd4f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/streams/stream_context.h"

#include "base/bind.h"
#include "content/browser/streams/stream_registry.h"
#include "content/public/browser/browser_context.h"

using base::UserDataAdapter;

namespace {
const char* kStreamContextKeyName = "content_stream_context";
}

namespace content {

StreamContext::StreamContext() {}

StreamContext* StreamContext::GetFor(BrowserContext* context) {
  if (!context->GetUserData(kStreamContextKeyName)) {
    scoped_refptr<StreamContext> stream = new StreamContext();
    context->SetUserData(kStreamContextKeyName,
                         new UserDataAdapter<StreamContext>(stream.get()));
    // Check first to avoid memory leak in unittests.
    if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
      BrowserThread::PostTask(
          BrowserThread::IO, FROM_HERE,
          base::Bind(&StreamContext::InitializeOnIOThread, stream));
    }
  }

  return UserDataAdapter<StreamContext>::Get(context, kStreamContextKeyName);
}

void StreamContext::InitializeOnIOThread() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  registry_.reset(new StreamRegistry());
}

StreamContext::~StreamContext() {}

void StreamContext::DeleteOnCorrectThread() const {
  // In many tests, there isn't a valid IO thread.  In that case, just delete on
  // the current thread.
  // TODO(zork): Remove this custom deleter, and fix the leaks in all the
  // tests.
  if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) &&
      !BrowserThread::CurrentlyOn(BrowserThread::IO)) {
    BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this);
    return;
  }
  delete this;
}

}  // namespace content