Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: chrome/browser/bookmarks/bookmark_html_writer.cc

Issue 10701050: net: Implement canceling of all async operations in FileStream. (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/bookmarks/bookmark_html_writer.h" 5 #include "chrome/browser/bookmarks/bookmark_html_writer.h"
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/bind_helpers.h" 9 #include "base/bind_helpers.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 // Class responsible for the actual writing. Takes ownership of favicons_map. 90 // Class responsible for the actual writing. Takes ownership of favicons_map.
91 class Writer : public base::RefCountedThreadSafe<Writer> { 91 class Writer : public base::RefCountedThreadSafe<Writer> {
92 public: 92 public:
93 Writer(base::Value* bookmarks, 93 Writer(base::Value* bookmarks,
94 const FilePath& path, 94 const FilePath& path,
95 BookmarkFaviconFetcher::URLFaviconMap* favicons_map, 95 BookmarkFaviconFetcher::URLFaviconMap* favicons_map,
96 BookmarksExportObserver* observer) 96 BookmarksExportObserver* observer)
97 : bookmarks_(bookmarks), 97 : bookmarks_(bookmarks),
98 path_(path), 98 path_(path),
99 favicons_map_(favicons_map), 99 favicons_map_(favicons_map),
100 observer_(observer), 100 observer_(observer) {
101 file_stream_(NULL) {
102 } 101 }
103 102
104 // Writing bookmarks and favicons data to file. 103 // Writing bookmarks and favicons data to file.
105 void DoWrite() { 104 void DoWrite() {
106 if (!OpenFile()) 105 if (!OpenFile())
107 return; 106 return;
108 107
109 Value* roots = NULL; 108 Value* roots = NULL;
110 if (!Write(kHeader) || 109 if (!Write(kHeader) ||
111 bookmarks_->GetType() != Value::TYPE_DICTIONARY || 110 bookmarks_->GetType() != Value::TYPE_DICTIONARY ||
(...skipping 30 matching lines...) Expand all
142 !WriteNode(*static_cast<DictionaryValue*>(mobile_folder_value), 141 !WriteNode(*static_cast<DictionaryValue*>(mobile_folder_value),
143 BookmarkNode::MOBILE)) { 142 BookmarkNode::MOBILE)) {
144 return; 143 return;
145 } 144 }
146 145
147 DecrementIndent(); 146 DecrementIndent();
148 147
149 Write(kFolderChildrenEnd); 148 Write(kFolderChildrenEnd);
150 Write(kNewline); 149 Write(kNewline);
151 // File stream close is forced so that unit test could read it. 150 // File stream close is forced so that unit test could read it.
152 file_stream_.CloseSync(); 151 file_stream_.reset();
153 152
154 NotifyOnFinish(); 153 NotifyOnFinish();
155 } 154 }
156 155
157 private: 156 private:
158 friend class base::RefCountedThreadSafe<Writer>; 157 friend class base::RefCountedThreadSafe<Writer>;
159 158
160 // Types of text being written out. The type dictates how the text is 159 // Types of text being written out. The type dictates how the text is
161 // escaped. 160 // escaped.
162 enum TextType { 161 enum TextType {
163 // The text is the value of an html attribute, eg foo in 162 // The text is the value of an html attribute, eg foo in
164 // <a href="foo">. 163 // <a href="foo">.
165 ATTRIBUTE_VALUE, 164 ATTRIBUTE_VALUE,
166 165
167 // Actual content, eg foo in <h1>foo</h2>. 166 // Actual content, eg foo in <h1>foo</h2>.
168 CONTENT 167 CONTENT
169 }; 168 };
170 169
171 ~Writer() {} 170 ~Writer() {}
172 171
173 // Opens the file, returning true on success. 172 // Opens the file, returning true on success.
174 bool OpenFile() { 173 bool OpenFile() {
174 file_stream_.reset(new net::FileStream(NULL));
175 int flags = base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE; 175 int flags = base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE;
176 return (file_stream_.OpenSync(path_, flags) == net::OK); 176 return (file_stream_->OpenSync(path_, flags) == net::OK);
177 } 177 }
178 178
179 // Increments the indent. 179 // Increments the indent.
180 void IncrementIndent() { 180 void IncrementIndent() {
181 indent_.resize(indent_.size() + kIndentSize, ' '); 181 indent_.resize(indent_.size() + kIndentSize, ' ');
182 } 182 }
183 183
184 // Decrements the indent. 184 // Decrements the indent.
185 void DecrementIndent() { 185 void DecrementIndent() {
186 DCHECK(!indent_.empty()); 186 DCHECK(!indent_.empty());
187 indent_.resize(indent_.size() - kIndentSize, ' '); 187 indent_.resize(indent_.size() - kIndentSize, ' ');
188 } 188 }
189 189
190 // Called at the end of the export process. 190 // Called at the end of the export process.
191 void NotifyOnFinish() { 191 void NotifyOnFinish() {
192 if (observer_ != NULL) { 192 if (observer_ != NULL) {
193 observer_->OnExportFinished(); 193 observer_->OnExportFinished();
194 } 194 }
195 } 195 }
196 196
197 // Writes raw text out returning true on success. This does not escape 197 // Writes raw text out returning true on success. This does not escape
198 // the text in anyway. 198 // the text in anyway.
199 bool Write(const std::string& text) { 199 bool Write(const std::string& text) {
200 size_t wrote = file_stream_.WriteSync(text.c_str(), text.length()); 200 size_t wrote = file_stream_->WriteSync(text.c_str(), text.length());
201 bool result = (wrote == text.length()); 201 bool result = (wrote == text.length());
202 DCHECK(result); 202 DCHECK(result);
203 return result; 203 return result;
204 } 204 }
205 205
206 // Writes out the text string (as UTF8). The text is escaped based on 206 // Writes out the text string (as UTF8). The text is escaped based on
207 // type. 207 // type.
208 bool Write(const std::string& text, TextType type) { 208 bool Write(const std::string& text, TextType type) {
209 DCHECK(IsStringUTF8(text)); 209 DCHECK(IsStringUTF8(text));
210 std::string utf8_string; 210 std::string utf8_string;
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 // Path we're writing to. 367 // Path we're writing to.
368 FilePath path_; 368 FilePath path_;
369 369
370 // Map that stores favicon per URL. 370 // Map that stores favicon per URL.
371 scoped_ptr<BookmarkFaviconFetcher::URLFaviconMap> favicons_map_; 371 scoped_ptr<BookmarkFaviconFetcher::URLFaviconMap> favicons_map_;
372 372
373 // Observer to be notified on finish. 373 // Observer to be notified on finish.
374 BookmarksExportObserver* observer_; 374 BookmarksExportObserver* observer_;
375 375
376 // File we're writing to. 376 // File we're writing to.
377 net::FileStream file_stream_; 377 scoped_ptr<net::FileStream> file_stream_;
378 378
379 // How much we indent when writing a bookmark/folder. This is modified 379 // How much we indent when writing a bookmark/folder. This is modified
380 // via IncrementIndent and DecrementIndent. 380 // via IncrementIndent and DecrementIndent.
381 std::string indent_; 381 std::string indent_;
382 382
383 DISALLOW_COPY_AND_ASSIGN(Writer); 383 DISALLOW_COPY_AND_ASSIGN(Writer);
384 }; 384 };
385 385
386 } // namespace 386 } // namespace
387 387
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 // BookmarkModel isn't thread safe (nor would we want to lock it down 502 // BookmarkModel isn't thread safe (nor would we want to lock it down
503 // for the duration of the write), as such we make a copy of the 503 // for the duration of the write), as such we make a copy of the
504 // BookmarkModel using BookmarkCodec then write from that. 504 // BookmarkModel using BookmarkCodec then write from that.
505 if (fetcher == NULL) { 505 if (fetcher == NULL) {
506 fetcher = new BookmarkFaviconFetcher(profile, path, observer); 506 fetcher = new BookmarkFaviconFetcher(profile, path, observer);
507 fetcher->ExportBookmarks(); 507 fetcher->ExportBookmarks();
508 } 508 }
509 } 509 }
510 510
511 } // namespace bookmark_html_writer 511 } // namespace bookmark_html_writer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698