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_PREF_PARSER_WIN_H_
6#define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_ITUNES_PREF_PARSER_WIN_H_
7
8#include <string>
9
10#include "base/callback.h"
11#include "base/compiler_specific.h"
12#include "content/public/browser/utility_process_host_client.h"
13
14namespace base {
15class FilePath;
16}
17
18namespace IPC {
19class Message;
20}
21
22namespace itunes {
23
24// SafeITunesPrefParserWin parses the given iTunes preferences XML data safely
25// via a utility process. The SafeITunesPrefParserWin object is ref-counted and
26// kept alive after Start() is called until the ParserCallback is called.
27// The ParserCallback is guaranteed to be called eventually either when the
28// utility process replies or when it dies.
29class SafeITunesPrefParserWin : public content::UtilityProcessHostClient {
30 public:
31  typedef base::Callback<void(const base::FilePath&)> ParserCallback;
32
33  SafeITunesPrefParserWin(const std::string& unsafe_xml,
34                          const ParserCallback& callback);
35
36  void Start();
37
38 private:
39  enum ParserState {
40    INITIAL_STATE,
41    STARTED_PARSING_STATE,
42    FINISHED_PARSING_STATE,
43  };
44
45  // Private because content::UtilityProcessHostClient is ref-counted.
46  virtual ~SafeITunesPrefParserWin();
47
48  void StartWorkOnIOThread();
49
50  // Handles the results from OnProcessCrashed() and OnMessageReceived() on
51  // the IO thread.
52  void OnGotITunesDirectory(const base::FilePath& library_file);
53
54  // UtilityProcessHostClient implementation.
55  virtual void OnProcessCrashed(int exit_code) OVERRIDE;
56  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
57
58  const std::string unsafe_xml_;
59  const ParserCallback callback_;
60
61  // Verifies the messages from the utility process came at the right time.
62  // Initialized on the FILE thread, but only accessed on the IO thread.
63  ParserState parser_state_;
64
65  DISALLOW_COPY_AND_ASSIGN(SafeITunesPrefParserWin);
66};
67
68}  // namespace itunes
69
70#endif  // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_ITUNES_PREF_PARSER_WIN_H_
71