summaryrefslogtreecommitdiffstats
path: root/chrome/common/db_message_filter.cc
blob: 396ca7cc8b1fb3052f17b9b79f5f7d7f013a3585 (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
// 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 "db_message_filter.h"

#include "base/atomic_sequence_num.h"
#include "base/id_map.h"
#include "base/lock.h"
#include "base/message_loop.h"
#include "base/platform_file.h"
#include "base/task.h"
#include "chrome/common/child_process.h"
#include "chrome/common/render_messages.h"
#include "ipc/ipc_channel.h"
#include "ipc/ipc_message.h"

DBMessageFilter* DBMessageFilter::instance_ = NULL;

DBMessageFilter::DBMessageFilter()
  : io_thread_message_loop_(ChildProcess::current()->io_message_loop()),
    channel_(NULL), channel_lock_(new Lock()),
    shutdown_event_(ChildProcess::current()->GetShutDownEvent()),
    messages_awaiting_replies_(new IDMap<DBMessageState>()),
    unique_id_generator_(new base::AtomicSequenceNumber()) {
    DCHECK(!instance_);
    instance_ = this;
}

DBMessageFilter::~DBMessageFilter() {
  instance_ = NULL;
}

DBMessageFilter* DBMessageFilter::GetInstance() {
  return instance_;
}

int DBMessageFilter::GetUniqueID() {
  return unique_id_generator_->GetNext();
}

static void SendMessageOnIOThread(IPC::Message* message,
                                  IPC::Channel* channel,
                                  Lock* channel_lock) {
  AutoLock channel_auto_lock(*channel_lock);
  if (!channel) {
    delete message;
  } else {
    channel->Send(message);
  }
}

void DBMessageFilter::Send(IPC::Message* message) {
  io_thread_message_loop_->PostTask(FROM_HERE,
    NewRunnableFunction(SendMessageOnIOThread, message,
      channel_, channel_lock_.get()));
}

void DBMessageFilter::OnFilterAdded(IPC::Channel* channel) {
  AutoLock channel_auto_lock(*channel_lock_);
  channel_ = channel;
}

void DBMessageFilter::OnChannelError() {
  AutoLock channel_auto_lock(*channel_lock_);
  channel_ = NULL;
}

void DBMessageFilter::OnChannelClosing() {
  AutoLock channel_auto_lock(*channel_lock_);
  channel_ = NULL;
}

bool DBMessageFilter::OnMessageReceived(const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(DBMessageFilter, message)
    IPC_MESSAGE_HANDLER(ViewMsg_DatabaseOpenFileResponse,
                        OnResponse<ViewMsg_DatabaseOpenFileResponse_Params>)
    IPC_MESSAGE_HANDLER(ViewMsg_DatabaseDeleteFileResponse,
                        OnResponse<int>)
    IPC_MESSAGE_HANDLER(ViewMsg_DatabaseGetFileAttributesResponse,
                        OnResponse<uint32>)
    IPC_MESSAGE_HANDLER(ViewMsg_DatabaseGetFileSizeResponse,
                        OnResponse<int64>)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}