summaryrefslogtreecommitdiffstats
path: root/content/test/test_browser_thread.cc
blob: 849e394335c05a627685772428ffc70e1109f280 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2011 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/public/test/test_browser_thread.h"

#include "base/message_loop.h"
#include "base/threading/thread.h"
#include "content/browser/browser_thread_impl.h"
#include "content/browser/notification_service_impl.h"

namespace content {

// This gives access to set_message_loop().
class TestBrowserThreadImpl : public BrowserThreadImpl {
 public:
  explicit TestBrowserThreadImpl(BrowserThread::ID identifier)
      : BrowserThreadImpl(identifier),
        notification_service_(NULL) {
  }

  TestBrowserThreadImpl(BrowserThread::ID identifier,
                        MessageLoop* message_loop)
      : BrowserThreadImpl(identifier, message_loop),
        notification_service_(NULL) {
  }

  virtual ~TestBrowserThreadImpl() {
    Stop();
  }

  void set_message_loop(MessageLoop* loop) {
    Thread::set_message_loop(loop);
  }

  virtual void Init() OVERRIDE {
    notification_service_ = new NotificationServiceImpl;
    BrowserThreadImpl::Init();
  }

  virtual void CleanUp() OVERRIDE {
    delete notification_service_;
    notification_service_ = NULL;
    BrowserThreadImpl::CleanUp();
  }

 private:
  NotificationService* notification_service_;
  DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadImpl);
};

TestBrowserThread::TestBrowserThread(BrowserThread::ID identifier)
    : impl_(new TestBrowserThreadImpl(identifier)) {
}

TestBrowserThread::TestBrowserThread(BrowserThread::ID identifier,
                                     MessageLoop* message_loop)
    : impl_(new TestBrowserThreadImpl(identifier, message_loop)) {
}

TestBrowserThread::~TestBrowserThread() {
  Stop();
}

bool TestBrowserThread::Start() {
  return impl_->Start();
}

bool TestBrowserThread::StartIOThread() {
  base::Thread::Options options;
  options.message_loop_type = MessageLoop::TYPE_IO;
  return impl_->StartWithOptions(options);
}

void TestBrowserThread::Stop() {
  impl_->Stop();
}

bool TestBrowserThread::IsRunning() {
  return impl_->IsRunning();
}

base::Thread* TestBrowserThread::DeprecatedGetThreadObject() {
  return impl_.get();
}

void TestBrowserThread::DeprecatedSetMessageLoop(MessageLoop* loop) {
  impl_->set_message_loop(loop);
}

}  // namespace content