XWalk WebView patchset, README and LICENSE files.
[platform/framework/web/xwalk_webview.git] / patchset / 0012-WebView-re-factoring-no-dep-to-elm-memory-management.patch
1 From 4acef7c4becad5d74cbdfda9045d968a8628eb27 Mon Sep 17 00:00:00 2001
2 From: Mikhail Pozdnyakov <mikhail.pozdnyakov@intel.com>
3 Date: Thu, 1 Aug 2013 13:57:05 +0300
4 Subject: [PATCH 12/33] WebView re-factoring: no dep to elm, memory management.
5
6 ---
7  .../renderer_host/render_widget_host_view_efl.cc   |   4 +-
8  .../renderer_host/render_widget_host_view_efl.h    |   2 +-
9  efl_webview/examples/main.cc                       |   2 +-
10  efl_webview/lib/webview.cc                         | 192 ++++++++++++++-------
11  efl_webview/lib/webview.h                          |  29 +++-
12  efl_webview/public/xwalk_main.cc                   |   2 +-
13  efl_webview/public/xwalk_view.cc                   |  39 ++---
14  efl_webview/public/xwalk_view.h                    |   2 +-
15  8 files changed, 170 insertions(+), 102 deletions(-)
16
17 diff --git a/content/browser/renderer_host/render_widget_host_view_efl.cc b/content/browser/renderer_host/render_widget_host_view_efl.cc
18 index 1e12159..76b19f3 100644
19 --- a/content/browser/renderer_host/render_widget_host_view_efl.cc
20 +++ b/content/browser/renderer_host/render_widget_host_view_efl.cc
21 @@ -200,11 +200,11 @@ bool RenderWidgetHostViewEfl::OnMessageReceived(const IPC::Message& message) {
22  
23  void RenderWidgetHostViewEfl::InitAsChild(
24      gfx::NativeView parent_view) {
25 -  preserve_window_ = gfx::PreserveWindow::Create(this, evas_object_evas_get(parent_view));
26 +  preserve_window_.reset(gfx::PreserveWindow::Create(this, evas_object_evas_get(parent_view)));
27    evas_object_size_hint_align_set(preserve_window_->SmartObject(), EVAS_HINT_FILL, EVAS_HINT_FILL);
28    evas_object_size_hint_weight_set(preserve_window_->SmartObject(), EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
29 -  elm_box_pack_end(parent_view, preserve_window_->SmartObject());
30    evas_object_show(preserve_window_->SmartObject());
31 +  evas_object_box_append(parent_view, preserve_window_->SmartObject());
32    compositing_surface_ = preserve_window_->EmbeddedXWindow();
33  }
34  
35 diff --git a/content/browser/renderer_host/render_widget_host_view_efl.h b/content/browser/renderer_host/render_widget_host_view_efl.h
36 index e20f51b..f047e35 100644
37 --- a/content/browser/renderer_host/render_widget_host_view_efl.h
38 +++ b/content/browser/renderer_host/render_widget_host_view_efl.h
39 @@ -287,7 +287,7 @@ class CONTENT_EXPORT RenderWidgetHostViewEfl
40  
41    gfx::PluginWindowHandle compositing_surface_;
42  
43 -  gfx::PreserveWindow* preserve_window_;
44 +  scoped_ptr<gfx::PreserveWindow> preserve_window_;
45  
46    scoped_ptr<BrowserAccessibilityManager> browser_accessibility_manager_;
47  };
48 diff --git a/efl_webview/examples/main.cc b/efl_webview/examples/main.cc
49 index 6dac4e8..b9ece50 100644
50 --- a/efl_webview/examples/main.cc
51 +++ b/efl_webview/examples/main.cc
52 @@ -103,7 +103,7 @@ static void window_create()
53    evas_object_show(url_entry);
54  
55    /* Create WebView */
56 -  Evas_Object* web_view = xwalk_view_add(elm_window);
57 +  Evas_Object* web_view = xwalk_view_add(evas_object_evas_get(elm_window));
58    evas_object_size_hint_weight_set(web_view, EVAS_HINT_EXPAND,
59                                     EVAS_HINT_EXPAND);
60    evas_object_size_hint_align_set(web_view, EVAS_HINT_FILL, EVAS_HINT_FILL);
61 diff --git a/efl_webview/lib/webview.cc b/efl_webview/lib/webview.cc
62 index 61ecb96..ae886e7 100644
63 --- a/efl_webview/lib/webview.cc
64 +++ b/efl_webview/lib/webview.cc
65 @@ -4,10 +4,6 @@
66  
67  #include "efl_webview/lib/webview.h"
68  
69 -#include <Elementary.h>
70 -
71 -#include <map>
72 -
73  #include "base/command_line.h"
74  #include "base/file_util.h"
75  #include "base/files/file_path.h"
76 @@ -20,35 +16,108 @@
77  
78  namespace {
79  
80 -typedef std::map<const Evas_Object*, xwalk::WebView*> EvasWebViewMap;
81 +const char evas_smart_xwalk_view_type[] = "Evas_Smart_Xwalk_View";
82 +
83 +struct XWalk_View_Smart_Data {
84 +    Evas_Object_Box_Data base_;
85 +    xwalk::WebViewPrivate* priv_;
86 +};
87 +
88 +bool IsXWalkViewEvasObject(const Evas_Object* evas_object) {
89 +  DCHECK(evas_object);
90 +
91 +  const char* evas_object_type = evas_object_type_get(evas_object);
92 +  if (!evas_object_smart_type_check(evas_object, evas_smart_xwalk_view_type)) {
93 +    LOG(ERROR) << evas_object << " is not of an "
94 +               << evas_object_type << "!";
95 +    return false;
96 +  }
97 +
98 +  const Evas_Smart* evas_smart = evas_object_smart_smart_get(evas_object);
99 +  if (!evas_smart) {
100 +    LOG(ERROR) << evas_object << "("
101 +               << evas_object_type << ") is not a smart object!";
102 +    return false;
103 +  }
104 +
105 +  const Evas_Smart_Class* smart_class = evas_smart_class_get(evas_smart);
106 +  if (!smart_class) {
107 +    LOG(ERROR) << evas_object << "("
108 +               << evas_object_type << ") is not a smart class object!";
109 +    return false;
110 +  }
111 +
112 +  return true;
113 +}
114 +
115 +inline XWalk_View_Smart_Data* ToSmartData(const Evas_Object* evas_object) {
116 +  DCHECK(evas_object);
117 +  DCHECK(IsXWalkViewEvasObject(evas_object));
118 +  CHECK(evas_object_smart_data_get(evas_object));
119 +  return static_cast<XWalk_View_Smart_Data*>(
120 +            evas_object_smart_data_get(evas_object));
121 +}
122 +
123 +EVAS_SMART_SUBCLASS_NEW(evas_smart_xwalk_view_type, xwalk_view,
124 +                        Evas_Object_Box_Api, Evas_Object_Box_Api,
125 +                        evas_object_box_smart_class_get, 0);
126 +
127 +void xwalk_view_smart_add(Evas_Object* o) {
128 +  XWalk_View_Smart_Data* smart_data =
129 +        static_cast<XWalk_View_Smart_Data*>(evas_object_smart_data_get(o));
130  
131 -inline EvasWebViewMap& EvasObjectToWebViewMap() {
132 -// FIXME: Temporary solution until web view has its own smart class.
133 -  static EvasWebViewMap map;
134 -  return map;
135 +  if (!smart_data) {
136 +    smart_data = static_cast<XWalk_View_Smart_Data*>(
137 +        calloc(1, sizeof(XWalk_View_Smart_Data)));
138 +
139 +    const Evas_Smart* smart = evas_object_smart_smart_get(o);
140 +    const Evas_Smart_Class* smart_class = evas_smart_class_get(smart);
141 +    smart_data->base_.api =
142 +        reinterpret_cast<const Evas_Object_Box_Api*>(smart_class);
143 +
144 +    smart_data->priv_ = 0;
145 +
146 +    evas_object_smart_data_set(o, smart_data);
147 +  }
148 +
149 +  xwalk_view_parent_sc->base.add(o);
150 +}
151 +
152 +void xwalk_view_smart_del(Evas_Object* object) {
153 +  XWalk_View_Smart_Data* smart_data = ToSmartData(object);
154 +  DCHECK(smart_data->priv_);
155 +  delete smart_data->priv_;
156 +  xwalk_view_parent_sc->base.del(object);
157 +}
158 +
159 +void xwalk_view_smart_set_user(Evas_Object_Box_Api* smart_class) {
160 +  smart_class->base.add = xwalk_view_smart_add;
161 +  smart_class->base.del = xwalk_view_smart_del;
162  }
163  
164  }  // namespace
165  
166  namespace xwalk {
167  
168 -struct WebView::Private {
169 -  Evas_Object* root_window;
170 -  Evas_Object* view_box;
171 -  scoped_refptr<WebRuntimeContext> context;
172 -  scoped_ptr<WebContentsDelegateXWalk> webContentsDelegate;
173 -  static GURL s_startup_url;
174 -};
175 +GURL WebViewPrivate::s_startup_url = GURL();
176  
177 -GURL WebView::Private::s_startup_url = GURL();
178 +Evas_Object* CreateWebView(Evas* canvas) {
179 +  DCHECK(canvas);
180  
181 -// static
182 -WebView* WebView::Create(Evas_Object* root_window) {
183 -  return new WebView(root_window);
184 +  Evas_Object* view_object =
185 +      evas_object_smart_add(canvas, xwalk_view_smart_class_new());
186 +
187 +  XWalk_View_Smart_Data* smart_data = ToSmartData(view_object);
188 +  DCHECK(smart_data);
189 +
190 +  DCHECK(!smart_data->priv_);
191 +  smart_data->priv_ = new WebViewPrivate(view_object);
192 +
193 +  return view_object;
194  }
195  
196  // static
197 -void WebView::CommandLineInit(int argc, char** argv) {
198 +void WebViewPrivate::CommandLineInit(int argc, char** argv) {
199    CommandLine::Init(argc, argv);
200  
201    CommandLine* command_line = CommandLine::ForCurrentProcess();
202 @@ -61,86 +130,75 @@ void WebView::CommandLineInit(int argc, char** argv) {
203    if (!(url.is_valid() && url.has_scheme()))
204      url = net::FilePathToFileURL(base::FilePath(args[0]));
205  
206 -  WebView::Private::s_startup_url = GURL(url);
207 +  WebViewPrivate::s_startup_url = GURL(url);
208  }
209  
210 -WebView::WebView(Evas_Object* root_window)
211 -    : private_(new Private) {
212 +WebViewPrivate::WebViewPrivate(Evas_Object* view_object)
213 +    : view_object_(view_object)
214 +    , context_(WebRuntimeContext::current()) {
215    {
216 -    if (!WebView::Private::s_startup_url.is_valid())
217 -      WebView::Private::s_startup_url = GURL("about:blank");
218 +    if (!WebViewPrivate::s_startup_url.is_valid())
219 +      WebViewPrivate::s_startup_url = GURL("about:blank");
220    }
221  
222 -  private_->root_window = root_window;
223 -  private_->view_box = elm_box_add(private_->root_window);
224 -  // FIXME: In future this has to be a separate Smart Class representing web view.
225 -  // 'this' should be set as smart_data->priv
226 -  EvasObjectToWebViewMap()[private_->view_box] = this;
227 -
228 -  elm_object_focus_allow_set(private_->view_box, EINA_TRUE);
229 -
230 -  private_->context = WebRuntimeContext::current();
231 +  DCHECK(view_object_);
232    content::BrowserContext* browser_context =
233 -      private_->context->BrowserContext();
234 -  private_->webContentsDelegate.reset(
235 -      new WebContentsDelegateXWalk(browser_context, private_->view_box));
236 +      context_->BrowserContext();
237 +  web_contents_delegate_.reset(
238 +      new WebContentsDelegateXWalk(browser_context, view_object_));
239  
240    content::WebContentsViewEfl* content_view =
241 -      static_cast<content::WebContentsViewEfl*>(private_->
242 -          webContentsDelegate->WebContents()->GetView());
243 -  // FIXME: Don't pass elm_box to WebContentsViewEfl. Need focus handling of PreserveWindow evas object here.
244 -  content_view->SetViewContainerBox(private_->view_box);
245 +      static_cast<content::WebContentsViewEfl*>(web_contents_delegate_->
246 +          WebContents()->GetView());
247 +  content_view->SetViewContainerBox(view_object_);
248    static_cast<WebContentsViewDelegateXWalk*>(content_view->delegate())->
249 -      SetViewContainerBox(private_->view_box);
250 +      SetViewContainerBox(view_object_);
251  
252 -  LoadURL(WebView::Private::s_startup_url);
253 +  LoadURL(WebViewPrivate::s_startup_url);
254  }
255  
256 -WebView::~WebView() {
257 -// FIXME : And by the way who will invoke it?
258 -  EvasObjectToWebViewMap().erase(private_->view_box);
259 -  evas_object_del(private_->view_box);
260 +WebViewPrivate::~WebViewPrivate() {
261  }
262  
263 -bool WebView::CanGoBack() const {
264 -  return private_->webContentsDelegate->WebContents()->GetController().CanGoBack();
265 +bool WebViewPrivate::CanGoBack() const {
266 +  return web_contents_delegate_->WebContents()->GetController().CanGoBack();
267  }
268  
269 -bool WebView::CanGoForward() const {
270 -  return private_->webContentsDelegate->WebContents()->GetController().CanGoForward();
271 +bool WebViewPrivate::CanGoForward() const {
272 +  return web_contents_delegate_->WebContents()->GetController().CanGoForward();
273  }
274  
275 -void WebView::GoForward() {
276 -  private_->webContentsDelegate->WebContents()->GetController().GoForward();
277 +void WebViewPrivate::GoForward() {
278 +  web_contents_delegate_->WebContents()->GetController().GoForward();
279  }
280  
281 -void WebView::GoBack() {
282 -  private_->webContentsDelegate->WebContents()->GetController().GoBack();
283 +void WebViewPrivate::GoBack() {
284 +  web_contents_delegate_->WebContents()->GetController().GoBack();
285  }
286  
287 -void WebView::Reload() {
288 -  private_->webContentsDelegate->WebContents()->GetController().Reload(false);
289 +void WebViewPrivate::Reload() {
290 +  web_contents_delegate_->WebContents()->GetController().Reload(false);
291  }
292  
293 -void WebView::LoadURL(const GURL& url) {
294 +void WebViewPrivate::LoadURL(const GURL& url) {
295    url_ = EinaSharedString(url.spec());
296    content::NavigationController::LoadURLParams params(url);
297    params.transition_type = content::PageTransitionFromInt(
298        content::PAGE_TRANSITION_TYPED |
299        content::PAGE_TRANSITION_FROM_ADDRESS_BAR);
300 -  private_->webContentsDelegate->WebContents()->
301 +  web_contents_delegate_->WebContents()->
302        GetController().LoadURLWithParams(params);
303 -  private_->webContentsDelegate->WebContents()->GetView()->Focus();
304 +  web_contents_delegate_->WebContents()->GetView()->Focus();
305  }
306  
307 -Evas_Object* WebView::EvasObject() {
308 -  return private_->view_box;
309 +Evas_Object* WebViewPrivate::EvasObject() {
310 +  return view_object_;
311  }
312  
313 -WebView* ToWebView(const Evas_Object* evas_object) {
314 -  EvasWebViewMap::iterator found = EvasObjectToWebViewMap().find(evas_object);
315 -  if (found != EvasObjectToWebViewMap().end())
316 -    return found->second;
317 +WebViewPrivate* ToWebViewPrivate(const Evas_Object* evas_object) {
318 +  if (evas_object && IsXWalkViewEvasObject(evas_object))
319 +    return ToSmartData(evas_object)->priv_;
320 +
321    return 0;
322  }
323  
324 diff --git a/efl_webview/lib/webview.h b/efl_webview/lib/webview.h
325 index ae4f98f..62a5772 100644
326 --- a/efl_webview/lib/webview.h
327 +++ b/efl_webview/lib/webview.h
328 @@ -8,18 +8,21 @@
329  #include <Evas.h>
330  
331  #include "base/basictypes.h"
332 +#include "base/memory/ref_counted.h"
333  #include "base/memory/scoped_ptr.h"
334  #include "base/strings/efl/eina_shared_string.h"
335  #include "googleurl/src/gurl.h"
336  
337  namespace xwalk {
338  
339 -class EAPI WebView {
340 +class WebContentsDelegateXWalk;
341 +class WebRuntimeContext;
342 +
343 +class WebViewPrivate {
344   public:
345 -  EAPI static WebView* Create(Evas_Object* root_window);
346 -  EAPI static void CommandLineInit(int argc, char** argv);
347 +  static void CommandLineInit(int argc, char** argv);
348  
349 -  ~WebView();
350 +  ~WebViewPrivate();
351  
352    EAPI Evas_Object* EvasObject();
353  
354 @@ -33,16 +36,24 @@ class EAPI WebView {
355    const char* url() const { return url_; }
356  
357   private:
358 -  explicit WebView(Evas_Object* root_window);
359 +  explicit WebViewPrivate(Evas_Object* evas_object);
360  
361 -  struct Private;
362 -  scoped_ptr<Private> private_;
363 +  Evas_Object* view_object_;
364 +  scoped_refptr<WebRuntimeContext> context_;
365 +  scoped_ptr<WebContentsDelegateXWalk> web_contents_delegate_;
366    EinaSharedString url_;
367  
368 -  DISALLOW_COPY_AND_ASSIGN(WebView);
369 +  static GURL s_startup_url;
370 +
371 +  friend Evas_Object* CreateWebView(Evas* canvas);
372 +
373 +  DISALLOW_COPY_AND_ASSIGN(WebViewPrivate);
374  };
375  
376 -WebView* ToWebView(const Evas_Object*);
377 +
378 +Evas_Object* CreateWebView(Evas* canvas);
379 +
380 +WebViewPrivate* ToWebViewPrivate(const Evas_Object* web_view);
381  
382  }  // namespace xwalk
383  
384 diff --git a/efl_webview/public/xwalk_main.cc b/efl_webview/public/xwalk_main.cc
385 index 218c55d..2dfc902 100644
386 --- a/efl_webview/public/xwalk_main.cc
387 +++ b/efl_webview/public/xwalk_main.cc
388 @@ -6,5 +6,5 @@
389  #include "efl_webview/lib/webview.h"
390  
391  void xwalk_init(int argc, char *argv[]) {
392 -  xwalk::WebView::CommandLineInit(argc, argv);
393 +  xwalk::WebViewPrivate::CommandLineInit(argc, argv);
394  }
395 diff --git a/efl_webview/public/xwalk_view.cc b/efl_webview/public/xwalk_view.cc
396 index b000143..0acd334 100644
397 --- a/efl_webview/public/xwalk_view.cc
398 +++ b/efl_webview/public/xwalk_view.cc
399 @@ -5,55 +5,54 @@
400  #include "efl_webview/public/xwalk_view.h"
401  #include "efl_webview/lib/webview.h"
402  
403 -using namespace xwalk;
404 -
405 -#define XWALK_VIEW_GET_IMPL_OR_RETURN(xwalk_view, impl, ...)         \
406 -  WebView* impl = ToWebView(xwalk_view);                             \
407 +#define XWALK_VIEW_GET_PRIVATE_OR_RETURN(xwalk_view, priv, ...)      \
408 +  xwalk::WebViewPrivate* priv = xwalk::ToWebViewPrivate(xwalk_view); \
409    do {                                                               \
410 -    if (!impl) {                                                     \
411 +    if (!priv) {                                                     \
412        EINA_LOG_CRIT("no private data for object %p", xwalk_view);    \
413        return __VA_ARGS__;                                            \
414      }                                                                \
415    } while (0)
416  
417  
418 -Evas_Object* xwalk_view_add(Evas_Object* root_window) {
419 -  return WebView::Create(root_window)->EvasObject();
420 +Evas_Object* xwalk_view_add(Evas* canvas) {
421 +  EINA_SAFETY_ON_NULL_RETURN_VAL(canvas, 0);
422 +
423 +  return xwalk::CreateWebView(canvas);
424  }
425  
426  Eina_Bool xwalk_view_url_set(Evas_Object* evas_object, const char* url) {
427 -  XWALK_VIEW_GET_IMPL_OR_RETURN(evas_object, impl, false);
428 +  XWALK_VIEW_GET_PRIVATE_OR_RETURN(evas_object, priv, false);
429    EINA_SAFETY_ON_NULL_RETURN_VAL(url, false);
430  
431 -  impl->LoadURL(GURL(url));
432 +  priv->LoadURL(GURL(url));
433    return true;
434  }
435  
436  const char* xwalk_view_url_get(const Evas_Object* evas_object) {
437 -  XWALK_VIEW_GET_IMPL_OR_RETURN(evas_object, impl, "");
438 -  return impl->url();
439 +  XWALK_VIEW_GET_PRIVATE_OR_RETURN(evas_object, priv, "");
440 +  return priv->url();
441  }
442  
443  Eina_Bool xwalk_view_reload(Evas_Object* evas_object) {
444 -  XWALK_VIEW_GET_IMPL_OR_RETURN(evas_object, impl, false);
445 -  impl->Reload();
446 -
447 +  XWALK_VIEW_GET_PRIVATE_OR_RETURN(evas_object, priv, false);
448 +  priv->Reload();
449    return true;
450  }
451  
452  Eina_Bool xwalk_view_back(Evas_Object* evas_object) {
453 -  XWALK_VIEW_GET_IMPL_OR_RETURN(evas_object, impl, false);
454 -  if (impl->CanGoBack()) {
455 -    impl->GoBack();
456 +  XWALK_VIEW_GET_PRIVATE_OR_RETURN(evas_object, priv, false);
457 +  if (priv->CanGoBack()) {
458 +    priv->GoBack();
459      return true;
460    }
461    return false;
462  }
463  
464  Eina_Bool xwalk_view_forward(Evas_Object* evas_object) {
465 -  XWALK_VIEW_GET_IMPL_OR_RETURN(evas_object, impl, false);
466 -  if (impl->CanGoForward()) {
467 -    impl->GoForward();
468 +  XWALK_VIEW_GET_PRIVATE_OR_RETURN(evas_object, priv, false);
469 +  if (priv->CanGoForward()) {
470 +    priv->GoForward();
471      return true;
472    }
473    return false;
474 diff --git a/efl_webview/public/xwalk_view.h b/efl_webview/public/xwalk_view.h
475 index a4c85d58..fbde786 100644
476 --- a/efl_webview/public/xwalk_view.h
477 +++ b/efl_webview/public/xwalk_view.h
478 @@ -11,7 +11,7 @@
479  extern "C" {
480  #endif
481  
482 -EAPI Evas_Object *xwalk_view_add(Evas_Object *root_window);
483 +EAPI Evas_Object *xwalk_view_add(Evas *canvas);
484  
485  EAPI Eina_Bool xwalk_view_url_set(Evas_Object *obj, const char *url);
486  
487 -- 
488 1.8.1.2
489