Remove all npapi plugins from windows metro chrome
[chromium-blink-merge.git] / base / message_loop.h
blobef8d59f4071eac1d3785dba417f8b346503515d7
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_MESSAGE_LOOP_H_
6 #define BASE_MESSAGE_LOOP_H_
7 #pragma once
9 #include <queue>
10 #include <string>
12 #include "base/base_export.h"
13 #include "base/basictypes.h"
14 #include "base/callback_forward.h"
15 #include "base/location.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/message_loop_proxy.h"
18 #include "base/message_pump.h"
19 #include "base/observer_list.h"
20 #include "base/pending_task.h"
21 #include "base/sequenced_task_runner_helpers.h"
22 #include "base/synchronization/lock.h"
23 #include "base/tracking_info.h"
24 #include "base/time.h"
26 #if defined(OS_WIN)
27 // We need this to declare base::MessagePumpWin::Dispatcher, which we should
28 // really just eliminate.
29 #include "base/message_pump_win.h"
30 #elif defined(OS_POSIX)
31 #include "base/message_pump_libevent.h"
32 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
34 #if defined(USE_AURA)
35 #include "base/message_pump_x.h"
36 #else
37 #include "base/message_pump_gtk.h"
38 #endif
40 #endif
41 #endif
43 namespace base {
44 class Histogram;
45 class ThreadTaskRunnerHandle;
46 } // namespace base
48 // A MessageLoop is used to process events for a particular thread. There is
49 // at most one MessageLoop instance per thread.
51 // Events include at a minimum Task instances submitted to PostTask or those
52 // managed by TimerManager. Depending on the type of message pump used by the
53 // MessageLoop other events such as UI messages may be processed. On Windows
54 // APC calls (as time permits) and signals sent to a registered set of HANDLEs
55 // may also be processed.
57 // NOTE: Unless otherwise specified, a MessageLoop's methods may only be called
58 // on the thread where the MessageLoop's Run method executes.
60 // NOTE: MessageLoop has task reentrancy protection. This means that if a
61 // task is being processed, a second task cannot start until the first task is
62 // finished. Reentrancy can happen when processing a task, and an inner
63 // message pump is created. That inner pump then processes native messages
64 // which could implicitly start an inner task. Inner message pumps are created
65 // with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions
66 // (DoDragDrop), printer functions (StartDoc) and *many* others.
68 // Sample workaround when inner task processing is needed:
69 // HRESULT hr;
70 // {
71 // MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
72 // hr = DoDragDrop(...); // Implicitly runs a modal message loop.
73 // }
74 // // Process |hr| (the result returned by DoDragDrop()).
76 // Please be SURE your task is reentrant (nestable) and all global variables
77 // are stable and accessible before calling SetNestableTasksAllowed(true).
79 class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate {
80 public:
82 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
83 typedef base::MessagePumpDispatcher Dispatcher;
84 typedef base::MessagePumpObserver Observer;
85 #endif
87 // A MessageLoop has a particular type, which indicates the set of
88 // asynchronous events it may process in addition to tasks and timers.
90 // TYPE_DEFAULT
91 // This type of ML only supports tasks and timers.
93 // TYPE_UI
94 // This type of ML also supports native UI events (e.g., Windows messages).
95 // See also MessageLoopForUI.
97 // TYPE_IO
98 // This type of ML also supports asynchronous IO. See also
99 // MessageLoopForIO.
101 enum Type {
102 TYPE_DEFAULT,
103 TYPE_UI,
104 TYPE_IO
107 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it
108 // is typical to make use of the current thread's MessageLoop instance.
109 explicit MessageLoop(Type type = TYPE_DEFAULT);
110 virtual ~MessageLoop();
112 // Returns the MessageLoop object for the current thread, or null if none.
113 static MessageLoop* current();
115 static void EnableHistogrammer(bool enable_histogrammer);
117 typedef base::MessagePump* (MessagePumpFactory)();
118 // Using the given base::MessagePumpForUIFactory to override the default
119 // MessagePump implementation for 'TYPE_UI'.
120 static void InitMessagePumpForUIFactory(MessagePumpFactory* factory);
122 // A DestructionObserver is notified when the current MessageLoop is being
123 // destroyed. These observers are notified prior to MessageLoop::current()
124 // being changed to return NULL. This gives interested parties the chance to
125 // do final cleanup that depends on the MessageLoop.
127 // NOTE: Any tasks posted to the MessageLoop during this notification will
128 // not be run. Instead, they will be deleted.
130 class BASE_EXPORT DestructionObserver {
131 public:
132 virtual void WillDestroyCurrentMessageLoop() = 0;
134 protected:
135 virtual ~DestructionObserver();
138 // Add a DestructionObserver, which will start receiving notifications
139 // immediately.
140 void AddDestructionObserver(DestructionObserver* destruction_observer);
142 // Remove a DestructionObserver. It is safe to call this method while a
143 // DestructionObserver is receiving a notification callback.
144 void RemoveDestructionObserver(DestructionObserver* destruction_observer);
146 // The "PostTask" family of methods call the task's Run method asynchronously
147 // from within a message loop at some point in the future.
149 // With the PostTask variant, tasks are invoked in FIFO order, inter-mixed
150 // with normal UI or IO event processing. With the PostDelayedTask variant,
151 // tasks are called after at least approximately 'delay_ms' have elapsed.
153 // The NonNestable variants work similarly except that they promise never to
154 // dispatch the task from a nested invocation of MessageLoop::Run. Instead,
155 // such tasks get deferred until the top-most MessageLoop::Run is executing.
157 // The MessageLoop takes ownership of the Task, and deletes it after it has
158 // been Run().
160 // NOTE: These methods may be called on any thread. The Task will be invoked
161 // on the thread that executes MessageLoop::Run().
162 void PostTask(
163 const tracked_objects::Location& from_here,
164 const base::Closure& task);
166 void PostDelayedTask(
167 const tracked_objects::Location& from_here,
168 const base::Closure& task, int64 delay_ms);
170 void PostDelayedTask(
171 const tracked_objects::Location& from_here,
172 const base::Closure& task,
173 base::TimeDelta delay);
175 void PostNonNestableTask(
176 const tracked_objects::Location& from_here,
177 const base::Closure& task);
179 void PostNonNestableDelayedTask(
180 const tracked_objects::Location& from_here,
181 const base::Closure& task, int64 delay_ms);
183 void PostNonNestableDelayedTask(
184 const tracked_objects::Location& from_here,
185 const base::Closure& task,
186 base::TimeDelta delay);
188 // A variant on PostTask that deletes the given object. This is useful
189 // if the object needs to live until the next run of the MessageLoop (for
190 // example, deleting a RenderProcessHost from within an IPC callback is not
191 // good).
193 // NOTE: This method may be called on any thread. The object will be deleted
194 // on the thread that executes MessageLoop::Run(). If this is not the same
195 // as the thread that calls PostDelayedTask(FROM_HERE, ), then T MUST inherit
196 // from RefCountedThreadSafe<T>!
197 template <class T>
198 void DeleteSoon(const tracked_objects::Location& from_here, const T* object) {
199 base::subtle::DeleteHelperInternal<T, void>::DeleteViaSequencedTaskRunner(
200 this, from_here, object);
203 // A variant on PostTask that releases the given reference counted object
204 // (by calling its Release method). This is useful if the object needs to
205 // live until the next run of the MessageLoop, or if the object needs to be
206 // released on a particular thread.
208 // NOTE: This method may be called on any thread. The object will be
209 // released (and thus possibly deleted) on the thread that executes
210 // MessageLoop::Run(). If this is not the same as the thread that calls
211 // PostDelayedTask(FROM_HERE, ), then T MUST inherit from
212 // RefCountedThreadSafe<T>!
213 template <class T>
214 void ReleaseSoon(const tracked_objects::Location& from_here,
215 const T* object) {
216 base::subtle::ReleaseHelperInternal<T, void>::ReleaseViaSequencedTaskRunner(
217 this, from_here, object);
220 // Run the message loop.
221 void Run();
223 // Process all pending tasks, windows messages, etc., but don't wait/sleep.
224 // Return as soon as all items that can be run are taken care of.
225 void RunAllPending();
227 // Signals the Run method to return after it is done processing all pending
228 // messages. This method may only be called on the same thread that called
229 // Run, and Run must still be on the call stack.
231 // Use QuitClosure if you need to Quit another thread's MessageLoop, but note
232 // that doing so is fairly dangerous if the target thread makes nested calls
233 // to MessageLoop::Run. The problem being that you won't know which nested
234 // run loop you are quitting, so be careful!
235 void Quit();
237 // This method is a variant of Quit, that does not wait for pending messages
238 // to be processed before returning from Run.
239 void QuitNow();
241 // Invokes Quit on the current MessageLoop when run. Useful to schedule an
242 // arbitrary MessageLoop to Quit.
243 static base::Closure QuitClosure();
245 // Returns the type passed to the constructor.
246 Type type() const { return type_; }
248 // Optional call to connect the thread name with this loop.
249 void set_thread_name(const std::string& thread_name) {
250 DCHECK(thread_name_.empty()) << "Should not rename this thread!";
251 thread_name_ = thread_name;
253 const std::string& thread_name() const { return thread_name_; }
255 // Gets the message loop proxy associated with this message loop.
256 scoped_refptr<base::MessageLoopProxy> message_loop_proxy() {
257 return message_loop_proxy_.get();
260 // Enables or disables the recursive task processing. This happens in the case
261 // of recursive message loops. Some unwanted message loop may occurs when
262 // using common controls or printer functions. By default, recursive task
263 // processing is disabled.
265 // Please utilize |ScopedNestableTaskAllower| instead of calling these methods
266 // directly. In general nestable message loops are to be avoided. They are
267 // dangerous and difficult to get right, so please use with extreme caution.
269 // The specific case where tasks get queued is:
270 // - The thread is running a message loop.
271 // - It receives a task #1 and execute it.
272 // - The task #1 implicitly start a message loop, like a MessageBox in the
273 // unit test. This can also be StartDoc or GetSaveFileName.
274 // - The thread receives a task #2 before or while in this second message
275 // loop.
276 // - With NestableTasksAllowed set to true, the task #2 will run right away.
277 // Otherwise, it will get executed right after task #1 completes at "thread
278 // message loop level".
279 void SetNestableTasksAllowed(bool allowed);
280 bool NestableTasksAllowed() const;
282 // Enables nestable tasks on |loop| while in scope.
283 class ScopedNestableTaskAllower {
284 public:
285 explicit ScopedNestableTaskAllower(MessageLoop* loop)
286 : loop_(loop),
287 old_state_(loop_->NestableTasksAllowed()) {
288 loop_->SetNestableTasksAllowed(true);
290 ~ScopedNestableTaskAllower() {
291 loop_->SetNestableTasksAllowed(old_state_);
294 private:
295 MessageLoop* loop_;
296 bool old_state_;
299 // Enables or disables the restoration during an exception of the unhandled
300 // exception filter that was active when Run() was called. This can happen
301 // if some third party code call SetUnhandledExceptionFilter() and never
302 // restores the previous filter.
303 void set_exception_restoration(bool restore) {
304 exception_restoration_ = restore;
307 // Returns true if we are currently running a nested message loop.
308 bool IsNested();
310 // A TaskObserver is an object that receives task notifications from the
311 // MessageLoop.
313 // NOTE: A TaskObserver implementation should be extremely fast!
314 class BASE_EXPORT TaskObserver {
315 public:
316 TaskObserver();
318 // This method is called before processing a task.
319 virtual void WillProcessTask(base::TimeTicks time_posted) = 0;
321 // This method is called after processing a task.
322 virtual void DidProcessTask(base::TimeTicks time_posted) = 0;
324 protected:
325 virtual ~TaskObserver();
328 // These functions can only be called on the same thread that |this| is
329 // running on.
330 void AddTaskObserver(TaskObserver* task_observer);
331 void RemoveTaskObserver(TaskObserver* task_observer);
333 // Returns true if the message loop has high resolution timers enabled.
334 // Provided for testing.
335 bool high_resolution_timers_enabled() {
336 #if defined(OS_WIN)
337 return !high_resolution_timer_expiration_.is_null();
338 #else
339 return true;
340 #endif
343 // When we go into high resolution timer mode, we will stay in hi-res mode
344 // for at least 1s.
345 static const int kHighResolutionTimerModeLeaseTimeMs = 1000;
347 // Asserts that the MessageLoop is "idle".
348 void AssertIdle() const;
350 #if defined(OS_WIN)
351 void set_os_modal_loop(bool os_modal_loop) {
352 os_modal_loop_ = os_modal_loop;
355 bool os_modal_loop() const {
356 return os_modal_loop_;
358 #endif // OS_WIN
360 // Can only be called from the thread that owns the MessageLoop.
361 bool is_running() const;
363 //----------------------------------------------------------------------------
364 protected:
365 struct RunState {
366 // Used to count how many Run() invocations are on the stack.
367 int run_depth;
369 // Used to record that Quit() was called, or that we should quit the pump
370 // once it becomes idle.
371 bool quit_received;
373 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
374 Dispatcher* dispatcher;
375 #endif
378 #if defined(OS_ANDROID)
379 // Android Java process manages the UI thread message loop. So its
380 // MessagePumpForUI needs to keep the RunState.
381 public:
382 #endif
383 class BASE_EXPORT AutoRunState : RunState {
384 public:
385 explicit AutoRunState(MessageLoop* loop);
386 ~AutoRunState();
387 private:
388 MessageLoop* loop_;
389 RunState* previous_state_;
391 #if defined(OS_ANDROID)
392 protected:
393 #endif
395 #if defined(OS_WIN)
396 base::MessagePumpWin* pump_win() {
397 return static_cast<base::MessagePumpWin*>(pump_.get());
399 #elif defined(OS_POSIX)
400 base::MessagePumpLibevent* pump_libevent() {
401 return static_cast<base::MessagePumpLibevent*>(pump_.get());
403 #endif
405 // A function to encapsulate all the exception handling capability in the
406 // stacks around the running of a main message loop. It will run the message
407 // loop in a SEH try block or not depending on the set_SEH_restoration()
408 // flag invoking respectively RunInternalInSEHFrame() or RunInternal().
409 void RunHandler();
411 #if defined(OS_WIN)
412 __declspec(noinline) void RunInternalInSEHFrame();
413 #endif
415 // A surrounding stack frame around the running of the message loop that
416 // supports all saving and restoring of state, as is needed for any/all (ugly)
417 // recursive calls.
418 void RunInternal();
420 // Called to process any delayed non-nestable tasks.
421 bool ProcessNextDelayedNonNestableTask();
423 // Runs the specified PendingTask.
424 void RunTask(const base::PendingTask& pending_task);
426 // Calls RunTask or queues the pending_task on the deferred task list if it
427 // cannot be run right now. Returns true if the task was run.
428 bool DeferOrRunPendingTask(const base::PendingTask& pending_task);
430 // Adds the pending task to delayed_work_queue_.
431 void AddToDelayedWorkQueue(const base::PendingTask& pending_task);
433 // Adds the pending task to our incoming_queue_.
435 // Caller retains ownership of |pending_task|, but this function will
436 // reset the value of pending_task->task. This is needed to ensure
437 // that the posting call stack does not retain pending_task->task
438 // beyond this function call.
439 void AddToIncomingQueue(base::PendingTask* pending_task);
441 // Load tasks from the incoming_queue_ into work_queue_ if the latter is
442 // empty. The former requires a lock to access, while the latter is directly
443 // accessible on this thread.
444 void ReloadWorkQueue();
446 // Delete tasks that haven't run yet without running them. Used in the
447 // destructor to make sure all the task's destructors get called. Returns
448 // true if some work was done.
449 bool DeletePendingTasks();
451 // Calculates the time at which a PendingTask should run.
452 base::TimeTicks CalculateDelayedRuntime(int64 delay_ms);
454 // Start recording histogram info about events and action IF it was enabled
455 // and IF the statistics recorder can accept a registration of our histogram.
456 void StartHistogrammer();
458 // Add occurrence of event to our histogram, so that we can see what is being
459 // done in a specific MessageLoop instance (i.e., specific thread).
460 // If message_histogram_ is NULL, this is a no-op.
461 void HistogramEvent(int event);
463 // base::MessagePump::Delegate methods:
464 virtual bool DoWork() OVERRIDE;
465 virtual bool DoDelayedWork(base::TimeTicks* next_delayed_work_time) OVERRIDE;
466 virtual bool DoIdleWork() OVERRIDE;
468 Type type_;
470 // A list of tasks that need to be processed by this instance. Note that
471 // this queue is only accessed (push/pop) by our current thread.
472 base::TaskQueue work_queue_;
474 // Contains delayed tasks, sorted by their 'delayed_run_time' property.
475 base::DelayedTaskQueue delayed_work_queue_;
477 // A recent snapshot of Time::Now(), used to check delayed_work_queue_.
478 base::TimeTicks recent_time_;
480 // A queue of non-nestable tasks that we had to defer because when it came
481 // time to execute them we were in a nested message loop. They will execute
482 // once we're out of nested message loops.
483 base::TaskQueue deferred_non_nestable_work_queue_;
485 scoped_refptr<base::MessagePump> pump_;
487 ObserverList<DestructionObserver> destruction_observers_;
489 // A recursion block that prevents accidentally running additional tasks when
490 // insider a (accidentally induced?) nested message pump.
491 bool nestable_tasks_allowed_;
493 bool exception_restoration_;
495 std::string thread_name_;
496 // A profiling histogram showing the counts of various messages and events.
497 base::Histogram* message_histogram_;
499 // A null terminated list which creates an incoming_queue of tasks that are
500 // acquired under a mutex for processing on this instance's thread. These
501 // tasks have not yet been sorted out into items for our work_queue_ vs items
502 // that will be handled by the TimerManager.
503 base::TaskQueue incoming_queue_;
504 // Protect access to incoming_queue_.
505 mutable base::Lock incoming_queue_lock_;
507 RunState* state_;
509 #if defined(OS_WIN)
510 base::TimeTicks high_resolution_timer_expiration_;
511 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc
512 // which enter a modal message loop.
513 bool os_modal_loop_;
514 #endif
516 // The next sequence number to use for delayed tasks.
517 int next_sequence_num_;
519 ObserverList<TaskObserver> task_observers_;
521 // The message loop proxy associated with this message loop, if one exists.
522 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
523 scoped_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_;
525 private:
526 template <class T, class R> friend class base::subtle::DeleteHelperInternal;
527 template <class T, class R> friend class base::subtle::ReleaseHelperInternal;
529 void DeleteSoonInternal(const tracked_objects::Location& from_here,
530 void(*deleter)(const void*),
531 const void* object);
532 void ReleaseSoonInternal(const tracked_objects::Location& from_here,
533 void(*releaser)(const void*),
534 const void* object);
537 DISALLOW_COPY_AND_ASSIGN(MessageLoop);
540 //-----------------------------------------------------------------------------
541 // MessageLoopForUI extends MessageLoop with methods that are particular to a
542 // MessageLoop instantiated with TYPE_UI.
544 // This class is typically used like so:
545 // MessageLoopForUI::current()->...call some method...
547 class BASE_EXPORT MessageLoopForUI : public MessageLoop {
548 public:
549 MessageLoopForUI() : MessageLoop(TYPE_UI) {
552 // Returns the MessageLoopForUI of the current thread.
553 static MessageLoopForUI* current() {
554 MessageLoop* loop = MessageLoop::current();
555 DCHECK(loop);
556 DCHECK_EQ(MessageLoop::TYPE_UI, loop->type());
557 return static_cast<MessageLoopForUI*>(loop);
560 #if defined(OS_WIN)
561 void DidProcessMessage(const MSG& message);
562 #endif // defined(OS_WIN)
564 #if defined(OS_ANDROID)
565 // On Android, the UI message loop is handled by Java side. So Run() should
566 // never be called. Instead use Start(), which will forward all the native UI
567 // events to the Java message loop.
568 void Start();
569 #elif !defined(OS_MACOSX)
570 // Please see message_pump_win/message_pump_glib for definitions of these
571 // methods.
572 void AddObserver(Observer* observer);
573 void RemoveObserver(Observer* observer);
574 void RunWithDispatcher(Dispatcher* dispatcher);
575 void RunAllPendingWithDispatcher(Dispatcher* dispatcher);
577 protected:
578 // TODO(rvargas): Make this platform independent.
579 base::MessagePumpForUI* pump_ui() {
580 return static_cast<base::MessagePumpForUI*>(pump_.get());
582 #endif // !defined(OS_MACOSX)
585 // Do not add any member variables to MessageLoopForUI! This is important b/c
586 // MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra
587 // data that you need should be stored on the MessageLoop's pump_ instance.
588 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI),
589 MessageLoopForUI_should_not_have_extra_member_variables);
591 //-----------------------------------------------------------------------------
592 // MessageLoopForIO extends MessageLoop with methods that are particular to a
593 // MessageLoop instantiated with TYPE_IO.
595 // This class is typically used like so:
596 // MessageLoopForIO::current()->...call some method...
598 class BASE_EXPORT MessageLoopForIO : public MessageLoop {
599 public:
600 #if defined(OS_WIN)
601 typedef base::MessagePumpForIO::IOHandler IOHandler;
602 typedef base::MessagePumpForIO::IOContext IOContext;
603 typedef base::MessagePumpForIO::IOObserver IOObserver;
604 #elif defined(OS_POSIX)
605 typedef base::MessagePumpLibevent::Watcher Watcher;
606 typedef base::MessagePumpLibevent::FileDescriptorWatcher
607 FileDescriptorWatcher;
608 typedef base::MessagePumpLibevent::IOObserver IOObserver;
610 enum Mode {
611 WATCH_READ = base::MessagePumpLibevent::WATCH_READ,
612 WATCH_WRITE = base::MessagePumpLibevent::WATCH_WRITE,
613 WATCH_READ_WRITE = base::MessagePumpLibevent::WATCH_READ_WRITE
616 #endif
618 MessageLoopForIO() : MessageLoop(TYPE_IO) {
621 // Returns the MessageLoopForIO of the current thread.
622 static MessageLoopForIO* current() {
623 MessageLoop* loop = MessageLoop::current();
624 DCHECK_EQ(MessageLoop::TYPE_IO, loop->type());
625 return static_cast<MessageLoopForIO*>(loop);
628 void AddIOObserver(IOObserver* io_observer) {
629 pump_io()->AddIOObserver(io_observer);
632 void RemoveIOObserver(IOObserver* io_observer) {
633 pump_io()->RemoveIOObserver(io_observer);
636 #if defined(OS_WIN)
637 // Please see MessagePumpWin for definitions of these methods.
638 void RegisterIOHandler(HANDLE file_handle, IOHandler* handler);
639 bool WaitForIOCompletion(DWORD timeout, IOHandler* filter);
641 protected:
642 // TODO(rvargas): Make this platform independent.
643 base::MessagePumpForIO* pump_io() {
644 return static_cast<base::MessagePumpForIO*>(pump_.get());
647 #elif defined(OS_POSIX)
648 // Please see MessagePumpLibevent for definition.
649 bool WatchFileDescriptor(int fd,
650 bool persistent,
651 Mode mode,
652 FileDescriptorWatcher* controller,
653 Watcher* delegate);
655 private:
656 base::MessagePumpLibevent* pump_io() {
657 return static_cast<base::MessagePumpLibevent*>(pump_.get());
659 #endif // defined(OS_POSIX)
662 // Do not add any member variables to MessageLoopForIO! This is important b/c
663 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra
664 // data that you need should be stored on the MessageLoop's pump_ instance.
665 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
666 MessageLoopForIO_should_not_have_extra_member_variables);
668 #endif // BASE_MESSAGE_LOOP_H_