Chromium Code Reviews| 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 } | |
|
vandebo (ex-Chrome)
2013/07/10 19:54:03
} // namesapce
tommycli
2013/07/10 22:22:27
Done.
| |
| 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 weak_factory_(this) { | |
| 40 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
| 41 DCHECK(!callback_.is_null()); | |
| 42 | |
| 43 for (AlbumMap::const_iterator it = albums.begin(); it != albums.end(); ++it) { | |
|
vandebo (ex-Chrome)
2013/07/10 19:54:03
nit: you may omit {}'s here.
tommycli
2013/07/10 22:22:27
Done.
| |
| 44 album_uids_.insert(it->second.uid); | |
| 45 } | |
| 46 | |
| 47 for (AlbumMap::const_iterator it = folders.begin(); it != folders.end(); | |
| 48 ++it) { | |
| 49 folders_queue_.push(it->second.path); | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 void SafePicasaAlbumsIndexer::Start() { | |
| 54 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
| 55 DCHECK_EQ(INITIAL_STATE, parser_state_); | |
| 56 | |
| 57 parser_state_ = STARTED_READING_INI_FILES_STATE; | |
| 58 | |
| 59 ProcessFoldersBatch(); | |
| 60 } | |
| 61 | |
| 62 SafePicasaAlbumsIndexer::~SafePicasaAlbumsIndexer() { | |
| 63 } | |
| 64 | |
| 65 void SafePicasaAlbumsIndexer::ProcessFoldersBatch() { | |
| 66 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
| 67 DCHECK_EQ(STARTED_READING_INI_FILES_STATE, parser_state_); | |
| 68 | |
| 69 for (int i = 0; i < kPicasaINIReadBatchSize && !folders_queue_.empty(); ++i) { | |
| 70 base::FilePath folder_path = folders_queue_.front(); | |
| 71 folders_queue_.pop(); | |
| 72 | |
| 73 FolderINIContents folder_ini; | |
| 74 folder_ini.folder_path = folder_path; | |
| 75 | |
| 76 // See kPicasaINIFilename declaration for details. | |
| 77 if (file_util::ReadFileToString( | |
| 78 folder_path.AppendASCII(kPicasaINIFilename), | |
| 79 &folder_ini.ini_contents) || | |
| 80 file_util::ReadFileToString( | |
| 81 folder_path.AppendASCII(kPicasaINIFilenameLegacy), | |
| 82 &folder_ini.ini_contents)) { | |
| 83 folders_inis_.push_back(folder_ini); | |
|
vandebo (ex-Chrome)
2013/07/10 19:54:03
It might make sense to push first to minimize the
tommycli
2013/07/10 22:22:27
folder_inis_ is a vector. Are you suggesting pre-a
vandebo (ex-Chrome)
2013/07/11 16:20:50
If copying a std::string was truly cheap, we would
tommycli
2013/07/11 17:24:13
Done.
| |
| 84 } | |
| 85 } | |
| 86 | |
| 87 // If queue of folders to process not empty, post self onto task runner again. | |
| 88 if (!folders_queue_.empty()) { | |
| 89 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | |
| 90 FROM_HERE, | |
| 91 base::Bind(&SafePicasaAlbumsIndexer::ProcessFoldersBatch, | |
| 92 weak_factory_.GetWeakPtr())); | |
| 93 } else { | |
| 94 parser_state_ = FINISHED_READING_INI_FILES_STATE; | |
| 95 BrowserThread::PostTask( | |
| 96 BrowserThread::IO, | |
| 97 FROM_HERE, | |
| 98 base::Bind(&SafePicasaAlbumsIndexer::StartWorkOnIOThread, this)); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 void SafePicasaAlbumsIndexer::StartWorkOnIOThread() { | |
| 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 104 DCHECK_EQ(FINISHED_READING_INI_FILES_STATE, parser_state_); | |
| 105 | |
| 106 std::vector<picasa::FolderINIContents> folder_inis; | |
| 107 | |
| 108 UtilityProcessHost* host = | |
| 109 UtilityProcessHost::Create(this, base::MessageLoopProxy::current()); | |
| 110 host->EnableZygote(); | |
| 111 host->Send(new ChromeUtilityMsg_IndexPicasaAlbumsContents(album_uids_, | |
| 112 folder_inis)); | |
|
vandebo (ex-Chrome)
2013/07/10 19:54:03
Don't you want to send folders_inis_ ?
tommycli
2013/07/10 22:22:27
Yep! thanks.
| |
| 113 parser_state_ = STARTED_PARSING_STATE; | |
| 114 } | |
| 115 | |
| 116 void SafePicasaAlbumsIndexer::OnIndexPicasaAlbumsContentsFinished( | |
| 117 const AlbumImagesMap& albums_images) { | |
| 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 119 if (parser_state_ != STARTED_PARSING_STATE) | |
| 120 return; | |
| 121 | |
| 122 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | |
| 123 FROM_HERE, | |
| 124 base::Bind(callback_, albums_images)); | |
| 125 parser_state_ = FINISHED_PARSING_STATE; | |
| 126 } | |
| 127 | |
| 128 void SafePicasaAlbumsIndexer::OnProcessCrashed(int exit_code) { | |
| 129 OnIndexPicasaAlbumsContentsFinished(AlbumImagesMap()); | |
| 130 } | |
| 131 | |
| 132 bool SafePicasaAlbumsIndexer::OnMessageReceived( | |
| 133 const IPC::Message& message) { | |
| 134 bool handled = true; | |
| 135 IPC_BEGIN_MESSAGE_MAP(SafePicasaAlbumsIndexer, message) | |
| 136 IPC_MESSAGE_HANDLER( | |
| 137 ChromeUtilityHostMsg_IndexPicasaAlbumsContents_Finished, | |
| 138 OnIndexPicasaAlbumsContentsFinished) | |
| 139 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 140 IPC_END_MESSAGE_MAP() | |
| 141 return handled; | |
| 142 } | |
| 143 | |
| 144 } // namespace picasa | |
| OLD | NEW |