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 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_IAPPS_LIBRARY_PARSER_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_IAPPS_LIBRARY_PARSER_H_
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/files/file_path.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/platform_file.h"
15 #include "chrome/common/media_galleries/iphoto_library.h"
16 #include "chrome/common/media_galleries/itunes_library.h"
17 #include "content/public/browser/utility_process_host.h"
18 #include "content/public/browser/utility_process_host_client.h"
26 // SafeIAppsLibraryParser parses the given iTunes library XML file safely via
27 // a utility process. The SafeIAppsLibraryParser object is ref-counted and
28 // kept alive after Start() is called until the ParserCallback is called.
29 // The ParserCallback is guaranteed to be called eventually either when the
30 // utility process replies or when it dies.
31 // Since iApps library XML files can be big, SafeIAppsLibraryParser passes
32 // the file handle to the utility process.
33 // SafeIAppsLibraryParser lives on the Media Task Runner unless otherwise
35 class SafeIAppsLibraryParser
: public content::UtilityProcessHostClient
{
37 typedef base::Callback
<void(bool, const iphoto::parser::Library
&)>
39 typedef base::Callback
<void(bool, const itunes::parser::Library
&)>
42 SafeIAppsLibraryParser();
44 // Start the parse of the iPhoto library file.
45 void ParseIPhotoLibrary(const base::FilePath
& library_file
,
46 const IPhotoParserCallback
& callback
);
48 // Start the parse of the iTunes library file.
49 void ParseITunesLibrary(const base::FilePath
& library_file
,
50 const ITunesParserCallback
& callback
);
56 PINGED_UTILITY_PROCESS_STATE
,
57 STARTED_PARSING_STATE
,
58 FINISHED_PARSING_STATE
,
61 // content::UtilityProcessHostClient is ref-counted.
62 virtual ~SafeIAppsLibraryParser();
64 // Posts a task to start the XML parsing in the utility process.
67 // Launches the utility process. Must run on the IO thread.
68 void StartProcessOnIOThread();
70 // Notification that the utility process is running, and we can now get its
72 // Runs on the IO thread.
73 void OnUtilityProcessStarted();
75 // Notification from the utility process when it finishes parsing the
76 // iPhoto XML. Runs on the IO thread.
77 #if defined(OS_MACOSX)
78 void OnGotIPhotoLibrary(bool result
, const iphoto::parser::Library
& library
);
81 // Notification from the utility process when it finishes parsing the
82 // iTunes XML. Runs on the IO thread.
83 void OnGotITunesLibrary(bool result
, const itunes::parser::Library
& library
);
85 // Sets |parser_state_| in case the library XML file cannot be opened.
86 // Runs on the IO thread.
87 void OnOpenLibraryFileFailed();
89 // Communicates an error to the callback given to the constructor.
92 // UtilityProcessHostClient implementation.
93 // Runs on the IO thread.
94 virtual void OnProcessCrashed(int exit_code
) OVERRIDE
;
95 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
97 base::FilePath library_file_
;
99 // Once we have opened the file, we store the handle so that we can use it
100 // once the utility process has launched.
101 base::PlatformFile library_platform_file_
;
103 // Only accessed on the IO thread.
104 base::WeakPtr
<content::UtilityProcessHost
> utility_process_host_
;
106 // Only accessed on the Media Task Runner.
107 ITunesParserCallback itunes_callback_
;
109 // Only accessed on the Media Task Runner.
110 IPhotoParserCallback iphoto_callback_
;
112 // Verifies the messages from the utility process came at the right time.
113 // Initialized on the Media Task Runner, but only accessed on the IO thread.
114 ParserState parser_state_
;
116 DISALLOW_COPY_AND_ASSIGN(SafeIAppsLibraryParser
);
121 #endif // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_IAPPS_LIBRARY_PARSER_H_