Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / media_galleries / fileapi / safe_iapps_library_parser.cc
bloba4389b7ceed69c8554ca58711bf7d6dd2fdc5c0c
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.
5 #include "chrome/browser/media_galleries/fileapi/safe_iapps_library_parser.h"
7 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h"
8 #include "chrome/common/chrome_utility_messages.h"
9 #include "chrome/common/extensions/chrome_utility_extensions_messages.h"
10 #include "chrome/grit/generated_resources.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/child_process_data.h"
13 #include "ipc/ipc_platform_file.h"
14 #include "ui/base/l10n/l10n_util.h"
16 using content::BrowserThread;
17 using content::UtilityProcessHost;
19 namespace iapps {
21 SafeIAppsLibraryParser::SafeIAppsLibraryParser()
22 : parser_state_(INITIAL_STATE) {}
24 void SafeIAppsLibraryParser::ParseIPhotoLibrary(
25 const base::FilePath& library_file,
26 const IPhotoParserCallback& callback) {
27 library_file_path_ = library_file;
28 iphoto_callback_ = callback;
29 Start();
32 void SafeIAppsLibraryParser::ParseITunesLibrary(
33 const base::FilePath& library_file,
34 const ITunesParserCallback& callback) {
35 library_file_path_ = library_file;
36 itunes_callback_ = callback;
37 Start();
40 void SafeIAppsLibraryParser::Start() {
41 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
43 // |library_file_| will be closed on the IO thread once it has been handed
44 // off to the child process.
45 library_file_.Initialize(library_file_path_,
46 base::File::FLAG_OPEN | base::File::FLAG_READ);
47 if (!library_file_.IsValid()) {
48 VLOG(1) << "Could not open iApps library XML file: "
49 << library_file_path_.value();
50 BrowserThread::PostTask(
51 BrowserThread::IO, FROM_HERE,
52 base::Bind(&SafeIAppsLibraryParser::OnOpenLibraryFileFailed, this));
53 return;
56 BrowserThread::PostTask(
57 BrowserThread::IO, FROM_HERE,
58 base::Bind(&SafeIAppsLibraryParser::StartProcessOnIOThread, this));
61 SafeIAppsLibraryParser::~SafeIAppsLibraryParser() {}
63 void SafeIAppsLibraryParser::StartProcessOnIOThread() {
64 DCHECK_CURRENTLY_ON(BrowserThread::IO);
65 DCHECK_EQ(INITIAL_STATE, parser_state_);
67 scoped_refptr<base::SingleThreadTaskRunner> task_runner =
68 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
69 utility_process_host_ =
70 UtilityProcessHost::Create(this, task_runner.get())->AsWeakPtr();
71 utility_process_host_->SetName(l10n_util::GetStringUTF16(
72 IDS_UTILITY_PROCESS_MEDIA_LIBRARY_FILE_CHECKER_NAME));
73 // Wait for the startup notification before sending the main IPC to the
74 // utility process, so that we can dup the file handle.
75 utility_process_host_->Send(new ChromeUtilityMsg_StartupPing);
76 parser_state_ = PINGED_UTILITY_PROCESS_STATE;
79 void SafeIAppsLibraryParser::OnUtilityProcessStarted() {
80 DCHECK_CURRENTLY_ON(BrowserThread::IO);
81 if (parser_state_ != PINGED_UTILITY_PROCESS_STATE)
82 return;
84 if (utility_process_host_->GetData().handle == base::kNullProcessHandle) {
85 DLOG(ERROR) << "Child process handle is null";
86 OnError();
87 return;
90 if (!itunes_callback_.is_null()) {
91 utility_process_host_->Send(
92 new ChromeUtilityMsg_ParseITunesLibraryXmlFile(
93 IPC::TakeFileHandleForProcess(
94 library_file_.Pass(),
95 utility_process_host_->GetData().handle)));
96 } else if (!iphoto_callback_.is_null()) {
97 #if defined(OS_MACOSX)
98 utility_process_host_->Send(
99 new ChromeUtilityMsg_ParseIPhotoLibraryXmlFile(
100 IPC::TakeFileHandleForProcess(
101 library_file_.Pass(),
102 utility_process_host_->GetData().handle)));
103 #endif
106 parser_state_ = STARTED_PARSING_STATE;
109 #if defined(OS_MACOSX)
110 void SafeIAppsLibraryParser::OnGotIPhotoLibrary(
111 bool result, const iphoto::parser::Library& library) {
112 DCHECK_CURRENTLY_ON(BrowserThread::IO);
113 DCHECK(!iphoto_callback_.is_null());
115 if (parser_state_ != STARTED_PARSING_STATE)
116 return;
118 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
119 FROM_HERE,
120 base::Bind(iphoto_callback_, result, library));
121 parser_state_ = FINISHED_PARSING_STATE;
123 #endif
125 void SafeIAppsLibraryParser::OnGotITunesLibrary(
126 bool result, const itunes::parser::Library& library) {
127 DCHECK_CURRENTLY_ON(BrowserThread::IO);
128 DCHECK(!itunes_callback_.is_null());
130 if (parser_state_ != STARTED_PARSING_STATE)
131 return;
133 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
134 FROM_HERE,
135 base::Bind(itunes_callback_, result, library));
136 parser_state_ = FINISHED_PARSING_STATE;
139 void SafeIAppsLibraryParser::OnOpenLibraryFileFailed() {
140 DCHECK_CURRENTLY_ON(BrowserThread::IO);
141 OnError();
144 void SafeIAppsLibraryParser::OnProcessCrashed(int exit_code) {
145 OnError();
148 void SafeIAppsLibraryParser::OnError() {
149 parser_state_ = FINISHED_PARSING_STATE;
150 if (!itunes_callback_.is_null())
151 OnGotITunesLibrary(false /* failed */, itunes::parser::Library());
153 #if defined(OS_MACOSX)
154 if (!iphoto_callback_.is_null())
155 OnGotIPhotoLibrary(false /* failed */, iphoto::parser::Library());
156 #endif
159 bool SafeIAppsLibraryParser::OnMessageReceived(
160 const IPC::Message& message) {
161 bool handled = true;
162 IPC_BEGIN_MESSAGE_MAP(SafeIAppsLibraryParser, message)
163 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted,
164 OnUtilityProcessStarted)
165 #if defined(OS_MACOSX)
166 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GotIPhotoLibrary,
167 OnGotIPhotoLibrary)
168 #endif
169 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GotITunesLibrary,
170 OnGotITunesLibrary)
171 IPC_MESSAGE_UNHANDLED(handled = false)
172 IPC_END_MESSAGE_MAP()
173 return handled;
176 } // namespace iapps