12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#ifndef CHROME_BROWSER_APPS_APP_SHIM_APP_SHIM_HOST_MAC_H_
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define CHROME_BROWSER_APPS_APP_SHIM_APP_SHIM_HOST_MAC_H_
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include <vector>
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/files/file_path.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/threading/non_thread_safe.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/apps/app_shim/app_shim_handler_mac.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ipc/ipc_listener.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ipc/ipc_sender.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace IPC {
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)struct ChannelHandle;
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ChannelProxy;
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Message;
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace IPC
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This is the counterpart to AppShimController in
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// chrome/app/chrome_main_app_mode_mac.mm. The AppShimHost owns itself, and is
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// destroyed when the app it corresponds to is closed or when the channel
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// connected to the app shim is closed.
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class AppShimHost : public IPC::Listener,
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    public IPC::Sender,
30a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                    public apps::AppShimHandler::Host,
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    public base::NonThreadSafe {
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AppShimHost();
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~AppShimHost();
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Creates a new server-side IPC channel at |handle|, which should contain a
375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // file descriptor of a channel created by an UnixDomainSocketAcceptor,
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // and begins listening for messages on it.
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void ServeChannel(const IPC::ChannelHandle& handle);
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
41a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) protected:
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // IPC::Listener implementation.
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
44a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  virtual void OnChannelError() OVERRIDE;
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // IPC::Sender implementation.
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool Send(IPC::Message* message) OVERRIDE;
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // The app shim process is requesting to be associated with the given profile
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // and app_id. Once the profile and app_id are stored, and all future
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // messages from the app shim relate to this app. The app is launched
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // immediately if |launch_now| is true.
5458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void OnLaunchApp(const base::FilePath& profile_dir,
5558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                   const std::string& app_id,
5658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                   apps::AppShimLaunchType launch_type,
5758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                   const std::vector<base::FilePath>& files);
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
59eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Called when the app shim process notifies that the app was focused.
6058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void OnFocus(apps::AppShimFocusType focus_type,
6158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)               const std::vector<base::FilePath>& files);
62eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
63eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  void OnSetHidden(bool hidden);
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Called when the app shim process notifies that the app should quit.
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void OnQuit();
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // apps::AppShimHandler::Host overrides:
697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual void OnAppLaunchComplete(apps::AppShimLaunchResult result) OVERRIDE;
70a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  virtual void OnAppClosed() OVERRIDE;
7158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual void OnAppHide() OVERRIDE;
7203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  virtual void OnAppRequestUserAttention(
7303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      apps::AppShimAttentionType type) OVERRIDE;
74eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual base::FilePath GetProfilePath() const OVERRIDE;
7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual std::string GetAppId() const OVERRIDE;
76a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Closes the channel and destroys the AppShimHost.
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void Close();
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<IPC::ChannelProxy> channel_;
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string app_id_;
82eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  base::FilePath profile_path_;
837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  bool initial_launch_finished_;
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif  // CHROME_BROWSER_APPS_APP_SHIM_APP_SHIM_HOST_MAC_H_
87