Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: chrome/browser/media_galleries/fileapi/safe_picasa_albums_indexer.cc

Issue 18562007: Media Galleries API Picasa: Put INI indexing step into sandboxed utility process. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@0035-picasa-import-sandbox-pmp-reading
Patch Set: Add success flag that's false when utility process crashes. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 weak_factory_(this) {
40 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
41 DCHECK(!callback_.is_null());
42
43 folders_inis_.reserve(folders.size());
44
45 for (AlbumMap::const_iterator it = albums.begin(); it != albums.end(); ++it)
46 album_uids_.insert(it->second.uid);
47
48 for (AlbumMap::const_iterator it = folders.begin(); it != folders.end(); ++it)
49 folders_queue_.push(it->second.path);
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() {
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 folders_inis_.push_back(FolderINIContents());
73
74 bool ini_read =
75 file_util::ReadFileToString(
76 folder_path.AppendASCII(kPicasaINIFilename),
77 &folders_inis_.back().ini_contents) ||
78 file_util::ReadFileToString(
79 folder_path.AppendASCII(kPicasaINIFilenameLegacy),
80 &folders_inis_.back().ini_contents);
81
82 // See kPicasaINIFilename declaration for details.
83 if (ini_read)
84 folders_inis_.back().folder_path = folder_path;
85 else
86 folders_inis_.pop_back();
87 }
88
89 // If queue of folders to process not empty, post self onto task runner again.
90 if (!folders_queue_.empty()) {
91 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
92 FROM_HERE,
93 base::Bind(&SafePicasaAlbumsIndexer::ProcessFoldersBatch,
94 weak_factory_.GetWeakPtr()));
95 } else {
96 parser_state_ = FINISHED_READING_INI_FILES_STATE;
97 BrowserThread::PostTask(
98 BrowserThread::IO,
99 FROM_HERE,
100 base::Bind(&SafePicasaAlbumsIndexer::StartWorkOnIOThread, this));
101 }
102 }
103
104 void SafePicasaAlbumsIndexer::StartWorkOnIOThread() {
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
106 DCHECK_EQ(FINISHED_READING_INI_FILES_STATE, parser_state_);
vandebo (ex-Chrome) 2013/07/24 18:15:54 nit: shouldn't access parser_state_ from multiple
tommycli 2013/07/25 17:02:51 Done. Removed parser_state_ access on MediaTaskRun
Lei Zhang 2013/07/25 23:14:58 Sounds good. The communication with the utility pr
107
108 UtilityProcessHost* host =
109 UtilityProcessHost::Create(this, base::MessageLoopProxy::current());
110 host->EnableZygote();
111 host->Send(new ChromeUtilityMsg_IndexPicasaAlbumsContents(album_uids_,
112 folders_inis_));
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_, true, albums_images));
125 parser_state_ = FINISHED_PARSING_STATE;
126 }
127
128 void SafePicasaAlbumsIndexer::OnProcessCrashed(int exit_code) {
129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
130
131 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
132 FROM_HERE,
133 base::Bind(callback_, false, AlbumImagesMap()));
134 }
135
136 bool SafePicasaAlbumsIndexer::OnMessageReceived(
137 const IPC::Message& message) {
138 bool handled = true;
139 IPC_BEGIN_MESSAGE_MAP(SafePicasaAlbumsIndexer, message)
140 IPC_MESSAGE_HANDLER(
141 ChromeUtilityHostMsg_IndexPicasaAlbumsContents_Finished,
142 OnIndexPicasaAlbumsContentsFinished)
143 IPC_MESSAGE_UNHANDLED(handled = false)
144 IPC_END_MESSAGE_MAP()
145 return handled;
146 }
147
148 } // namespace picasa
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698