chromoting_instance.cc revision a02191e04bc25c4935f804f2c080ae28663d096d
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.
4
5#include "remoting/client/plugin/chromoting_instance.h"
6
7#include <algorithm>
8#include <string>
9#include <vector>
10
11#include "base/bind.h"
12#include "base/callback.h"
13#include "base/json/json_reader.h"
14#include "base/json/json_writer.h"
15#include "base/lazy_instance.h"
16#include "base/logging.h"
17#include "base/strings/string_split.h"
18#include "base/strings/stringprintf.h"
19#include "base/synchronization/lock.h"
20#include "base/threading/thread.h"
21#include "base/values.h"
22#include "crypto/random.h"
23#include "jingle/glue/thread_wrapper.h"
24#include "media/base/media.h"
25#include "net/socket/ssl_server_socket.h"
26#include "ppapi/cpp/completion_callback.h"
27#include "ppapi/cpp/dev/url_util_dev.h"
28#include "ppapi/cpp/image_data.h"
29#include "ppapi/cpp/input_event.h"
30#include "ppapi/cpp/rect.h"
31#include "remoting/base/constants.h"
32#include "remoting/base/util.h"
33#include "remoting/client/chromoting_client.h"
34#include "remoting/client/client_config.h"
35#include "remoting/client/frame_consumer_proxy.h"
36#include "remoting/client/plugin/delegating_signal_strategy.h"
37#include "remoting/client/plugin/media_source_video_renderer.h"
38#include "remoting/client/plugin/pepper_audio_player.h"
39#include "remoting/client/plugin/pepper_input_handler.h"
40#include "remoting/client/plugin/pepper_port_allocator.h"
41#include "remoting/client/plugin/pepper_token_fetcher.h"
42#include "remoting/client/plugin/pepper_view.h"
43#include "remoting/client/software_video_renderer.h"
44#include "remoting/protocol/connection_to_host.h"
45#include "remoting/protocol/host_stub.h"
46#include "remoting/protocol/libjingle_transport_factory.h"
47#include "third_party/libjingle/source/talk/base/helpers.h"
48#include "url/gurl.h"
49
50// Windows defines 'PostMessage', so we have to undef it.
51#if defined(PostMessage)
52#undef PostMessage
53#endif
54
55namespace remoting {
56
57namespace {
58
59// 32-bit BGRA is 4 bytes per pixel.
60const int kBytesPerPixel = 4;
61
62#if defined(ARCH_CPU_LITTLE_ENDIAN)
63const uint32_t kPixelAlphaMask = 0xff000000;
64#else  // !defined(ARCH_CPU_LITTLE_ENDIAN)
65const uint32_t kPixelAlphaMask = 0x000000ff;
66#endif  // !defined(ARCH_CPU_LITTLE_ENDIAN)
67
68// Default DPI to assume for old clients that use notifyClientResolution.
69const int kDefaultDPI = 96;
70
71// Interval at which to sample performance statistics.
72const int kPerfStatsIntervalMs = 1000;
73
74// URL scheme used by Chrome apps and extensions.
75const char kChromeExtensionUrlScheme[] = "chrome-extension";
76
77// Maximum width and height of a mouse cursor supported by PPAPI.
78const int kMaxCursorWidth = 32;
79const int kMaxCursorHeight = 32;
80
81#if defined(USE_OPENSSL)
82// Size of the random seed blob used to initialize RNG in libjingle. Libjingle
83// uses the seed only for OpenSSL builds. OpenSSL needs at least 32 bytes of
84// entropy (see http://wiki.openssl.org/index.php/Random_Numbers), but stores
85// 1039 bytes of state, so we initialize it with 1k or random data.
86const int kRandomSeedSize = 1024;
87#endif  // defined(USE_OPENSSL)
88
89std::string ConnectionStateToString(protocol::ConnectionToHost::State state) {
90  // Values returned by this function must match the
91  // remoting.ClientSession.State enum in JS code.
92  switch (state) {
93    case protocol::ConnectionToHost::INITIALIZING:
94      return "INITIALIZING";
95    case protocol::ConnectionToHost::CONNECTING:
96      return "CONNECTING";
97    case protocol::ConnectionToHost::AUTHENTICATED:
98      // Report the authenticated state as 'CONNECTING' to avoid changing
99      // the interface between the plugin and webapp.
100      return "CONNECTING";
101    case protocol::ConnectionToHost::CONNECTED:
102      return "CONNECTED";
103    case protocol::ConnectionToHost::CLOSED:
104      return "CLOSED";
105    case protocol::ConnectionToHost::FAILED:
106      return "FAILED";
107  }
108  NOTREACHED();
109  return std::string();
110}
111
112// TODO(sergeyu): Ideally we should just pass ErrorCode to the webapp
113// and let it handle it, but it would be hard to fix it now because
114// client plugin and webapp versions may not be in sync. It should be
115// easy to do after we are finished moving the client plugin to NaCl.
116std::string ConnectionErrorToString(protocol::ErrorCode error) {
117  // Values returned by this function must match the
118  // remoting.ClientSession.Error enum in JS code.
119  switch (error) {
120    case protocol::OK:
121      return "NONE";
122
123    case protocol::PEER_IS_OFFLINE:
124      return "HOST_IS_OFFLINE";
125
126    case protocol::SESSION_REJECTED:
127    case protocol::AUTHENTICATION_FAILED:
128      return "SESSION_REJECTED";
129
130    case protocol::INCOMPATIBLE_PROTOCOL:
131      return "INCOMPATIBLE_PROTOCOL";
132
133    case protocol::HOST_OVERLOAD:
134      return "HOST_OVERLOAD";
135
136    case protocol::CHANNEL_CONNECTION_ERROR:
137    case protocol::SIGNALING_ERROR:
138    case protocol::SIGNALING_TIMEOUT:
139    case protocol::UNKNOWN_ERROR:
140      return "NETWORK_FAILURE";
141  }
142  DLOG(FATAL) << "Unknown error code" << error;
143  return std::string();
144}
145
146// Returns true if |pixel| is not completely transparent.
147bool IsVisiblePixel(uint32_t pixel) {
148  return (pixel & kPixelAlphaMask) != 0;
149}
150
151// Returns true if there is at least one visible pixel in the given range.
152bool IsVisibleRow(const uint32_t* begin, const uint32_t* end) {
153  return std::find_if(begin, end, &IsVisiblePixel) != end;
154}
155
156// This flag blocks LOGs to the UI if we're already in the middle of logging
157// to the UI. This prevents a potential infinite loop if we encounter an error
158// while sending the log message to the UI.
159bool g_logging_to_plugin = false;
160bool g_has_logging_instance = false;
161base::LazyInstance<scoped_refptr<base::SingleThreadTaskRunner> >::Leaky
162    g_logging_task_runner = LAZY_INSTANCE_INITIALIZER;
163base::LazyInstance<base::WeakPtr<ChromotingInstance> >::Leaky
164    g_logging_instance = LAZY_INSTANCE_INITIALIZER;
165base::LazyInstance<base::Lock>::Leaky
166    g_logging_lock = LAZY_INSTANCE_INITIALIZER;
167logging::LogMessageHandlerFunction g_logging_old_handler = NULL;
168
169}  // namespace
170
171// String sent in the "hello" message to the webapp to describe features.
172const char ChromotingInstance::kApiFeatures[] =
173    "highQualityScaling injectKeyEvent sendClipboardItem remapKey trapKey "
174    "notifyClientResolution pauseVideo pauseAudio asyncPin thirdPartyAuth "
175    "pinlessAuth extensionMessage allowMouseLock mediaSourceRendering";
176
177const char ChromotingInstance::kRequestedCapabilities[] = "";
178const char ChromotingInstance::kSupportedCapabilities[] = "desktopShape";
179
180bool ChromotingInstance::ParseAuthMethods(const std::string& auth_methods_str,
181                                          ClientConfig* config) {
182  std::vector<std::string> auth_methods;
183  base::SplitString(auth_methods_str, ',', &auth_methods);
184  for (std::vector<std::string>::iterator it = auth_methods.begin();
185       it != auth_methods.end(); ++it) {
186    protocol::AuthenticationMethod authentication_method =
187        protocol::AuthenticationMethod::FromString(*it);
188    if (authentication_method.is_valid())
189      config->authentication_methods.push_back(authentication_method);
190  }
191  if (config->authentication_methods.empty()) {
192    LOG(ERROR) << "No valid authentication methods specified.";
193    return false;
194  }
195
196  return true;
197}
198
199ChromotingInstance::ChromotingInstance(PP_Instance pp_instance)
200    : pp::Instance(pp_instance),
201      initialized_(false),
202      plugin_task_runner_(new PluginThreadTaskRunner(&plugin_thread_delegate_)),
203      context_(plugin_task_runner_.get()),
204      input_tracker_(&mouse_input_filter_),
205      key_mapper_(&input_tracker_),
206      normalizing_input_filter_(CreateNormalizingInputFilter(&key_mapper_)),
207      input_handler_(this, normalizing_input_filter_.get()),
208      use_async_pin_dialog_(false),
209      use_media_source_rendering_(false),
210      weak_factory_(this) {
211  RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE | PP_INPUTEVENT_CLASS_WHEEL);
212  RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD);
213
214  // Resister this instance to handle debug log messsages.
215  RegisterLoggingInstance();
216
217#if defined(USE_OPENSSL)
218  // Initialize random seed for libjingle. It's necessary only with OpenSSL.
219  char random_seed[kRandomSeedSize];
220  crypto::RandBytes(random_seed, sizeof(random_seed));
221  talk_base::InitRandom(random_seed, sizeof(random_seed));
222#endif  // defined(USE_OPENSSL)
223
224  // Send hello message.
225  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
226  data->SetInteger("apiVersion", kApiVersion);
227  data->SetString("apiFeatures", kApiFeatures);
228  data->SetInteger("apiMinVersion", kApiMinMessagingVersion);
229  data->SetString("requestedCapabilities", kRequestedCapabilities);
230  data->SetString("supportedCapabilities", kSupportedCapabilities);
231
232  PostLegacyJsonMessage("hello", data.Pass());
233}
234
235ChromotingInstance::~ChromotingInstance() {
236  DCHECK(plugin_task_runner_->BelongsToCurrentThread());
237
238  // Unregister this instance so that debug log messages will no longer be sent
239  // to it. This will stop all logging in all Chromoting instances.
240  UnregisterLoggingInstance();
241
242  // PepperView must be destroyed before the client.
243  view_weak_factory_.reset();
244  view_.reset();
245
246  client_.reset();
247
248  plugin_task_runner_->Quit();
249
250  // Ensure that nothing touches the plugin thread delegate after this point.
251  plugin_task_runner_->DetachAndRunShutdownLoop();
252
253  // Stopping the context shuts down all chromoting threads.
254  context_.Stop();
255}
256
257bool ChromotingInstance::Init(uint32_t argc,
258                              const char* argn[],
259                              const char* argv[]) {
260  CHECK(!initialized_);
261  initialized_ = true;
262
263  VLOG(1) << "Started ChromotingInstance::Init";
264
265  // Check to make sure the media library is initialized.
266  // http://crbug.com/91521.
267  if (!media::IsMediaLibraryInitialized()) {
268    LOG(ERROR) << "Media library not initialized.";
269    return false;
270  }
271
272  // Check that the calling content is part of an app or extension.
273  if (!IsCallerAppOrExtension()) {
274    LOG(ERROR) << "Not an app or extension";
275    return false;
276  }
277
278  // Start all the threads.
279  context_.Start();
280
281  return true;
282}
283
284void ChromotingInstance::HandleMessage(const pp::Var& message) {
285  if (!message.is_string()) {
286    LOG(ERROR) << "Received a message that is not a string.";
287    return;
288  }
289
290  scoped_ptr<base::Value> json(
291      base::JSONReader::Read(message.AsString(),
292                             base::JSON_ALLOW_TRAILING_COMMAS));
293  base::DictionaryValue* message_dict = NULL;
294  std::string method;
295  base::DictionaryValue* data = NULL;
296  if (!json.get() ||
297      !json->GetAsDictionary(&message_dict) ||
298      !message_dict->GetString("method", &method) ||
299      !message_dict->GetDictionary("data", &data)) {
300    LOG(ERROR) << "Received invalid message:" << message.AsString();
301    return;
302  }
303
304  if (method == "connect") {
305    HandleConnect(*data);
306  } else if (method == "disconnect") {
307    HandleDisconnect(*data);
308  } else if (method == "incomingIq") {
309    HandleOnIncomingIq(*data);
310  } else if (method == "releaseAllKeys") {
311    HandleReleaseAllKeys(*data);
312  } else if (method == "injectKeyEvent") {
313    HandleInjectKeyEvent(*data);
314  } else if (method == "remapKey") {
315    HandleRemapKey(*data);
316  } else if (method == "trapKey") {
317    HandleTrapKey(*data);
318  } else if (method == "sendClipboardItem") {
319    HandleSendClipboardItem(*data);
320  } else if (method == "notifyClientResolution") {
321    HandleNotifyClientResolution(*data);
322  } else if (method == "pauseVideo") {
323    HandlePauseVideo(*data);
324  } else if (method == "pauseAudio") {
325    HandlePauseAudio(*data);
326  } else if (method == "useAsyncPinDialog") {
327    use_async_pin_dialog_ = true;
328  } else if (method == "onPinFetched") {
329    HandleOnPinFetched(*data);
330  } else if (method == "onThirdPartyTokenFetched") {
331    HandleOnThirdPartyTokenFetched(*data);
332  } else if (method == "requestPairing") {
333    HandleRequestPairing(*data);
334  } else if (method == "extensionMessage") {
335    HandleExtensionMessage(*data);
336  } else if (method == "allowMouseLock") {
337    HandleAllowMouseLockMessage();
338  } else if (method == "enableMediaSourceRendering") {
339    HandleEnableMediaSourceRendering();
340  }
341}
342
343void ChromotingInstance::DidChangeFocus(bool has_focus) {
344  DCHECK(plugin_task_runner_->BelongsToCurrentThread());
345
346  input_handler_.DidChangeFocus(has_focus);
347}
348
349void ChromotingInstance::DidChangeView(const pp::View& view) {
350  DCHECK(plugin_task_runner_->BelongsToCurrentThread());
351
352  plugin_view_ = view;
353  mouse_input_filter_.set_input_size(
354      webrtc::DesktopSize(view.GetRect().width(), view.GetRect().height()));
355
356  if (view_)
357    view_->SetView(view);
358}
359
360bool ChromotingInstance::HandleInputEvent(const pp::InputEvent& event) {
361  DCHECK(plugin_task_runner_->BelongsToCurrentThread());
362
363  if (!IsConnected())
364    return false;
365
366  return input_handler_.HandleInputEvent(event);
367}
368
369void ChromotingInstance::SetDesktopSize(const webrtc::DesktopSize& size,
370                                        const webrtc::DesktopVector& dpi) {
371  mouse_input_filter_.set_output_size(size);
372
373  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
374  data->SetInteger("width", size.width());
375  data->SetInteger("height", size.height());
376  if (dpi.x())
377    data->SetInteger("x_dpi", dpi.x());
378  if (dpi.y())
379    data->SetInteger("y_dpi", dpi.y());
380  PostLegacyJsonMessage("onDesktopSize", data.Pass());
381}
382
383void ChromotingInstance::SetDesktopShape(const webrtc::DesktopRegion& shape) {
384  if (desktop_shape_ && shape.Equals(*desktop_shape_))
385    return;
386
387  desktop_shape_.reset(new webrtc::DesktopRegion(shape));
388
389  scoped_ptr<base::ListValue> rects_value(new base::ListValue());
390  for (webrtc::DesktopRegion::Iterator i(shape); !i.IsAtEnd(); i.Advance()) {
391    const webrtc::DesktopRect& rect = i.rect();
392    scoped_ptr<base::ListValue> rect_value(new base::ListValue());
393    rect_value->AppendInteger(rect.left());
394    rect_value->AppendInteger(rect.top());
395    rect_value->AppendInteger(rect.width());
396    rect_value->AppendInteger(rect.height());
397    rects_value->Append(rect_value.release());
398  }
399
400  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
401  data->Set("rects", rects_value.release());
402  PostLegacyJsonMessage("onDesktopShape", data.Pass());
403}
404
405void ChromotingInstance::OnConnectionState(
406    protocol::ConnectionToHost::State state,
407    protocol::ErrorCode error) {
408  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
409  data->SetString("state", ConnectionStateToString(state));
410  data->SetString("error", ConnectionErrorToString(error));
411  PostLegacyJsonMessage("onConnectionStatus", data.Pass());
412}
413
414void ChromotingInstance::FetchThirdPartyToken(
415    const GURL& token_url,
416    const std::string& host_public_key,
417    const std::string& scope,
418    base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher) {
419  // Once the Session object calls this function, it won't continue the
420  // authentication until the callback is called (or connection is canceled).
421  // So, it's impossible to reach this with a callback already registered.
422  DCHECK(!pepper_token_fetcher_.get());
423  pepper_token_fetcher_ = pepper_token_fetcher;
424  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
425  data->SetString("tokenUrl", token_url.spec());
426  data->SetString("hostPublicKey", host_public_key);
427  data->SetString("scope", scope);
428  PostLegacyJsonMessage("fetchThirdPartyToken", data.Pass());
429}
430
431void ChromotingInstance::OnConnectionReady(bool ready) {
432  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
433  data->SetBoolean("ready", ready);
434  PostLegacyJsonMessage("onConnectionReady", data.Pass());
435}
436
437void ChromotingInstance::OnRouteChanged(const std::string& channel_name,
438                                        const protocol::TransportRoute& route) {
439  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
440  std::string message = "Channel " + channel_name + " using " +
441      protocol::TransportRoute::GetTypeString(route.type) + " connection.";
442  data->SetString("message", message);
443  PostLegacyJsonMessage("logDebugMessage", data.Pass());
444}
445
446void ChromotingInstance::SetCapabilities(const std::string& capabilities) {
447  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
448  data->SetString("capabilities", capabilities);
449  PostLegacyJsonMessage("setCapabilities", data.Pass());
450}
451
452void ChromotingInstance::SetPairingResponse(
453    const protocol::PairingResponse& pairing_response) {
454  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
455  data->SetString("clientId", pairing_response.client_id());
456  data->SetString("sharedSecret", pairing_response.shared_secret());
457  PostLegacyJsonMessage("pairingResponse", data.Pass());
458}
459
460void ChromotingInstance::DeliverHostMessage(
461    const protocol::ExtensionMessage& message) {
462  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
463  data->SetString("type", message.type());
464  data->SetString("data", message.data());
465  PostLegacyJsonMessage("extensionMessage", data.Pass());
466}
467
468void ChromotingInstance::FetchSecretFromDialog(
469    bool pairing_supported,
470    const protocol::SecretFetchedCallback& secret_fetched_callback) {
471  // Once the Session object calls this function, it won't continue the
472  // authentication until the callback is called (or connection is canceled).
473  // So, it's impossible to reach this with a callback already registered.
474  DCHECK(secret_fetched_callback_.is_null());
475  secret_fetched_callback_ = secret_fetched_callback;
476  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
477  data->SetBoolean("pairingSupported", pairing_supported);
478  PostLegacyJsonMessage("fetchPin", data.Pass());
479}
480
481void ChromotingInstance::FetchSecretFromString(
482    const std::string& shared_secret,
483    bool pairing_supported,
484    const protocol::SecretFetchedCallback& secret_fetched_callback) {
485  secret_fetched_callback.Run(shared_secret);
486}
487
488protocol::ClipboardStub* ChromotingInstance::GetClipboardStub() {
489  // TODO(sergeyu): Move clipboard handling to a separate class.
490  // crbug.com/138108
491  return this;
492}
493
494protocol::CursorShapeStub* ChromotingInstance::GetCursorShapeStub() {
495  // TODO(sergeyu): Move cursor shape code to a separate class.
496  // crbug.com/138108
497  return this;
498}
499
500scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher>
501ChromotingInstance::GetTokenFetcher(const std::string& host_public_key) {
502  return scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher>(
503      new PepperTokenFetcher(weak_factory_.GetWeakPtr(), host_public_key));
504}
505
506void ChromotingInstance::InjectClipboardEvent(
507    const protocol::ClipboardEvent& event) {
508  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
509  data->SetString("mimeType", event.mime_type());
510  data->SetString("item", event.data());
511  PostLegacyJsonMessage("injectClipboardItem", data.Pass());
512}
513
514void ChromotingInstance::SetCursorShape(
515    const protocol::CursorShapeInfo& cursor_shape) {
516  COMPILE_ASSERT(sizeof(uint32_t) == kBytesPerPixel, rgba_pixels_are_32bit);
517
518  // pp::MouseCursor requires image to be in the native format.
519  if (pp::ImageData::GetNativeImageDataFormat() !=
520      PP_IMAGEDATAFORMAT_BGRA_PREMUL) {
521    LOG(WARNING) << "Unable to set cursor shape - native image format is not"
522                    " premultiplied BGRA";
523    return;
524  }
525
526  int width = cursor_shape.width();
527  int height = cursor_shape.height();
528
529  int hotspot_x = cursor_shape.hotspot_x();
530  int hotspot_y = cursor_shape.hotspot_y();
531  int bytes_per_row = width * kBytesPerPixel;
532  int src_stride = width;
533  const uint32_t* src_row_data = reinterpret_cast<const uint32_t*>(
534      cursor_shape.data().data());
535  const uint32_t* src_row_data_end = src_row_data + src_stride * height;
536
537  scoped_ptr<pp::ImageData> cursor_image;
538  pp::Point cursor_hotspot;
539
540  // Check if the cursor is visible.
541  if (IsVisibleRow(src_row_data, src_row_data_end)) {
542    // If the cursor exceeds the size permitted by PPAPI then crop it, keeping
543    // the hotspot as close to the center of the new cursor shape as possible.
544    if (height > kMaxCursorHeight) {
545      int y = hotspot_y - (kMaxCursorHeight / 2);
546      y = std::max(y, 0);
547      y = std::min(y, height - kMaxCursorHeight);
548
549      src_row_data += src_stride * y;
550      height = kMaxCursorHeight;
551      hotspot_y -= y;
552    }
553    if (width > kMaxCursorWidth) {
554      int x = hotspot_x - (kMaxCursorWidth / 2);
555      x = std::max(x, 0);
556      x = std::min(x, height - kMaxCursorWidth);
557
558      src_row_data += x;
559      width = kMaxCursorWidth;
560      bytes_per_row = width * kBytesPerPixel;
561      hotspot_x -= x;
562    }
563
564    cursor_image.reset(new pp::ImageData(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL,
565                                          pp::Size(width, height), false));
566    cursor_hotspot = pp::Point(hotspot_x, hotspot_y);
567
568    uint8* dst_row_data = reinterpret_cast<uint8*>(cursor_image->data());
569    for (int row = 0; row < height; row++) {
570      memcpy(dst_row_data, src_row_data, bytes_per_row);
571      src_row_data += src_stride;
572      dst_row_data += cursor_image->stride();
573    }
574  }
575
576  input_handler_.SetMouseCursor(cursor_image.Pass(), cursor_hotspot);
577}
578
579void ChromotingInstance::OnFirstFrameReceived() {
580  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
581  PostLegacyJsonMessage("onFirstFrameReceived", data.Pass());
582}
583
584void ChromotingInstance::HandleConnect(const base::DictionaryValue& data) {
585  ClientConfig config;
586  std::string local_jid;
587  std::string auth_methods;
588  if (!data.GetString("hostJid", &config.host_jid) ||
589      !data.GetString("hostPublicKey", &config.host_public_key) ||
590      !data.GetString("localJid", &local_jid) ||
591      !data.GetString("authenticationMethods", &auth_methods) ||
592      !ParseAuthMethods(auth_methods, &config) ||
593      !data.GetString("authenticationTag", &config.authentication_tag)) {
594    LOG(ERROR) << "Invalid connect() data.";
595    return;
596  }
597  data.GetString("clientPairingId", &config.client_pairing_id);
598  data.GetString("clientPairedSecret", &config.client_paired_secret);
599  if (use_async_pin_dialog_) {
600    config.fetch_secret_callback =
601        base::Bind(&ChromotingInstance::FetchSecretFromDialog,
602                   weak_factory_.GetWeakPtr());
603  } else {
604    std::string shared_secret;
605    if (!data.GetString("sharedSecret", &shared_secret)) {
606      LOG(ERROR) << "sharedSecret not specified in connect().";
607      return;
608    }
609    config.fetch_secret_callback =
610        base::Bind(&ChromotingInstance::FetchSecretFromString, shared_secret);
611  }
612
613  // Read the list of capabilities, if any.
614  if (data.HasKey("capabilities")) {
615    if (!data.GetString("capabilities", &config.capabilities)) {
616      LOG(ERROR) << "Invalid connect() data.";
617      return;
618    }
619  }
620
621  ConnectWithConfig(config, local_jid);
622}
623
624void ChromotingInstance::ConnectWithConfig(const ClientConfig& config,
625                                           const std::string& local_jid) {
626  DCHECK(plugin_task_runner_->BelongsToCurrentThread());
627
628  jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
629
630  if (use_media_source_rendering_) {
631    video_renderer_.reset(new MediaSourceVideoRenderer(this));
632  } else {
633    view_.reset(new PepperView(this, &context_));
634    view_weak_factory_.reset(
635        new base::WeakPtrFactory<FrameConsumer>(view_.get()));
636
637    // SoftwareVideoRenderer runs on a separate thread so for now we wrap
638    // PepperView with a ref-counted proxy object.
639    scoped_refptr<FrameConsumerProxy> consumer_proxy =
640        new FrameConsumerProxy(plugin_task_runner_,
641                               view_weak_factory_->GetWeakPtr());
642
643    SoftwareVideoRenderer* renderer =
644        new SoftwareVideoRenderer(context_.main_task_runner(),
645                                  context_.decode_task_runner(),
646                                  consumer_proxy);
647    view_->Initialize(renderer);
648    if (!plugin_view_.is_null())
649      view_->SetView(plugin_view_);
650    video_renderer_.reset(renderer);
651  }
652
653  host_connection_.reset(new protocol::ConnectionToHost(true));
654  scoped_ptr<AudioPlayer> audio_player(new PepperAudioPlayer(this));
655  client_.reset(new ChromotingClient(config, &context_, host_connection_.get(),
656                                     this, video_renderer_.get(),
657                                     audio_player.Pass()));
658
659  // Connect the input pipeline to the protocol stub & initialize components.
660  mouse_input_filter_.set_input_stub(host_connection_->input_stub());
661  if (!plugin_view_.is_null()) {
662    mouse_input_filter_.set_input_size(webrtc::DesktopSize(
663        plugin_view_.GetRect().width(), plugin_view_.GetRect().height()));
664  }
665
666  VLOG(0) << "Connecting to " << config.host_jid
667          << ". Local jid: " << local_jid << ".";
668
669  // Setup the signal strategy.
670  signal_strategy_.reset(new DelegatingSignalStrategy(
671      local_jid, base::Bind(&ChromotingInstance::SendOutgoingIq,
672                            weak_factory_.GetWeakPtr())));
673
674  scoped_ptr<cricket::HttpPortAllocatorBase> port_allocator(
675      PepperPortAllocator::Create(this));
676  scoped_ptr<protocol::TransportFactory> transport_factory(
677      new protocol::LibjingleTransportFactory(
678          signal_strategy_.get(), port_allocator.Pass(),
679          NetworkSettings(NetworkSettings::NAT_TRAVERSAL_ENABLED)));
680
681  // Kick off the connection.
682  client_->Start(signal_strategy_.get(), transport_factory.Pass());
683
684  // Start timer that periodically sends perf stats.
685  plugin_task_runner_->PostDelayedTask(
686      FROM_HERE, base::Bind(&ChromotingInstance::SendPerfStats,
687                            weak_factory_.GetWeakPtr()),
688      base::TimeDelta::FromMilliseconds(kPerfStatsIntervalMs));
689}
690
691void ChromotingInstance::HandleDisconnect(const base::DictionaryValue& data) {
692  DCHECK(plugin_task_runner_->BelongsToCurrentThread());
693
694  // PepperView must be destroyed before the client.
695  view_weak_factory_.reset();
696  view_.reset();
697
698  VLOG(0) << "Disconnecting from host.";
699
700  client_.reset();
701
702  // Disconnect the input pipeline and teardown the connection.
703  mouse_input_filter_.set_input_stub(NULL);
704  host_connection_.reset();
705}
706
707void ChromotingInstance::HandleOnIncomingIq(const base::DictionaryValue& data) {
708  std::string iq;
709  if (!data.GetString("iq", &iq)) {
710    LOG(ERROR) << "Invalid incomingIq() data.";
711    return;
712  }
713
714  // Just ignore the message if it's received before Connect() is called. It's
715  // likely to be a leftover from a previous session, so it's safe to ignore it.
716  if (signal_strategy_)
717    signal_strategy_->OnIncomingMessage(iq);
718}
719
720void ChromotingInstance::HandleReleaseAllKeys(
721    const base::DictionaryValue& data) {
722  if (IsConnected())
723    input_tracker_.ReleaseAll();
724}
725
726void ChromotingInstance::HandleInjectKeyEvent(
727      const base::DictionaryValue& data) {
728  int usb_keycode = 0;
729  bool is_pressed = false;
730  if (!data.GetInteger("usbKeycode", &usb_keycode) ||
731      !data.GetBoolean("pressed", &is_pressed)) {
732    LOG(ERROR) << "Invalid injectKeyEvent.";
733    return;
734  }
735
736  protocol::KeyEvent event;
737  event.set_usb_keycode(usb_keycode);
738  event.set_pressed(is_pressed);
739
740  // Inject after the KeyEventMapper, so the event won't get mapped or trapped.
741  if (IsConnected())
742    input_tracker_.InjectKeyEvent(event);
743}
744
745void ChromotingInstance::HandleRemapKey(const base::DictionaryValue& data) {
746  int from_keycode = 0;
747  int to_keycode = 0;
748  if (!data.GetInteger("fromKeycode", &from_keycode) ||
749      !data.GetInteger("toKeycode", &to_keycode)) {
750    LOG(ERROR) << "Invalid remapKey.";
751    return;
752  }
753
754  key_mapper_.RemapKey(from_keycode, to_keycode);
755}
756
757void ChromotingInstance::HandleTrapKey(const base::DictionaryValue& data) {
758  int keycode = 0;
759  bool trap = false;
760  if (!data.GetInteger("keycode", &keycode) ||
761      !data.GetBoolean("trap", &trap)) {
762    LOG(ERROR) << "Invalid trapKey.";
763    return;
764  }
765
766  key_mapper_.TrapKey(keycode, trap);
767}
768
769void ChromotingInstance::HandleSendClipboardItem(
770    const base::DictionaryValue& data) {
771  std::string mime_type;
772  std::string item;
773  if (!data.GetString("mimeType", &mime_type) ||
774      !data.GetString("item", &item)) {
775    LOG(ERROR) << "Invalid sendClipboardItem data.";
776    return;
777  }
778  if (!IsConnected()) {
779    return;
780  }
781  protocol::ClipboardEvent event;
782  event.set_mime_type(mime_type);
783  event.set_data(item);
784  host_connection_->clipboard_stub()->InjectClipboardEvent(event);
785}
786
787void ChromotingInstance::HandleNotifyClientResolution(
788    const base::DictionaryValue& data) {
789  int width = 0;
790  int height = 0;
791  int x_dpi = kDefaultDPI;
792  int y_dpi = kDefaultDPI;
793  if (!data.GetInteger("width", &width) ||
794      !data.GetInteger("height", &height) ||
795      !data.GetInteger("x_dpi", &x_dpi) ||
796      !data.GetInteger("y_dpi", &y_dpi) ||
797      width <= 0 || height <= 0 ||
798      x_dpi <= 0 || y_dpi <= 0) {
799    LOG(ERROR) << "Invalid notifyClientResolution.";
800    return;
801  }
802
803  if (!IsConnected()) {
804    return;
805  }
806
807  protocol::ClientResolution client_resolution;
808  client_resolution.set_width(width);
809  client_resolution.set_height(height);
810  client_resolution.set_x_dpi(x_dpi);
811  client_resolution.set_y_dpi(y_dpi);
812
813  // Include the legacy width & height in DIPs for use by older hosts.
814  client_resolution.set_dips_width((width * kDefaultDPI) / x_dpi);
815  client_resolution.set_dips_height((height * kDefaultDPI) / y_dpi);
816
817  host_connection_->host_stub()->NotifyClientResolution(client_resolution);
818}
819
820void ChromotingInstance::HandlePauseVideo(const base::DictionaryValue& data) {
821  bool pause = false;
822  if (!data.GetBoolean("pause", &pause)) {
823    LOG(ERROR) << "Invalid pauseVideo.";
824    return;
825  }
826  if (!IsConnected()) {
827    return;
828  }
829  protocol::VideoControl video_control;
830  video_control.set_enable(!pause);
831  host_connection_->host_stub()->ControlVideo(video_control);
832}
833
834void ChromotingInstance::HandlePauseAudio(const base::DictionaryValue& data) {
835  bool pause = false;
836  if (!data.GetBoolean("pause", &pause)) {
837    LOG(ERROR) << "Invalid pauseAudio.";
838    return;
839  }
840  if (!IsConnected()) {
841    return;
842  }
843  protocol::AudioControl audio_control;
844  audio_control.set_enable(!pause);
845  host_connection_->host_stub()->ControlAudio(audio_control);
846}
847void ChromotingInstance::HandleOnPinFetched(const base::DictionaryValue& data) {
848  std::string pin;
849  if (!data.GetString("pin", &pin)) {
850    LOG(ERROR) << "Invalid onPinFetched.";
851    return;
852  }
853  if (!secret_fetched_callback_.is_null()) {
854    secret_fetched_callback_.Run(pin);
855    secret_fetched_callback_.Reset();
856  } else {
857    LOG(WARNING) << "Ignored OnPinFetched received without a pending fetch.";
858  }
859}
860
861void ChromotingInstance::HandleOnThirdPartyTokenFetched(
862    const base::DictionaryValue& data) {
863  std::string token;
864  std::string shared_secret;
865  if (!data.GetString("token", &token) ||
866      !data.GetString("sharedSecret", &shared_secret)) {
867    LOG(ERROR) << "Invalid onThirdPartyTokenFetched data.";
868    return;
869  }
870  if (pepper_token_fetcher_.get()) {
871    pepper_token_fetcher_->OnTokenFetched(token, shared_secret);
872    pepper_token_fetcher_.reset();
873  } else {
874    LOG(WARNING) << "Ignored OnThirdPartyTokenFetched without a pending fetch.";
875  }
876}
877
878void ChromotingInstance::HandleRequestPairing(
879    const base::DictionaryValue& data) {
880  std::string client_name;
881  if (!data.GetString("clientName", &client_name)) {
882    LOG(ERROR) << "Invalid requestPairing";
883    return;
884  }
885  if (!IsConnected()) {
886    return;
887  }
888  protocol::PairingRequest pairing_request;
889  pairing_request.set_client_name(client_name);
890  host_connection_->host_stub()->RequestPairing(pairing_request);
891}
892
893void ChromotingInstance::HandleExtensionMessage(
894    const base::DictionaryValue& data) {
895  std::string type;
896  std::string message_data;
897  if (!data.GetString("type", &type) ||
898      !data.GetString("data", &message_data)) {
899    LOG(ERROR) << "Invalid extensionMessage.";
900    return;
901  }
902  if (!IsConnected()) {
903    return;
904  }
905  protocol::ExtensionMessage message;
906  message.set_type(type);
907  message.set_data(message_data);
908  host_connection_->host_stub()->DeliverClientMessage(message);
909}
910
911void ChromotingInstance::HandleAllowMouseLockMessage() {
912  input_handler_.AllowMouseLock();
913}
914
915void ChromotingInstance::HandleEnableMediaSourceRendering() {
916  use_media_source_rendering_ = true;
917}
918
919ChromotingStats* ChromotingInstance::GetStats() {
920  if (!video_renderer_.get())
921    return NULL;
922  return video_renderer_->GetStats();
923}
924
925void ChromotingInstance::PostChromotingMessage(const std::string& method,
926                                               const pp::VarDictionary& data) {
927  pp::VarDictionary message;
928  message.Set(pp::Var("method"), pp::Var(method));
929  message.Set(pp::Var("data"), data);
930  PostMessage(message);
931}
932
933void ChromotingInstance::PostLegacyJsonMessage(
934    const std::string& method,
935    scoped_ptr<base::DictionaryValue> data) {
936  scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue());
937  message->SetString("method", method);
938  message->Set("data", data.release());
939
940  std::string message_json;
941  base::JSONWriter::Write(message.get(), &message_json);
942  PostMessage(pp::Var(message_json));
943}
944
945void ChromotingInstance::SendTrappedKey(uint32 usb_keycode, bool pressed) {
946  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
947  data->SetInteger("usbKeycode", usb_keycode);
948  data->SetBoolean("pressed", pressed);
949  PostLegacyJsonMessage("trappedKeyEvent", data.Pass());
950}
951
952void ChromotingInstance::SendOutgoingIq(const std::string& iq) {
953  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
954  data->SetString("iq", iq);
955  PostLegacyJsonMessage("sendOutgoingIq", data.Pass());
956}
957
958void ChromotingInstance::SendPerfStats() {
959  if (!video_renderer_.get()) {
960    return;
961  }
962
963  plugin_task_runner_->PostDelayedTask(
964      FROM_HERE, base::Bind(&ChromotingInstance::SendPerfStats,
965                            weak_factory_.GetWeakPtr()),
966      base::TimeDelta::FromMilliseconds(kPerfStatsIntervalMs));
967
968  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
969  ChromotingStats* stats = video_renderer_->GetStats();
970  data->SetDouble("videoBandwidth", stats->video_bandwidth()->Rate());
971  data->SetDouble("videoFrameRate", stats->video_frame_rate()->Rate());
972  data->SetDouble("captureLatency", stats->video_capture_ms()->Average());
973  data->SetDouble("encodeLatency", stats->video_encode_ms()->Average());
974  data->SetDouble("decodeLatency", stats->video_decode_ms()->Average());
975  data->SetDouble("renderLatency", stats->video_paint_ms()->Average());
976  data->SetDouble("roundtripLatency", stats->round_trip_ms()->Average());
977  PostLegacyJsonMessage("onPerfStats", data.Pass());
978}
979
980// static
981void ChromotingInstance::RegisterLogMessageHandler() {
982  base::AutoLock lock(g_logging_lock.Get());
983
984  VLOG(1) << "Registering global log handler";
985
986  // Record previous handler so we can call it in a chain.
987  g_logging_old_handler = logging::GetLogMessageHandler();
988
989  // Set up log message handler.
990  // This is not thread-safe so we need it within our lock.
991  logging::SetLogMessageHandler(&LogToUI);
992}
993
994void ChromotingInstance::RegisterLoggingInstance() {
995  base::AutoLock lock(g_logging_lock.Get());
996
997  // Register this instance as the one that will handle all logging calls
998  // and display them to the user.
999  // If multiple plugins are run, then the last one registered will handle all
1000  // logging for all instances.
1001  g_logging_instance.Get() = weak_factory_.GetWeakPtr();
1002  g_logging_task_runner.Get() = plugin_task_runner_;
1003  g_has_logging_instance = true;
1004}
1005
1006void ChromotingInstance::UnregisterLoggingInstance() {
1007  base::AutoLock lock(g_logging_lock.Get());
1008
1009  // Don't unregister unless we're the currently registered instance.
1010  if (this != g_logging_instance.Get().get())
1011    return;
1012
1013  // Unregister this instance for logging.
1014  g_has_logging_instance = false;
1015  g_logging_instance.Get().reset();
1016  g_logging_task_runner.Get() = NULL;
1017
1018  VLOG(1) << "Unregistering global log handler";
1019}
1020
1021// static
1022bool ChromotingInstance::LogToUI(int severity, const char* file, int line,
1023                                 size_t message_start,
1024                                 const std::string& str) {
1025  // Note that we're reading |g_has_logging_instance| outside of a lock.
1026  // This lockless read is done so that we don't needlessly slow down global
1027  // logging with a lock for each log message.
1028  //
1029  // This lockless read is safe because:
1030  //
1031  // Misreading a false value (when it should be true) means that we'll simply
1032  // skip processing a few log messages.
1033  //
1034  // Misreading a true value (when it should be false) means that we'll take
1035  // the lock and check |g_logging_instance| unnecessarily. This is not
1036  // problematic because we always set |g_logging_instance| inside a lock.
1037  if (g_has_logging_instance) {
1038    scoped_refptr<base::SingleThreadTaskRunner> logging_task_runner;
1039    base::WeakPtr<ChromotingInstance> logging_instance;
1040
1041    {
1042      base::AutoLock lock(g_logging_lock.Get());
1043      // If we're on the logging thread and |g_logging_to_plugin| is set then
1044      // this LOG message came from handling a previous LOG message and we
1045      // should skip it to avoid an infinite loop of LOG messages.
1046      if (!g_logging_task_runner.Get()->BelongsToCurrentThread() ||
1047          !g_logging_to_plugin) {
1048        logging_task_runner = g_logging_task_runner.Get();
1049        logging_instance = g_logging_instance.Get();
1050      }
1051    }
1052
1053    if (logging_task_runner.get()) {
1054      std::string message = remoting::GetTimestampString();
1055      message += (str.c_str() + message_start);
1056
1057      logging_task_runner->PostTask(
1058          FROM_HERE, base::Bind(&ChromotingInstance::ProcessLogToUI,
1059                                logging_instance, message));
1060    }
1061  }
1062
1063  if (g_logging_old_handler)
1064    return (g_logging_old_handler)(severity, file, line, message_start, str);
1065  return false;
1066}
1067
1068void ChromotingInstance::ProcessLogToUI(const std::string& message) {
1069  DCHECK(plugin_task_runner_->BelongsToCurrentThread());
1070
1071  // This flag (which is set only here) is used to prevent LogToUI from posting
1072  // new tasks while we're in the middle of servicing a LOG call. This can
1073  // happen if the call to LogDebugInfo tries to LOG anything.
1074  // Since it is read on the plugin thread, we don't need to lock to set it.
1075  g_logging_to_plugin = true;
1076  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
1077  data->SetString("message", message);
1078  PostLegacyJsonMessage("logDebugMessage", data.Pass());
1079  g_logging_to_plugin = false;
1080}
1081
1082bool ChromotingInstance::IsCallerAppOrExtension() {
1083  const pp::URLUtil_Dev* url_util = pp::URLUtil_Dev::Get();
1084  if (!url_util)
1085    return false;
1086
1087  PP_URLComponents_Dev url_components;
1088  pp::Var url_var = url_util->GetDocumentURL(this, &url_components);
1089  if (!url_var.is_string())
1090    return false;
1091
1092  std::string url = url_var.AsString();
1093  std::string url_scheme = url.substr(url_components.scheme.begin,
1094                                      url_components.scheme.len);
1095  return url_scheme == kChromeExtensionUrlScheme;
1096}
1097
1098bool ChromotingInstance::IsConnected() {
1099  return host_connection_.get() &&
1100    (host_connection_->state() == protocol::ConnectionToHost::CONNECTED);
1101}
1102
1103void ChromotingInstance::OnMediaSourceSize(const webrtc::DesktopSize& size,
1104                                           const webrtc::DesktopVector& dpi) {
1105  SetDesktopSize(size, dpi);
1106}
1107
1108void ChromotingInstance::OnMediaSourceShape(
1109    const webrtc::DesktopRegion& shape) {
1110  SetDesktopShape(shape);
1111}
1112
1113void ChromotingInstance::OnMediaSourceReset(const std::string& format) {
1114  scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
1115  data->SetString("format", format);
1116  PostLegacyJsonMessage("mediaSourceReset", data.Pass());
1117}
1118
1119void ChromotingInstance::OnMediaSourceData(uint8_t* buffer,
1120                                           size_t buffer_size) {
1121  pp::VarArrayBuffer array_buffer(buffer_size);
1122  void* data_ptr = array_buffer.Map();
1123  memcpy(data_ptr, buffer, buffer_size);
1124  array_buffer.Unmap();
1125  pp::VarDictionary data_dictionary;
1126  data_dictionary.Set(pp::Var("buffer"), array_buffer);
1127  PostChromotingMessage("mediaSourceData", data_dictionary);
1128}
1129
1130}  // namespace remoting
1131