OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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 #ifndef CHROME_BROWSER_BOOKMARK_STORAGE_H_ | |
6 #define CHROME_BROWSER_BOOKMARK_STORAGE_H_ | |
7 | |
8 #include "base/ref_counted.h" | |
9 #include "base/task.h" | |
10 #include "chrome/browser/browser_process.h" | |
11 | |
12 class BookmarkBarModel; | |
13 class Profile; | |
14 class Value; | |
15 | |
16 // BookmarkStorage handles reading/write the bookmark bar model. The | |
17 // BookmarkBarModel uses the BookmarkStorage to load bookmarks from | |
18 // disk, as well as notifying the BookmarkStorage every time the model | |
19 // changes. | |
20 // | |
21 // Internally BookmarkStorage uses BookmarkCodec to do the actual read/write. | |
22 | |
23 class BookmarkStorage : public base::RefCountedThreadSafe<BookmarkStorage> { | |
24 friend class BookmarkStorageBackend; | |
25 | |
26 public: | |
27 // Creates a BookmarkStorage for the specified model | |
28 BookmarkStorage(Profile* profile, BookmarkBarModel* model); | |
29 | |
30 // Loads the bookmarks into the model, notifying the model when done. If | |
31 // load_from_history is true, the bookmarks are loaded from the file written | |
32 // by history (StarredURLDatabase). | |
33 void LoadBookmarks(bool load_from_history); | |
34 | |
35 // Schedules saving the bookmark bar model to disk. | |
36 void ScheduleSave(); | |
37 | |
38 // Notification the bookmark bar model is going to be deleted. If there is | |
39 // a pending save, it is saved immediately. | |
40 void BookmarkModelDeleted(); | |
41 | |
42 private: | |
43 // Callback from backend with the results of the bookmark file. | |
44 void LoadedBookmarks(Value* root_value, | |
45 bool bookmark_file_exists, | |
46 bool loaded_from_history); | |
47 | |
48 // Schedules a save on the backend thread. | |
49 void SaveNow(); | |
50 | |
51 // Returns the thread the backend is run on. | |
52 base::Thread* backend_thread() const { return backend_thread_; } | |
53 | |
54 // The model. The model is NULL once BookmarkModelDeleted has been invoked. | |
55 BookmarkBarModel* model_; | |
56 | |
57 // Used to delay saves. | |
58 ScopedRunnableMethodFactory<BookmarkStorage> save_factory_; | |
59 | |
60 // The backend handles actual reading/writing to disk. | |
61 scoped_refptr<BookmarkStorageBackend> backend_; | |
62 | |
63 // Thread read/writing is run on. This comes from the profile, and is null | |
64 // during testing. | |
65 base::Thread* backend_thread_; | |
66 | |
67 DISALLOW_COPY_AND_ASSIGN(BookmarkStorage); | |
68 }; | |
69 | |
70 // Used to save the bookmarks on the file thread. | |
71 class BookmarkStorageBackend : | |
72 public base::RefCountedThreadSafe<BookmarkStorageBackend> { | |
73 public: | |
74 explicit BookmarkStorageBackend(const std::wstring& path, | |
75 const std::wstring& tmp_histor_path); | |
76 | |
77 // Writes the specified value to disk. This takes ownership of |value| and | |
78 // deletes it when done. | |
79 void Write(Value* value); | |
80 | |
81 // Reads the bookmarks from kBookmarksFileName. Notifies |service| with | |
82 // the results on the specified MessageLoop. | |
83 void Read(scoped_refptr<BookmarkStorage> service, | |
84 MessageLoop* message_loop, | |
85 bool load_from_history); | |
86 | |
87 private: | |
88 // Path we read/write to. | |
89 const std::wstring path_; | |
90 | |
91 // Path bookmarks are read from if asked to load from history file. | |
92 const std::wstring tmp_history_path_; | |
93 | |
94 DISALLOW_EVIL_CONSTRUCTORS(BookmarkStorageBackend); | |
95 }; | |
96 | |
97 #endif // CHROME_BROWSER_BOOKMARK_STORAGE_H_ | |
98 | |
OLD | NEW |