summaryrefslogtreecommitdiffstats
path: root/chrome_frame/chrome_active_document.h
blob: ee2cf6e0ab6b3a99fe86821b281cf0780af9f9c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_FRAME_CHROME_ACTIVE_DOCUMENT_H_
#define CHROME_FRAME_CHROME_ACTIVE_DOCUMENT_H_

#include <atlbase.h>
#include <atlcom.h>
#include <atlctl.h>
#include <map>
#include <mshtmcid.h>

#include "base/scoped_ptr.h"
#include "base/scoped_comptr_win.h"
#include "base/thread.h"

#include "chrome_frame/chrome_frame_activex_base.h"
#include "chrome_frame/com_type_info_holder.h"
#include "chrome_frame/find_dialog.h"
#include "chrome_frame/in_place_menu.h"
#include "chrome_frame/ole_document_impl.h"
#include "chrome_frame/resource.h"
#include "chrome_frame/extra_system_apis.h"

class Thread;
class TabProxy;
class ChromeActiveDocument;

// A call to IOleCommandTarget::Exec on the webbrowser with this command id
// and a command group of CGID_EXPLORER causes IE to finalize the current
// travel log entry and move to a new location (pruning any forward entries
// if needed)
#define INTERNAL_CMDID_FINALIZE_TRAVEL_LOG (38)

// To set the lock icon status call IOleCommandTarget::Exec on site with
// this command id and a command group of CGID_EXPLORER  The in arg is one of
// the values: SECURELOCK_SET_UNSECURE, SECURELOCK_SET_MIXED,
// SECURELOCK_SET_SECURE128BIT etc declared in shdeprecated.h
#define INTERNAL_CMDID_SET_SSL_LOCK (37)

// A call to IOleCommandTarget::Exec on the webbrowser with this command id
// and a command group of CGID_EXPLORER causes IE to replace the URL in the
// current travel log entry
#define INTERNAL_CMDID_REPLACE_CURRENT_TRAVEL_LOG_ENTRY_URL (40)

#define INTERNAL_IE_CONTEXTMENU_VIEWSOURCE    (2139)

#ifndef SBCMDID_MIXEDZONE
// This command is sent by the frame to allow the document to return the URL
// security zone for display.
// Please refer to http://msdn.microsoft.com/en-us/library/aa770042(VS.85).aspx
// for more information.
#define SBCMDID_MIXEDZONE                   39
#endif  // SBCMDID_MIXEDZONE

// This macro should be defined in the public section of the class.
#define BEGIN_EXEC_COMMAND_MAP(theClass) \
  public: \
   HRESULT ProcessExecCommand(const GUID* cmd_group_guid, DWORD command_id, \
                              DWORD cmd_exec_opt, VARIANT* in_args, \
                              VARIANT* out_args) { \
   HRESULT hr = OLECMDERR_E_NOTSUPPORTED; \
   switch (command_id) {


#define EXEC_COMMAND_HANDLER(id, handler) \
  case id: { \
    hr = S_OK;  \
    handler(cmd_group_guid, command_id, cmd_exec_opt, in_args, out_args);  \
    break;  \
  }

#define EXEC_COMMAND_HANDLER_NO_ARGS(id, handler) \
  case id: { \
    hr = S_OK;  \
    handler();  \
    break;  \
  }

#define EXEC_COMMAND_HANDLER_GENERIC(id, code) \
  case id: { \
    hr = S_OK;  \
    code;  \
    break;  \
  }

#define END_EXEC_COMMAND_MAP()  \
    default: \
      break; \
  } \
  return hr; \
}

// ChromeActiveDocument: Implementation of the Active Document object that is
// responsible for rendering pages in Chrome. This object delegates to
// Chrome.exe (via the Chrome IPC-based automation mechanism) for the actual
// rendering
class ATL_NO_VTABLE ChromeActiveDocument
     : public ChromeFrameActivexBase<ChromeActiveDocument,
                                     CLSID_ChromeActiveDocument>,
       public IOleDocumentImpl<ChromeActiveDocument>,
       public IOleDocumentViewImpl<ChromeActiveDocument>,
       public IPersistMoniker,
       public IOleCommandTarget,
       public InPlaceMenu<ChromeActiveDocument>,
       public IWebBrowserEventsUrlService {
 public:
  typedef ChromeFrameActivexBase<ChromeActiveDocument,
      CLSID_ChromeActiveDocument> Base;
  ChromeActiveDocument();
  ~ChromeActiveDocument();

  DECLARE_REGISTRY_RESOURCEID(IDR_CHROMEACTIVEDOCUMENT)

BEGIN_COM_MAP(ChromeActiveDocument)
  COM_INTERFACE_ENTRY(IOleDocument)
  COM_INTERFACE_ENTRY(IOleDocumentView)
  COM_INTERFACE_ENTRY(IPersistMoniker)
  COM_INTERFACE_ENTRY(IOleCommandTarget)
  COM_INTERFACE_ENTRY(IWebBrowserEventsUrlService)
  COM_INTERFACE_ENTRY_CHAIN(Base)
END_COM_MAP()

BEGIN_MSG_MAP(ChromeActiveDocument)
  CHAIN_MSG_MAP(Base)
END_MSG_MAP()

  HRESULT FinalConstruct();

#define FORWARD_TAB_COMMAND(id, command) \
  EXEC_COMMAND_HANDLER_GENERIC(id, GetTabProxy() ? GetTabProxy()->command() : 1)

BEGIN_EXEC_COMMAND_MAP(ChromeActiveDocument)
  EXEC_COMMAND_HANDLER_GENERIC(OLECMDID_PRINT, automation_client_->PrintTab())
  EXEC_COMMAND_HANDLER_NO_ARGS(OLECMDID_FIND, OnFindInPage)
  EXEC_COMMAND_HANDLER_NO_ARGS(IDM_FIND, OnFindInPage)
  EXEC_COMMAND_HANDLER_NO_ARGS(INTERNAL_IE_CONTEXTMENU_VIEWSOURCE, OnViewSource)
  FORWARD_TAB_COMMAND(OLECMDID_SELECTALL, SelectAll)
  FORWARD_TAB_COMMAND(OLECMDID_CUT, Cut)
  FORWARD_TAB_COMMAND(OLECMDID_COPY, Copy)
  FORWARD_TAB_COMMAND(OLECMDID_PASTE, Paste)
  FORWARD_TAB_COMMAND(OLECMDID_REFRESH, ReloadAsync)
  FORWARD_TAB_COMMAND(OLECMDID_STOP, StopAsync)
  EXEC_COMMAND_HANDLER(SBCMDID_MIXEDZONE, OnDetermineSecurityZone)
  EXEC_COMMAND_HANDLER(IDM_BASELINEFONT1, SetPageFontSize)
  EXEC_COMMAND_HANDLER(IDM_BASELINEFONT2, SetPageFontSize)
  EXEC_COMMAND_HANDLER(IDM_BASELINEFONT3, SetPageFontSize)
  EXEC_COMMAND_HANDLER(IDM_BASELINEFONT4, SetPageFontSize)
  EXEC_COMMAND_HANDLER(IDM_BASELINEFONT5, SetPageFontSize)
END_EXEC_COMMAND_MAP()

  // IPCs from automation server.
  virtual void OnNavigationStateChanged(int tab_handle, int flags,
                                        const IPC::NavigationInfo& nav_info);
  virtual void OnUpdateTargetUrl(int tab_handle,
                                 const std::wstring& new_target_url);
  virtual void OnAcceleratorPressed(int tab_handle, const MSG& accel_message);
  virtual void OnTabbedOut(int tab_handle, bool reverse);
  virtual void OnDidNavigate(int tab_handle,
                             const IPC::NavigationInfo& nav_info);
  // Override DoVerb
  STDMETHOD(DoVerb)(LONG verb,
                    LPMSG msg,
                    IOleClientSite* active_site,
                    LONG index,
                    HWND parent_window,
                    LPCRECT pos);

  // Override IOleInPlaceActiveObjectImpl::OnDocWindowActivate
  STDMETHOD(OnDocWindowActivate)(BOOL activate);
  STDMETHOD(TranslateAccelerator)(MSG* msg);

  // IPersistMoniker methods
  STDMETHOD(GetClassID)(CLSID* class_id);
  STDMETHOD(IsDirty)();
  STDMETHOD(GetCurMoniker)(IMoniker** moniker_name);
  STDMETHOD(Load)(BOOL fully_avalable,
                  IMoniker* moniker_name,
                  LPBC bind_context,
                  DWORD mode);
  STDMETHOD(Save)(IMoniker* moniker_name,
                  LPBC bind_context,
                  BOOL remember);
  STDMETHOD(SaveCompleted)(IMoniker* moniker_name,
                  LPBC bind_context);

  // IOleCommandTarget methods
  STDMETHOD(QueryStatus)(const GUID* cmd_group_guid,
                         ULONG number_of_commands,
                         OLECMD commands[],
                         OLECMDTEXT* command_text);
  STDMETHOD(Exec)(const GUID* cmd_group_guid, DWORD command_id,
                  DWORD cmd_exec_opt,
                  VARIANT* in_args,
                  VARIANT* out_args);

  // IWebBrowserEventsUrlService methods
  STDMETHOD(GetUrlForEvents)(BSTR* url);

  // ChromeFrameActivexBase overrides
  HRESULT IOleObject_SetClientSite(IOleClientSite* client_site);

  HRESULT ActiveXDocActivate(LONG verb);

  // Callbacks from ChromeFramePlugin<T>
  bool PreProcessContextMenu(HMENU menu);
  bool HandleContextMenuCommand(UINT cmd);

  // Should connections initiated by this class try to block
  // responses served with the X-Frame-Options header?
  bool is_frame_busting_enabled();

 protected:
  // ChromeFrameActivexBase overrides
  virtual void OnOpenURL(int tab_handle, const GURL& url_to_open,
                         const GURL& referrer, int open_disposition);

  virtual void OnLoad(int tab_handle, const GURL& url);
  virtual void OnGoToHistoryEntryOffset(int tab_handle, int offset);

  // A helper method that updates our internal navigation state
  // as well as IE's navigation state (viz Title and current URL).
  // The navigation_flags is a TabContents::InvalidateTypes enum
  void UpdateNavigationState(const IPC::NavigationInfo& nav_info);

  TabProxy* GetTabProxy() const {
    if (automation_client_.get())
      return automation_client_->tab();
    return NULL;
  }

  // Exec command handlers
  void OnFindInPage();
  void OnViewSource();
  void OnDetermineSecurityZone(const GUID* cmd_group_guid, DWORD command_id,
                               DWORD cmd_exec_opt, VARIANT* in_args,
                               VARIANT* out_args);

  // Call exec on our site's command target
  HRESULT IEExec(const GUID* cmd_group_guid, DWORD command_id,
                 DWORD cmd_exec_opt, VARIANT* in_args, VARIANT* out_args);

  bool IsUrlZoneRestricted(const std::wstring& url);

  // Parses the URL and returns information whether it is a new navigation and
  // the actual url after stripping out the cf: prefix if any.
  // This function also checks if the url scheme is valid for navigation within
  // chrome and whether it is a restricted URL as per IE settings. In either of
  // these cases it returns false.
  bool ParseUrl(const std::wstring& url, bool* is_new_navigation,
                bool* is_chrome_protocol, std::wstring* parsed_url);

  // Initiates navigation to the URL passed in.
  // Returns true on success.
  bool LaunchUrl(const std::wstring& url, bool is_new_navigation);

  // Handler to set the page font size in Chrome.
  HRESULT SetPageFontSize(const GUID* cmd_group_guid,
                          DWORD command_id,
                          DWORD cmd_exec_opt,
                          VARIANT* in_args,
                          VARIANT* out_args);

 protected:
  typedef std::map<int, bool> EnabledCommandsMap;

  IPC::NavigationInfo navigation_info_;
  bool is_doc_object_;

  // This indicates whether this is the first navigation in this
  // active document. It is initalize to true and it is set to false
  // after we get a navigation notification from Chrome
  bool first_navigation_;

  // Our find dialog
  CFFindDialog find_dialog_;

  // Contains the list of enabled commands ids.
  EnabledCommandsMap enabled_commands_map_;

  // Set to true if the automation_client_ member is initialized from
  // an existing ChromeActiveDocument instance which is going away and
  // a new ChromeActiveDocument instance is taking its place.
  bool is_automation_client_reused_;

  ScopedComPtr<IInternetSecurityManager> security_manager_;

 public:
  ScopedComPtr<IOleInPlaceFrame> in_place_frame_;
  OLEINPLACEFRAMEINFO frame_info_;
};

OBJECT_ENTRY_AUTO(__uuidof(ChromeActiveDocument), ChromeActiveDocument)

#endif  // CHROME_FRAME_CHROME_ACTIVE_DOCUMENT_H_