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.
5 #ifndef WIN8_VIEWER_METRO_VIEWER_PROCESS_HOST_H_
6 #define WIN8_VIEWER_METRO_VIEWER_PROCESS_HOST_H_
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string16.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "ipc/ipc_channel_proxy.h"
15 #include "ipc/ipc_listener.h"
16 #include "ipc/ipc_sender.h"
17 #include "ipc/message_filter.h"
18 #include "ui/gfx/native_widget_types.h"
19 #include "win8/viewer/metro_viewer_exports.h"
22 class SingleThreadTaskRunner
;
33 // Abstract base class for various Metro viewer process host implementations.
34 class METRO_VIEWER_EXPORT MetroViewerProcessHost
: public IPC::Listener
,
36 public base::NonThreadSafe
{
38 typedef base::Callback
<void(const base::FilePath
&, int, void*)>
41 typedef base::Callback
<void(const std::vector
<base::FilePath
>&, void*)>
42 OpenMultipleFilesCompletion
;
44 typedef base::Callback
<void(const base::FilePath
&, int, void*)>
47 typedef base::Callback
<void(const base::FilePath
&, int, void*)>
48 SelectFolderCompletion
;
50 typedef base::Callback
<void(void*)> FileSelectionCanceled
;
52 // Initializes a viewer process host to connect to the Metro viewer process
53 // over IPC. The given task runner correspond to a thread on which
54 // IPC::Channel is created and used (e.g. IO thread). Instantly connects to
55 // the viewer process if one is already connected to |ipc_channel_name|; a
56 // viewer can otherwise be launched synchronously via
57 // LaunchViewerAndWaitForConnection().
58 explicit MetroViewerProcessHost(
59 const scoped_refptr
<base::SingleThreadTaskRunner
>& ipc_task_runner
);
60 virtual ~MetroViewerProcessHost();
62 // Returns the process id of the viewer process if one is connected to this
63 // host, returns base::kNullProcessId otherwise.
64 base::ProcessId
GetViewerProcessId();
66 // Launches the viewer process associated with the given |app_user_model_id|
67 // and blocks until that viewer process connects or until a timeout is
68 // reached. Returns true if the viewer process connects before the timeout is
69 // reached. NOTE: this assumes that the app referred to by |app_user_model_id|
70 // is registered as the default browser.
71 bool LaunchViewerAndWaitForConnection(
72 const base::string16
& app_user_model_id
);
74 // Handles the activate desktop command for Metro Chrome Ash. The |ash_exit|
75 // parameter indicates whether the Ash process would be shutdown after
76 // activating the desktop.
77 static void HandleActivateDesktop(const base::FilePath
& shortcut
,
80 // Handles the metro exit command. Notifies the metro viewer to shutdown
82 static void HandleMetroExit();
84 // Handles the open file operation for Metro Chrome Ash. The on_success
85 // callback passed in is invoked when we receive the opened file name from
86 // the metro viewer. The on failure callback is invoked on failure.
87 static void HandleOpenFile(const base::string16
& title
,
88 const base::FilePath
& default_path
,
89 const base::string16
& filter
,
90 const OpenFileCompletion
& on_success
,
91 const FileSelectionCanceled
& on_failure
);
93 // Handles the open multiple file operation for Metro Chrome Ash. The
94 // on_success callback passed in is invoked when we receive the opened file
95 // names from the metro viewer. The on failure callback is invoked on failure.
96 static void HandleOpenMultipleFiles(
97 const base::string16
& title
,
98 const base::FilePath
& default_path
,
99 const base::string16
& filter
,
100 const OpenMultipleFilesCompletion
& on_success
,
101 const FileSelectionCanceled
& on_failure
);
103 // Handles the save file operation for Metro Chrome Ash. The on_success
104 // callback passed in is invoked when we receive the saved file name from
105 // the metro viewer. The on failure callback is invoked on failure.
106 static void HandleSaveFile(const base::string16
& title
,
107 const base::FilePath
& default_path
,
108 const base::string16
& filter
,
110 const base::string16
& default_extension
,
111 const SaveFileCompletion
& on_success
,
112 const FileSelectionCanceled
& on_failure
);
114 // Handles the select folder for Metro Chrome Ash. The on_success
115 // callback passed in is invoked when we receive the folder name from the
116 // metro viewer. The on failure callback is invoked on failure.
117 static void HandleSelectFolder(const base::string16
& title
,
118 const SelectFolderCompletion
& on_success
,
119 const FileSelectionCanceled
& on_failure
);
122 // IPC::Sender implementation:
123 virtual bool Send(IPC::Message
* msg
) override
;
125 // IPC::Listener implementation:
126 virtual bool OnMessageReceived(const IPC::Message
& message
) override
;
127 virtual void OnChannelError() override
= 0;
130 // The following are the implementation for the corresponding static methods
131 // above, see them for descriptions.
132 void HandleOpenFileImpl(const base::string16
& title
,
133 const base::FilePath
& default_path
,
134 const base::string16
& filter
,
135 const OpenFileCompletion
& on_success
,
136 const FileSelectionCanceled
& on_failure
);
137 void HandleOpenMultipleFilesImpl(
138 const base::string16
& title
,
139 const base::FilePath
& default_path
,
140 const base::string16
& filter
,
141 const OpenMultipleFilesCompletion
& on_success
,
142 const FileSelectionCanceled
& on_failure
);
143 void HandleSaveFileImpl(const base::string16
& title
,
144 const base::FilePath
& default_path
,
145 const base::string16
& filter
,
147 const base::string16
& default_extension
,
148 const SaveFileCompletion
& on_success
,
149 const FileSelectionCanceled
& on_failure
);
150 void HandleSelectFolderImpl(const base::string16
& title
,
151 const SelectFolderCompletion
& on_success
,
152 const FileSelectionCanceled
& on_failure
);
154 // Called over IPC by the viewer process to tell this host that it should be
155 // drawing to |target_surface|.
156 virtual void OnSetTargetSurface(gfx::NativeViewId target_surface
,
157 float device_scale
) = 0;
159 // Called over IPC by the viewer process to request that the url passed in be
161 virtual void OnOpenURL(const base::string16
& url
) = 0;
163 // Called over IPC by the viewer process to request that the search string
164 // passed in is passed to the default search provider and a URL navigation be
166 virtual void OnHandleSearchRequest(const base::string16
& search_string
) = 0;
168 // Called over IPC by the viewer process when the window size has changed.
169 virtual void OnWindowSizeChanged(uint32 width
, uint32 height
) = 0;
171 void NotifyChannelConnected();
173 // IPC message handing methods:
174 void OnFileSaveAsDone(bool success
,
175 const base::FilePath
& filename
,
177 void OnFileOpenDone(bool success
, const base::FilePath
& filename
);
178 void OnMultiFileOpenDone(bool success
,
179 const std::vector
<base::FilePath
>& files
);
180 void OnSelectFolderDone(bool success
, const base::FilePath
& folder
);
182 // Inner message filter used to handle connection event on the IPC channel
183 // proxy's background thread. This prevents consumers of
184 // MetroViewerProcessHost from having to pump messages on their own message
186 class InternalMessageFilter
: public IPC::MessageFilter
{
188 InternalMessageFilter(MetroViewerProcessHost
* owner
);
190 // IPC::MessageFilter implementation.
191 virtual void OnChannelConnected(int32 peer_pid
) override
;
194 MetroViewerProcessHost
* owner_
;
195 DISALLOW_COPY_AND_ASSIGN(InternalMessageFilter
);
198 scoped_ptr
<IPC::ChannelProxy
> channel_
;
199 scoped_ptr
<base::WaitableEvent
> channel_connected_event_
;
200 scoped_refptr
<InternalMessageFilter
> message_filter_
;
202 static MetroViewerProcessHost
* instance_
;
204 // Saved callbacks which inform the caller about the result of the open file/
205 // save file/select operations.
206 OpenFileCompletion file_open_completion_callback_
;
207 OpenMultipleFilesCompletion multi_file_open_completion_callback_
;
208 SaveFileCompletion file_saveas_completion_callback_
;
209 SelectFolderCompletion select_folder_completion_callback_
;
210 FileSelectionCanceled failure_callback_
;
212 DISALLOW_COPY_AND_ASSIGN(MetroViewerProcessHost
);
217 #endif // WIN8_VIEWER_METRO_VIEWER_PROCESS_HOST_H_