chromeos: dbus: add Bluetooth properties support
[chromium-blink-merge.git] / chrome_frame / chrome_frame_delegate.h
blob856fcb84510acd120440dd819bc6f0fa3ce5f094
1 // Copyright (c) 2011 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 CHROME_FRAME_CHROME_FRAME_DELEGATE_H_
6 #define CHROME_FRAME_CHROME_FRAME_DELEGATE_H_
7 #pragma once
9 #include <atlbase.h>
10 #include <atlwin.h>
11 #include <queue>
12 #include <string>
13 #include <vector>
15 #include "base/callback.h"
16 #include "base/file_path.h"
17 #include "base/location.h"
18 #include "base/pending_task.h"
19 #include "base/synchronization/lock.h"
20 #include "chrome/common/automation_constants.h"
21 #include "ipc/ipc_message.h"
23 class GURL;
24 struct AttachExternalTabParams;
25 struct AutomationURLRequest;
26 struct ContextMenuModel;
27 struct MiniContextMenuParams;
28 struct NavigationInfo;
30 namespace net {
31 class URLRequestStatus;
34 namespace gfx {
35 class Rect;
38 // A common interface supported by all the browser specific ChromeFrame
39 // implementations.
40 class ChromeFrameDelegate {
41 public:
42 typedef HWND WindowType;
44 virtual WindowType GetWindow() const = 0;
45 virtual void GetBounds(RECT* bounds) = 0;
46 virtual std::string GetDocumentUrl() = 0;
47 virtual void OnAutomationServerReady() = 0;
48 virtual void OnAutomationServerLaunchFailed(
49 AutomationLaunchResult reason, const std::string& server_version) = 0;
50 virtual bool OnMessageReceived(const IPC::Message& msg) = 0;
51 virtual void OnChannelError() = 0;
53 // This remains in interface since we call it if Navigate()
54 // returns immediate error.
55 virtual void OnLoadFailed(int error_code, const std::string& url) = 0;
57 // Returns true if this instance is alive and well for processing automation
58 // messages.
59 virtual bool IsValid() const = 0;
61 // To be called when the top-most window of an application hosting
62 // ChromeFrame is moved.
63 virtual void OnHostMoved() = 0;
65 protected:
66 virtual ~ChromeFrameDelegate() {}
69 extern UINT kAutomationServerReady;
70 extern UINT kMessageFromChromeFrame;
72 class ChromeFrameDelegateImpl : public ChromeFrameDelegate {
73 public:
74 virtual WindowType GetWindow() { return NULL; }
75 virtual void GetBounds(RECT* bounds) {}
76 virtual std::string GetDocumentUrl() { return std::string(); }
77 virtual void OnAutomationServerReady() {}
78 virtual void OnAutomationServerLaunchFailed(
79 AutomationLaunchResult reason, const std::string& server_version) {}
80 virtual void OnLoadFailed(int error_code, const std::string& url) {}
81 virtual bool OnMessageReceived(const IPC::Message& msg);
82 virtual void OnChannelError() {}
84 static bool IsTabMessage(const IPC::Message& message);
86 virtual bool IsValid() const {
87 return true;
90 virtual void OnHostMoved() {}
92 protected:
93 // Protected methods to be overridden.
94 virtual void OnNavigationStateChanged(
95 int flags, const NavigationInfo& nav_info) {}
96 virtual void OnUpdateTargetUrl(const std::wstring& new_target_url) {}
97 virtual void OnAcceleratorPressed(const MSG& accel_message) {}
98 virtual void OnTabbedOut(bool reverse) {}
99 virtual void OnOpenURL(
100 const GURL& url, const GURL& referrer, int open_disposition) {}
101 virtual void OnDidNavigate(const NavigationInfo& navigation_info) {}
102 virtual void OnNavigationFailed(int error_code, const GURL& gurl) {}
103 virtual void OnLoad(const GURL& url) {}
104 virtual void OnMoveWindow(const gfx::Rect& pos) {}
105 virtual void OnMessageFromChromeFrame(const std::string& message,
106 const std::string& origin,
107 const std::string& target) {}
108 virtual void OnHandleContextMenu(const ContextMenuModel& context_menu_model,
109 int align_flags,
110 const MiniContextMenuParams& params) {}
111 virtual void OnRequestStart(
112 int request_id, const AutomationURLRequest& request) {}
113 virtual void OnRequestRead(int request_id, int bytes_to_read) {}
114 virtual void OnRequestEnd(int request_id,
115 const net::URLRequestStatus& status) {}
116 virtual void OnDownloadRequestInHost(int request_id) {}
117 virtual void OnSetCookieAsync(const GURL& url, const std::string& cookie) {}
118 virtual void OnAttachExternalTab(
119 const AttachExternalTabParams& attach_params) {}
120 virtual void OnGoToHistoryEntryOffset(int offset) {}
122 virtual void OnGetCookiesFromHost(const GURL& url, int cookie_id) {}
123 virtual void OnCloseTab() {}
126 // This interface enables tasks to be marshaled to desired threads.
127 class TaskMarshaller { // NOLINT
128 public:
129 virtual void PostTask(const tracked_objects::Location& from_here,
130 const base::Closure& task) = 0;
133 // T is expected to be something CWindowImpl derived, or at least to have
134 // PostMessage(UINT, WPARAM) method. Do not forget to CHAIN_MSG_MAP
135 template <class T> class TaskMarshallerThroughWindowsMessages
136 : public TaskMarshaller {
137 public:
138 TaskMarshallerThroughWindowsMessages() {}
139 virtual void PostTask(const tracked_objects::Location& posted_from,
140 const base::Closure& task) OVERRIDE {
141 T* this_ptr = static_cast<T*>(this);
142 if (this_ptr->IsWindow()) {
143 this_ptr->AddRef();
144 base::PendingTask* pending_task =
145 new base::PendingTask(posted_from, task);
146 PushTask(pending_task);
147 this_ptr->PostMessage(MSG_EXECUTE_TASK,
148 reinterpret_cast<WPARAM>(pending_task));
149 } else {
150 DVLOG(1) << "Dropping MSG_EXECUTE_TASK message for destroyed window.";
154 protected:
155 ~TaskMarshallerThroughWindowsMessages() {
156 DeleteAllPendingTasks();
159 void DeleteAllPendingTasks() {
160 base::AutoLock lock(lock_);
161 DVLOG_IF(1, !pending_tasks_.empty()) << "Destroying "
162 << pending_tasks_.size()
163 << " pending tasks";
164 while (!pending_tasks_.empty()) {
165 base::PendingTask* task = pending_tasks_.front();
166 pending_tasks_.pop();
167 delete task;
171 BEGIN_MSG_MAP(PostMessageMarshaller)
172 MESSAGE_HANDLER(MSG_EXECUTE_TASK, ExecuteTask)
173 END_MSG_MAP()
175 private:
176 enum { MSG_EXECUTE_TASK = WM_APP + 6 };
177 inline LRESULT ExecuteTask(UINT, WPARAM wparam, LPARAM,
178 BOOL& handled) { // NOLINT
179 base::PendingTask* pending_task =
180 reinterpret_cast<base::PendingTask*>(wparam);
181 if (pending_task && PopTask(pending_task)) {
182 pending_task->task.Run();
183 delete pending_task;
186 T* this_ptr = static_cast<T*>(this);
187 this_ptr->Release();
188 return 0;
191 inline void PushTask(base::PendingTask* pending_task) {
192 base::AutoLock lock(lock_);
193 pending_tasks_.push(pending_task);
196 // If |pending_task| is front of the queue, removes the task and returns true,
197 // otherwise we assume this is an already destroyed task (but Window message
198 // had remained in the thread queue).
199 inline bool PopTask(base::PendingTask* pending_task) {
200 base::AutoLock lock(lock_);
201 if (!pending_tasks_.empty() && pending_task == pending_tasks_.front()) {
202 pending_tasks_.pop();
203 return true;
206 return false;
209 base::Lock lock_;
210 std::queue<base::PendingTask*> pending_tasks_;
213 #endif // CHROME_FRAME_CHROME_FRAME_DELEGATE_H_