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_client_host.h" 21 #include "content/public/browser/devtools_frontend_host_delegate.h" 22 #include "content/public/browser/notification_observer.h" 23 #include "content/public/browser/notification_registrar.h" 24 #include "ui/gfx/size.h" 25 26 class InfoBarService; 27 class Profile; 28 29 namespace content { 30 class DevToolsClientHost; 31 struct FileChooserParams; 32 class WebContents; 33 } 34 35 // Base implementation of DevTools bindings around front-end. 36 class DevToolsUIBindings : public content::NotificationObserver, 37 public content::DevToolsFrontendHostDelegate, 38 public DevToolsEmbedderMessageDispatcher::Delegate, 39 public DevToolsAndroidBridge::DeviceCountListener { 40 public: 41 static GURL ApplyThemeToURL(Profile* profile, const GURL& base_url); 42 43 class Delegate { 44 public: ~Delegate()45 virtual ~Delegate() {} 46 virtual void ActivateWindow() = 0; 47 virtual void CloseWindow() = 0; 48 virtual void SetInspectedPageBounds(const gfx::Rect& rect) = 0; 49 virtual void SetContentsResizingStrategy( 50 const gfx::Insets& insets, const gfx::Size& min_size) = 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 DevToolsUIBindings(content::WebContents* web_contents, const GURL& url); 64 virtual ~DevToolsUIBindings(); 65 web_contents()66 content::WebContents* web_contents() { return web_contents_; } profile()67 Profile* profile() { return profile_; } frontend_host()68 content::DevToolsClientHost* frontend_host() { return frontend_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 DispatchEventOnFrontend(const std::string& event_type, 77 const base::Value* event_data); 78 private: 79 // content::NotificationObserver: 80 virtual void Observe(int type, 81 const content::NotificationSource& source, 82 const content::NotificationDetails& details) OVERRIDE; 83 84 // content::DevToolsFrontendHostDelegate override: 85 virtual void InspectedContentsClosing() OVERRIDE; 86 virtual void DispatchOnEmbedder(const std::string& message) OVERRIDE; 87 88 // DevToolsEmbedderMessageDispatcher::Delegate overrides: 89 virtual void ActivateWindow() OVERRIDE; 90 virtual void CloseWindow() OVERRIDE; 91 virtual void SetInspectedPageBounds(const gfx::Rect& rect) OVERRIDE; 92 virtual void SetContentsResizingStrategy( 93 const gfx::Insets& insets, const gfx::Size& min_size) OVERRIDE; 94 virtual void InspectElementCompleted() OVERRIDE; 95 virtual void InspectedURLChanged(const std::string& url) OVERRIDE; 96 virtual void MoveWindow(int x, int y) OVERRIDE; 97 virtual void SetIsDocked(bool is_docked) OVERRIDE; 98 virtual void OpenInNewTab(const std::string& url) OVERRIDE; 99 virtual void SaveToFile(const std::string& url, 100 const std::string& content, 101 bool save_as) OVERRIDE; 102 virtual void AppendToFile(const std::string& url, 103 const std::string& content) OVERRIDE; 104 virtual void RequestFileSystems() OVERRIDE; 105 virtual void AddFileSystem() OVERRIDE; 106 virtual void RemoveFileSystem(const std::string& file_system_path) OVERRIDE; 107 virtual void UpgradeDraggedFileSystemPermissions( 108 const std::string& file_system_url) OVERRIDE; 109 virtual void IndexPath(int request_id, 110 const std::string& file_system_path) OVERRIDE; 111 virtual void StopIndexing(int request_id) OVERRIDE; 112 virtual void SearchInPath(int request_id, 113 const std::string& file_system_path, 114 const std::string& query) OVERRIDE; 115 virtual void SetWhitelistedShortcuts(const std::string& message) OVERRIDE; 116 virtual void ZoomIn() OVERRIDE; 117 virtual void ZoomOut() OVERRIDE; 118 virtual void ResetZoom() OVERRIDE; 119 virtual void OpenUrlOnRemoteDeviceAndInspect(const std::string& browser_id, 120 const std::string& url) OVERRIDE; 121 virtual void Subscribe(const std::string& event_type) OVERRIDE; 122 virtual void Unsubscribe(const std::string& event_type) OVERRIDE; 123 124 void EnableRemoteDeviceCounter(bool enable); 125 126 // DevToolsAndroidBridge::DeviceCountListener override: 127 virtual void DeviceCountChanged(int count) OVERRIDE; 128 129 // Forwards discovered devices to frontend. 130 virtual void PopulateRemoteDevices(const std::string& source, 131 scoped_ptr<base::ListValue> targets); 132 133 void DocumentOnLoadCompletedInMainFrame(); 134 135 // DevToolsFileHelper callbacks. 136 void FileSavedAs(const std::string& url); 137 void CanceledFileSaveAs(const std::string& url); 138 void AppendedTo(const std::string& url); 139 void FileSystemsLoaded( 140 const std::vector<DevToolsFileHelper::FileSystem>& file_systems); 141 void FileSystemAdded(const DevToolsFileHelper::FileSystem& file_system); 142 void IndexingTotalWorkCalculated(int request_id, 143 const std::string& file_system_path, 144 int total_work); 145 void IndexingWorked(int request_id, 146 const std::string& file_system_path, 147 int worked); 148 void IndexingDone(int request_id, const std::string& file_system_path); 149 void SearchCompleted(int request_id, 150 const std::string& file_system_path, 151 const std::vector<std::string>& file_paths); 152 typedef base::Callback<void(bool)> InfoBarCallback; 153 void ShowDevToolsConfirmInfoBar(const base::string16& message, 154 const InfoBarCallback& callback); 155 156 // Theme and extensions support. 157 void UpdateTheme(); 158 void AddDevToolsExtensionsToClient(); 159 160 class FrontendWebContentsObserver; 161 friend class FrontendWebContentsObserver; 162 scoped_ptr<FrontendWebContentsObserver> frontend_contents_observer_; 163 164 Profile* profile_; 165 content::WebContents* web_contents_; 166 scoped_ptr<Delegate> delegate_; 167 bool device_listener_enabled_; 168 content::NotificationRegistrar registrar_; 169 scoped_ptr<content::DevToolsClientHost> frontend_host_; 170 scoped_ptr<DevToolsFileHelper> file_helper_; 171 scoped_refptr<DevToolsFileSystemIndexer> file_system_indexer_; 172 typedef std::map< 173 int, 174 scoped_refptr<DevToolsFileSystemIndexer::FileSystemIndexingJob> > 175 IndexingJobsMap; 176 IndexingJobsMap indexing_jobs_; 177 178 typedef std::set<std::string> Subscribers; 179 Subscribers subscribers_; 180 scoped_ptr<DevToolsTargetsUIHandler> remote_targets_handler_; 181 scoped_ptr<DevToolsEmbedderMessageDispatcher> embedder_message_dispatcher_; 182 GURL url_; 183 base::WeakPtrFactory<DevToolsUIBindings> weak_factory_; 184 185 DISALLOW_COPY_AND_ASSIGN(DevToolsUIBindings); 186 }; 187 188 #endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_UI_BINDINGS_H_ 189