Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/media_galleries/fileapi/safe_picasa_album_table_reader. h" | 5 #include "chrome/browser/media_galleries/fileapi/safe_picasa_albums_indexer.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | |
| 7 #include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_p rovider.h" | 8 #include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_p rovider.h" |
| 8 #include "chrome/common/chrome_utility_messages.h" | 9 #include "chrome/common/chrome_utility_messages.h" |
| 9 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/child_process_data.h" | 11 #include "content/public/browser/child_process_data.h" |
| 12 #include "content/public/browser/utility_process_host.h" | |
| 11 | 13 |
| 12 using chrome::MediaFileSystemMountPointProvider; | 14 using chrome::MediaFileSystemMountPointProvider; |
| 13 using content::BrowserThread; | 15 using content::BrowserThread; |
| 16 using content::UtilityProcessHost; | |
| 14 | 17 |
| 15 namespace picasa { | 18 namespace picasa { |
| 16 | 19 |
| 17 SafePicasaAlbumTableReader::SafePicasaAlbumTableReader( | 20 namespace { |
| 18 const AlbumTableFiles& album_table_files, | 21 |
| 22 // Picasa INI files are named either ".picasa.ini" OR "picasa.ini" for | |
|
Lei Zhang
2013/07/02 22:08:17
This sentence is a bit hard to parse. I think you
tommycli
2013/07/03 00:58:28
Done.
| |
| 23 // Windows versions before 71.18. | |
| 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 } | |
| 29 | |
| 30 SafePicasaAlbumsIndexer::SafePicasaAlbumsIndexer( | |
| 31 const AlbumMap& albums, | |
| 32 const AlbumMap& folders, | |
| 19 const ParserCallback& callback) | 33 const ParserCallback& callback) |
| 20 : album_table_files_(album_table_files), | 34 : callback_(callback), |
| 21 callback_(callback), | |
| 22 parser_state_(INITIAL_STATE) { | 35 parser_state_(INITIAL_STATE) { |
| 23 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); | 36 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); |
| 24 DCHECK(!callback_.is_null()); | 37 DCHECK(!callback_.is_null()); |
| 38 | |
| 39 for (AlbumMap::const_iterator it = albums.begin(); it != albums.end(); ++it) { | |
| 40 album_uids_.insert(it->second.uid); | |
| 41 } | |
| 42 | |
| 43 for (AlbumMap::const_iterator it = folders.begin(); it != folders.end(); | |
| 44 ++it) { | |
| 45 folder_paths_.insert(it->second.path); | |
| 46 } | |
| 25 } | 47 } |
| 26 | 48 |
| 27 void SafePicasaAlbumTableReader::Start() { | 49 void SafePicasaAlbumsIndexer::Start() { |
| 28 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); | 50 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); |
| 29 BrowserThread::PostTask( | 51 BrowserThread::PostTask( |
| 30 BrowserThread::IO, | 52 BrowserThread::IO, |
| 31 FROM_HERE, | 53 FROM_HERE, |
| 32 base::Bind(&SafePicasaAlbumTableReader::StartWorkOnIOThread, this)); | 54 base::Bind(&SafePicasaAlbumsIndexer::StartWorkOnIOThread, this)); |
| 33 } | 55 } |
| 34 | 56 |
| 35 SafePicasaAlbumTableReader::~SafePicasaAlbumTableReader() { | 57 SafePicasaAlbumsIndexer::~SafePicasaAlbumsIndexer() { |
| 36 } | 58 } |
| 37 | 59 |
| 38 void SafePicasaAlbumTableReader::StartWorkOnIOThread() { | 60 void SafePicasaAlbumsIndexer::StartWorkOnIOThread() { |
| 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 40 DCHECK_EQ(INITIAL_STATE, parser_state_); | 62 DCHECK_EQ(INITIAL_STATE, parser_state_); |
| 41 | 63 |
| 42 utility_process_host_ = content::UtilityProcessHost::Create( | 64 std::vector<picasa::FolderINIContents> folder_inis; |
| 43 this, | |
| 44 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get()) | |
| 45 ->AsWeakPtr(); | |
| 46 // Wait for the startup notification before sending the main IPC to the | |
| 47 // utility process, so that we can dup the file handle. | |
| 48 utility_process_host_->Send(new ChromeUtilityMsg_StartupPing); | |
| 49 parser_state_ = PINGED_UTILITY_PROCESS_STATE; | |
| 50 } | |
| 51 | 65 |
| 52 void SafePicasaAlbumTableReader::OnProcessStarted() { | 66 for (std::vector<base::FilePath>::const_iterator it = folder_paths_.begin(); |
| 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 67 it != folder_paths_.end(); ++it) { |
| 54 if (parser_state_ != PINGED_UTILITY_PROCESS_STATE) | 68 FolderINIContents folder_ini; |
| 55 return; | 69 folder_ini.folder_path = *it; |
| 56 | 70 |
| 57 if (utility_process_host_->GetData().handle == base::kNullProcessHandle) { | 71 // See kPicasaINIFilename declaration for details. |
| 58 DLOG(ERROR) << "Child process handle is null"; | 72 if (file_util::ReadFileToString(it->Append(kPicasaINIFilename), |
| 73 &folder_ini.ini_contents) || | |
| 74 file_util::ReadFileToString(it->Append(kPicasaINIFilenameLegacy), | |
| 75 &folder_ini.ini_contents)) { | |
| 76 folder_inis.push_back(folder_ini); | |
| 77 } | |
| 59 } | 78 } |
| 60 AlbumTableFilesForTransit files_for_transit; | 79 |
| 61 files_for_transit.indicator_file = IPC::GetFileHandleForProcess( | 80 UtilityProcessHost* host = |
| 62 album_table_files_.indicator_file, | 81 UtilityProcessHost::Create(this, base::MessageLoopProxy::current()); |
| 63 utility_process_host_->GetData().handle, | 82 host->EnableZygote(); |
| 64 true /* close_source_handle */); | 83 host->Send(new ChromeUtilityMsg_IndexPicasaAlbumsContents(album_uids_, |
| 65 files_for_transit.category_file = IPC::GetFileHandleForProcess( | 84 folder_inis)); |
| 66 album_table_files_.category_file, | |
| 67 utility_process_host_->GetData().handle, | |
| 68 true /* close_source_handle */); | |
| 69 files_for_transit.date_file = IPC::GetFileHandleForProcess( | |
| 70 album_table_files_.date_file, | |
| 71 utility_process_host_->GetData().handle, | |
| 72 true /* close_source_handle */); | |
| 73 files_for_transit.filename_file = IPC::GetFileHandleForProcess( | |
| 74 album_table_files_.filename_file, | |
| 75 utility_process_host_->GetData().handle, | |
| 76 true /* close_source_handle */); | |
| 77 files_for_transit.name_file = IPC::GetFileHandleForProcess( | |
| 78 album_table_files_.name_file, | |
| 79 utility_process_host_->GetData().handle, | |
| 80 true /* close_source_handle */); | |
| 81 files_for_transit.token_file = IPC::GetFileHandleForProcess( | |
| 82 album_table_files_.token_file, | |
| 83 utility_process_host_->GetData().handle, | |
| 84 true /* close_source_handle */); | |
| 85 files_for_transit.uid_file = IPC::GetFileHandleForProcess( | |
| 86 album_table_files_.uid_file, | |
| 87 utility_process_host_->GetData().handle, | |
| 88 true /* close_source_handle */); | |
| 89 utility_process_host_->Send(new ChromeUtilityMsg_ParsePicasaPMPDatabase( | |
| 90 files_for_transit)); | |
| 91 parser_state_ = STARTED_PARSING_STATE; | 85 parser_state_ = STARTED_PARSING_STATE; |
| 92 } | 86 } |
| 93 | 87 |
| 94 void SafePicasaAlbumTableReader::OnParsePicasaPMPDatabaseFinished( | 88 void OnIndexPicasaAlbumsContentsFinished(const AlbumImagesMap& albums_images) { |
| 95 bool parse_success, | |
| 96 const std::vector<AlbumInfo>& albums, | |
| 97 const std::vector<AlbumInfo>& folders) { | |
| 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 99 if (parser_state_ != STARTED_PARSING_STATE) | 90 if (parser_state_ != STARTED_PARSING_STATE) |
| 100 return; | 91 return; |
| 101 | 92 |
| 102 MediaFileSystemMountPointProvider::MediaTaskRunner()->PostTask( | 93 MediaFileSystemMountPointProvider::MediaTaskRunner()->PostTask( |
| 103 FROM_HERE, | 94 FROM_HERE, |
| 104 base::Bind(callback_, parse_success, albums, folders)); | 95 base::Bind(callback_, albums_images)); |
| 105 parser_state_ = FINISHED_PARSING_STATE; | 96 parser_state_ = FINISHED_PARSING_STATE; |
| 106 } | 97 } |
| 107 | 98 |
| 108 void SafePicasaAlbumTableReader::OnProcessCrashed(int exit_code) { | 99 void SafePicasaAlbumsIndexer::OnProcessCrashed(int exit_code) { |
| 109 OnParsePicasaPMPDatabaseFinished(false, std::vector<AlbumInfo>(), | 100 OnIndexPicasaAlbumsContentsFinished(AlbumImagesMap()); |
| 110 std::vector<AlbumInfo>()); | |
| 111 } | 101 } |
| 112 | 102 |
| 113 bool SafePicasaAlbumTableReader::OnMessageReceived( | 103 bool SafePicasaAlbumsIndexer::OnMessageReceived( |
| 114 const IPC::Message& message) { | 104 const IPC::Message& message) { |
| 115 bool handled = true; | 105 bool handled = true; |
| 116 IPC_BEGIN_MESSAGE_MAP(SafePicasaAlbumTableReader, message) | 106 IPC_BEGIN_MESSAGE_MAP(SafePicasaAlbumsIndexer, message) |
| 117 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted, | 107 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_IndexPicasaAlbumContents_Finished, |
| 118 OnProcessStarted) | 108 OnIndexPicasaAlbumsContentsFinished) |
| 119 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParsePicasaPMPDatabase_Finished, | |
| 120 OnParsePicasaPMPDatabaseFinished) | |
| 121 IPC_MESSAGE_UNHANDLED(handled = false) | 109 IPC_MESSAGE_UNHANDLED(handled = false) |
| 122 IPC_END_MESSAGE_MAP() | 110 IPC_END_MESSAGE_MAP() |
| 123 return handled; | 111 return handled; |
| 124 } | 112 } |
| 125 | 113 |
| 126 } // namespace picasa | 114 } // namespace picasa |
| OLD | NEW |