1// Copyright (c) 2012 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_EXTENSIONS_API_SYNC_FILE_SYSTEM_EXTENSION_SYNC_EVENT_OBSERVER_H_
6#define CHROME_BROWSER_EXTENSIONS_API_SYNC_FILE_SYSTEM_EXTENSION_SYNC_EVENT_OBSERVER_H_
7
8#include "base/basictypes.h"
9#include "base/compiler_specific.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/values.h"
12#include "chrome/browser/sync_file_system/sync_event_observer.h"
13#include "extensions/browser/browser_context_keyed_api_factory.h"
14
15namespace content {
16class BrowserContext;
17}
18
19namespace sync_file_system {
20class SyncFileSystemService;
21}
22
23namespace extensions {
24
25// Observes changes in SyncFileSystem and relays events to JS Extension API.
26class ExtensionSyncEventObserver : public sync_file_system::SyncEventObserver,
27                                   public BrowserContextKeyedAPI {
28 public:
29  static BrowserContextKeyedAPIFactory<ExtensionSyncEventObserver>*
30      GetFactoryInstance();
31
32  explicit ExtensionSyncEventObserver(content::BrowserContext* context);
33  virtual ~ExtensionSyncEventObserver();
34
35  void InitializeForService(
36      sync_file_system::SyncFileSystemService* sync_service);
37
38  // KeyedService override.
39  virtual void Shutdown() OVERRIDE;
40
41  // sync_file_system::SyncEventObserver interface implementation.
42  virtual void OnSyncStateUpdated(
43      const GURL& app_origin,
44      sync_file_system::SyncServiceState state,
45      const std::string& description) OVERRIDE;
46
47  virtual void OnFileSynced(const storage::FileSystemURL& url,
48                            sync_file_system::SyncFileStatus status,
49                            sync_file_system::SyncAction action,
50                            sync_file_system::SyncDirection direction) OVERRIDE;
51
52 private:
53  friend class BrowserContextKeyedAPIFactory<ExtensionSyncEventObserver>;
54
55  // Returns an empty string if the extension |app_origin| cannot be found
56  // in the installed extension list.
57  std::string GetExtensionId(const GURL& app_origin);
58
59  // BrowserContextKeyedAPI implementation.
60  static const char* service_name() { return "ExtensionSyncEventObserver"; }
61  static const bool kServiceIsCreatedWithBrowserContext = false;
62
63  content::BrowserContext* browser_context_;
64
65  // Not owned. If not null, then this is registered to SyncFileSystemService.
66  sync_file_system::SyncFileSystemService* sync_service_;
67
68  void BroadcastOrDispatchEvent(const GURL& app_origin,
69                                const std::string& event_name,
70                                scoped_ptr<base::ListValue> value);
71
72  DISALLOW_COPY_AND_ASSIGN(ExtensionSyncEventObserver);
73};
74
75template <>
76void BrowserContextKeyedAPIFactory<
77    ExtensionSyncEventObserver>::DeclareFactoryDependencies();
78
79}  // namespace extensions
80
81#endif  // CHROME_BROWSER_EXTENSIONS_API_SYNC_FILE_SYSTEM_EXTENSION_SYNC_EVENT_OBSERVER_H_
82