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_APPS_APP_SHIM_APP_SHIM_HOST_MANAGER_MAC_H_
6#define CHROME_BROWSER_APPS_APP_SHIM_APP_SHIM_HOST_MANAGER_MAC_H_
7
8#include "base/memory/ref_counted.h"
9#include "chrome/browser/apps/app_shim/extension_app_shim_handler_mac.h"
10#include "chrome/browser/apps/app_shim/unix_domain_socket_acceptor.h"
11#include "content/public/browser/browser_thread.h"
12
13namespace base {
14class FilePath;
15}
16
17namespace test {
18class AppShimHostManagerTestApi;
19}
20
21// The AppShimHostManager receives connections from app shims on a UNIX
22// socket (|acceptor_|) and creates a helper object to manage the connection.
23class AppShimHostManager : public apps::UnixDomainSocketAcceptor::Delegate,
24                           public base::RefCountedThreadSafe<
25                               AppShimHostManager,
26                               content::BrowserThread::DeleteOnUIThread> {
27 public:
28  AppShimHostManager();
29
30  // Init passes this AppShimHostManager to PostTask which requires it to have
31  // a non-zero refcount. Therefore, Init cannot be called in the constructor
32  // since the refcount is zero at that point.
33  void Init();
34
35  apps::ExtensionAppShimHandler* extension_app_shim_handler() {
36    return &extension_app_shim_handler_;
37  }
38
39 private:
40  friend class base::RefCountedThreadSafe<AppShimHostManager>;
41  friend struct content::BrowserThread::DeleteOnThread<
42      content::BrowserThread::UI>;
43  friend class base::DeleteHelper<AppShimHostManager>;
44  friend class test::AppShimHostManagerTestApi;
45  virtual ~AppShimHostManager();
46
47  // UnixDomainSocketAcceptor::Delegate implementation.
48  virtual void OnClientConnected(const IPC::ChannelHandle& handle) OVERRIDE;
49  virtual void OnListenError() OVERRIDE;
50
51  // The |acceptor_| must be created on a thread which allows blocking I/O, so
52  // part of the initialization of this class must be carried out on the file
53  // thread.
54  void InitOnFileThread();
55
56  // Called on the IO thread to begin listening for connections from app shims.
57  void ListenOnIOThread();
58
59  // The AppShimHostManager is only initialized if the Chrome process
60  // successfully took the singleton lock. This prevents subsequent processes
61  // from deleting existing app shim socket files.
62  bool did_init_;
63
64  base::FilePath directory_in_tmp_;
65
66  scoped_ptr<apps::UnixDomainSocketAcceptor> acceptor_;
67
68  apps::ExtensionAppShimHandler extension_app_shim_handler_;
69
70  DISALLOW_COPY_AND_ASSIGN(AppShimHostManager);
71};
72
73#endif  // CHROME_BROWSER_APPS_APP_SHIM_APP_SHIM_HOST_MANAGER_MAC_H_
74