blob: 0952d27569fb2a0ecf9f656e08464487ab250dbb [file] [log] [blame]
[email protected]27a112c2011-01-06 04:19:301// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]f7817822009-09-24 05:11:582// 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_FRAME_CHROME_FRAME_DELEGATE_H_
6#define CHROME_FRAME_CHROME_FRAME_DELEGATE_H_
[email protected]27a112c2011-01-06 04:19:307#pragma once
[email protected]f7817822009-09-24 05:11:588
[email protected]5778de6e2009-10-24 01:29:209#include <atlbase.h>
10#include <atlwin.h>
[email protected]4c3fe4ae2010-03-01 20:01:2411#include <queue>
[email protected]efd4dfc22010-03-26 20:14:4012#include <string>
13#include <vector>
[email protected]5778de6e2009-10-24 01:29:2014
[email protected]a1e62d12010-03-16 02:18:4315#include "base/file_path.h"
[email protected]20305ec2011-01-21 04:55:5216#include "base/synchronization/lock.h"
[email protected]a22f7e02011-02-09 07:15:3517#include "base/task.h"
18#include "chrome/common/automation_constants.h"
[email protected]f7817822009-09-24 05:11:5819#include "ipc/ipc_message.h"
20
[email protected]a22f7e02011-02-09 07:15:3521class GURL;
22struct AttachExternalTabParams;
23struct AutomationURLRequest;
24struct MiniContextMenuParams;
25struct NavigationInfo;
26
27namespace net {
28class URLRequestStatus;
29}
30
[email protected]326d3b72011-03-29 20:38:2431namespace gfx {
32class Rect;
33}
34
[email protected]f7817822009-09-24 05:11:5835// A common interface supported by all the browser specific ChromeFrame
36// implementations.
37class ChromeFrameDelegate {
38 public:
[email protected]f7817822009-09-24 05:11:5839 typedef HWND WindowType;
40
41 virtual WindowType GetWindow() const = 0;
42 virtual void GetBounds(RECT* bounds) = 0;
43 virtual std::string GetDocumentUrl() = 0;
44 virtual void OnAutomationServerReady() = 0;
45 virtual void OnAutomationServerLaunchFailed(
46 AutomationLaunchResult reason, const std::string& server_version) = 0;
[email protected]00f6b772009-10-23 17:03:4147 virtual void OnExtensionInstalled(
48 const FilePath& path,
49 void* user_data,
50 AutomationMsg_ExtensionResponseValues response) = 0;
[email protected]a1e62d12010-03-16 02:18:4351 virtual void OnGetEnabledExtensionsComplete(
52 void* user_data,
53 const std::vector<FilePath>& extension_directories) = 0;
[email protected]a95986a82010-12-24 06:19:2854 virtual bool OnMessageReceived(const IPC::Message& msg) = 0;
[email protected]efd4dfc22010-03-26 20:14:4055 virtual void OnChannelError() = 0;
[email protected]f7817822009-09-24 05:11:5856
57 // This remains in interface since we call it if Navigate()
58 // returns immediate error.
59 virtual void OnLoadFailed(int error_code, const std::string& url) = 0;
60
61 // Returns true if this instance is alive and well for processing automation
62 // messages.
63 virtual bool IsValid() const = 0;
64
[email protected]2b19e2fe2010-02-16 02:24:1865 // To be called when the top-most window of an application hosting
66 // ChromeFrame is moved.
67 virtual void OnHostMoved() = 0;
68
[email protected]f7817822009-09-24 05:11:5869 protected:
[email protected]efd4dfc22010-03-26 20:14:4070 virtual ~ChromeFrameDelegate() {}
[email protected]f7817822009-09-24 05:11:5871};
72
[email protected]c56428f22010-06-16 02:17:2373// Disable refcounting of ChromeFrameDelegate.
74DISABLE_RUNNABLE_METHOD_REFCOUNT(ChromeFrameDelegate);
[email protected]f7817822009-09-24 05:11:5875
76extern UINT kAutomationServerReady;
77extern UINT kMessageFromChromeFrame;
78
79class ChromeFrameDelegateImpl : public ChromeFrameDelegate {
80 public:
81 virtual WindowType GetWindow() { return NULL; }
82 virtual void GetBounds(RECT* bounds) {}
83 virtual std::string GetDocumentUrl() { return std::string(); }
84 virtual void OnAutomationServerReady() {}
85 virtual void OnAutomationServerLaunchFailed(
86 AutomationLaunchResult reason, const std::string& server_version) {}
[email protected]00f6b772009-10-23 17:03:4187 virtual void OnExtensionInstalled(
88 const FilePath& path,
89 void* user_data,
90 AutomationMsg_ExtensionResponseValues response) {}
[email protected]a1e62d12010-03-16 02:18:4391 virtual void OnGetEnabledExtensionsComplete(
92 void* user_data,
93 const std::vector<FilePath>& extension_directories) {}
[email protected]f7817822009-09-24 05:11:5894 virtual void OnLoadFailed(int error_code, const std::string& url) {}
[email protected]a95986a82010-12-24 06:19:2895 virtual bool OnMessageReceived(const IPC::Message& msg);
[email protected]efd4dfc22010-03-26 20:14:4096 virtual void OnChannelError() {}
[email protected]00f6b772009-10-23 17:03:4197
[email protected]f5494d42010-12-23 22:15:3498 static bool IsTabMessage(const IPC::Message& message);
[email protected]f7817822009-09-24 05:11:5899
100 virtual bool IsValid() const {
101 return true;
102 }
103
[email protected]2b19e2fe2010-02-16 02:24:18104 virtual void OnHostMoved() {}
105
[email protected]f7817822009-09-24 05:11:58106 protected:
107 // Protected methods to be overriden.
[email protected]f5494d42010-12-23 22:15:34108 virtual void OnNavigationStateChanged(
109 int flags, const NavigationInfo& nav_info) {}
110 virtual void OnUpdateTargetUrl(const std::wstring& new_target_url) {}
111 virtual void OnAcceleratorPressed(const MSG& accel_message) {}
112 virtual void OnTabbedOut(bool reverse) {}
113 virtual void OnOpenURL(
114 const GURL& url, const GURL& referrer, int open_disposition) {}
115 virtual void OnDidNavigate(const NavigationInfo& navigation_info) {}
116 virtual void OnNavigationFailed(int error_code, const GURL& gurl) {}
117 virtual void OnLoad(const GURL& url) {}
[email protected]326d3b72011-03-29 20:38:24118 virtual void OnMoveWindow(const gfx::Rect& pos) {}
[email protected]f5494d42010-12-23 22:15:34119 virtual void OnMessageFromChromeFrame(const std::string& message,
[email protected]f7817822009-09-24 05:11:58120 const std::string& origin,
121 const std::string& target) {}
[email protected]f5494d42010-12-23 22:15:34122 virtual void OnHandleContextMenu(HANDLE menu_handle, int align_flags,
123 const MiniContextMenuParams& params) {}
124 virtual void OnRequestStart(
125 int request_id, const AutomationURLRequest& request) {}
126 virtual void OnRequestRead(int request_id, int bytes_to_read) {}
[email protected]27a112c2011-01-06 04:19:30127 virtual void OnRequestEnd(int request_id,
128 const net::URLRequestStatus& status) {}
[email protected]f5494d42010-12-23 22:15:34129 virtual void OnDownloadRequestInHost(int request_id) {}
130 virtual void OnSetCookieAsync(const GURL& url, const std::string& cookie) {}
131 virtual void OnAttachExternalTab(
132 const AttachExternalTabParams& attach_params) {}
133 virtual void OnGoToHistoryEntryOffset(int offset) {}
[email protected]70daf0b2010-03-02 19:13:00134
[email protected]f5494d42010-12-23 22:15:34135 virtual void OnGetCookiesFromHost(const GURL& url, int cookie_id) {}
136 virtual void OnCloseTab() {}
[email protected]f7817822009-09-24 05:11:58137};
138
[email protected]efd4dfc22010-03-26 20:14:40139// This interface enables tasks to be marshaled to desired threads.
140class TaskMarshaller { // NOLINT
[email protected]5778de6e2009-10-24 01:29:20141 public:
142 virtual void PostTask(const tracked_objects::Location& from_here,
143 Task* task) = 0;
144};
145
146// T is expected to be something CWindowImpl derived, or at least to have
147// PostMessage(UINT, WPARAM) method. Do not forget to CHAIN_MSG_MAP
148template <class T> class TaskMarshallerThroughWindowsMessages
149 : public TaskMarshaller {
150 public:
[email protected]4c3fe4ae2010-03-01 20:01:24151 TaskMarshallerThroughWindowsMessages() {}
[email protected]5778de6e2009-10-24 01:29:20152 virtual void PostTask(const tracked_objects::Location& from_here,
153 Task* task) {
154 task->SetBirthPlace(from_here);
155 T* this_ptr = static_cast<T*>(this);
156 if (this_ptr->IsWindow()) {
157 this_ptr->AddRef();
[email protected]4c3fe4ae2010-03-01 20:01:24158 PushTask(task);
[email protected]5778de6e2009-10-24 01:29:20159 this_ptr->PostMessage(MSG_EXECUTE_TASK, reinterpret_cast<WPARAM>(task));
160 } else {
[email protected]2b9a9f162010-10-19 20:30:45161 DVLOG(1) << "Dropping MSG_EXECUTE_TASK message for destroyed window.";
[email protected]4c3fe4ae2010-03-01 20:01:24162 delete task;
163 }
164 }
165
[email protected]4c3fe4ae2010-03-01 20:01:24166 protected:
167 ~TaskMarshallerThroughWindowsMessages() {
168 DeleteAllPendingTasks();
169 }
170
171 void DeleteAllPendingTasks() {
[email protected]20305ec2011-01-21 04:55:52172 base::AutoLock lock(lock_);
[email protected]5044da82010-10-27 01:09:16173 DVLOG_IF(1, !pending_tasks_.empty()) << "Destroying "
174 << pending_tasks_.size()
175 << " pending tasks";
[email protected]4c3fe4ae2010-03-01 20:01:24176 while (!pending_tasks_.empty()) {
177 Task* task = pending_tasks_.front();
178 pending_tasks_.pop();
179 delete task;
[email protected]5778de6e2009-10-24 01:29:20180 }
181 }
182
183 BEGIN_MSG_MAP(PostMessageMarshaller)
184 MESSAGE_HANDLER(MSG_EXECUTE_TASK, ExecuteTask)
185 END_MSG_MAP()
186
187 private:
188 enum { MSG_EXECUTE_TASK = WM_APP + 6 };
189 inline LRESULT ExecuteTask(UINT, WPARAM wparam, LPARAM,
190 BOOL& handled) { // NOLINT
191 Task* task = reinterpret_cast<Task*>(wparam);
[email protected]3148c0ae2010-03-11 18:06:00192 if (task && PopTask(task)) {
193 task->Run();
194 delete task;
195 }
196
[email protected]5778de6e2009-10-24 01:29:20197 T* this_ptr = static_cast<T*>(this);
198 this_ptr->Release();
199 return 0;
200 }
[email protected]4c3fe4ae2010-03-01 20:01:24201
202 inline void PushTask(Task* task) {
[email protected]20305ec2011-01-21 04:55:52203 base::AutoLock lock(lock_);
[email protected]4c3fe4ae2010-03-01 20:01:24204 pending_tasks_.push(task);
205 }
206
[email protected]3148c0ae2010-03-11 18:06:00207 // If the given task is front of the queue, removes the task and returns true,
208 // otherwise we assume this is an already destroyed task (but Window message
209 // had remained in the thread queue).
210 inline bool PopTask(Task* task) {
[email protected]20305ec2011-01-21 04:55:52211 base::AutoLock lock(lock_);
[email protected]ada3d0b23842010-03-19 01:04:24212 if (!pending_tasks_.empty() && task == pending_tasks_.front()) {
[email protected]3148c0ae2010-03-11 18:06:00213 pending_tasks_.pop();
214 return true;
215 }
216
217 return false;
[email protected]4c3fe4ae2010-03-01 20:01:24218 }
219
[email protected]20305ec2011-01-21 04:55:52220 base::Lock lock_;
[email protected]4c3fe4ae2010-03-01 20:01:24221 std::queue<Task*> pending_tasks_;
[email protected]5778de6e2009-10-24 01:29:20222};
223
[email protected]f7817822009-09-24 05:11:58224#endif // CHROME_FRAME_CHROME_FRAME_DELEGATE_H_