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.
5 // This implements a browser-side endpoint for UI automation activity.
6 // The client-side endpoint is implemented by AutomationProxy.
7 // The entire lifetime of this object should be contained within that of
8 // the BrowserProcess, and in particular the NotificationService that's
11 #ifndef CHROME_BROWSER_AUTOMATION_AUTOMATION_PROVIDER_H_
12 #define CHROME_BROWSER_AUTOMATION_AUTOMATION_PROVIDER_H_
19 #include "base/basictypes.h"
20 #include "base/compiler_specific.h"
21 #include "base/memory/scoped_ptr.h"
22 #include "base/memory/weak_ptr.h"
23 #include "base/observer_list.h"
24 #include "base/sequenced_task_runner_helpers.h"
25 #include "base/strings/string16.h"
26 #include "chrome/browser/common/cancelable_request.h"
27 #include "chrome/common/automation_constants.h"
28 #include "chrome/common/content_settings.h"
29 #include "components/autofill/core/browser/field_types.h"
30 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/notification_observer.h"
32 #include "ipc/ipc_channel.h"
33 #include "ipc/ipc_listener.h"
34 #include "ipc/ipc_sender.h"
36 class AutomationBrowserTracker
;
37 class AutomationTabTracker
;
38 class AutomationWindowTracker
;
40 class ExternalTabContainer
;
41 class FindInPageNotificationObserver
;
42 class InitialLoadObserver
;
44 class MetricEventDurationObserver
;
45 class NavigationControllerRestoredObserver
;
47 struct AutomationMsg_Find_Params
;
54 class NavigationController
;
59 class DictionaryValue
;
71 class AutomationProvider
72 : public IPC::Listener
,
74 public base::SupportsWeakPtr
<AutomationProvider
>,
75 public base::RefCountedThreadSafe
<
76 AutomationProvider
, content::BrowserThread::DeleteOnUIThread
> {
78 explicit AutomationProvider(Profile
* profile
);
80 Profile
* profile() const { return profile_
; }
82 void set_profile(Profile
* profile
);
84 // Initializes a channel for a connection to an AutomationProxy.
85 // If channel_id starts with kNamedInterfacePrefix, it will act
86 // as a server, create a named IPC socket with channel_id as its
87 // path, and will listen on the socket for incoming connections.
88 // If channel_id does not, it will act as a client and establish
89 // a connection on its primary IPC channel. See ipc/ipc_channel_posix.cc
90 // for more information about kPrimaryIPCChannel.
91 bool InitializeChannel(const std::string
& channel_id
) WARN_UNUSED_RESULT
;
93 virtual IPC::Channel::Mode
GetChannelMode(bool use_named_interface
);
95 // Sets the number of tabs that we expect; when this number of tabs has
96 // loaded, an AutomationMsg_InitialLoadsComplete message is sent.
97 void SetExpectedTabCount(size_t expected_tabs
);
99 // Called when the inital set of tabs has finished loading.
100 // Call SetExpectedTabCount(0) to set this to true immediately.
101 void OnInitialTabLoadsComplete();
103 // Called when the chromeos WebUI OOBE/Login is ready.
104 void OnOOBEWebuiReady();
106 // Checks all of the initial load conditions, then sends the
107 // InitialLoadsComplete message over the automation channel.
108 void SendInitialLoadMessage();
110 // Call this before calling InitializeChannel. If called, send the
111 // InitialLoadsComplete message immediately when the automation channel is
112 // connected, without waiting for the initial load conditions to be met.
113 void DisableInitialLoadObservers();
115 // Get the index of a particular NavigationController object
116 // in the given parent window. This method uses
117 // TabStrip::GetIndexForNavigationController to get the index.
118 int GetIndexForNavigationController(
119 const content::NavigationController
* controller
,
120 const Browser
* parent
) const;
122 // IPC::Sender implementation.
123 virtual bool Send(IPC::Message
* msg
) OVERRIDE
;
125 // IPC::Listener implementation.
126 virtual void OnChannelConnected(int pid
) OVERRIDE
;
127 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
128 virtual void OnChannelError() OVERRIDE
;
130 IPC::Message
* reply_message_release() {
131 IPC::Message
* reply_message
= reply_message_
;
132 reply_message_
= NULL
;
133 return reply_message
;
136 // Get the DictionaryValue equivalent for a download item. Caller owns the
138 base::DictionaryValue
* GetDictionaryFromDownloadItem(
139 const content::DownloadItem
* download
,
143 friend struct content::BrowserThread::DeleteOnThread
<
144 content::BrowserThread::UI
>;
145 friend class base::DeleteHelper
<AutomationProvider
>;
146 virtual ~AutomationProvider();
148 // Helper function to find the browser window that contains a given
149 // NavigationController and activate that tab.
150 // Returns the Browser if found.
151 Browser
* FindAndActivateTab(content::NavigationController
* contents
);
153 // Convert a tab handle into a WebContents. If |tab| is non-NULL a pointer
154 // to the tab is also returned. Returns NULL in case of failure or if the tab
155 // is not of the WebContents type.
156 content::WebContents
* GetWebContentsForHandle(
157 int handle
, content::NavigationController
** tab
);
159 // Returns the protocol version which typically is the module version.
160 virtual std::string
GetProtocolVersion();
162 // Returns the associated view for the tab handle passed in.
163 // Returns NULL on failure.
164 content::RenderViewHost
* GetViewForTab(int tab_handle
);
166 // Called on IPC message deserialization failure. Prints an error message
167 // and closes the IPC channel.
168 void OnMessageDeserializationFailure();
170 scoped_ptr
<AutomationBrowserTracker
> browser_tracker_
;
171 scoped_ptr
<InitialLoadObserver
> initial_load_observer_
;
172 scoped_ptr
<MetricEventDurationObserver
> metric_event_duration_observer_
;
173 scoped_ptr
<AutomationTabTracker
> tab_tracker_
;
174 scoped_ptr
<AutomationWindowTracker
> window_tracker_
;
178 // A pointer to reply message used when we do asynchronous processing in the
180 // TODO(phajdan.jr): Remove |reply_message_|, it is error-prone.
181 IPC::Message
* reply_message_
;
183 // Consumer for asynchronous history queries.
184 CancelableRequestConsumer consumer_
;
186 // Sends a find request for a given query.
187 void SendFindRequest(
188 content::WebContents
* web_contents
,
190 const base::string16
& search_string
,
194 IPC::Message
* reply_message
);
196 // True iff we should open a new automation IPC channel if it closes.
197 bool reinitialize_on_channel_error_
;
200 void OnUnhandledMessage(const IPC::Message
& message
);
202 // Clear and reinitialize the automation IPC channel.
203 bool ReinitializeChannel();
205 void HandleUnused(const IPC::Message
& message
, int handle
);
206 void GetFilteredInetHitCount(int* hit_count
);
208 // Responds to the FindInPage request, retrieves the search query parameters,
209 // launches an observer to listen for results and issues a StartFind request.
210 void HandleFindRequest(int handle
,
211 const AutomationMsg_Find_Params
& params
,
212 IPC::Message
* reply_message
);
214 void OnSetPageFontSize(int tab_handle
, int font_size
);
216 // Notify the JavaScript engine in the render to change its parameters
217 // while performing stress testing. See
218 // |ViewHostMsg_JavaScriptStressTestControl_Commands| in render_messages.h
219 // for information on the arguments.
220 void JavaScriptStressTestControl(int handle
, int cmd
, int param
);
222 void BeginTracing(const std::string
& category_patterns
, bool* success
);
223 void EndTracing(IPC::Message
* reply_message
);
224 void OnTraceDataCollected(IPC::Message
* reply_message
,
225 const base::FilePath
& path
);
227 // Uses the specified encoding to override the encoding of the page in the
229 void OverrideEncoding(int tab_handle
,
230 const std::string
& encoding_name
,
233 // Selects all contents on the page.
234 void SelectAll(int tab_handle
);
236 // Edit operations on the page.
237 void Cut(int tab_handle
);
238 void Copy(int tab_handle
);
239 void Paste(int tab_handle
);
241 void ReloadAsync(int tab_handle
);
242 void StopAsync(int tab_handle
);
244 // Method called by the popup menu tracker when a popup menu is opened.
245 void NotifyPopupMenuOpened();
247 scoped_ptr
<IPC::ChannelProxy
> channel_
;
248 scoped_ptr
<FindInPageNotificationObserver
> find_in_page_observer_
;
250 // True iff we should enable observers that check for initial load conditions.
251 bool use_initial_load_observers_
;
253 // True iff connected to an AutomationProxy.
256 // True iff browser finished loading initial set of tabs.
257 bool initial_tab_loads_complete_
;
259 // True iff ChromeOS webui login ui is ready.
260 bool login_webui_ready_
;
262 // ID of automation channel.
263 std::string channel_id_
;
265 DISALLOW_COPY_AND_ASSIGN(AutomationProvider
);
268 #endif // CHROME_BROWSER_AUTOMATION_AUTOMATION_PROVIDER_H_