• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2014 Marshall A. Greenblatt. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //    * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //    * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //    * Neither the name of Google Inc. nor the name Chromium Embedded
14 // Framework nor the names of its contributors may be used to endorse
15 // or promote products derived from this software without specific prior
16 // written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // ---------------------------------------------------------------------------
31 //
32 // The contents of this file are only available to applications that link
33 // against the libcef_dll_wrapper target.
34 //
35 
36 #ifndef CEF_INCLUDE_WRAPPER_CEF_HELPERS_H_
37 #define CEF_INCLUDE_WRAPPER_CEF_HELPERS_H_
38 #pragma once
39 
40 #include <cstring>
41 #include <string>
42 #include <vector>
43 
44 #include "include/base/cef_bind.h"
45 #include "include/base/cef_logging.h"
46 #include "include/base/cef_macros.h"
47 #include "include/cef_task.h"
48 
49 #define CEF_REQUIRE_UI_THREAD() DCHECK(CefCurrentlyOn(TID_UI));
50 #define CEF_REQUIRE_IO_THREAD() DCHECK(CefCurrentlyOn(TID_IO));
51 #define CEF_REQUIRE_FILE_THREAD() DCHECK(CefCurrentlyOn(TID_FILE));
52 #define CEF_REQUIRE_RENDERER_THREAD() DCHECK(CefCurrentlyOn(TID_RENDERER));
53 
54 // Use this struct in conjuction with refcounted types to ensure that an
55 // object is deleted on the specified thread. For example:
56 //
57 // class Foo : public base::RefCountedThreadSafe<Foo, CefDeleteOnUIThread> {
58 //  public:
59 //   Foo();
60 //   void DoSomething();
61 //
62 //  private:
63 //   // Allow deletion via scoped_refptr only.
64 //   friend struct CefDeleteOnThread<TID_UI>;
65 //   friend class base::RefCountedThreadSafe<Foo, CefDeleteOnUIThread>;
66 //
67 //   virtual ~Foo() {}
68 // };
69 //
70 // base::scoped_refptr<Foo> foo = new Foo();
71 // foo->DoSomething();
72 // foo = NULL;  // Deletion of |foo| will occur on the UI thread.
73 //
74 template <CefThreadId thread>
75 struct CefDeleteOnThread {
76   template <typename T>
DestructCefDeleteOnThread77   static void Destruct(const T* x) {
78     if (CefCurrentlyOn(thread)) {
79       delete x;
80     } else {
81       CefPostTask(thread,
82                   base::Bind(&CefDeleteOnThread<thread>::Destruct<T>, x));
83     }
84   }
85 };
86 
87 struct CefDeleteOnUIThread : public CefDeleteOnThread<TID_UI> {};
88 struct CefDeleteOnIOThread : public CefDeleteOnThread<TID_IO> {};
89 struct CefDeleteOnFileThread : public CefDeleteOnThread<TID_FILE> {};
90 struct CefDeleteOnRendererThread : public CefDeleteOnThread<TID_RENDERER> {};
91 
92 // Same as IMPLEMENT_REFCOUNTING() but using the specified Destructor.
93 #define IMPLEMENT_REFCOUNTING_EX(ClassName, Destructor)              \
94  public:                                                             \
95   void AddRef() const OVERRIDE { ref_count_.AddRef(); }              \
96   bool Release() const OVERRIDE {                                    \
97     if (ref_count_.Release()) {                                      \
98       Destructor::Destruct(this);                                    \
99       return true;                                                   \
100     }                                                                \
101     return false;                                                    \
102   }                                                                  \
103   bool HasOneRef() const OVERRIDE { return ref_count_.HasOneRef(); } \
104   bool HasAtLeastOneRef() const OVERRIDE {                           \
105     return ref_count_.HasAtLeastOneRef();                            \
106   }                                                                  \
107                                                                      \
108  private:                                                            \
109   CefRefCount ref_count_
110 
111 #define IMPLEMENT_REFCOUNTING_DELETE_ON_UIT(ClassName) \
112   IMPLEMENT_REFCOUNTING_EX(ClassName, CefDeleteOnUIThread)
113 
114 #define IMPLEMENT_REFCOUNTING_DELETE_ON_IOT(ClassName) \
115   IMPLEMENT_REFCOUNTING_EX(ClassName, CefDeleteOnIOThread)
116 
117 
118 ///
119 // Helper class to manage a scoped copy of |argv|.
120 ///
121 class CefScopedArgArray {
122  public:
CefScopedArgArray(int argc,char * argv[])123   CefScopedArgArray(int argc, char* argv[]) {
124     // argv should have (argc + 1) elements, the last one always being NULL.
125     array_ = new char*[argc + 1];
126     values_.resize(argc);
127     for (int i = 0; i < argc; ++i) {
128       values_[i] = argv[i];
129       array_[i] = const_cast<char*>(values_[i].c_str());
130     }
131     array_[argc] = NULL;
132   }
~CefScopedArgArray()133   ~CefScopedArgArray() { delete[] array_; }
134 
array()135   char** array() const { return array_; }
136 
137  private:
138   char** array_;
139 
140   // Keep values in a vector separate from |array_| because various users may
141   // modify |array_| and we still want to clean up memory properly.
142   std::vector<std::string> values_;
143 
144   DISALLOW_COPY_AND_ASSIGN(CefScopedArgArray);
145 };
146 
147 #endif  // CEF_INCLUDE_WRAPPER_CEF_HELPERS_H_
148