OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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 "chrome/browser/media_galleries/fileapi/safe_picasa_albums_indexer.h" | |
6 | |
7 #include "base/file_util.h" | |
8 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" | |
9 #include "chrome/common/chrome_utility_messages.h" | |
10 #include "content/public/browser/browser_thread.h" | |
11 #include "content/public/browser/child_process_data.h" | |
12 #include "content/public/browser/utility_process_host.h" | |
13 | |
14 using chrome::MediaFileSystemBackend; | |
15 using content::BrowserThread; | |
16 using content::UtilityProcessHost; | |
17 | |
18 namespace picasa { | |
19 | |
20 namespace { | |
21 | |
22 // Picasa INI files are named "picasa.ini" on Picasa for Windows before version | |
23 // 71.18. Later versions and Picasa for Mac uses ".picasa.ini". | |
24 // See: https://support.google.com/picasa/answer/11257?hl=en | |
25 const char kPicasaINIFilename[] = ".picasa.ini"; | |
26 const char kPicasaINIFilenameLegacy[] = "picasa.ini"; | |
27 | |
28 // Arbitrarily chosen to be a decent size but not block thread too much. | |
29 const int kPicasaINIReadBatchSize = 10; | |
30 | |
31 } // namespace | |
32 | |
33 SafePicasaAlbumsIndexer::SafePicasaAlbumsIndexer( | |
34 const AlbumMap& albums, | |
35 const AlbumMap& folders, | |
36 const DoneCallback& callback) | |
37 : callback_(callback), | |
38 parser_state_(INITIAL_STATE) { | |
39 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
40 DCHECK(!callback_.is_null()); | |
41 | |
42 folders_inis_.reserve(folders.size()); | |
43 | |
44 for (AlbumMap::const_iterator it = albums.begin(); it != albums.end(); ++it) | |
45 album_uids_.insert(it->second.uid); | |
46 | |
47 for (AlbumMap::const_iterator it = folders.begin(); it != folders.end(); ++it) | |
48 folders_queue_.push(it->second.path); | |
49 } | |
50 | |
51 void SafePicasaAlbumsIndexer::Start() { | |
52 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
53 | |
54 ProcessFoldersBatch(); | |
55 } | |
56 | |
57 SafePicasaAlbumsIndexer::~SafePicasaAlbumsIndexer() { | |
58 } | |
59 | |
60 void SafePicasaAlbumsIndexer::ProcessFoldersBatch() { | |
61 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
62 | |
63 for (int i = 0; i < kPicasaINIReadBatchSize && !folders_queue_.empty(); ++i) { | |
64 base::FilePath folder_path = folders_queue_.front(); | |
65 folders_queue_.pop(); | |
66 | |
67 folders_inis_.push_back(FolderINIContents()); | |
68 | |
69 bool ini_read = | |
70 file_util::ReadFileToString( | |
71 folder_path.AppendASCII(kPicasaINIFilename), | |
72 &folders_inis_.back().ini_contents) || | |
73 file_util::ReadFileToString( | |
74 folder_path.AppendASCII(kPicasaINIFilenameLegacy), | |
75 &folders_inis_.back().ini_contents); | |
76 | |
77 // See kPicasaINIFilename declaration for details. | |
78 if (ini_read) | |
79 folders_inis_.back().folder_path = folder_path; | |
80 else | |
81 folders_inis_.pop_back(); | |
82 } | |
83 | |
84 // If queue of folders to process not empty, post self onto task runner again. | |
85 if (!folders_queue_.empty()) { | |
86 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | |
87 FROM_HERE, | |
88 base::Bind(&SafePicasaAlbumsIndexer::ProcessFoldersBatch, | |
89 weak_factory_.GetWeakPtr())); | |
Lei Zhang
2013/07/25 21:36:57
I don't think this compiles?
tommycli
2013/07/25 21:58:06
Done.
| |
90 } else { | |
91 BrowserThread::PostTask( | |
92 BrowserThread::IO, | |
93 FROM_HERE, | |
94 base::Bind(&SafePicasaAlbumsIndexer::StartWorkOnIOThread, this)); | |
95 } | |
96 } | |
97 | |
98 void SafePicasaAlbumsIndexer::StartWorkOnIOThread() { | |
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
100 DCHECK_EQ(INITIAL_STATE, parser_state_); | |
101 | |
102 UtilityProcessHost* host = | |
103 UtilityProcessHost::Create(this, base::MessageLoopProxy::current()); | |
104 host->EnableZygote(); | |
105 host->Send(new ChromeUtilityMsg_IndexPicasaAlbumsContents(album_uids_, | |
106 folders_inis_)); | |
107 parser_state_ = STARTED_PARSING_STATE; | |
108 } | |
109 | |
110 void SafePicasaAlbumsIndexer::OnIndexPicasaAlbumsContentsFinished( | |
111 const AlbumImagesMap& albums_images) { | |
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
113 if (parser_state_ != STARTED_PARSING_STATE) | |
114 return; | |
115 | |
116 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | |
117 FROM_HERE, | |
118 base::Bind(callback_, true, albums_images)); | |
119 parser_state_ = FINISHED_PARSING_STATE; | |
120 } | |
121 | |
122 void SafePicasaAlbumsIndexer::OnProcessCrashed(int exit_code) { | |
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
124 | |
125 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | |
126 FROM_HERE, | |
127 base::Bind(callback_, false, AlbumImagesMap())); | |
128 } | |
129 | |
130 bool SafePicasaAlbumsIndexer::OnMessageReceived( | |
131 const IPC::Message& message) { | |
132 bool handled = true; | |
133 IPC_BEGIN_MESSAGE_MAP(SafePicasaAlbumsIndexer, message) | |
134 IPC_MESSAGE_HANDLER( | |
135 ChromeUtilityHostMsg_IndexPicasaAlbumsContents_Finished, | |
136 OnIndexPicasaAlbumsContentsFinished) | |
137 IPC_MESSAGE_UNHANDLED(handled = false) | |
138 IPC_END_MESSAGE_MAP() | |
139 return handled; | |
140 } | |
141 | |
142 } // namespace picasa | |
OLD | NEW |