OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/base_paths.h" |
| 6 #include "base/bind.h" |
| 7 #include "base/logging.h" |
| 8 #include "base/path_service.h" |
| 9 #include "base/string16.h" |
| 10 #include "content/browser/hyphenator/hyphenator_message_filter.h" |
| 11 #include "content/common/hyphenator_messages.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/render_process_host.h" |
| 14 #include "content/public/common/content_paths.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 // A helper function that closes the specified file in the FILE thread. This |
| 19 // function may be called after the HyphenatorMessageFilter object that owns the |
| 20 // specified file is deleted, i.e. this function must not depend on the object. |
| 21 void CloseDictionary(base::PlatformFile file) { |
| 22 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 23 base::ClosePlatformFile(file); |
| 24 } |
| 25 |
| 26 } // namespace |
| 27 |
| 28 namespace content { |
| 29 |
| 30 HyphenatorMessageFilter::HyphenatorMessageFilter( |
| 31 content::RenderProcessHost* render_process_host) |
| 32 : render_process_host_(render_process_host), |
| 33 dictionary_file_(base::kInvalidPlatformFileValue), |
| 34 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 35 } |
| 36 |
| 37 HyphenatorMessageFilter::~HyphenatorMessageFilter() { |
| 38 // Post a FILE task that deletes the dictionary file. This message filter is |
| 39 // usually deleted on the IO thread, which does not allow file operations. |
| 40 if (dictionary_file_ != base::kInvalidPlatformFileValue) { |
| 41 content::BrowserThread::PostTask( |
| 42 BrowserThread::FILE, |
| 43 FROM_HERE, |
| 44 base::Bind(&CloseDictionary, dictionary_file_)); |
| 45 } |
| 46 } |
| 47 |
| 48 void HyphenatorMessageFilter::SetDictionaryBase(const FilePath& base) { |
| 49 dictionary_base_ = base; |
| 50 } |
| 51 |
| 52 void HyphenatorMessageFilter::OverrideThreadForMessage( |
| 53 const IPC::Message& message, |
| 54 BrowserThread::ID* thread) { |
| 55 if (message.type() == HyphenatorHostMsg_OpenDictionary::ID) |
| 56 *thread = BrowserThread::UI; |
| 57 } |
| 58 |
| 59 bool HyphenatorMessageFilter::OnMessageReceived( |
| 60 const IPC::Message& message, |
| 61 bool* message_was_ok) { |
| 62 bool handled = true; |
| 63 IPC_BEGIN_MESSAGE_MAP_EX(HyphenatorMessageFilter, |
| 64 message, |
| 65 *message_was_ok) |
| 66 IPC_MESSAGE_HANDLER(HyphenatorHostMsg_OpenDictionary, OnOpenDictionary) |
| 67 IPC_MESSAGE_UNHANDLED(handled = false) |
| 68 IPC_END_MESSAGE_MAP_EX() |
| 69 return handled; |
| 70 } |
| 71 |
| 72 void HyphenatorMessageFilter::OnOpenDictionary(const string16& locale) { |
| 73 if (dictionary_file_ != base::kInvalidPlatformFileValue) { |
| 74 SendDictionary(); |
| 75 return; |
| 76 } |
| 77 content::BrowserThread::PostTaskAndReply( |
| 78 content::BrowserThread::FILE, |
| 79 FROM_HERE, |
| 80 base::Bind(&HyphenatorMessageFilter::OpenDictionary, this, locale), |
| 81 base::Bind(&HyphenatorMessageFilter::SendDictionary, |
| 82 weak_factory_.GetWeakPtr())); |
| 83 } |
| 84 |
| 85 void HyphenatorMessageFilter::OpenDictionary(const string16& locale) { |
| 86 DCHECK(dictionary_file_ == base::kInvalidPlatformFileValue); |
| 87 |
| 88 if (dictionary_base_.empty()) |
| 89 PathService::Get(base::DIR_EXE, &dictionary_base_); |
| 90 std::string rule_file = locale.empty() ? "en-US" : UTF16ToASCII(locale); |
| 91 rule_file.append("-1-0.dic"); |
| 92 FilePath rule_path = dictionary_base_.AppendASCII(rule_file); |
| 93 dictionary_file_ = base::CreatePlatformFile( |
| 94 rule_path, |
| 95 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_OPEN, |
| 96 NULL, NULL); |
| 97 } |
| 98 |
| 99 void HyphenatorMessageFilter::SendDictionary() { |
| 100 IPC::PlatformFileForTransit file = IPC::InvalidPlatformFileForTransit(); |
| 101 if (dictionary_file_ != base::kInvalidPlatformFileValue) { |
| 102 file = IPC::GetFileHandleForProcess( |
| 103 dictionary_file_, |
| 104 render_process_host_->GetHandle(), |
| 105 false); |
| 106 } |
| 107 Send(new HyphenatorMsg_SetDictionary(file)); |
| 108 } |
| 109 |
| 110 } // namespace content |
OLD | NEW |