Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media_galleries / fileapi / safe_iapps_library_parser.h
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 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_IAPPS_LIBRARY_PARSER_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_IAPPS_LIBRARY_PARSER_H_
7
8 #include <string>
9
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/files/file.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/weak_ptr.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"
19
20 namespace IPC {
21 class Message;
22 }
23
24 namespace iapps {
25
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
34 // noted.
35 class SafeIAppsLibraryParser : public content::UtilityProcessHostClient {
36  public:
37   typedef base::Callback<void(bool, const iphoto::parser::Library&)>
38       IPhotoParserCallback;
39   typedef base::Callback<void(bool, const itunes::parser::Library&)>
40       ITunesParserCallback;
41
42   SafeIAppsLibraryParser();
43
44   // Start the parse of the iPhoto library file.
45   void ParseIPhotoLibrary(const base::FilePath& library_file,
46                           const IPhotoParserCallback& callback);
47
48   // Start the parse of the iTunes library file.
49   void ParseITunesLibrary(const base::FilePath& library_file,
50                           const ITunesParserCallback& callback);
51
52
53  private:
54   enum ParserState {
55     INITIAL_STATE,
56     PINGED_UTILITY_PROCESS_STATE,
57     STARTED_PARSING_STATE,
58     FINISHED_PARSING_STATE,
59   };
60
61   // content::UtilityProcessHostClient is ref-counted.
62   ~SafeIAppsLibraryParser() override;
63
64   // Posts a task to start the XML parsing in the utility process.
65   void Start();
66
67   // Launches the utility process.  Must run on the IO thread.
68   void StartProcessOnIOThread();
69
70   // Notification that the utility process is running, and we can now get its
71   // process handle.
72   // Runs on the IO thread.
73   void OnUtilityProcessStarted();
74
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);
79 #endif
80
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);
84
85   // Sets |parser_state_| in case the library XML file cannot be opened.
86   // Runs on the IO thread.
87   void OnOpenLibraryFileFailed();
88
89   // Communicates an error to the callback given to the constructor.
90   void OnError();
91
92   // UtilityProcessHostClient implementation.
93   // Runs on the IO thread.
94   void OnProcessCrashed(int exit_code) override;
95   bool OnMessageReceived(const IPC::Message& message) override;
96
97   base::FilePath library_file_path_;
98
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::File library_file_;
102
103   // Only accessed on the IO thread.
104   base::WeakPtr<content::UtilityProcessHost> utility_process_host_;
105
106   // Only accessed on the Media Task Runner.
107   ITunesParserCallback itunes_callback_;
108
109   // Only accessed on the Media Task Runner.
110   IPhotoParserCallback iphoto_callback_;
111
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_;
115
116   DISALLOW_COPY_AND_ASSIGN(SafeIAppsLibraryParser);
117 };
118
119 }  // namespace iapps
120
121 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_IAPPS_LIBRARY_PARSER_H_