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

Side by Side Diff: chromeos/dbus/session_manager_client.cc

Issue 1111653002: Switch SessionManagerClient::RestartJob to use RestartJobWithAuth (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add comments, make passing of FD ownership more clear Created 5 years, 7 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "chromeos/dbus/session_manager_client.h" 5 #include "chromeos/dbus/session_manager_client.h"
6 6
7 #include <sys/socket.h>
8
7 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h"
8 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
9 #include "base/files/file_util.h" 12 #include "base/files/file_util.h"
10 #include "base/location.h" 13 #include "base/location.h"
11 #include "base/path_service.h" 14 #include "base/path_service.h"
12 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
14 #include "base/task_runner_util.h" 17 #include "base/task_runner_util.h"
15 #include "base/threading/worker_pool.h" 18 #include "base/threading/worker_pool.h"
16 #include "chromeos/chromeos_paths.h" 19 #include "chromeos/chromeos_paths.h"
17 #include "chromeos/dbus/blocking_method_caller.h" 20 #include "chromeos/dbus/blocking_method_caller.h"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 // Helper to write a file in a background thread. 54 // Helper to write a file in a background thread.
52 void StoreFile(const base::FilePath& path, const std::string& data) { 55 void StoreFile(const base::FilePath& path, const std::string& data) {
53 const int size = static_cast<int>(data.size()); 56 const int size = static_cast<int>(data.size());
54 if (path.empty() || 57 if (path.empty() ||
55 !base::CreateDirectory(path.DirName()) || 58 !base::CreateDirectory(path.DirName()) ||
56 base::WriteFile(path, data.data(), size) != size) { 59 base::WriteFile(path, data.data(), size) != size) {
57 LOG(WARNING) << "Failed to write to " << path.value(); 60 LOG(WARNING) << "Failed to write to " << path.value();
58 } 61 }
59 } 62 }
60 63
64 // Creates a pair of file descriptors that form a conduit for trustworthy
65 // transfer of credentials between Chrome and the session_manager
66 void CreateValidCredConduit(dbus::FileDescriptor* local_auth_fd,
67 dbus::FileDescriptor* remote_auth_fd) {
68 int sockets[2] = {-1, -1};
69 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0) {
70 PLOG(ERROR) << "Failed to create a unix domain socketpair";
71 return;
72 }
73
74 local_auth_fd->PutValue(sockets[0]);
75 local_auth_fd->CheckValidity();
76
77 remote_auth_fd->PutValue(sockets[1]);
78 remote_auth_fd->CheckValidity();
79 }
80
61 } // namespace 81 } // namespace
62 82
63 // The SessionManagerClient implementation used in production. 83 // The SessionManagerClient implementation used in production.
64 class SessionManagerClientImpl : public SessionManagerClient { 84 class SessionManagerClientImpl : public SessionManagerClient {
65 public: 85 public:
66 SessionManagerClientImpl() 86 SessionManagerClientImpl()
67 : session_manager_proxy_(NULL), 87 : session_manager_proxy_(NULL),
68 screen_is_locked_(false), 88 screen_is_locked_(false),
69 weak_ptr_factory_(this) {} 89 weak_ptr_factory_(this) {}
70 90
(...skipping 18 matching lines...) Expand all
89 109
90 bool IsScreenLocked() const override { return screen_is_locked_; } 110 bool IsScreenLocked() const override { return screen_is_locked_; }
91 111
92 void EmitLoginPromptVisible() override { 112 void EmitLoginPromptVisible() override {
93 SimpleMethodCallToSessionManager( 113 SimpleMethodCallToSessionManager(
94 login_manager::kSessionManagerEmitLoginPromptVisible); 114 login_manager::kSessionManagerEmitLoginPromptVisible);
95 FOR_EACH_OBSERVER(Observer, observers_, EmitLoginPromptVisibleCalled()); 115 FOR_EACH_OBSERVER(Observer, observers_, EmitLoginPromptVisibleCalled());
96 } 116 }
97 117
98 void RestartJob(int pid, const std::string& command_line) override { 118 void RestartJob(int pid, const std::string& command_line) override {
99 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, 119 dbus::ScopedFileDescriptor local_auth_fd(new dbus::FileDescriptor());
100 login_manager::kSessionManagerRestartJob); 120 dbus::ScopedFileDescriptor remote_auth_fd(new dbus::FileDescriptor());
101 dbus::MessageWriter writer(&method_call); 121
102 writer.AppendInt32(pid); 122 // The session_manager provides a new method to replace RestartJob, called
103 writer.AppendString(command_line); 123 // RestartJobWithAuth, that is able to be used correctly within a PID
104 session_manager_proxy_->CallMethod( 124 // namespace. To use it, the caller must create a unix domain socket pair
105 &method_call, 125 // and pass one end over dbus while holding the local end open for the
106 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, 126 // duration of the call.
107 base::Bind(&SessionManagerClientImpl::OnRestartJob, 127 // Here, we call CreateValidCredConduit() to create the socket pair,
108 weak_ptr_factory_.GetWeakPtr())); 128 // and then pass both ends along to CallRestartJobWithValidFd(), which
129 // takes care of them from there.
130 // NB: PostTaskAndReply ensures that the second callback (which owns the
131 // ScopedFileDescriptor objects) outlives the first, so passing the
132 // bare pointers to CreateValidCredConduit is safe.
133 base::WorkerPool::PostTaskAndReply(
134 FROM_HERE, base::Bind(&CreateValidCredConduit, local_auth_fd.get(),
135 remote_auth_fd.get()),
136 base::Bind(&SessionManagerClientImpl::CallRestartJobWithValidFd,
137 weak_ptr_factory_.GetWeakPtr(), base::Passed(&local_auth_fd),
138 base::Passed(&remote_auth_fd), command_line),
139 false);
109 } 140 }
110 141
111 void StartSession(const std::string& user_email) override { 142 void StartSession(const std::string& user_email) override {
112 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, 143 dbus::MethodCall method_call(login_manager::kSessionManagerInterface,
113 login_manager::kSessionManagerStartSession); 144 login_manager::kSessionManagerStartSession);
114 dbus::MessageWriter writer(&method_call); 145 dbus::MessageWriter writer(&method_call);
115 writer.AppendString(user_email); 146 writer.AppendString(user_email);
116 writer.AppendString(""); // Unique ID is deprecated 147 writer.AppendString(""); // Unique ID is deprecated
117 session_manager_proxy_->CallMethod( 148 session_manager_proxy_->CallMethod(
118 &method_call, 149 &method_call,
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 session_manager_proxy_->CallMethod( 403 session_manager_proxy_->CallMethod(
373 &method_call, 404 &method_call,
374 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, 405 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
375 base::Bind( 406 base::Bind(
376 &SessionManagerClientImpl::OnStorePolicy, 407 &SessionManagerClientImpl::OnStorePolicy,
377 weak_ptr_factory_.GetWeakPtr(), 408 weak_ptr_factory_.GetWeakPtr(),
378 method_name, 409 method_name,
379 callback)); 410 callback));
380 } 411 }
381 412
413 // Calls RestartJobWithAuth to tell the session manager to restart the
414 // browser using the contents of command_line, authorizing the call
415 // using credentials acquired via remote_auth_fd.
416 // Ownership of local_auth_fd is held for the duration of the dbus call.
417 void CallRestartJobWithValidFd(dbus::ScopedFileDescriptor local_auth_fd,
418 dbus::ScopedFileDescriptor remote_auth_fd,
419 const std::string& command_line) {
420 dbus::MethodCall method_call(
421 login_manager::kSessionManagerInterface,
422 login_manager::kSessionManagerRestartJobWithAuth);
423 dbus::MessageWriter writer(&method_call);
424 writer.AppendFileDescriptor(*remote_auth_fd);
425 writer.AppendString(command_line);
426
427 // Ownership of local_auth_fd is passed to the callback that is to be
428 // called on completion of this method call. This keeps the browser end
stevenjb 2015/04/28 22:29:48 Comment truncated?
Chris Masone 2015/04/28 23:43:25 Done.
429 session_manager_proxy_->CallMethod(
430 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
431 base::Bind(&SessionManagerClientImpl::OnRestartJob,
432 weak_ptr_factory_.GetWeakPtr(),
433 base::Passed(&local_auth_fd)));
434 }
435
382 // Called when kSessionManagerRestartJob method is complete. 436 // Called when kSessionManagerRestartJob method is complete.
383 void OnRestartJob(dbus::Response* response) { 437 // Now that the call is complete, local_auth_fd can be closed and discarded,
438 // which will happen automatically when it goes out of scope.
439 void OnRestartJob(dbus::ScopedFileDescriptor local_auth_fd,
440 dbus::Response* response) {
384 LOG_IF(ERROR, !response) 441 LOG_IF(ERROR, !response)
385 << "Failed to call " 442 << "Failed to call "
386 << login_manager::kSessionManagerRestartJob; 443 << login_manager::kSessionManagerRestartJob;
387 } 444 }
388 445
389 // Called when kSessionManagerStartSession method is complete. 446 // Called when kSessionManagerStartSession method is complete.
390 void OnStartSession(dbus::Response* response) { 447 void OnStartSession(dbus::Response* response) {
391 LOG_IF(ERROR, !response) 448 LOG_IF(ERROR, !response)
392 << "Failed to call " 449 << "Failed to call "
393 << login_manager::kSessionManagerStartSession; 450 << login_manager::kSessionManagerStartSession;
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 801
745 SessionManagerClient* SessionManagerClient::Create( 802 SessionManagerClient* SessionManagerClient::Create(
746 DBusClientImplementationType type) { 803 DBusClientImplementationType type) {
747 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) 804 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
748 return new SessionManagerClientImpl(); 805 return new SessionManagerClientImpl();
749 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); 806 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
750 return new SessionManagerClientStubImpl(); 807 return new SessionManagerClientStubImpl();
751 } 808 }
752 809
753 } // namespace chromeos 810 } // namespace chromeos
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698