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 } | |
32 | |
33 SafePicasaAlbumsIndexer::SafePicasaAlbumsIndexer( | |
34 const AlbumMap& albums, | |
35 const AlbumMap& folders, | |
36 const ParserCallback& callback) | |
37 : callback_(callback), | |
38 parser_state_(INITIAL_STATE) { | |
39 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
40 DCHECK(!callback_.is_null()); | |
41 | |
42 for (AlbumMap::const_iterator it = albums.begin(); it != albums.end(); ++it) { | |
43 album_uids_.insert(it->second.uid); | |
44 } | |
45 | |
46 for (AlbumMap::const_iterator it = folders.begin(); it != folders.end(); | |
47 ++it) { | |
48 folders_queue.push(it->second.path); | |
49 } | |
50 } | |
51 | |
52 void SafePicasaAlbumsIndexer::Start() { | |
53 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
54 DCHECK_EQ(INITIAL_STATE, parser_state_); | |
55 | |
56 parser_state_ = STARTED_READING_INI_FILES_STATE; | |
57 | |
58 ProcessFoldersBatch(); | |
59 } | |
60 | |
61 SafePicasaAlbumsIndexer::~SafePicasaAlbumsIndexer() { | |
62 } | |
63 | |
64 void SafePicasaAlbumsIndexer::ProcessFoldersBatch() { | |
tommycli
2013/07/09 21:52:57
This callback is posted repeatedly until done.
| |
65 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
66 DCHECK_EQ(STARTED_READING_INI_FILES_STATE, parser_state_); | |
67 | |
68 for (int i = 0; i < kPicasaINIReadBatchSize && !folders_queue.empty(); ++i) { | |
69 base::FilePath folder_path = folders_queue.front(); | |
70 folders_queue.pop(); | |
71 | |
72 FolderINIContents folder_ini; | |
73 folder_ini.folder_path = folder_path; | |
74 | |
75 // See kPicasaINIFilename declaration for details. | |
76 if (file_util::ReadFileToString( | |
77 folder_path.AppendASCII(kPicasaINIFilename), | |
78 &folder_ini.ini_contents) || | |
79 file_util::ReadFileToString( | |
80 folder_path.AppendASCII(kPicasaINIFilenameLegacy), | |
81 &folder_ini.ini_contents)) { | |
82 folders_inis_.push_back(folder_ini); | |
83 } | |
84 } | |
85 | |
86 if (!folders_queue.empty()) { | |
vandebo (ex-Chrome)
2013/07/10 17:43:52
Should this be folder_inis_?
tommycli
2013/07/10 18:43:32
No. While queue is not empty, post itself onto the
vandebo (ex-Chrome)
2013/07/10 19:54:03
Hmm, right.
tommycli
2013/07/10 22:22:27
Done.
| |
87 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | |
88 FROM_HERE, | |
89 base::Bind(SafePicasaAlbumsIndexer::ProcessFoldersBatch, | |
90 weak_factory_.GetWeakPtr())); | |
91 } else { | |
92 parser_state_ = FINISHED_READING_INI_FILES_STATE; | |
vandebo (ex-Chrome)
2013/07/10 17:43:52
This seems ok for now. It still has the disadvant
tommycli
2013/07/10 18:43:32
Yeah I agree we should keep that idea in our back
| |
93 BrowserThread::PostTask( | |
94 BrowserThread::IO, | |
95 FROM_HERE, | |
96 base::Bind(&SafePicasaAlbumsIndexer::StartWorkOnIOThread, this)); | |
97 } | |
98 } | |
99 | |
100 void SafePicasaAlbumsIndexer::StartWorkOnIOThread() { | |
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
102 DCHECK_EQ(FINISHED_READING_INI_FILES_STATE, parser_state_); | |
103 | |
104 std::vector<picasa::FolderINIContents> folder_inis; | |
105 | |
106 UtilityProcessHost* host = | |
107 UtilityProcessHost::Create(this, base::MessageLoopProxy::current()); | |
108 host->EnableZygote(); | |
109 host->Send(new ChromeUtilityMsg_IndexPicasaAlbumsContents(album_uids_, | |
110 folder_inis)); | |
111 parser_state_ = STARTED_PARSING_STATE; | |
112 } | |
113 | |
114 void SafePicasaAlbumsIndexer::OnIndexPicasaAlbumsContentsFinished( | |
115 const AlbumImagesMap& albums_images) { | |
116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
117 if (parser_state_ != STARTED_PARSING_STATE) | |
118 return; | |
119 | |
120 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | |
121 FROM_HERE, | |
122 base::Bind(callback_, albums_images)); | |
123 parser_state_ = FINISHED_PARSING_STATE; | |
124 } | |
125 | |
126 void SafePicasaAlbumsIndexer::OnProcessCrashed(int exit_code) { | |
127 OnIndexPicasaAlbumsContentsFinished(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 |