1// Copyright 2014 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 CHROME_BROWSER_DEVTOOLS_DEVTOOLS_UI_BINDINGS_H_
6#define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_UI_BINDINGS_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
14#include "base/strings/string16.h"
15#include "chrome/browser/devtools/device/devtools_android_bridge.h"
16#include "chrome/browser/devtools/devtools_embedder_message_dispatcher.h"
17#include "chrome/browser/devtools/devtools_file_helper.h"
18#include "chrome/browser/devtools/devtools_file_system_indexer.h"
19#include "chrome/browser/devtools/devtools_targets_ui.h"
20#include "content/public/browser/devtools_agent_host.h"
21#include "content/public/browser/devtools_frontend_host.h"
22#include "content/public/browser/notification_observer.h"
23#include "content/public/browser/notification_registrar.h"
24#include "ui/gfx/size.h"
25
26class InfoBarService;
27class Profile;
28
29namespace content {
30struct FileChooserParams;
31class WebContents;
32}
33
34// Base implementation of DevTools bindings around front-end.
35class DevToolsUIBindings : public content::NotificationObserver,
36                           public content::DevToolsFrontendHost::Delegate,
37                           public DevToolsEmbedderMessageDispatcher::Delegate,
38                           public DevToolsAndroidBridge::DeviceCountListener,
39                           public content::DevToolsAgentHostClient {
40 public:
41  static DevToolsUIBindings* ForWebContents(
42      content::WebContents* web_contents);
43  static GURL ApplyThemeToURL(Profile* profile, const GURL& base_url);
44
45  class Delegate {
46   public:
47    virtual ~Delegate() {}
48    virtual void ActivateWindow() = 0;
49    virtual void CloseWindow() = 0;
50    virtual void SetInspectedPageBounds(const gfx::Rect& rect) = 0;
51    virtual void InspectElementCompleted() = 0;
52    virtual void MoveWindow(int x, int y) = 0;
53    virtual void SetIsDocked(bool is_docked) = 0;
54    virtual void OpenInNewTab(const std::string& url) = 0;
55    virtual void SetWhitelistedShortcuts(const std::string& message) = 0;
56
57    virtual void InspectedContentsClosing() = 0;
58    virtual void OnLoadCompleted() = 0;
59    virtual InfoBarService* GetInfoBarService() = 0;
60    virtual void RenderProcessGone() = 0;
61  };
62
63  explicit DevToolsUIBindings(content::WebContents* web_contents);
64  virtual ~DevToolsUIBindings();
65
66  content::WebContents* web_contents() { return web_contents_; }
67  Profile* profile() { return profile_; }
68  content::DevToolsAgentHost* agent_host() { return agent_host_.get(); }
69
70  // Takes ownership over the |delegate|.
71  void SetDelegate(Delegate* delegate);
72  void CallClientFunction(const std::string& function_name,
73                          const base::Value* arg1,
74                          const base::Value* arg2,
75                          const base::Value* arg3);
76  void AttachTo(const scoped_refptr<content::DevToolsAgentHost>& agent_host);
77  void Reattach();
78  void Detach();
79  bool IsAttachedTo(content::DevToolsAgentHost* agent_host);
80
81 private:
82  // content::NotificationObserver overrides.
83  virtual void Observe(int type,
84                       const content::NotificationSource& source,
85                       const content::NotificationDetails& details) OVERRIDE;
86
87  // content::DevToolsFrontendHost::Delegate implementation.
88  virtual void HandleMessageFromDevToolsFrontend(
89      const std::string& message) OVERRIDE;
90  virtual void HandleMessageFromDevToolsFrontendToBackend(
91      const std::string& message) OVERRIDE;
92
93  // content::DevToolsAgentHostClient implementation.
94  virtual void DispatchProtocolMessage(
95      content::DevToolsAgentHost* agent_host,
96      const std::string& message) OVERRIDE;
97  virtual void AgentHostClosed(
98       content::DevToolsAgentHost* agent_host,
99       bool replaced_with_another_client) OVERRIDE;
100
101  // DevToolsEmbedderMessageDispatcher::Delegate implementation.
102  virtual void ActivateWindow() OVERRIDE;
103  virtual void CloseWindow() OVERRIDE;
104  virtual void SetInspectedPageBounds(const gfx::Rect& rect) OVERRIDE;
105  virtual void InspectElementCompleted() OVERRIDE;
106  virtual void InspectedURLChanged(const std::string& url) OVERRIDE;
107  virtual void MoveWindow(int x, int y) OVERRIDE;
108  virtual void SetIsDocked(bool is_docked) OVERRIDE;
109  virtual void OpenInNewTab(const std::string& url) OVERRIDE;
110  virtual void SaveToFile(const std::string& url,
111                          const std::string& content,
112                          bool save_as) OVERRIDE;
113  virtual void AppendToFile(const std::string& url,
114                            const std::string& content) OVERRIDE;
115  virtual void RequestFileSystems() OVERRIDE;
116  virtual void AddFileSystem() OVERRIDE;
117  virtual void RemoveFileSystem(const std::string& file_system_path) OVERRIDE;
118  virtual void UpgradeDraggedFileSystemPermissions(
119      const std::string& file_system_url) OVERRIDE;
120  virtual void IndexPath(int request_id,
121                         const std::string& file_system_path) OVERRIDE;
122  virtual void StopIndexing(int request_id) OVERRIDE;
123  virtual void SearchInPath(int request_id,
124                            const std::string& file_system_path,
125                            const std::string& query) OVERRIDE;
126  virtual void SetWhitelistedShortcuts(const std::string& message) OVERRIDE;
127  virtual void ZoomIn() OVERRIDE;
128  virtual void ZoomOut() OVERRIDE;
129  virtual void ResetZoom() OVERRIDE;
130  virtual void OpenUrlOnRemoteDeviceAndInspect(const std::string& browser_id,
131                                               const std::string& url) OVERRIDE;
132  virtual void SetDeviceCountUpdatesEnabled(bool enabled) OVERRIDE;
133  virtual void SetDevicesUpdatesEnabled(bool enabled) OVERRIDE;
134  virtual void SendMessageToBrowser(const std::string& message) OVERRIDE;
135
136  void EnableRemoteDeviceCounter(bool enable);
137
138  // DevToolsAndroidBridge::DeviceCountListener override:
139  virtual void DeviceCountChanged(int count) OVERRIDE;
140
141  // Forwards discovered devices to frontend.
142  virtual void DevicesUpdated(const std::string& source,
143                              const base::ListValue& targets);
144
145  void DocumentOnLoadCompletedInMainFrame();
146
147  // DevToolsFileHelper callbacks.
148  void FileSavedAs(const std::string& url);
149  void CanceledFileSaveAs(const std::string& url);
150  void AppendedTo(const std::string& url);
151  void FileSystemsLoaded(
152      const std::vector<DevToolsFileHelper::FileSystem>& file_systems);
153  void FileSystemAdded(const DevToolsFileHelper::FileSystem& file_system);
154  void IndexingTotalWorkCalculated(int request_id,
155                                   const std::string& file_system_path,
156                                   int total_work);
157  void IndexingWorked(int request_id,
158                      const std::string& file_system_path,
159                      int worked);
160  void IndexingDone(int request_id, const std::string& file_system_path);
161  void SearchCompleted(int request_id,
162                       const std::string& file_system_path,
163                       const std::vector<std::string>& file_paths);
164  typedef base::Callback<void(bool)> InfoBarCallback;
165  void ShowDevToolsConfirmInfoBar(const base::string16& message,
166                                  const InfoBarCallback& callback);
167
168  // Theme and extensions support.
169  void UpdateTheme();
170  void AddDevToolsExtensionsToClient();
171
172  class FrontendWebContentsObserver;
173  friend class FrontendWebContentsObserver;
174  scoped_ptr<FrontendWebContentsObserver> frontend_contents_observer_;
175
176  Profile* profile_;
177  content::WebContents* web_contents_;
178  scoped_ptr<Delegate> delegate_;
179  scoped_refptr<content::DevToolsAgentHost> agent_host_;
180  content::NotificationRegistrar registrar_;
181  scoped_ptr<content::DevToolsFrontendHost> frontend_host_;
182  scoped_ptr<DevToolsFileHelper> file_helper_;
183  scoped_refptr<DevToolsFileSystemIndexer> file_system_indexer_;
184  typedef std::map<
185      int,
186      scoped_refptr<DevToolsFileSystemIndexer::FileSystemIndexingJob> >
187      IndexingJobsMap;
188  IndexingJobsMap indexing_jobs_;
189
190  bool device_count_updates_enabled_;
191  bool devices_updates_enabled_;
192  scoped_ptr<DevToolsTargetsUIHandler> remote_targets_handler_;
193  scoped_ptr<DevToolsEmbedderMessageDispatcher> embedder_message_dispatcher_;
194  GURL url_;
195  base::WeakPtrFactory<DevToolsUIBindings> weak_factory_;
196
197  DISALLOW_COPY_AND_ASSIGN(DevToolsUIBindings);
198};
199
200#endif  // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_UI_BINDINGS_H_
201