summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
authorestade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-16 22:55:17 +0000
committerestade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-16 22:55:17 +0000
commitcb6037d67847a6341eea166baf1d4051667a0b88 (patch)
tree2b766762057e96d758385b96e2a576d7b6690d54 /chrome/browser
parentf9be34d622561d5599603b41d1c43d595e503426 (diff)
downloadchromium_src-cb6037d67847a6341eea166baf1d4051667a0b88.zip
chromium_src-cb6037d67847a6341eea166baf1d4051667a0b88.tar.gz
chromium_src-cb6037d67847a6341eea166baf1d4051667a0b88.tar.bz2
reland 31875. Revert was:
------ Revert 31875 to see whether it fixes reliability bot. BUG=25677 TEST=None ------ TBR=huanr Review URL: http://codereview.chromium.org/397017 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@32112 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r--chrome/browser/profile.cc4
-rw-r--r--chrome/browser/renderer_host/browser_render_process_host.cc15
-rw-r--r--chrome/browser/spellcheck_host.cc84
-rw-r--r--chrome/browser/spellcheck_host.h22
4 files changed, 90 insertions, 35 deletions
diff --git a/chrome/browser/profile.cc b/chrome/browser/profile.cc
index 75ba5c0..269bf95 100644
--- a/chrome/browser/profile.cc
+++ b/chrome/browser/profile.cc
@@ -1334,8 +1334,8 @@ void ProfileImpl::ReinitializeSpellCheckHost(bool force) {
}
void ProfileImpl::SpellCheckHostInitialized() {
- spellcheck_host_ready_ =
- spellcheck_host_ && spellcheck_host_->bdict_fd().fd != -1;
+ spellcheck_host_ready_ = spellcheck_host_ &&
+ spellcheck_host_->bdict_file() != base::kInvalidPlatformFileValue;
NotificationService::current()->Notify(
NotificationType::SPELLCHECK_HOST_REINITIALIZED,
Source<Profile>(this), NotificationService::NoDetails());
diff --git a/chrome/browser/renderer_host/browser_render_process_host.cc b/chrome/browser/renderer_host/browser_render_process_host.cc
index 0face77..200fecf 100644
--- a/chrome/browser/renderer_host/browser_render_process_host.cc
+++ b/chrome/browser/renderer_host/browser_render_process_host.cc
@@ -57,6 +57,7 @@
#include "grit/generated_resources.h"
#include "ipc/ipc_logging.h"
#include "ipc/ipc_message.h"
+#include "ipc/ipc_platform_file.h"
#include "ipc/ipc_switches.h"
#if defined(OS_WIN)
@@ -1136,13 +1137,23 @@ void BrowserRenderProcessHost::InitSpellChecker() {
SpellCheckHost* spellcheck_host = profile()->GetSpellCheckHost();
if (spellcheck_host) {
PrefService* prefs = profile()->GetPrefs();
+ IPC::PlatformFileForTransit file;
+#if defined(OS_POSIX)
+ file = base::FileDescriptor(spellcheck_host->bdict_file(), false);
+#elif defined(OS_WIN)
+ ::DuplicateHandle(::GetCurrentProcess(), spellcheck_host->bdict_file(),
+ GetHandle(), &file, 0, false, DUPLICATE_SAME_ACCESS);
+#endif
Send(new ViewMsg_SpellChecker_Init(
- spellcheck_host->bdict_fd(), spellcheck_host->custom_words(),
+ file,
+ spellcheck_host->custom_words(),
spellcheck_host->language(),
prefs->GetBoolean(prefs::kEnableAutoSpellCorrect)));
} else {
Send(new ViewMsg_SpellChecker_Init(
- base::FileDescriptor(), std::vector<std::string>(), std::string(),
+ IPC::PlatformFileForTransit(),
+ std::vector<std::string>(),
+ std::string(),
false));
}
}
diff --git a/chrome/browser/spellcheck_host.cc b/chrome/browser/spellcheck_host.cc
index 6e77e19..e276473 100644
--- a/chrome/browser/spellcheck_host.cc
+++ b/chrome/browser/spellcheck_host.cc
@@ -121,6 +121,18 @@ FilePath GetVersionedFileName(const std::string& input_language,
return dict_dir.AppendASCII(versioned_bdict_file_name);
}
+FilePath GetFirstChoiceFilePath(const std::string& language) {
+ FilePath dict_dir;
+ PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir);
+ return GetVersionedFileName(language, dict_dir);
+}
+
+FilePath GetFallbackFilePath(const FilePath& first_choice) {
+ FilePath dict_dir;
+ PathService::Get(chrome::DIR_USER_DATA, &dict_dir);
+ return dict_dir.Append(first_choice.BaseName());
+}
+
} // namespace
// Constructed on UI thread.
@@ -129,31 +141,28 @@ SpellCheckHost::SpellCheckHost(Observer* observer,
URLRequestContextGetter* request_context_getter)
: observer_(observer),
language_(language),
+ file_(base::kInvalidPlatformFileValue),
tried_to_download_(false),
request_context_getter_(request_context_getter) {
DCHECK(observer_);
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
- // TODO(estade): for Windows, we need to fall back to DIR_USER_DATA if
- // DIR_APP_DICTIONARIES is not writeable.
- FilePath dict_dir;
- PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir);
- bdict_file_ = GetVersionedFileName(language, dict_dir);
-
FilePath personal_file_directory;
PathService::Get(chrome::DIR_USER_DATA, &personal_file_directory);
custom_dictionary_file_ =
personal_file_directory.Append(chrome::kCustomDictionaryFileName);
+
+ bdict_file_path_ = GetFirstChoiceFilePath(language);
}
SpellCheckHost::~SpellCheckHost() {
- if (fd_.fd != -1)
- close(fd_.fd);
+ if (file_ != base::kInvalidPlatformFileValue)
+ base::ClosePlatformFile(file_);
}
void SpellCheckHost::Initialize() {
ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE,
- NewRunnableMethod(this, &SpellCheckHost::InitializeInternal));
+ NewRunnableMethod(this, &SpellCheckHost::InitializeDictionaryLocation));
}
void SpellCheckHost::UnsetObserver() {
@@ -174,24 +183,39 @@ void SpellCheckHost::AddWord(const std::string& word) {
Source<SpellCheckHost>(this), NotificationService::NoDetails());
}
+void SpellCheckHost::InitializeDictionaryLocation() {
+ DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
+
+#if defined(OS_WIN)
+ // Check if the dictionary exists in the fallback location. If so, use it
+ // rather than downloading anew.
+ FilePath fallback = GetFallbackFilePath(bdict_file_path_);
+ if (!file_util::PathExists(bdict_file_path_) &&
+ file_util::PathExists(fallback)) {
+ bdict_file_path_ = fallback;
+ }
+#endif
+
+ InitializeInternal();
+}
+
void SpellCheckHost::InitializeInternal() {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
if (!observer_)
return;
- // We set |auto_close| to false because we don't want IPC to close the fd.
- // We will close it manually in the destructor.
- fd_ = base::FileDescriptor(open(bdict_file_.value().c_str(), O_RDONLY),
- false);
+ file_ = base::CreatePlatformFile(bdict_file_path_,
+ base::PLATFORM_FILE_READ | base::PLATFORM_FILE_OPEN,
+ NULL);
// File didn't exist. Download it.
- if (fd_.fd == -1 && !tried_to_download_) {
+ if (file_ == base::kInvalidPlatformFileValue && !tried_to_download_) {
DownloadDictionary();
return;
}
- if (fd_.fd != -1) {
+ if (file_ != base::kInvalidPlatformFileValue) {
// Load custom dictionary.
std::string contents;
file_util::ReadFileToString(custom_dictionary_file_, &contents);
@@ -220,7 +244,7 @@ void SpellCheckHost::DownloadDictionary() {
static const char kDownloadServerUrl[] =
"http://cache.pack.google.com/edgedl/chrome/dict/";
GURL url = GURL(std::string(kDownloadServerUrl) + WideToUTF8(
- l10n_util::ToLower(bdict_file_.BaseName().ToWStringHack())));
+ l10n_util::ToLower(bdict_file_path_.BaseName().ToWStringHack())));
fetcher_.reset(new URLFetcher(url, URLFetcher::GET, this));
fetcher_->set_request_context(request_context_getter_.get());
tried_to_download_ = true;
@@ -265,15 +289,27 @@ void SpellCheckHost::OnURLFetchComplete(const URLFetcher* source,
}
size_t bytes_written =
- file_util::WriteFile(bdict_file_, data.data(), data.length());
+ file_util::WriteFile(bdict_file_path_, data.data(), data.length());
if (bytes_written != data.length()) {
- LOG(ERROR) << "Failure to save dictionary.";
- // To avoid trying to load a partially saved dictionary, shortcut the
- // Initialize() call.
- ChromeThread::PostTask(ChromeThread::UI, FROM_HERE,
- NewRunnableMethod(this,
- &SpellCheckHost::InformObserverOfInitialization));
- return;
+ bool success = false;
+#if defined(OS_WIN)
+ bdict_file_path_ = GetFallbackFilePath(bdict_file_path_);
+ bytes_written =
+ file_util::WriteFile(GetFallbackFilePath(bdict_file_path_),
+ data.data(), data.length());
+ if (bytes_written == data.length())
+ success = true;
+#endif
+
+ if (!success) {
+ LOG(ERROR) << "Failure to save dictionary.";
+ // To avoid trying to load a partially saved dictionary, shortcut the
+ // Initialize() call.
+ ChromeThread::PostTask(ChromeThread::UI, FROM_HERE,
+ NewRunnableMethod(this,
+ &SpellCheckHost::InformObserverOfInitialization));
+ return;
+ }
}
Initialize();
diff --git a/chrome/browser/spellcheck_host.h b/chrome/browser/spellcheck_host.h
index ac540f1..57bacdd 100644
--- a/chrome/browser/spellcheck_host.h
+++ b/chrome/browser/spellcheck_host.h
@@ -8,8 +8,8 @@
#include <string>
#include <vector>
-#include "base/file_descriptor_posix.h"
#include "base/file_path.h"
+#include "base/platform_file.h"
#include "base/ref_counted.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/net/url_fetcher.h"
@@ -37,7 +37,7 @@ class SpellCheckHost : public base::RefCountedThreadSafe<SpellCheckHost,
// update.
void AddWord(const std::string& word);
- const base::FileDescriptor& bdict_fd() const { return fd_; };
+ const base::PlatformFile& bdict_file() const { return file_; }
const std::vector<std::string>& custom_words() const { return custom_words_; }
@@ -52,6 +52,14 @@ class SpellCheckHost : public base::RefCountedThreadSafe<SpellCheckHost,
virtual ~SpellCheckHost();
+ // Figure out the location for the dictionary. This is only non-trivial for
+ // Windows:
+ // The default place whether the spellcheck dictionary can reside is
+ // chrome::DIR_APP_DICTIONARIES. However, for systemwide installations,
+ // this directory may not have permissions for download. In that case, the
+ // alternate directory for download is chrome::DIR_USER_DATA.
+ void InitializeDictionaryLocation();
+
// Load and parse the custom words dictionary and open the bdic file.
// Executed on the file thread.
void InitializeInternal();
@@ -66,7 +74,7 @@ class SpellCheckHost : public base::RefCountedThreadSafe<SpellCheckHost,
void WriteWordToCustomDictionary(const std::string& word);
// URLFetcher::Delegate implementation. Called when we finish downloading the
- // spellcheck dictionary; saves the dictionary to disk.
+ // spellcheck dictionary; saves the dictionary to |data_|.
virtual void OnURLFetchComplete(const URLFetcher* source,
const GURL& url,
const URLRequestStatus& status,
@@ -77,8 +85,8 @@ class SpellCheckHost : public base::RefCountedThreadSafe<SpellCheckHost,
// May be NULL.
Observer* observer_;
- // The desired location of the dictionary file (whether or not it exists yet).
- FilePath bdict_file_;
+ // The desired location of the dictionary file (whether or not t exists yet).
+ FilePath bdict_file_path_;
// The location of the custom words file.
FilePath custom_dictionary_file_;
@@ -86,8 +94,8 @@ class SpellCheckHost : public base::RefCountedThreadSafe<SpellCheckHost,
// The language of the dictionary file.
std::string language_;
- // On POSIX, the file descriptor for the dictionary file.
- base::FileDescriptor fd_;
+ // The file descriptor/handle for the dictionary file.
+ base::PlatformFile file_;
// In-memory cache of the custom words file.
std::vector<std::string> custom_words_;