Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: remoting/host/setup/me2me_native_messaging_host.cc

Issue 2149983003: Refactoring Elevated Host Communication Channel into its own class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@process_helper
Patch Set: Merging again (including previous dependency CLs that have been checked in). Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/setup/me2me_native_messaging_host.h" 5 #include "remoting/host/setup/me2me_native_messaging_host.h"
6 6
7 #include <cstdint> 7 #include <cstdint>
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/command_line.h" 14 #include "base/command_line.h"
15 #include "base/json/json_reader.h" 15 #include "base/json/json_reader.h"
16 #include "base/json/json_writer.h" 16 #include "base/json/json_writer.h"
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/macros.h" 18 #include "base/macros.h"
19 #include "base/single_thread_task_runner.h" 19 #include "base/single_thread_task_runner.h"
20 #include "base/strings/stringize_macros.h" 20 #include "base/strings/stringize_macros.h"
21 #include "base/time/time.h"
21 #include "base/values.h" 22 #include "base/values.h"
22 #include "build/build_config.h" 23 #include "build/build_config.h"
23 #include "google_apis/gaia/gaia_oauth_client.h" 24 #include "google_apis/gaia/gaia_oauth_client.h"
24 #include "google_apis/google_api_keys.h" 25 #include "google_apis/google_api_keys.h"
25 #include "net/base/network_interfaces.h" 26 #include "net/base/network_interfaces.h"
26 #include "remoting/base/auto_thread_task_runner.h" 27 #include "remoting/base/auto_thread_task_runner.h"
27 #include "remoting/base/rsa_key_pair.h" 28 #include "remoting/base/rsa_key_pair.h"
28 #include "remoting/host/chromoting_host_context.h" 29 #include "remoting/host/chromoting_host_context.h"
29 #include "remoting/host/native_messaging/log_message_handler.h" 30 #include "remoting/host/native_messaging/log_message_handler.h"
30 #include "remoting/host/native_messaging/pipe_messaging_channel.h"
31 #include "remoting/host/pin_hash.h" 31 #include "remoting/host/pin_hash.h"
32 #include "remoting/host/setup/oauth_client.h" 32 #include "remoting/host/setup/oauth_client.h"
33 #include "remoting/host/switches.h"
34 #include "remoting/protocol/pairing_registry.h" 33 #include "remoting/protocol/pairing_registry.h"
35 34
36 #if defined(OS_WIN) 35 #if defined(OS_WIN)
37 #include "base/win/scoped_handle.h" 36 #include "remoting/host/win/elevated_native_messaging_host.h"
38 #include "base/win/win_util.h"
39 #include "remoting/host/win/launch_native_messaging_host_process.h"
40 #endif // defined(OS_WIN) 37 #endif // defined(OS_WIN)
41 38
42 namespace { 39 namespace {
43 40
44 #if defined(OS_WIN) 41 #if defined(OS_WIN)
45 const int kElevatedHostTimeoutSeconds = 300; 42 const int kElevatedHostTimeoutSeconds = 300;
46 #endif // defined(OS_WIN) 43 #endif // defined(OS_WIN)
47 44
48 // redirect_uri to use when authenticating service accounts (service account 45 // redirect_uri to use when authenticating service accounts (service account
49 // codes are obtained "out-of-band", i.e., not through an OAuth redirect). 46 // codes are obtained "out-of-band", i.e., not through an OAuth redirect).
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 531
535 if (!error_message.empty()) { 532 if (!error_message.empty()) {
536 LOG(ERROR) << error_message; 533 LOG(ERROR) << error_message;
537 } 534 }
538 535
539 // Trigger a host shutdown by sending an empty message. 536 // Trigger a host shutdown by sending an empty message.
540 client_->CloseChannel(std::string()); 537 client_->CloseChannel(std::string());
541 } 538 }
542 539
543 #if defined(OS_WIN) 540 #if defined(OS_WIN)
544 Me2MeNativeMessagingHost::ElevatedChannelEventHandler::
545 ElevatedChannelEventHandler(extensions::NativeMessageHost::Client* client)
546 : client_(client) {}
547
548 void Me2MeNativeMessagingHost::ElevatedChannelEventHandler::OnMessage(
549 std::unique_ptr<base::Value> message) {
550 DCHECK(thread_checker_.CalledOnValidThread());
551
552 // Simply pass along the response from the elevated host to the client.
553 std::string message_json;
554 base::JSONWriter::Write(*message, &message_json);
555 client_->PostMessageFromNativeHost(message_json);
556 }
557
558 void Me2MeNativeMessagingHost::ElevatedChannelEventHandler::OnDisconnect() {
559 DCHECK(thread_checker_.CalledOnValidThread());
560 client_->CloseChannel(std::string());
561 }
562 541
563 bool Me2MeNativeMessagingHost::DelegateToElevatedHost( 542 bool Me2MeNativeMessagingHost::DelegateToElevatedHost(
564 std::unique_ptr<base::DictionaryValue> message) { 543 std::unique_ptr<base::DictionaryValue> message) {
565 DCHECK(task_runner()->BelongsToCurrentThread()); 544 DCHECK(task_runner()->BelongsToCurrentThread());
566
567 EnsureElevatedHostCreated();
568
569 // elevated_channel_ will be null if user rejects the UAC request.
570 if (elevated_channel_)
571 elevated_channel_->SendMessage(std::move(message));
572
573 return elevated_channel_ != nullptr;
574 }
575
576 void Me2MeNativeMessagingHost::EnsureElevatedHostCreated() {
577 DCHECK(task_runner()->BelongsToCurrentThread());
578 DCHECK(needs_elevation_); 545 DCHECK(needs_elevation_);
579 546
580 if (elevated_channel_) 547 if (!elevated_host_) {
581 return; 548 elevated_host_.reset(new ElevatedNativeMessagingHost(
582 549 base::CommandLine::ForCurrentProcess()->GetProgram(),
583 base::win::ScopedHandle read_handle; 550 parent_window_handle_,
584 base::win::ScopedHandle write_handle; 551 /*elevate_process=*/true,
585 // Get the name of the binary to launch. 552 base::TimeDelta::FromSeconds(kElevatedHostTimeoutSeconds),
586 base::FilePath binary = base::CommandLine::ForCurrentProcess()->GetProgram(); 553 client_));
587 ProcessLaunchResult result = LaunchNativeMessagingHostProcess(
588 binary, parent_window_handle_,
589 /*elevate_process=*/true, &read_handle, &write_handle);
590 if (result != PROCESS_LAUNCH_RESULT_SUCCESS) {
591 if (result != PROCESS_LAUNCH_RESULT_CANCELLED) {
592 OnError(std::string());
593 }
594 return;
595 } 554 }
596 555
597 // Set up the native messaging channel to talk to the elevated host. 556 if (elevated_host_->EnsureElevatedHostCreated()) {
Sergey Ulanov 2016/08/02 21:18:27 EnsureElevatedHostCreated() may call client_->Clos
joedow 2016/08/08 23:45:16 Done.
598 // Note that input for the elevated channel is output for the elevated host. 557 elevated_host_->SendMessage(std::move(message));
599 elevated_channel_.reset(new PipeMessagingChannel( 558 return true;
600 base::File(read_handle.Take()), base::File(write_handle.Take()))); 559 }
601 560
602 elevated_channel_event_handler_.reset( 561 return false;
603 new Me2MeNativeMessagingHost::ElevatedChannelEventHandler(client_));
604 elevated_channel_->Start(elevated_channel_event_handler_.get());
605
606 elevated_host_timer_.Start(
607 FROM_HERE, base::TimeDelta::FromSeconds(kElevatedHostTimeoutSeconds),
608 this, &Me2MeNativeMessagingHost::DisconnectElevatedHost);
609 }
610
611 void Me2MeNativeMessagingHost::DisconnectElevatedHost() {
612 DCHECK(task_runner()->BelongsToCurrentThread());
613
614 // This will send an EOF to the elevated host, triggering its shutdown.
615 elevated_channel_.reset();
616 } 562 }
617 563
618 #else // defined(OS_WIN) 564 #else // defined(OS_WIN)
619 565
620 bool Me2MeNativeMessagingHost::DelegateToElevatedHost( 566 bool Me2MeNativeMessagingHost::DelegateToElevatedHost(
621 std::unique_ptr<base::DictionaryValue> message) { 567 std::unique_ptr<base::DictionaryValue> message) {
622 NOTREACHED(); 568 NOTREACHED();
623 return false; 569 return false;
624 } 570 }
625 571
626 #endif // !defined(OS_WIN) 572 #endif // !defined(OS_WIN)
627 573
628 } // namespace remoting 574 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698