summaryrefslogtreecommitdiffstats
path: root/chrome/browser/in_process_webkit/webkit_thread.cc
blob: 55772c6c6c88dea3cdec243b246dab19e636b0d7 (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
// Copyright (c) 2009 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 "chrome/browser/in_process_webkit/webkit_thread.h"

#include "chrome/browser/in_process_webkit/browser_webkitclient_impl.h"
#include "webkit/api/public/WebKit.h"

base::LazyInstance<Lock> WebKitThread::global_webkit_lock_(
    base::LINKER_INITIALIZED);
int WebKitThread::global_webkit_ref_count_ = 0;
WebKitThread::InternalWebKitThread* WebKitThread::global_webkit_thread_ = NULL;

WebKitThread::WebKitThread()
    : cached_webkit_thread_(NULL) {
  // The thread is started lazily by InitializeThread().
}

WebKitThread::InternalWebKitThread::InternalWebKitThread()
    : base::Thread("WebKit"),
      webkit_client_(NULL) {
}

void WebKitThread::InternalWebKitThread::Init() {
  DCHECK(!webkit_client_);
  webkit_client_ = new BrowserWebKitClientImpl;
  DCHECK(webkit_client_);
  WebKit::initialize(webkit_client_);
  // Don't do anything heavyweight here since this can block the IO thread from
  // executing (since InitializeThread() is often called on the IO thread).
}

void WebKitThread::InternalWebKitThread::CleanUp() {
  DCHECK(webkit_client_);
  WebKit::shutdown();
  delete webkit_client_;
  webkit_client_ = NULL;
}

WebKitThread::~WebKitThread() {
  AutoLock lock(global_webkit_lock_.Get());
  if (cached_webkit_thread_) {
    DCHECK(global_webkit_ref_count_ > 0);
    if (--global_webkit_ref_count_ == 0) {
      // TODO(jorlow): Make this safe.
      DCHECK(MessageLoop::current() != global_webkit_thread_->message_loop());
      global_webkit_thread_->Stop();
      delete global_webkit_thread_;
      global_webkit_thread_ = NULL;
    }
  }
}

void WebKitThread::InitializeThread() {
  AutoLock lock(global_webkit_lock_.Get());
  if (!cached_webkit_thread_) {
    if (!global_webkit_thread_) {
      global_webkit_thread_ = new InternalWebKitThread;
      DCHECK(global_webkit_thread_);
      bool started = global_webkit_thread_->Start();
      DCHECK(started);
    }
    ++global_webkit_ref_count_;
    // The cached version can be accessed outside of global_webkit_lock_.
    cached_webkit_thread_ = global_webkit_thread_;
  }
  DCHECK(cached_webkit_thread_->IsRunning());
}