OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/plugin/host_script_object.h" | 5 #include "remoting/host/plugin/host_script_object.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 | 71 |
72 } // namespace | 72 } // namespace |
73 | 73 |
74 // This flag blocks LOGs to the UI if we're already in the middle of logging | 74 // This flag blocks LOGs to the UI if we're already in the middle of logging |
75 // to the UI. This prevents a potential infinite loop if we encounter an error | 75 // to the UI. This prevents a potential infinite loop if we encounter an error |
76 // while sending the log message to the UI. | 76 // while sending the log message to the UI. |
77 static bool g_logging_to_plugin = false; | 77 static bool g_logging_to_plugin = false; |
78 static HostNPScriptObject* g_logging_scriptable_object = NULL; | 78 static HostNPScriptObject* g_logging_scriptable_object = NULL; |
79 static logging::LogMessageHandlerFunction g_logging_old_handler = NULL; | 79 static logging::LogMessageHandlerFunction g_logging_old_handler = NULL; |
80 | 80 |
81 HostNPScriptObject::HostNPScriptObject( | 81 HostNPScriptObject::HostNPScriptObject(NPP plugin, NPObject* parent) |
82 NPP plugin, | |
83 NPObject* parent, | |
84 PluginMessageLoopProxy::Delegate* plugin_thread_delegate) | |
85 : plugin_(plugin), | 82 : plugin_(plugin), |
86 parent_(parent), | 83 parent_(parent), |
87 state_(kDisconnected), | 84 state_(kDisconnected), |
88 np_thread_id_(base::PlatformThread::CurrentId()), | 85 np_thread_id_(base::PlatformThread::CurrentId()), |
89 plugin_message_loop_proxy_( | |
90 new PluginMessageLoopProxy(plugin_thread_delegate)), | |
91 host_context_(plugin_message_loop_proxy_), | |
92 failed_login_attempts_(0), | 86 failed_login_attempts_(0), |
93 disconnected_event_(true, false), | 87 disconnected_event_(true, false), |
94 nat_traversal_enabled_(false), | 88 nat_traversal_enabled_(false), |
95 policy_received_(false) { | 89 policy_received_(false) { |
96 // Set up log message handler. | 90 // Set up log message handler. |
97 // Note that this approach doesn't quite support having multiple instances | 91 // Note that this approach doesn't quite support having multiple instances |
98 // of Chromoting running. In that case, the most recently opened tab will | 92 // of Chromoting running. In that case, the most recently opened tab will |
99 // grab all the debug log messages, and when any Chromoting tab is closed | 93 // grab all the debug log messages, and when any Chromoting tab is closed |
100 // the logging handler will go away. | 94 // the logging handler will go away. |
101 // Since having multiple Chromoting tabs is not a primary use case, and this | 95 // Since having multiple Chromoting tabs is not a primary use case, and this |
102 // is just debug logging, we're punting improving debug log support for that | 96 // is just debug logging, we're punting improving debug log support for that |
103 // case. | 97 // case. |
104 if (g_logging_old_handler == NULL) | 98 if (g_logging_old_handler == NULL) |
105 g_logging_old_handler = logging::GetLogMessageHandler(); | 99 g_logging_old_handler = logging::GetLogMessageHandler(); |
106 logging::SetLogMessageHandler(&LogToUI); | 100 logging::SetLogMessageHandler(&LogToUI); |
107 g_logging_scriptable_object = this; | 101 g_logging_scriptable_object = this; |
| 102 |
| 103 VLOG(2) << "HostNPScriptObject"; |
| 104 host_context_.SetUITaskPostFunction(base::Bind( |
| 105 &HostNPScriptObject::PostTaskToNPThread, base::Unretained(this))); |
108 } | 106 } |
109 | 107 |
110 HostNPScriptObject::~HostNPScriptObject() { | 108 HostNPScriptObject::~HostNPScriptObject() { |
111 CHECK_EQ(base::PlatformThread::CurrentId(), np_thread_id_); | 109 CHECK_EQ(base::PlatformThread::CurrentId(), np_thread_id_); |
112 | 110 |
113 // Shutdown DesktopEnvironment first so that it doesn't try to post | 111 // Shutdown DesktopEnvironment first so that it doesn't try to post |
114 // tasks on the UI thread while we are stopping the host. | 112 // tasks on the UI thread while we are stopping the host. |
115 desktop_environment_->Shutdown(); | 113 desktop_environment_->Shutdown(); |
116 | 114 |
117 logging::SetLogMessageHandler(g_logging_old_handler); | 115 logging::SetLogMessageHandler(g_logging_old_handler); |
118 g_logging_old_handler = NULL; | 116 g_logging_old_handler = NULL; |
119 g_logging_scriptable_object = NULL; | 117 g_logging_scriptable_object = NULL; |
120 | 118 |
121 plugin_message_loop_proxy_->Detach(); | |
122 | |
123 // Stop listening for policy updates. | 119 // Stop listening for policy updates. |
124 if (nat_policy_.get()) { | 120 if (nat_policy_.get()) { |
125 base::WaitableEvent nat_policy_stopped_(true, false); | 121 base::WaitableEvent nat_policy_stopped_(true, false); |
126 nat_policy_->StopWatching(&nat_policy_stopped_); | 122 nat_policy_->StopWatching(&nat_policy_stopped_); |
127 nat_policy_stopped_.Wait(); | 123 nat_policy_stopped_.Wait(); |
128 nat_policy_.reset(); | 124 nat_policy_.reset(); |
129 } | 125 } |
130 | 126 |
131 // Disconnect synchronously. We cannot disconnect asynchronously | 127 // Disconnect synchronously. We cannot disconnect asynchronously |
132 // here because |host_context_| needs to be stopped on the plugin | 128 // here because |host_context_| needs to be stopped on the plugin |
133 // thread, but the plugin thread may not exist after the instance | 129 // thread, but the plugin thread may not exist after the instance |
134 // is destroyed. | 130 // is destroyed. |
| 131 destructing_.Set(); |
135 disconnected_event_.Reset(); | 132 disconnected_event_.Reset(); |
136 DisconnectInternal(); | 133 DisconnectInternal(); |
137 disconnected_event_.Wait(); | 134 disconnected_event_.Wait(); |
138 | 135 |
139 // Stop all threads. | 136 // Stop all threads. |
140 host_context_.Stop(); | 137 host_context_.Stop(); |
141 } | 138 } |
142 | 139 |
143 bool HostNPScriptObject::Init() { | 140 bool HostNPScriptObject::Init() { |
144 VLOG(2) << "Init"; | 141 VLOG(2) << "Init"; |
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 access_code_lifetime_ = lifetime; | 570 access_code_lifetime_ = lifetime; |
574 | 571 |
575 // Tell the ChromotingHost the access code, to use as shared-secret. | 572 // Tell the ChromotingHost the access code, to use as shared-secret. |
576 host_->set_access_code(access_code_); | 573 host_->set_access_code(access_code_); |
577 | 574 |
578 // Let the caller know that life is good. | 575 // Let the caller know that life is good. |
579 OnStateChanged(kReceivedAccessCode); | 576 OnStateChanged(kReceivedAccessCode); |
580 } | 577 } |
581 | 578 |
582 void HostNPScriptObject::OnStateChanged(State state) { | 579 void HostNPScriptObject::OnStateChanged(State state) { |
583 if (!plugin_message_loop_proxy_->BelongsToCurrentThread()) { | 580 if (destructing_.IsSet()) |
584 plugin_message_loop_proxy_->PostTask( | 581 return; |
| 582 |
| 583 if (!host_context_.IsUIThread()) { |
| 584 host_context_.PostTaskToUIThread( |
585 FROM_HERE, base::Bind(&HostNPScriptObject::OnStateChanged, | 585 FROM_HERE, base::Bind(&HostNPScriptObject::OnStateChanged, |
586 base::Unretained(this), state)); | 586 base::Unretained(this), state)); |
587 return; | 587 return; |
588 } | 588 } |
589 state_ = state; | 589 state_ = state; |
590 if (on_state_changed_func_.get()) { | 590 if (on_state_changed_func_.get()) { |
591 VLOG(2) << "Calling state changed " << state; | 591 VLOG(2) << "Calling state changed " << state; |
592 bool is_good = InvokeAndIgnoreResult(on_state_changed_func_.get(), NULL, 0); | 592 bool is_good = InvokeAndIgnoreResult(on_state_changed_func_.get(), NULL, 0); |
593 LOG_IF(ERROR, !is_good) << "OnStateChanged failed"; | 593 LOG_IF(ERROR, !is_good) << "OnStateChanged failed"; |
594 } | 594 } |
(...skipping 13 matching lines...) Expand all Loading... |
608 message += (str.c_str() + message_start); | 608 message += (str.c_str() + message_start); |
609 g_logging_scriptable_object->LogDebugInfo(message); | 609 g_logging_scriptable_object->LogDebugInfo(message); |
610 g_logging_to_plugin = false; | 610 g_logging_to_plugin = false; |
611 } | 611 } |
612 if (g_logging_old_handler) | 612 if (g_logging_old_handler) |
613 return (g_logging_old_handler)(severity, file, line, message_start, str); | 613 return (g_logging_old_handler)(severity, file, line, message_start, str); |
614 return false; | 614 return false; |
615 } | 615 } |
616 | 616 |
617 void HostNPScriptObject::LogDebugInfo(const std::string& message) { | 617 void HostNPScriptObject::LogDebugInfo(const std::string& message) { |
618 if (!plugin_message_loop_proxy_->BelongsToCurrentThread()) { | 618 if (destructing_.IsSet()) |
619 plugin_message_loop_proxy_->PostTask( | 619 return; |
| 620 |
| 621 if (!host_context_.IsUIThread()) { |
| 622 host_context_.PostTaskToUIThread( |
620 FROM_HERE, base::Bind(&HostNPScriptObject::LogDebugInfo, | 623 FROM_HERE, base::Bind(&HostNPScriptObject::LogDebugInfo, |
621 base::Unretained(this), message)); | 624 base::Unretained(this), message)); |
622 return; | 625 return; |
623 } | 626 } |
624 | 627 |
625 if (log_debug_info_func_.get()) { | 628 if (log_debug_info_func_.get()) { |
626 NPVariant log_message; | 629 NPVariant log_message; |
627 STRINGZ_TO_NPVARIANT(message.c_str(), log_message); | 630 STRINGZ_TO_NPVARIANT(message.c_str(), log_message); |
628 bool is_good = InvokeAndIgnoreResult(log_debug_info_func_.get(), | 631 bool is_good = InvokeAndIgnoreResult(log_debug_info_func_.get(), |
629 &log_message, 1); | 632 &log_message, 1); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
694 const NPVariant* args, | 697 const NPVariant* args, |
695 uint32_t argCount) { | 698 uint32_t argCount) { |
696 NPVariant np_result; | 699 NPVariant np_result; |
697 bool is_good = g_npnetscape_funcs->invokeDefault(plugin_, func, args, | 700 bool is_good = g_npnetscape_funcs->invokeDefault(plugin_, func, args, |
698 argCount, &np_result); | 701 argCount, &np_result); |
699 if (is_good) | 702 if (is_good) |
700 g_npnetscape_funcs->releasevariantvalue(&np_result); | 703 g_npnetscape_funcs->releasevariantvalue(&np_result); |
701 return is_good; | 704 return is_good; |
702 } | 705 } |
703 | 706 |
| 707 void HostNPScriptObject::PostTaskToNPThread( |
| 708 const tracked_objects::Location& from_here, const base::Closure& task) { |
| 709 // The NPAPI functions cannot make use of |from_here|, but this method is |
| 710 // passed as a callback to ChromotingHostContext, so it needs to have the |
| 711 // appropriate signature. |
| 712 |
| 713 // Copy task to the heap so that we can pass it to NPTaskSpringboard(). |
| 714 base::Closure* task_in_heap = new base::Closure(task); |
| 715 |
| 716 // Can be called from any thread. |
| 717 g_npnetscape_funcs->pluginthreadasynccall(plugin_, &NPTaskSpringboard, |
| 718 task_in_heap); |
| 719 } |
| 720 |
| 721 // static |
| 722 void HostNPScriptObject::NPTaskSpringboard(void* task) { |
| 723 base::Closure* real_task = reinterpret_cast<base::Closure*>(task); |
| 724 real_task->Run(); |
| 725 delete real_task; |
| 726 } |
| 727 |
704 } // namespace remoting | 728 } // namespace remoting |
OLD | NEW |