summaryrefslogtreecommitdiffstats
path: root/chrome/browser/mime_registry_dispatcher.cc
blob: bc8653f03bed91448ae3d8917e54e48e3150359f (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
92
93
94
95
96
// Copyright (c) 2010 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/mime_registry_dispatcher.h"

#include "chrome/browser/browser_thread.h"
#include "chrome/common/render_messages.h"
#include "net/base/mime_util.h"

MimeRegistryDispatcher::MimeRegistryDispatcher(IPC::Message::Sender* sender)
    : message_sender_(sender) {
  DCHECK(message_sender_);
}

MimeRegistryDispatcher::~MimeRegistryDispatcher() {
}

void MimeRegistryDispatcher::Shutdown() {
  message_sender_ = NULL;
}

bool MimeRegistryDispatcher::OnMessageReceived(const IPC::Message& message) {
  // On Windows MIME registry requests may access the Windows Registry so
  // they need to run on the FILE thread.
  if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
    // Return false if the message is not for us.
    if (message.type() != ViewHostMsg_GetMimeTypeFromExtension::ID &&
        message.type() != ViewHostMsg_GetMimeTypeFromFile::ID &&
        message.type() != ViewHostMsg_GetPreferredExtensionForMimeType::ID)
     return false;

    BrowserThread::PostTask(
        BrowserThread::FILE, FROM_HERE,
        NewRunnableMethod(
            this, &MimeRegistryDispatcher::OnMessageReceived, message));
    return true;
  }
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));

  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(MimeRegistryDispatcher, message)
    IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_GetMimeTypeFromExtension,
                                    OnGetMimeTypeFromExtension)
    IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_GetMimeTypeFromFile,
                                    OnGetMimeTypeFromFile)
    IPC_MESSAGE_HANDLER_DELAY_REPLY(
        ViewHostMsg_GetPreferredExtensionForMimeType,
        OnGetPreferredExtensionForMimeType)
    IPC_MESSAGE_UNHANDLED((handled = false))
  IPC_END_MESSAGE_MAP()
  return handled;
}

void MimeRegistryDispatcher::OnGetMimeTypeFromExtension(
    const FilePath::StringType& ext, IPC::Message* reply_msg) {
  std::string mime_type;
  net::GetMimeTypeFromExtension(ext, &mime_type);
  ViewHostMsg_GetMimeTypeFromExtension::WriteReplyParams(reply_msg, mime_type);
  Send(reply_msg);
}

void MimeRegistryDispatcher::OnGetMimeTypeFromFile(
    const FilePath& file_path, IPC::Message* reply_msg) {
  std::string mime_type;
  net::GetMimeTypeFromFile(file_path, &mime_type);
  ViewHostMsg_GetMimeTypeFromFile::WriteReplyParams(reply_msg, mime_type);
  Send(reply_msg);
}

void MimeRegistryDispatcher::OnGetPreferredExtensionForMimeType(
    const std::string& mime_type, IPC::Message* reply_msg) {
  FilePath::StringType ext;
  net::GetPreferredExtensionForMimeType(mime_type, &ext);
  ViewHostMsg_GetPreferredExtensionForMimeType::WriteReplyParams(
      reply_msg, ext);
  Send(reply_msg);
}

void MimeRegistryDispatcher::Send(IPC::Message* message) {
  if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
    if (!BrowserThread::PostTask(
            BrowserThread::IO, FROM_HERE, NewRunnableMethod(
                this, &MimeRegistryDispatcher::Send, message))) {
      // The IO thread is dead.
      delete message;
    }
    return;
  }

  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  if (message_sender_)
    message_sender_->Send(message);
  else
    delete message;
}