- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / media_galleries / fileapi / safe_picasa_albums_indexer.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_PICASA_ALBUMS_INDEXER_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_PICASA_ALBUMS_INDEXER_H_
7
8 #include <queue>
9 #include <vector>
10
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/weak_ptr.h"
14 #include "chrome/common/media_galleries/picasa_types.h"
15 #include "content/public/browser/utility_process_host_client.h"
16
17 namespace base {
18 class FilePath;
19 }
20
21 namespace IPC {
22 class Message;
23 }
24
25 namespace picasa {
26
27 // SafePicasaAlbumsIndexer indexes the contents of Picasa Albums by parsing the
28 // INI files found in Folders. The SafePicasaAlbumsIndexer object is ref-counted
29 // and kept alive after Start() is called until the ParserCallback is called.
30 // The ParserCallback is guaranteed to be called eventually either when the
31 // utility process replies or when it dies.
32 class SafePicasaAlbumsIndexer : public content::UtilityProcessHostClient {
33  public:
34   typedef base::Callback<
35       void(bool parse_success, const picasa::AlbumImagesMap&)>
36       DoneCallback;
37
38   SafePicasaAlbumsIndexer(const AlbumMap& albums, const AlbumMap& folders);
39
40   void Start(const DoneCallback& callback);
41
42  private:
43   enum ParserState {
44     INITIAL_STATE,
45     STARTED_PARSING_STATE,
46     FINISHED_PARSING_STATE,
47   };
48
49   // Private because content::UtilityProcessHostClient is ref-counted.
50   virtual ~SafePicasaAlbumsIndexer();
51
52   // Processes a batch of folders. Reposts itself until done, then starts IPC.
53   void ProcessFoldersBatch();
54
55   // Launches the utility process.  Must run on the IO thread.
56   void StartWorkOnIOThread();
57
58   // Notification from the utility process when it finshes indexing all the
59   // album contents. On error will return an empty map.
60   // Runs on the IO thread.
61   void OnIndexPicasaAlbumsContentsFinished(const AlbumImagesMap& albums_images);
62
63   // UtilityProcessHostClient implementation.
64   // Runs on the IO thread.
65   virtual void OnProcessCrashed(int exit_code) OVERRIDE;
66   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
67
68   AlbumUIDSet album_uids_;
69
70   // List of folders that still need their INI files read.
71   std::queue<base::FilePath> folders_queue_;
72
73   std::vector<picasa::FolderINIContents> folders_inis_;
74
75   // Only accessed on the Media Task Runner.
76   DoneCallback callback_;
77
78   // Verifies the messages from the utility process came at the right time.
79   // Initialized on the Media Task Runner, but only accessed on the IO thread.
80   ParserState parser_state_;
81
82   DISALLOW_COPY_AND_ASSIGN(SafePicasaAlbumsIndexer);
83 };
84
85 }  // namespace picasa
86
87 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_PICASA_ALBUMS_INDEXER_H_