Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / automation / automation_provider.h
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.
4
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
9 // hung off of it.
10
11 #ifndef CHROME_BROWSER_AUTOMATION_AUTOMATION_PROVIDER_H_
12 #define CHROME_BROWSER_AUTOMATION_AUTOMATION_PROVIDER_H_
13
14 #include <list>
15 #include <map>
16 #include <string>
17 #include <vector>
18
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"
35
36 class AutomationBrowserTracker;
37 class AutomationTabTracker;
38 class AutomationWindowTracker;
39 class Browser;
40 class ExternalTabContainer;
41 class FindInPageNotificationObserver;
42 class InitialLoadObserver;
43 class LoginHandler;
44 class MetricEventDurationObserver;
45 class NavigationControllerRestoredObserver;
46 class Profile;
47 struct AutomationMsg_Find_Params;
48
49 namespace IPC {
50 class ChannelProxy;
51 }
52
53 namespace content {
54 class NavigationController;
55 class RenderViewHost;
56 }
57
58 namespace base {
59 class DictionaryValue;
60 }
61
62 namespace content {
63 class DownloadItem;
64 class WebContents;
65 }
66
67 namespace gfx {
68 class Point;
69 }
70
71 class AutomationProvider
72     : public IPC::Listener,
73       public IPC::Sender,
74       public base::SupportsWeakPtr<AutomationProvider>,
75       public base::RefCountedThreadSafe<
76           AutomationProvider, content::BrowserThread::DeleteOnUIThread> {
77  public:
78   explicit AutomationProvider(Profile* profile);
79
80   Profile* profile() const { return profile_; }
81
82   void set_profile(Profile* profile);
83
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;
92
93   virtual IPC::Channel::Mode GetChannelMode(bool use_named_interface);
94
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);
98
99   // Called when the inital set of tabs has finished loading.
100   // Call SetExpectedTabCount(0) to set this to true immediately.
101   void OnInitialTabLoadsComplete();
102
103   // Called when the chromeos WebUI OOBE/Login is ready.
104   void OnOOBEWebuiReady();
105
106   // Checks all of the initial load conditions, then sends the
107   // InitialLoadsComplete message over the automation channel.
108   void SendInitialLoadMessage();
109
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();
114
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;
121
122   // IPC::Sender implementation.
123   virtual bool Send(IPC::Message* msg) OVERRIDE;
124
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;
129
130   IPC::Message* reply_message_release() {
131     IPC::Message* reply_message = reply_message_;
132     reply_message_ = NULL;
133     return reply_message;
134   }
135
136   // Get the DictionaryValue equivalent for a download item. Caller owns the
137   // DictionaryValue.
138   base::DictionaryValue* GetDictionaryFromDownloadItem(
139       const content::DownloadItem* download,
140       bool incognito);
141
142  protected:
143   friend struct content::BrowserThread::DeleteOnThread<
144       content::BrowserThread::UI>;
145   friend class base::DeleteHelper<AutomationProvider>;
146   virtual ~AutomationProvider();
147
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);
152
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);
158
159   // Returns the protocol version which typically is the module version.
160   virtual std::string GetProtocolVersion();
161
162   // Returns the associated view for the tab handle passed in.
163   // Returns NULL on failure.
164   content::RenderViewHost* GetViewForTab(int tab_handle);
165
166   // Called on IPC message deserialization failure. Prints an error message
167   // and closes the IPC channel.
168   void OnMessageDeserializationFailure();
169
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_;
175
176   Profile* profile_;
177
178   // A pointer to reply message used when we do asynchronous processing in the
179   // message handler.
180   // TODO(phajdan.jr): Remove |reply_message_|, it is error-prone.
181   IPC::Message* reply_message_;
182
183   // Consumer for asynchronous history queries.
184   CancelableRequestConsumer consumer_;
185
186   // Sends a find request for a given query.
187   void SendFindRequest(
188       content::WebContents* web_contents,
189       bool with_json,
190       const base::string16& search_string,
191       bool forward,
192       bool match_case,
193       bool find_next,
194       IPC::Message* reply_message);
195
196   // True iff we should open a new automation IPC channel if it closes.
197   bool reinitialize_on_channel_error_;
198
199  private:
200   void OnUnhandledMessage(const IPC::Message& message);
201
202   // Clear and reinitialize the automation IPC channel.
203   bool ReinitializeChannel();
204
205   void HandleUnused(const IPC::Message& message, int handle);
206   void GetFilteredInetHitCount(int* hit_count);
207
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);
213
214   void OnSetPageFontSize(int tab_handle, int font_size);
215
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);
221
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);
226
227   // Uses the specified encoding to override the encoding of the page in the
228   // specified tab.
229   void OverrideEncoding(int tab_handle,
230                         const std::string& encoding_name,
231                         bool* success);
232
233   void ReloadAsync(int tab_handle);
234   void StopAsync(int tab_handle);
235
236   // Method called by the popup menu tracker when a popup menu is opened.
237   void NotifyPopupMenuOpened();
238
239   scoped_ptr<IPC::ChannelProxy> channel_;
240   scoped_ptr<FindInPageNotificationObserver> find_in_page_observer_;
241
242   // True iff we should enable observers that check for initial load conditions.
243   bool use_initial_load_observers_;
244
245   // True iff connected to an AutomationProxy.
246   bool is_connected_;
247
248   // True iff browser finished loading initial set of tabs.
249   bool initial_tab_loads_complete_;
250
251   // True iff ChromeOS webui login ui is ready.
252   bool login_webui_ready_;
253
254   // ID of automation channel.
255   std::string channel_id_;
256
257   DISALLOW_COPY_AND_ASSIGN(AutomationProvider);
258 };
259
260 #endif  // CHROME_BROWSER_AUTOMATION_AUTOMATION_PROVIDER_H_