summaryrefslogtreecommitdiffstats
path: root/chrome/browser/media_galleries/fileapi/safe_picasa_albums_indexer.h
blob: 45ac9b9140f957da03ec8b6e7b391249625be11e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_PICASA_ALBUMS_INDEXER_H_
#define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_PICASA_ALBUMS_INDEXER_H_

#include <queue>
#include <vector>

#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h"
#include "chrome/common/media_galleries/picasa_types.h"
#include "content/public/browser/utility_process_host_client.h"

namespace base {
class FilePath;
}

namespace IPC {
class Message;
}

namespace picasa {

// SafePicasaAlbumsIndexer indexes the contents of Picasa Albums by parsing the
// INI files found in Folders. The SafePicasaAlbumsIndexer object is ref-counted
// and kept alive after Start() is called until the ParserCallback is called.
// The ParserCallback is guaranteed to be called eventually either when the
// utility process replies or when it dies.
class SafePicasaAlbumsIndexer : public content::UtilityProcessHostClient {
 public:
  typedef base::Callback<
      void(bool parse_success, const picasa::AlbumImagesMap&)>
      DoneCallback;

  SafePicasaAlbumsIndexer(const AlbumMap& albums, const AlbumMap& folders);

  void Start(const DoneCallback& callback);

 private:
  enum ParserState {
    INITIAL_STATE,
    STARTED_PARSING_STATE,
    FINISHED_PARSING_STATE,
  };

  // Private because content::UtilityProcessHostClient is ref-counted.
  ~SafePicasaAlbumsIndexer() override;

  // Processes a batch of folders. Reposts itself until done, then starts IPC.
  void ProcessFoldersBatch();

  // Launches the utility process.  Must run on the IO thread.
  void StartWorkOnIOThread();

  // Notification from the utility process when it finshes indexing all the
  // album contents. On error will return an empty map.
  // Runs on the IO thread.
  void OnIndexPicasaAlbumsContentsFinished(const AlbumImagesMap& albums_images);

  // UtilityProcessHostClient implementation.
  // Runs on the IO thread.
  void OnProcessCrashed(int exit_code) override;
  bool OnMessageReceived(const IPC::Message& message) override;

  AlbumUIDSet album_uids_;

  // List of folders that still need their INI files read.
  std::queue<base::FilePath> folders_queue_;

  std::vector<picasa::FolderINIContents> folders_inis_;

  // Only accessed on the Media Task Runner.
  DoneCallback callback_;

  // Verifies the messages from the utility process came at the right time.
  // Initialized on the Media Task Runner, but only accessed on the IO thread.
  ParserState parser_state_;

  DISALLOW_COPY_AND_ASSIGN(SafePicasaAlbumsIndexer);
};

}  // namespace picasa

#endif  // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_PICASA_ALBUMS_INDEXER_H_