summaryrefslogtreecommitdiffstats
path: root/chrome/browser/bookmark_storage.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chrome/browser/bookmark_storage.cc')
-rw-r--r--chrome/browser/bookmark_storage.cc172
1 files changed, 91 insertions, 81 deletions
diff --git a/chrome/browser/bookmark_storage.cc b/chrome/browser/bookmark_storage.cc
index e1fed29..8c402b8 100644
--- a/chrome/browser/bookmark_storage.cc
+++ b/chrome/browser/bookmark_storage.cc
@@ -39,6 +39,7 @@
#include "chrome/browser/bookmark_bar_model.h"
#include "chrome/browser/bookmark_codec.h"
#include "chrome/browser/profile.h"
+#include "chrome/common/chrome_constants.h"
#include "chrome/common/json_value_serializer.h"
namespace {
@@ -50,90 +51,36 @@ const wchar_t* const kBackupExtension = L"bak";
// kBookmarksFileName.
const wchar_t* const kTmpExtension = L"tmp";
-// Name of file containing bookmarks.
-const wchar_t* const kBookmarksFileName = L"bookmarks.json";
-
// How often we save.
-static const int kSaveDelayMS = 2500;
-
-class BookmarkStorageBackend :
- public base::RefCountedThreadSafe<BookmarkStorageBackend> {
- public:
- explicit BookmarkStorageBackend(const std::wstring& path);
-
- // Writes the specified value to disk. This takes ownership of |value| and
- // deletes it when done.
- void Write(Value* value);
-
- // Reads the bookmarks from kBookmarksFileName. Notifies |service| with
- // the results on the specified MessageLoop.
- void Read(scoped_refptr<BookmarkStorage> service,
- MessageLoop* message_loop);
-
- private:
- // Path we read/write to.
- const std::wstring path_;
-
- DISALLOW_EVIL_CONSTRUCTORS(BookmarkStorageBackend);
-};
-
-BookmarkStorageBackend::BookmarkStorageBackend(const std::wstring& path)
- : path_(path) {
- // Make a backup of the current file.
- std::wstring backup_path = path;
- file_util::ReplaceExtension(&backup_path, kBackupExtension);
- file_util::CopyFile(path, backup_path);
-}
-
-void BookmarkStorageBackend::Write(Value* value) {
- DCHECK(value);
-
- // We own Value.
- scoped_ptr<Value> value_ref(value);
-
- std::string content;
- JSONWriter::Write(value, true, &content);
-
- // Write to a temp file, then rename.
- std::wstring tmp_file = path_;
- file_util::ReplaceExtension(&tmp_file, kTmpExtension);
-
- int bytes_written = file_util::WriteFile(tmp_file, content.c_str(),
- static_cast<int>(content.length()));
- if (bytes_written != -1) {
- MoveFileEx(tmp_file.c_str(), path_.c_str(),
- MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING);
- }
-}
-
-void BookmarkStorageBackend::Read(scoped_refptr<BookmarkStorage> service,
- MessageLoop* message_loop) {
- JSONFileValueSerializer serializer(path_);
- Value* root = NULL;
- serializer.Deserialize(&root);
-
- // BookmarkStorage takes ownership of root.
- message_loop->PostTask(FROM_HERE, NewRunnableMethod(
- service.get(), &BookmarkStorage::LoadedBookmarks, root));
-}
+const int kSaveDelayMS = 2500;
} // namespace
+// BookmarkStorage -------------------------------------------------------------
+
BookmarkStorage::BookmarkStorage(Profile* profile, BookmarkBarModel* model)
: model_(model),
#pragma warning(suppress: 4355) // Okay to pass "this" here.
- save_factory_(this) {
+ save_factory_(this),
+ backend_thread_(g_browser_process->file_thread()) {
std::wstring path = profile->GetPath();
- file_util::AppendToPath(&path, kBookmarksFileName);
- backend_ = new BookmarkStorageBackend(path);
+ file_util::AppendToPath(&path, chrome::kBookmarksFileName);
+ std::wstring tmp_history_path = profile->GetPath();
+ file_util::AppendToPath(&tmp_history_path, chrome::kHistoryBookmarksFileName);
+ backend_ = new BookmarkStorageBackend(path, tmp_history_path);
}
-void BookmarkStorage::LoadBookmarks() {
- backend_thread()->message_loop()->PostTask(
- FROM_HERE,
- NewRunnableMethod(backend_.get(), &BookmarkStorageBackend::Read,
- scoped_refptr<BookmarkStorage>(this),
- MessageLoop::current()));
+void BookmarkStorage::LoadBookmarks(bool load_from_history) {
+ if (!backend_thread()) {
+ backend_->Read(scoped_refptr<BookmarkStorage>(this), NULL,
+ load_from_history);
+ } else {
+ backend_thread()->message_loop()->PostTask(
+ FROM_HERE,
+ NewRunnableMethod(backend_.get(), &BookmarkStorageBackend::Read,
+ scoped_refptr<BookmarkStorage>(this),
+ MessageLoop::current(), load_from_history));
+ }
}
void BookmarkStorage::ScheduleSave() {
@@ -154,7 +101,9 @@ void BookmarkStorage::BookmarkModelDeleted() {
model_ = NULL;
}
-void BookmarkStorage::LoadedBookmarks(Value* root_value) {
+void BookmarkStorage::LoadedBookmarks(Value* root_value,
+ bool bookmark_file_exists,
+ bool loaded_from_history) {
scoped_ptr<Value> value_ref(root_value);
if (model_) {
@@ -162,8 +111,8 @@ void BookmarkStorage::LoadedBookmarks(Value* root_value) {
BookmarkCodec codec;
codec.Decode(model_, *root_value);
}
- // TODO(sky): bug 1256202 need to invoke a method back on the model telling
- // it all has loaded.
+ model_->OnBookmarkStorageLoadedBookmarks(bookmark_file_exists,
+ loaded_from_history);
}
}
@@ -178,8 +127,69 @@ void BookmarkStorage::SaveNow() {
BookmarkCodec codec;
Value* value = codec.Encode(model_);
// The backend deletes value in write.
- backend_thread()->message_loop()->PostTask(
- FROM_HERE,
- NewRunnableMethod(backend_.get(), &BookmarkStorageBackend::Write,
- value));
+ if (!backend_thread()) {
+ backend_->Write(value);
+ } else {
+ backend_thread()->message_loop()->PostTask(
+ FROM_HERE,
+ NewRunnableMethod(backend_.get(), &BookmarkStorageBackend::Write,
+ value));
+ }
+}
+
+// BookmarkStorageBackend ------------------------------------------------------
+
+BookmarkStorageBackend::BookmarkStorageBackend(
+ const std::wstring& path,
+ const std::wstring& tmp_history_path)
+ : path_(path),
+ tmp_history_path_(tmp_history_path) {
+ // Make a backup of the current file.
+ std::wstring backup_path = path;
+ file_util::ReplaceExtension(&backup_path, kBackupExtension);
+ file_util::CopyFile(path, backup_path);
+}
+
+void BookmarkStorageBackend::Write(Value* value) {
+ DCHECK(value);
+
+ // We own Value.
+ scoped_ptr<Value> value_ref(value);
+
+ std::string content;
+ JSONWriter::Write(value, true, &content);
+
+ // Write to a temp file, then rename.
+ std::wstring tmp_file = path_;
+ file_util::ReplaceExtension(&tmp_file, kTmpExtension);
+
+ int bytes_written = file_util::WriteFile(tmp_file, content.c_str(),
+ static_cast<int>(content.length()));
+ if (bytes_written != -1) {
+ MoveFileEx(tmp_file.c_str(), path_.c_str(),
+ MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING);
+ // Nuke the history file so that we don't attempt to load from it again.
+ file_util::Delete(tmp_history_path_, false);
+ }
+}
+
+void BookmarkStorageBackend::Read(scoped_refptr<BookmarkStorage> service,
+ MessageLoop* message_loop,
+ bool load_from_history) {
+ const std::wstring& path = load_from_history ? tmp_history_path_ : path_;
+ bool bookmark_file_exists = file_util::PathExists(path);
+ Value* root = NULL;
+ if (bookmark_file_exists) {
+ JSONFileValueSerializer serializer(path);
+ serializer.Deserialize(&root);
+ }
+
+ // BookmarkStorage takes ownership of root.
+ if (message_loop) {
+ message_loop->PostTask(FROM_HERE, NewRunnableMethod(
+ service.get(), &BookmarkStorage::LoadedBookmarks, root,
+ bookmark_file_exists, load_from_history));
+ } else {
+ service->LoadedBookmarks(root, bookmark_file_exists, load_from_history);
+ }
}