Home | History | Annotate | Download | only in fileapi
      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_ITUNES_LIBRARY_PARSER_H_
      6 #define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_ITUNES_LIBRARY_PARSER_H_
      7 
      8 #include <string>
      9 
     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/itunes_library.h"
     16 #include "content/public/browser/utility_process_host.h"
     17 #include "content/public/browser/utility_process_host_client.h"
     18 
     19 namespace IPC {
     20 class Message;
     21 }
     22 
     23 namespace itunes {
     24 
     25 // SafeITunesLibraryParser parses the given iTunes library XML file safely via
     26 // a utility process. The SafeITunesLibraryParser object is ref-counted and
     27 // kept alive after Start() is called until the ParserCallback is called.
     28 // The ParserCallback is guaranteed to be called eventually either when the
     29 // utility process replies or when it dies.
     30 // Since iTunes library XML files can be big, SafeITunesLibraryParser passes
     31 // the file handle to the utility process.
     32 // SafeITunesLibraryParser lives on the Media Task Runner unless otherwise
     33 // noted.
     34 class SafeITunesLibraryParser : public content::UtilityProcessHostClient {
     35  public:
     36   typedef base::Callback<void(bool, const parser::Library&)> ParserCallback;
     37 
     38   SafeITunesLibraryParser(const base::FilePath& itunes_library_file,
     39                           const ParserCallback& callback);
     40 
     41   // Posts a task to start the XML parsing in the utility process.
     42   void Start();
     43 
     44  private:
     45   enum ParserState {
     46     INITIAL_STATE,
     47     PINGED_UTILITY_PROCESS_STATE,
     48     STARTED_PARSING_STATE,
     49     FINISHED_PARSING_STATE,
     50   };
     51 
     52   // content::UtilityProcessHostClient is ref-counted.
     53   virtual ~SafeITunesLibraryParser();
     54 
     55   // Launches the utility process.  Must run on the IO thread.
     56   void StartProcessOnIOThread();
     57 
     58   // Notification that the utility process is running, and we can now get its
     59   // process handle.
     60   // Runs on the IO thread.
     61   void OnUtilityProcessStarted();
     62 
     63   // Notification from the utility process when it finishes parsing the XML.
     64   // Runs on the IO thread.
     65   void OnGotITunesLibrary(bool result, const parser::Library& library);
     66 
     67   // Sets |parser_state_| in case the library XML file cannot be opened.
     68   // Runs on the IO thread.
     69   void OnOpenLibraryFileFailed();
     70 
     71   // UtilityProcessHostClient implementation.
     72   // Runs on the IO thread.
     73   virtual void OnProcessCrashed(int exit_code) OVERRIDE;
     74   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     75 
     76   const base::FilePath itunes_library_file_;
     77 
     78   // Once we have opened the file, we store the handle so that we can use it
     79   // once the utility process has launched.
     80   base::PlatformFile itunes_library_platform_file_;
     81 
     82   // Only accessed on the IO thread.
     83   base::WeakPtr<content::UtilityProcessHost> utility_process_host_;
     84 
     85   // Only accessed on the Media Task Runner.
     86   const ParserCallback callback_;
     87 
     88   // Verifies the messages from the utility process came at the right time.
     89   // Initialized on the Media Task Runner, but only accessed on the IO thread.
     90   ParserState parser_state_;
     91 
     92   DISALLOW_COPY_AND_ASSIGN(SafeITunesLibraryParser);
     93 };
     94 
     95 }  // namespace itunes
     96 
     97 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_ITUNES_LIBRARY_PARSER_H_
     98