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, this)); |
| 89 } else { |
| 90 BrowserThread::PostTask( |
| 91 BrowserThread::IO, |
| 92 FROM_HERE, |
| 93 base::Bind(&SafePicasaAlbumsIndexer::StartWorkOnIOThread, this)); |
| 94 } |
| 95 } |
| 96 |
| 97 void SafePicasaAlbumsIndexer::StartWorkOnIOThread() { |
| 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 99 DCHECK_EQ(INITIAL_STATE, parser_state_); |
| 100 |
| 101 UtilityProcessHost* host = |
| 102 UtilityProcessHost::Create(this, base::MessageLoopProxy::current()); |
| 103 host->EnableZygote(); |
| 104 host->Send(new ChromeUtilityMsg_IndexPicasaAlbumsContents(album_uids_, |
| 105 folders_inis_)); |
| 106 parser_state_ = STARTED_PARSING_STATE; |
| 107 } |
| 108 |
| 109 void SafePicasaAlbumsIndexer::OnIndexPicasaAlbumsContentsFinished( |
| 110 const AlbumImagesMap& albums_images) { |
| 111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 112 if (parser_state_ != STARTED_PARSING_STATE) |
| 113 return; |
| 114 |
| 115 MediaFileSystemBackend::MediaTaskRunner()->PostTask( |
| 116 FROM_HERE, |
| 117 base::Bind(callback_, true, albums_images)); |
| 118 parser_state_ = FINISHED_PARSING_STATE; |
| 119 } |
| 120 |
| 121 void SafePicasaAlbumsIndexer::OnProcessCrashed(int exit_code) { |
| 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 123 |
| 124 MediaFileSystemBackend::MediaTaskRunner()->PostTask( |
| 125 FROM_HERE, |
| 126 base::Bind(callback_, false, AlbumImagesMap())); |
| 127 } |
| 128 |
| 129 bool SafePicasaAlbumsIndexer::OnMessageReceived( |
| 130 const IPC::Message& message) { |
| 131 bool handled = true; |
| 132 IPC_BEGIN_MESSAGE_MAP(SafePicasaAlbumsIndexer, message) |
| 133 IPC_MESSAGE_HANDLER( |
| 134 ChromeUtilityHostMsg_IndexPicasaAlbumsContents_Finished, |
| 135 OnIndexPicasaAlbumsContentsFinished) |
| 136 IPC_MESSAGE_UNHANDLED(handled = false) |
| 137 IPC_END_MESSAGE_MAP() |
| 138 return handled; |
| 139 } |
| 140 |
| 141 } // namespace picasa |
OLD | NEW |