XWalk WebView patchset, README and LICENSE files.
[platform/framework/web/xwalk_webview.git] / patchset / 0010-Introducing-C-API-for-EFL-Web-View.patch
1 From 1fe3191d93a63d2279747388e237907439287698 Mon Sep 17 00:00:00 2001
2 From: Mikhail Pozdnyakov <mikhail.pozdnyakov@intel.com>
3 Date: Wed, 10 Jul 2013 17:23:24 +0300
4 Subject: [PATCH 10/33] Introducing C API for EFL Web View.
5
6 Wrap xwalk initialization
7 Introduce EinaSharedString
8 Introduce xwalk_view_url api
9 ---
10  base/base.gyp                          |  2 ++
11  base/strings/efl/eina_shared_string.cc | 53 ++++++++++++++++++++++++++++++
12  base/strings/efl/eina_shared_string.h  | 52 +++++++++++++++++++++++++++++
13  efl_webview/efl_webview.gyp            |  4 +++
14  efl_webview/examples/main.cc           | 39 +++++++++++-----------
15  efl_webview/lib/webview.cc             | 41 +++++++++++++++++++++--
16  efl_webview/lib/webview.h              | 24 +++++++++-----
17  efl_webview/public/xwalk_main.cc       | 10 ++++++
18  efl_webview/public/xwalk_main.h        | 19 +++++++++++
19  efl_webview/public/xwalk_view.cc       | 60 ++++++++++++++++++++++++++++++++++
20  efl_webview/public/xwalk_view.h        | 29 ++++++++++++++++
21  11 files changed, 303 insertions(+), 30 deletions(-)
22  create mode 100644 base/strings/efl/eina_shared_string.cc
23  create mode 100644 base/strings/efl/eina_shared_string.h
24  create mode 100644 efl_webview/public/xwalk_main.cc
25  create mode 100644 efl_webview/public/xwalk_main.h
26  create mode 100644 efl_webview/public/xwalk_view.cc
27  create mode 100644 efl_webview/public/xwalk_view.h
28
29 diff --git a/base/base.gyp b/base/base.gyp
30 index b9e7e36..6b80e40 100644
31 --- a/base/base.gyp
32 +++ b/base/base.gyp
33 @@ -87,6 +87,8 @@
34              '../build/linux/system.gyp:efl',
35            ],
36            'sources': [
37 +            'strings/efl/eina_shared_string.cc',
38 +            'strings/efl/eina_shared_string.h',
39              'message_pump_efl.cc',
40              'message_pump_efl.h',
41            ],
42 diff --git a/base/strings/efl/eina_shared_string.cc b/base/strings/efl/eina_shared_string.cc
43 new file mode 100644
44 index 0000000..48b4862
45 --- /dev/null
46 +++ b/base/strings/efl/eina_shared_string.cc
47 @@ -0,0 +1,53 @@
48 +// Copyright (c) 2013 Intel Corporation. All rights reserved.
49 +// Use of this source code is governed by a BSD-style license that can be
50 +// found in the LICENSE file.
51 +
52 +#include "base/strings/efl/eina_shared_string.h"
53 +
54 +EinaSharedString::EinaSharedString(const EinaSharedString& other)
55 +  : string_(eina_stringshare_ref(other.string_)) {
56 +}
57 +
58 +EinaSharedString::EinaSharedString(const char* str)
59 +  : string_(eina_stringshare_add(str)) {
60 +}
61 +
62 +EinaSharedString::EinaSharedString(const std::string& str)
63 +  : string_(eina_stringshare_add(str.c_str())) {
64 +}
65 +
66 +EinaSharedString::~EinaSharedString() {
67 +  if (string_)
68 +    eina_stringshare_del(string_);
69 +}
70 +
71 +EinaSharedString& EinaSharedString::operator=(const EinaSharedString& other) {
72 +  if (this != &other) {
73 +    if (string_)
74 +      eina_stringshare_del(string_);
75 +    string_ = eina_stringshare_ref(other.string_);
76 +  }
77 +  return *this;
78 +}
79 +
80 +EinaSharedString& EinaSharedString::operator=(const char* str) {
81 +  eina_stringshare_replace(&string_, str);
82 +  return *this;
83 +}
84 +
85 +bool EinaSharedString::operator==(const char* str) const {
86 +  return (!str || !string_) ? (str == string_) : !strcmp(string_, str);
87 +}
88 +
89 +EinaSharedString EinaSharedString::Adopt(Eina_Stringshare* string) {
90 +  EinaSharedString sharedString;
91 +  sharedString.string_ = static_cast<const char*>(string);
92 +  return sharedString;
93 +}
94 +
95 +Eina_Stringshare* EinaSharedString::LeakString() {
96 +  Eina_Stringshare* sharedString = string_;
97 +  string_ = 0;
98 +
99 +  return sharedString;
100 +}
101 diff --git a/base/strings/efl/eina_shared_string.h b/base/strings/efl/eina_shared_string.h
102 new file mode 100644
103 index 0000000..6fc0aee
104 --- /dev/null
105 +++ b/base/strings/efl/eina_shared_string.h
106 @@ -0,0 +1,52 @@
107 +// Copyright (c) 2013 Intel Corporation. All rights reserved.
108 +// Use of this source code is governed by a BSD-style license that can be
109 +// found in the LICENSE file.
110 +
111 +#ifndef EINA_SHARED_STRING_H_
112 +#define EINA_SHARED_STRING_H_
113 +
114 +#include "base/base_export.h"
115 +
116 +#include <Eina.h>
117 +
118 +#include <string>
119 +
120 +namespace base {
121 +
122 +class BASE_EXPORT EinaSharedString {
123 +public:
124 +  EinaSharedString() : string_(0) { }
125 +  EinaSharedString(const EinaSharedString& other);
126 +  EinaSharedString(const char* str);
127 +  explicit EinaSharedString(const std::string&);
128 +
129 +  ~EinaSharedString();
130 +
131 +  Eina_Stringshare* LeakString();
132 +
133 +  EinaSharedString& operator=(const EinaSharedString& other);
134 +  EinaSharedString& operator=(const char* str);
135 +
136 +  bool operator==(const EinaSharedString& other) const { return string_ == other.string_; }
137 +  bool operator!=(const EinaSharedString& other) const { return !(*this == other); }
138 +
139 +  bool operator==(const char* str) const;
140 +  bool operator!=(const char* str) const { return !(*this == str); }
141 +
142 +  operator const char* () const { return string_; }
143 +
144 +  bool IsNull() const { return !string_; }
145 +
146 +  size_t Length() const { return string_ ? static_cast<size_t>(eina_stringshare_strlen(string_)) : 0; }
147 +
148 +  static EinaSharedString Adopt(Eina_Stringshare*);
149 +
150 +private:
151 +  const char* string_;
152 +};
153 +
154 +} // namespace base
155 +
156 +using base::EinaSharedString;
157 +
158 +#endif // EINA_SHARED_STRING_H_
159 diff --git a/efl_webview/efl_webview.gyp b/efl_webview/efl_webview.gyp
160 index f49badd..c71eb7b 100644
161 --- a/efl_webview/efl_webview.gyp
162 +++ b/efl_webview/efl_webview.gyp
163 @@ -69,6 +69,10 @@
164          'lib/web_runtime_context.h',
165          'lib/webview.cc',
166          'lib/webview.h',
167 +        'public/xwalk_view.cc',
168 +        'public/xwalk_view.h',
169 +        'public/xwalk_main.cc',
170 +        'public/xwalk_main.h',
171        ],
172        'conditions': [
173          ['OS=="linux"', {
174 diff --git a/efl_webview/examples/main.cc b/efl_webview/examples/main.cc
175 index a41c986..db3a6dc 100644
176 --- a/efl_webview/examples/main.cc
177 +++ b/efl_webview/examples/main.cc
178 @@ -8,6 +8,8 @@
179  
180  #include "efl_webview/lib/process_main.h"
181  #include "efl_webview/lib/webview.h"
182 +#include "efl_webview/public/xwalk_main.h"
183 +#include "efl_webview/public/xwalk_view.h"
184  
185  static const char APP_NAME[] = "EFL WebView Example";
186  
187 @@ -17,26 +19,23 @@ static int window_height = 600;
188  static void
189  on_back_button_clicked(void *user_data, Evas_Object *back_button, void *event_info)
190  {
191 -  xwalk::WebView* webview = static_cast<xwalk::WebView*>(user_data);
192 -  webview->Back();
193 +  xwalk_view_back((Evas_Object*)user_data);
194  }
195  
196  static void
197  on_forward_button_clicked(void *user_data, Evas_Object *forward_button, void *event_info)
198  {
199 -  xwalk::WebView* webview = static_cast<xwalk::WebView*>(user_data);
200 -  webview->Forward();
201 +  xwalk_view_forward((Evas_Object*)user_data);
202  }
203  
204  static void
205  on_reload_button_clicked(void *user_data,
206                           Evas_Object *forward_button, void *event_info)
207  {
208 -  xwalk::WebView* webview = static_cast<xwalk::WebView*>(user_data);
209 -  webview->Reload();
210 +  xwalk_view_reload((Evas_Object*)user_data);
211  }
212  
213 -static void window_create()
214 +static void window_create(int argc)
215  {
216    /* Create window */
217    Evas_Object* elm_window = elm_win_util_standard_add("efl-webview-window", APP_NAME);
218 @@ -84,36 +83,38 @@ static void window_create()
219    evas_object_show(reload_button);
220  
221    /* Create WebView */
222 -  xwalk::WebView* webview_object = xwalk::WebView::Create(elm_window);
223 -  Evas_Object* webview = webview_object->EvasObject();
224 -  evas_object_size_hint_weight_set(webview, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
225 -  evas_object_size_hint_align_set(webview, EVAS_HINT_FILL, EVAS_HINT_FILL);
226 -  elm_box_pack_end(vertical_layout, webview);
227 -  elm_object_focus_set(webview, EINA_TRUE);
228 -  evas_object_show(webview);
229 +  Evas_Object* web_view = xwalk_view_add(elm_window);
230 +  evas_object_size_hint_weight_set(web_view, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
231 +  evas_object_size_hint_align_set(web_view, EVAS_HINT_FILL, EVAS_HINT_FILL);
232 +  elm_box_pack_end(vertical_layout, web_view);
233 +  elm_object_focus_set(web_view, EINA_TRUE);
234 +  evas_object_show(web_view);
235  
236    evas_object_smart_callback_add(back_button, "clicked",
237 -                                 on_back_button_clicked, webview_object);
238 +                                 on_back_button_clicked, web_view);
239    evas_object_smart_callback_add(forward_button, "clicked",
240 -                                 on_forward_button_clicked, webview_object);
241 +                                 on_forward_button_clicked, web_view);
242    evas_object_smart_callback_add(reload_button, "clicked",
243 -                                 on_reload_button_clicked, webview_object);
244 +                                 on_reload_button_clicked, web_view);
245  
246    evas_object_resize(elm_window, window_width, window_height);
247    evas_object_show(elm_window);
248 +
249 +  if (argc <= 1)
250 +    xwalk_view_url_set(web_view, "http://google.com");
251  }
252  
253  int main(int argc, char *argv[])
254  {
255    // FIXME: Handle chrome command line and url.
256    // It is needed only in development stage.
257 -  xwalk::WebView::CommandLineInit(argc, argv);
258 +  xwalk_init(argc, argv);
259  
260    elm_init(argc, argv);
261  
262    elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
263  
264 -  window_create();
265 +  window_create(argc);
266  
267    elm_run();
268    elm_shutdown();
269 diff --git a/efl_webview/lib/webview.cc b/efl_webview/lib/webview.cc
270 index 061d30c..61ecb96 100644
271 --- a/efl_webview/lib/webview.cc
272 +++ b/efl_webview/lib/webview.cc
273 @@ -5,6 +5,9 @@
274  #include "efl_webview/lib/webview.h"
275  
276  #include <Elementary.h>
277 +
278 +#include <map>
279 +
280  #include "base/command_line.h"
281  #include "base/file_util.h"
282  #include "base/files/file_path.h"
283 @@ -15,6 +18,18 @@
284  #include "efl_webview/lib/web_runtime_context.h"
285  #include "net/base/net_util.h"
286  
287 +namespace {
288 +
289 +typedef std::map<const Evas_Object*, xwalk::WebView*> EvasWebViewMap;
290 +
291 +inline EvasWebViewMap& EvasObjectToWebViewMap() {
292 +// FIXME: Temporary solution until web view has its own smart class.
293 +  static EvasWebViewMap map;
294 +  return map;
295 +}
296 +
297 +}  // namespace
298 +
299  namespace xwalk {
300  
301  struct WebView::Private {
302 @@ -58,6 +73,10 @@ WebView::WebView(Evas_Object* root_window)
303  
304    private_->root_window = root_window;
305    private_->view_box = elm_box_add(private_->root_window);
306 +  // FIXME: In future this has to be a separate Smart Class representing web view.
307 +  // 'this' should be set as smart_data->priv
308 +  EvasObjectToWebViewMap()[private_->view_box] = this;
309 +
310    elm_object_focus_allow_set(private_->view_box, EINA_TRUE);
311  
312    private_->context = WebRuntimeContext::current();
313 @@ -78,14 +97,24 @@ WebView::WebView(Evas_Object* root_window)
314  }
315  
316  WebView::~WebView() {
317 +// FIXME : And by the way who will invoke it?
318 +  EvasObjectToWebViewMap().erase(private_->view_box);
319    evas_object_del(private_->view_box);
320  }
321  
322 -void WebView::Forward() {
323 +bool WebView::CanGoBack() const {
324 +  return private_->webContentsDelegate->WebContents()->GetController().CanGoBack();
325 +}
326 +
327 +bool WebView::CanGoForward() const {
328 +  return private_->webContentsDelegate->WebContents()->GetController().CanGoForward();
329 +}
330 +
331 +void WebView::GoForward() {
332    private_->webContentsDelegate->WebContents()->GetController().GoForward();
333  }
334  
335 -void WebView::Back() {
336 +void WebView::GoBack() {
337    private_->webContentsDelegate->WebContents()->GetController().GoBack();
338  }
339  
340 @@ -94,6 +123,7 @@ void WebView::Reload() {
341  }
342  
343  void WebView::LoadURL(const GURL& url) {
344 +  url_ = EinaSharedString(url.spec());
345    content::NavigationController::LoadURLParams params(url);
346    params.transition_type = content::PageTransitionFromInt(
347        content::PAGE_TRANSITION_TYPED |
348 @@ -107,4 +137,11 @@ Evas_Object* WebView::EvasObject() {
349    return private_->view_box;
350  }
351  
352 +WebView* ToWebView(const Evas_Object* evas_object) {
353 +  EvasWebViewMap::iterator found = EvasObjectToWebViewMap().find(evas_object);
354 +  if (found != EvasObjectToWebViewMap().end())
355 +    return found->second;
356 +  return 0;
357 +}
358 +
359  }  // namespace xwalk
360 diff --git a/efl_webview/lib/webview.h b/efl_webview/lib/webview.h
361 index ca1757e..ae4f98f 100644
362 --- a/efl_webview/lib/webview.h
363 +++ b/efl_webview/lib/webview.h
364 @@ -2,13 +2,14 @@
365  // Use of this source code is governed by a BSD-style license that can be
366  // found in the LICENSE file.
367  
368 -#ifndef EFL_WEBVIEW_LIBWINDOW_H_
369 -#define EFL_WEBVIEW_LIBWINDOW_H_
370 +#ifndef EFL_WEBVIEW_LIB_WEBVIEW_H_
371 +#define EFL_WEBVIEW_LIB_WEBVIEW_H_
372  
373  #include <Evas.h>
374  
375  #include "base/basictypes.h"
376  #include "base/memory/scoped_ptr.h"
377 +#include "base/strings/efl/eina_shared_string.h"
378  #include "googleurl/src/gurl.h"
379  
380  namespace xwalk {
381 @@ -22,22 +23,27 @@ class EAPI WebView {
382  
383    EAPI Evas_Object* EvasObject();
384  
385 -  EAPI void Forward();
386 -  EAPI void Back();
387 -  EAPI void Reload();
388 -  EAPI void LoadURL(const GURL&);
389 +  bool CanGoBack() const;
390 +  bool CanGoForward() const;
391 +  void GoForward();
392 +  void GoBack();
393 +  void Reload();
394 +  void LoadURL(const GURL&);
395 +
396 +  const char* url() const { return url_; }
397  
398   private:
399 -  explicit WebView(Evas_Object*);
400 +  explicit WebView(Evas_Object* root_window);
401  
402    struct Private;
403    scoped_ptr<Private> private_;
404 +  EinaSharedString url_;
405  
406    DISALLOW_COPY_AND_ASSIGN(WebView);
407  };
408  
409 -WebView* ToWebView(Evas_Object*);
410 +WebView* ToWebView(const Evas_Object*);
411  
412  }  // namespace xwalk
413  
414 -#endif  // EFL_WEBVIEW_LIBWINDOW_H_
415 +#endif  // EFL_WEBVIEW_LIB_WEBVIEW_H_
416 diff --git a/efl_webview/public/xwalk_main.cc b/efl_webview/public/xwalk_main.cc
417 new file mode 100644
418 index 0000000..218c55d
419 --- /dev/null
420 +++ b/efl_webview/public/xwalk_main.cc
421 @@ -0,0 +1,10 @@
422 +// Copyright (c) 2013 Intel Corporation. All rights reserved.
423 +// Use of this source code is governed by a BSD-style license that can be
424 +// found in the LICENSE file.
425 +
426 +#include "efl_webview/public/xwalk_main.h"
427 +#include "efl_webview/lib/webview.h"
428 +
429 +void xwalk_init(int argc, char *argv[]) {
430 +  xwalk::WebView::CommandLineInit(argc, argv);
431 +}
432 diff --git a/efl_webview/public/xwalk_main.h b/efl_webview/public/xwalk_main.h
433 new file mode 100644
434 index 0000000..b6c2f83
435 --- /dev/null
436 +++ b/efl_webview/public/xwalk_main.h
437 @@ -0,0 +1,19 @@
438 +// Copyright (c) 2013 Intel Corporation. All rights reserved.
439 +// Use of this source code is governed by a BSD-style license that can be
440 +// found in the LICENSE file.
441 +
442 +#ifndef EFL_WEBVIEW_PUBLIC_XWALK_MAIN_H_
443 +#define EFL_WEBVIEW_PUBLIC_XWALK_MAIN_H_
444 +
445 +#include <Evas.h>
446 +
447 +#ifdef __cplusplus
448 +extern "C" {
449 +#endif
450 +
451 +EAPI void xwalk_init(int argc, char *argv[]);
452 +
453 +#ifdef __cplusplus
454 +}
455 +#endif
456 +#endif  // EFL_WEBVIEW_PUBLIC_XWALK_MAIN_H_
457 diff --git a/efl_webview/public/xwalk_view.cc b/efl_webview/public/xwalk_view.cc
458 new file mode 100644
459 index 0000000..b000143
460 --- /dev/null
461 +++ b/efl_webview/public/xwalk_view.cc
462 @@ -0,0 +1,60 @@
463 +// Copyright (c) 2013 Intel Corporation. All rights reserved.
464 +// Use of this source code is governed by a BSD-style license that can be
465 +// found in the LICENSE file.
466 +
467 +#include "efl_webview/public/xwalk_view.h"
468 +#include "efl_webview/lib/webview.h"
469 +
470 +using namespace xwalk;
471 +
472 +#define XWALK_VIEW_GET_IMPL_OR_RETURN(xwalk_view, impl, ...)         \
473 +  WebView* impl = ToWebView(xwalk_view);                             \
474 +  do {                                                               \
475 +    if (!impl) {                                                     \
476 +      EINA_LOG_CRIT("no private data for object %p", xwalk_view);    \
477 +      return __VA_ARGS__;                                            \
478 +    }                                                                \
479 +  } while (0)
480 +
481 +
482 +Evas_Object* xwalk_view_add(Evas_Object* root_window) {
483 +  return WebView::Create(root_window)->EvasObject();
484 +}
485 +
486 +Eina_Bool xwalk_view_url_set(Evas_Object* evas_object, const char* url) {
487 +  XWALK_VIEW_GET_IMPL_OR_RETURN(evas_object, impl, false);
488 +  EINA_SAFETY_ON_NULL_RETURN_VAL(url, false);
489 +
490 +  impl->LoadURL(GURL(url));
491 +  return true;
492 +}
493 +
494 +const char* xwalk_view_url_get(const Evas_Object* evas_object) {
495 +  XWALK_VIEW_GET_IMPL_OR_RETURN(evas_object, impl, "");
496 +  return impl->url();
497 +}
498 +
499 +Eina_Bool xwalk_view_reload(Evas_Object* evas_object) {
500 +  XWALK_VIEW_GET_IMPL_OR_RETURN(evas_object, impl, false);
501 +  impl->Reload();
502 +
503 +  return true;
504 +}
505 +
506 +Eina_Bool xwalk_view_back(Evas_Object* evas_object) {
507 +  XWALK_VIEW_GET_IMPL_OR_RETURN(evas_object, impl, false);
508 +  if (impl->CanGoBack()) {
509 +    impl->GoBack();
510 +    return true;
511 +  }
512 +  return false;
513 +}
514 +
515 +Eina_Bool xwalk_view_forward(Evas_Object* evas_object) {
516 +  XWALK_VIEW_GET_IMPL_OR_RETURN(evas_object, impl, false);
517 +  if (impl->CanGoForward()) {
518 +    impl->GoForward();
519 +    return true;
520 +  }
521 +  return false;
522 +}
523 diff --git a/efl_webview/public/xwalk_view.h b/efl_webview/public/xwalk_view.h
524 new file mode 100644
525 index 0000000..a4c85d58
526 --- /dev/null
527 +++ b/efl_webview/public/xwalk_view.h
528 @@ -0,0 +1,29 @@
529 +// Copyright (c) 2013 Intel Corporation. All rights reserved.
530 +// Use of this source code is governed by a BSD-style license that can be
531 +// found in the LICENSE file.
532 +
533 +#ifndef EFL_WEBVIEW_PUBLIC_XWALK_VIEW_H_
534 +#define EFL_WEBVIEW_PUBLIC_XWALK_VIEW_H_
535 +
536 +#include <Evas.h>
537 +
538 +#ifdef __cplusplus
539 +extern "C" {
540 +#endif
541 +
542 +EAPI Evas_Object *xwalk_view_add(Evas_Object *root_window);
543 +
544 +EAPI Eina_Bool xwalk_view_url_set(Evas_Object *obj, const char *url);
545 +
546 +EAPI const char *xwalk_view_url_get(const Evas_Object *obj);
547 +
548 +EAPI Eina_Bool xwalk_view_reload(Evas_Object *obj);
549 +
550 +EAPI Eina_Bool xwalk_view_back(Evas_Object *obj);
551 +
552 +EAPI Eina_Bool xwalk_view_forward(Evas_Object *obj);
553 +
554 +#ifdef __cplusplus
555 +}
556 +#endif
557 +#endif  // EFL_WEBVIEW_PUBLIC_XWALK_VIEW_H_
558 -- 
559 1.8.1.2
560