1 // Copyright (c) 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 WIN8_VIEWER_METRO_VIEWER_PROCESS_HOST_H_ 6 #define WIN8_VIEWER_METRO_VIEWER_PROCESS_HOST_H_ 7 8 #include "base/basictypes.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/strings/string16.h" 11 #include "base/threading/non_thread_safe.h" 12 #include "ipc/ipc_channel_proxy.h" 13 #include "ipc/ipc_listener.h" 14 #include "ipc/ipc_sender.h" 15 #include "ui/gfx/native_widget_types.h" 16 17 namespace base { 18 class SingleThreadTaskRunner; 19 class WaitableEvent; 20 } 21 22 namespace IPC { 23 class Message; 24 } 25 26 namespace win8 { 27 28 // Abstract base class for various Metro viewer process host implementations. 29 class MetroViewerProcessHost : public IPC::Listener, 30 public IPC::Sender, 31 public base::NonThreadSafe { 32 public: 33 // Initializes a viewer process host to connect to the Metro viewer process 34 // over IPC. The given task runner correspond to a thread on which 35 // IPC::Channel is created and used (e.g. IO thread). Instantly connects to 36 // the viewer process if one is already connected to |ipc_channel_name|; a 37 // viewer can otherwise be launched synchronously via 38 // LaunchViewerAndWaitForConnection(). 39 explicit MetroViewerProcessHost( 40 base::SingleThreadTaskRunner* ipc_task_runner); 41 virtual ~MetroViewerProcessHost(); 42 43 // Returns the process id of the viewer process if one is connected to this 44 // host, returns base::kNullProcessId otherwise. 45 base::ProcessId GetViewerProcessId(); 46 47 // Launches the viewer process associated with the given |app_user_model_id| 48 // and blocks until that viewer process connects or until a timeout is 49 // reached. Returns true if the viewer process connects before the timeout is 50 // reached. NOTE: this assumes that the app referred to by |app_user_model_id| 51 // is registered as the default browser. 52 bool LaunchViewerAndWaitForConnection( 53 const base::string16& app_user_model_id); 54 55 private: 56 // IPC::Sender implementation: 57 virtual bool Send(IPC::Message* msg) OVERRIDE; 58 59 // IPC::Listener implementation: 60 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 61 virtual void OnChannelError() OVERRIDE = 0; 62 63 // Called over IPC by the viewer process to tell this host that it should be 64 // drawing to |target_surface|. 65 virtual void OnSetTargetSurface(gfx::NativeViewId target_surface) = 0; 66 67 // Called over IPC by the viewer process to request that the url passed in be 68 // opened. 69 virtual void OnOpenURL(const string16& url) = 0; 70 71 // Called over IPC by the viewer process to request that the search string 72 // passed in is passed to the default search provider and a URL navigation be 73 // performed. 74 virtual void OnHandleSearchRequest(const string16& search_string) = 0; 75 76 void NotifyChannelConnected(); 77 78 // Inner message filter used to handle connection event on the IPC channel 79 // proxy's background thread. This prevents consumers of 80 // MetroViewerProcessHost from having to pump messages on their own message 81 // loop. 82 class InternalMessageFilter : public IPC::ChannelProxy::MessageFilter { 83 public: 84 InternalMessageFilter(MetroViewerProcessHost* owner); 85 86 // IPC::ChannelProxy::MessageFilter implementation. 87 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; 88 89 private: 90 MetroViewerProcessHost* owner_; 91 DISALLOW_COPY_AND_ASSIGN(InternalMessageFilter); 92 }; 93 94 scoped_ptr<IPC::ChannelProxy> channel_; 95 scoped_ptr<base::WaitableEvent> channel_connected_event_; 96 97 DISALLOW_COPY_AND_ASSIGN(MetroViewerProcessHost); 98 }; 99 100 } // namespace win8 101 102 #endif // WIN8_VIEWER_METRO_VIEWER_PROCESS_HOST_H_ 103