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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef CHROME_BROWSER_NATIVE_UI_CONTENTS_H__
#define CHROME_BROWSER_NATIVE_UI_CONTENTS_H__
#include "chrome/browser/page_state.h"
#include "chrome/browser/tab_contents.h"
#include "chrome/views/background.h"
#include "chrome/views/hwnd_view_container.h"
#include "chrome/views/link.h"
#include "chrome/views/native_button.h"
#include "chrome/views/text_field.h"
namespace ChromeViews {
class CheckBox;
class FocusTraversable;
class ScrollView;
class Throbber;
}
class NativeUIFactory;
class NativeUI;
////////////////////////////////////////////////////////////////////////////////
//
// NativeUIContents
//
// NativeUIContents is a TabContents that is used to show some pages made with
// some native user interface elements. NativeUIContents maintains a list of URL
// path mapping to specific NativeUI implementations.
//
////////////////////////////////////////////////////////////////////////////////
class NativeUIContents : public TabContents,
public ChromeViews::HWNDViewContainer {
public:
explicit NativeUIContents(Profile* profile);
virtual void CreateView(HWND parent_hwnd, const gfx::Rect& initial_bounds);
virtual HWND GetContainerHWND() const { return GetHWND(); }
virtual void GetContainerBounds(gfx::Rect *out) const;
// Sets the page state. NativeUIContents takes ownership of the supplied
// PageState. Use a value of NULL to set the state to empty.
void SetPageState(PageState* page_state);
// Returns the page state. This is intended for UIs that want to store page
// state.
const PageState& page_state() const { return *state_; }
//
// TabContents implementation
//
virtual bool Navigate(const NavigationEntry& entry, bool reload);
virtual const std::wstring GetDefaultTitle() const;
virtual SkBitmap GetFavIcon() const;
virtual bool ShouldDisplayURL() { return false; }
virtual bool ShouldDisplayFavIcon() { return true; }
virtual void DidBecomeSelected();
virtual void SetInitialFocus();
// Sets the current loading state. This is public for NativeUIs to update.
void SetIsLoading(bool is_loading, LoadNotificationDetails* details);
// FocusTraversable Implementation
virtual ChromeViews::View* FindNextFocusableView(
ChromeViews::View* starting_view,
bool reverse,
ChromeViews::FocusTraversable::Direction direction,
bool dont_loop,
ChromeViews::FocusTraversable** focus_traversable,
ChromeViews::View** focus_traversable_view);
virtual ChromeViews::RootView* GetContentsRootView() { return GetRootView(); }
// Return the scheme used. We currently use nativeui:
static std::string GetScheme();
// Register a NativeUIFactory for a given path.
static void RegisterNativeUIFactory(const GURL& url,
NativeUIFactory* factory);
protected:
// Should be deleted via CloseContents.
virtual ~NativeUIContents();
// Overridden to create a view that that handles drag and drop.
virtual ChromeViews::RootView* CreateRootView();
private:
// Initialize the factories. This is called the first time a NativeUIContents
// object is created. If you add a new factory, you need to add a line in this
// method.
static void InitializeNativeUIFactories();
// Instantiates a native UI for the provided URL. This is done by using the
// native factories which have been registered.
static NativeUI* InstantiateNativeUIForURL(const GURL& url,
NativeUIContents* contents);
// Returns the key to use based on the TabUI's url.
static std::string GetFactoryKey(const GURL& url);
// Size the current UI if any.
void Layout();
// Return the Native UI for the provided URL. The NativeUIs are returned from
// a cache. Returns NULL if no such UI exists.
NativeUI* GetNativeUIForURL(const GURL& url);
// Windows message handlers.
virtual LRESULT OnCreate(LPCREATESTRUCT create_struct);
virtual void OnDestroy();
virtual void OnSize(UINT size_command, const CSize& new_size);
virtual void OnWindowPosChanged(WINDOWPOS* position);
// Whether this contents is visible.
bool is_visible_;
// Path to NativeUI map. We keep reusing the same UIs.
typedef std::map<std::string, NativeUI*> PathToUI;
PathToUI path_to_native_uis_;
// The current UI.
NativeUI* current_ui_;
// The current view for the current UI. We don't ask again just in case the
// UI implementation keeps allocating new uis.
ChromeViews::View* current_view_;
// The current page state for the native contents.
scoped_ptr<PageState> state_;
// Whether factories have been initialized.
static bool g_ui_factories_initialized;
DISALLOW_EVIL_CONSTRUCTORS(NativeUIContents);
};
/////////////////////////////////////////////////////////////////////////////
//
// A native UI needs to implement the following interface to work with the
// NativeUIContents.
//
/////////////////////////////////////////////////////////////////////////////
class NativeUI {
public:
virtual ~NativeUI() {}
// Return the title for this user interface. The title is used as a tab title.
virtual const std::wstring GetTitle() const = 0;
// Return the favicon id for this user interface.
virtual const int GetFavIconID () const = 0;
// Return the view that should be used to render this user interface.
virtual ChromeViews::View* GetView() = 0;
// Inform the view that it is about to become visible.
virtual void WillBecomeVisible(NativeUIContents* parent) = 0;
// Inform the view that it is about to become invisible.
virtual void WillBecomeInvisible(NativeUIContents* parent) = 0;
// Inform the view that it should recreate the provided state. The state
// should be updated as needed by using the current navigation entry of
// the provided tab contents.
virtual void Navigate(const PageState& state) = 0;
// Requests the contents set the initial focus. A return value of true
// indicates the contents wants focus and requested focus. A return value of
// false indicates the contents does not want focus, and that focus should
// go to the location bar.
virtual bool SetInitialFocus() = 0;
};
/////////////////////////////////////////////////////////////////////////////
//
// NativeUIFactory defines the method necessary to instantiate a NativeUI
// object. Typically, each NativeUI implementation registers an object that
// can instantiate NativeUI objects given the necessary path.
//
/////////////////////////////////////////////////////////////////////////////
class NativeUIFactory {
public:
virtual ~NativeUIFactory() {}
// Request the factory to instantiate a NativeUI object given the provided
// url. The url is a nativeui: URL which contains the path for which this
// factory was registered.
//
// See NativeUIContents::RegisterNativeUI().
virtual NativeUI* CreateNativeUIForURL(const GURL& url,
NativeUIContents* contents) = 0;
};
////////////////////////////////////////////////////////////////////////////////
//
// A standard background for native UIs.
//
////////////////////////////////////////////////////////////////////////////////
class NativeUIBackground : public ChromeViews::Background {
public:
NativeUIBackground();
virtual ~NativeUIBackground();
virtual void Paint(ChromeCanvas* canvas, ChromeViews::View* view) const;
private:
DISALLOW_EVIL_CONSTRUCTORS(NativeUIBackground);
};
////////////////////////////////////////////////////////////////////////////////
//
// A view subclass used to implement native uis that feature a search field.
// This view contains a search field and a ScrollView for the contents. It
// implements a consistent look for these UIs.
//
////////////////////////////////////////////////////////////////////////////////
class SearchableUIContainer : public ChromeViews::View,
public ChromeViews::NativeButton::Listener,
public ChromeViews::LinkController,
public ChromeViews::TextField::Controller {
public:
// The Delegate is notified when the user clicks the search button.
class Delegate {
public:
virtual void DoSearch(const std::wstring& text) = 0;
virtual const std::wstring GetTitle() const = 0;
virtual const int GetSectionIconID() const = 0;
virtual const std::wstring GetSearchButtonText() const = 0;
};
// Create a new SearchableUIContainer given a delegate.
explicit SearchableUIContainer(Delegate* delegate);
virtual ~SearchableUIContainer();
// Add the view as the contents of the container.
void SetContents(ChromeViews::View* contents);
ChromeViews::View* GetContents();
virtual void Layout();
// Overriden to paint the container.
virtual void Paint(ChromeCanvas* canvas);
// Provide the mode access to various UI elements.
ChromeViews::TextField* GetSearchField() const;
ChromeViews::ScrollView* GetScrollView() const;
// Enable/disable the search text-field/button.
void SetSearchEnabled(bool enabled);
// Start and stop the throbber.
void StartThrobber();
void StopThrobber();
private:
// Invoked when the user presses the search button.
virtual void ButtonPressed(ChromeViews::NativeButton* sender);
// TextField method, does nothing.
virtual void ContentsChanged(ChromeViews::TextField* sender,
const std::wstring& new_contents) {}
// Textfield method, if key is the return key the search is updated.
virtual void HandleKeystroke(ChromeViews::TextField* sender,
UINT message,
TCHAR key,
UINT repeat_count,
UINT flags);
// Notifies the delegate to update the search.
void DoSearch();
void LinkActivated(ChromeViews::Link* link, int event_flags);
Delegate* delegate_;
ChromeViews::Link* title_link_;
ChromeViews::ImageView* title_image_;
ChromeViews::ImageView* product_logo_;
ChromeViews::TextField* search_field_;
ChromeViews::NativeButton* search_button_;
ChromeViews::ScrollView* scroll_view_;
ChromeViews::Throbber* throbber_;
DISALLOW_EVIL_CONSTRUCTORS(SearchableUIContainer);
};
#endif // CHROME_BROWSER_NATIVE_UI_CONTENTS_H__
|