| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/services/public/cpp/view_manager/lib/view_manager_client_impl.h" | 5 #include "mojo/services/public/cpp/view_manager/lib/view_manager_client_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "mojo/public/cpp/application/application_connection.h" | 9 #include "mojo/public/cpp/application/application_connection.h" |
| 10 #include "mojo/public/cpp/application/connect.h" | 10 #include "mojo/public/cpp/application/connect.h" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 NodePrivate(root_).view_manager())->RemoveRoot(root_); | 99 NodePrivate(root_).view_manager())->RemoveRoot(root_); |
| 100 node->RemoveObserver(this); | 100 node->RemoveObserver(this); |
| 101 delete this; | 101 delete this; |
| 102 } | 102 } |
| 103 | 103 |
| 104 Node* root_; | 104 Node* root_; |
| 105 | 105 |
| 106 DISALLOW_COPY_AND_ASSIGN(RootObserver); | 106 DISALLOW_COPY_AND_ASSIGN(RootObserver); |
| 107 }; | 107 }; |
| 108 | 108 |
| 109 class ViewManagerTransaction { | 109 bool CreateMapAndDupSharedBuffer(size_t size, |
| 110 public: | 110 void** memory, |
| 111 virtual ~ViewManagerTransaction() {} | 111 ScopedSharedBufferHandle* handle, |
| 112 ScopedSharedBufferHandle* duped) { |
| 113 MojoResult result = CreateSharedBuffer(NULL, size, handle); |
| 114 if (result != MOJO_RESULT_OK) |
| 115 return false; |
| 116 DCHECK(handle->is_valid()); |
| 112 | 117 |
| 113 void Commit() { | 118 result = DuplicateBuffer(handle->get(), NULL, duped); |
| 114 DCHECK(!committed_); | 119 if (result != MOJO_RESULT_OK) |
| 115 DoCommit(); | 120 return false; |
| 116 committed_ = true; | 121 DCHECK(duped->is_valid()); |
| 117 } | |
| 118 | 122 |
| 119 bool committed() const { return committed_; } | 123 result = MapBuffer( |
| 124 handle->get(), 0, size, memory, MOJO_MAP_BUFFER_FLAG_NONE); |
| 125 if (result != MOJO_RESULT_OK) |
| 126 return false; |
| 127 DCHECK(*memory); |
| 120 | 128 |
| 121 protected: | 129 return true; |
| 122 explicit ViewManagerTransaction(ViewManagerClientImpl* client) | 130 } |
| 123 : committed_(false), | |
| 124 client_(client) { | |
| 125 } | |
| 126 | |
| 127 // Overridden to perform transaction-specific commit actions. | |
| 128 virtual void DoCommit() = 0; | |
| 129 | |
| 130 // Overridden to perform transaction-specific cleanup on commit ack from the | |
| 131 // service. | |
| 132 virtual void DoActionCompleted(bool success) = 0; | |
| 133 | |
| 134 ViewManagerService* service() { return client_->service_; } | |
| 135 | |
| 136 // TODO(sky): nuke this and covert all to new one, then rename | |
| 137 // ActionCompletedCallbackWithErrorCode to ActionCompletedCallback. | |
| 138 base::Callback<void(bool)> ActionCompletedCallback() { | |
| 139 return base::Bind(&ViewManagerTransaction::OnActionCompleted, | |
| 140 base::Unretained(this)); | |
| 141 } | |
| 142 | |
| 143 base::Callback<void(ErrorCode)> ActionCompletedCallbackWithErrorCode() { | |
| 144 return base::Bind(&ViewManagerTransaction::OnActionCompletedWithErrorCode, | |
| 145 base::Unretained(this)); | |
| 146 } | |
| 147 | |
| 148 private: | |
| 149 // General callback to be used for commits to the service. | |
| 150 void OnActionCompleted(bool success) { | |
| 151 DoActionCompleted(success); | |
| 152 client_->RemoveFromPendingQueue(this); | |
| 153 } | |
| 154 | |
| 155 void OnActionCompletedWithErrorCode(ErrorCode error_code) { | |
| 156 DoActionCompleted(error_code == ERROR_CODE_NONE); | |
| 157 client_->RemoveFromPendingQueue(this); | |
| 158 } | |
| 159 | |
| 160 bool committed_; | |
| 161 ViewManagerClientImpl* client_; | |
| 162 | |
| 163 DISALLOW_COPY_AND_ASSIGN(ViewManagerTransaction); | |
| 164 }; | |
| 165 | |
| 166 class CreateViewTransaction : public ViewManagerTransaction { | |
| 167 public: | |
| 168 CreateViewTransaction(Id view_id, ViewManagerClientImpl* client) | |
| 169 : ViewManagerTransaction(client), | |
| 170 view_id_(view_id) {} | |
| 171 virtual ~CreateViewTransaction() {} | |
| 172 | |
| 173 private: | |
| 174 // Overridden from ViewManagerTransaction: | |
| 175 virtual void DoCommit() OVERRIDE { | |
| 176 service()->CreateView(view_id_, ActionCompletedCallback()); | |
| 177 } | |
| 178 virtual void DoActionCompleted(bool success) OVERRIDE { | |
| 179 // TODO(beng): failure. | |
| 180 DCHECK(success); | |
| 181 } | |
| 182 | |
| 183 const Id view_id_; | |
| 184 | |
| 185 DISALLOW_COPY_AND_ASSIGN(CreateViewTransaction); | |
| 186 }; | |
| 187 | |
| 188 class DestroyViewTransaction : public ViewManagerTransaction { | |
| 189 public: | |
| 190 DestroyViewTransaction(Id view_id, ViewManagerClientImpl* client) | |
| 191 : ViewManagerTransaction(client), | |
| 192 view_id_(view_id) {} | |
| 193 virtual ~DestroyViewTransaction() {} | |
| 194 | |
| 195 private: | |
| 196 // Overridden from ViewManagerTransaction: | |
| 197 virtual void DoCommit() OVERRIDE { | |
| 198 service()->DeleteView(view_id_, ActionCompletedCallback()); | |
| 199 } | |
| 200 virtual void DoActionCompleted(bool success) OVERRIDE { | |
| 201 // TODO(beng): recovery? | |
| 202 DCHECK(success); | |
| 203 } | |
| 204 | |
| 205 const Id view_id_; | |
| 206 | |
| 207 DISALLOW_COPY_AND_ASSIGN(DestroyViewTransaction); | |
| 208 }; | |
| 209 | |
| 210 class CreateNodeTransaction : public ViewManagerTransaction { | |
| 211 public: | |
| 212 CreateNodeTransaction(Id node_id, ViewManagerClientImpl* client) | |
| 213 : ViewManagerTransaction(client), | |
| 214 node_id_(node_id) {} | |
| 215 virtual ~CreateNodeTransaction() {} | |
| 216 | |
| 217 private: | |
| 218 // Overridden from ViewManagerTransaction: | |
| 219 virtual void DoCommit() OVERRIDE { | |
| 220 service()->CreateNode(node_id_, ActionCompletedCallbackWithErrorCode()); | |
| 221 } | |
| 222 virtual void DoActionCompleted(bool success) OVERRIDE { | |
| 223 // TODO(beng): Failure means we tried to create with an extant id for this | |
| 224 // connection. It also could mean we tried to do something | |
| 225 // invalid, or we tried applying a change out of order. Figure | |
| 226 // out what to do. | |
| 227 DCHECK(success); | |
| 228 } | |
| 229 | |
| 230 const Id node_id_; | |
| 231 | |
| 232 DISALLOW_COPY_AND_ASSIGN(CreateNodeTransaction); | |
| 233 }; | |
| 234 | |
| 235 class DestroyNodeTransaction : public ViewManagerTransaction { | |
| 236 public: | |
| 237 DestroyNodeTransaction(Id node_id, ViewManagerClientImpl* client) | |
| 238 : ViewManagerTransaction(client), | |
| 239 node_id_(node_id) {} | |
| 240 virtual ~DestroyNodeTransaction() {} | |
| 241 | |
| 242 private: | |
| 243 // Overridden from ViewManagerTransaction: | |
| 244 virtual void DoCommit() OVERRIDE { | |
| 245 service()->DeleteNode(node_id_, ActionCompletedCallback()); | |
| 246 } | |
| 247 virtual void DoActionCompleted(bool success) OVERRIDE { | |
| 248 // TODO(beng): recovery? | |
| 249 DCHECK(success); | |
| 250 } | |
| 251 | |
| 252 const Id node_id_; | |
| 253 DISALLOW_COPY_AND_ASSIGN(DestroyNodeTransaction); | |
| 254 }; | |
| 255 | |
| 256 class AddChildTransaction : public ViewManagerTransaction { | |
| 257 public: | |
| 258 AddChildTransaction(Id child_id, | |
| 259 Id parent_id, | |
| 260 ViewManagerClientImpl* client) | |
| 261 : ViewManagerTransaction(client), | |
| 262 child_id_(child_id), | |
| 263 parent_id_(parent_id) {} | |
| 264 virtual ~AddChildTransaction() {} | |
| 265 | |
| 266 private: | |
| 267 // Overridden from ViewManagerTransaction: | |
| 268 virtual void DoCommit() OVERRIDE { | |
| 269 service()->AddNode(parent_id_, child_id_, ActionCompletedCallback()); | |
| 270 } | |
| 271 | |
| 272 virtual void DoActionCompleted(bool success) OVERRIDE { | |
| 273 // TODO(beng): recovery? | |
| 274 DCHECK(success); | |
| 275 } | |
| 276 | |
| 277 const Id child_id_; | |
| 278 const Id parent_id_; | |
| 279 | |
| 280 DISALLOW_COPY_AND_ASSIGN(AddChildTransaction); | |
| 281 }; | |
| 282 | |
| 283 class RemoveChildTransaction : public ViewManagerTransaction { | |
| 284 public: | |
| 285 RemoveChildTransaction(Id child_id, ViewManagerClientImpl* client) | |
| 286 : ViewManagerTransaction(client), | |
| 287 child_id_(child_id) {} | |
| 288 virtual ~RemoveChildTransaction() {} | |
| 289 | |
| 290 private: | |
| 291 // Overridden from ViewManagerTransaction: | |
| 292 virtual void DoCommit() OVERRIDE { | |
| 293 service()->RemoveNodeFromParent(child_id_, ActionCompletedCallback()); | |
| 294 } | |
| 295 | |
| 296 virtual void DoActionCompleted(bool success) OVERRIDE { | |
| 297 // TODO(beng): recovery? | |
| 298 DCHECK(success); | |
| 299 } | |
| 300 | |
| 301 const Id child_id_; | |
| 302 | |
| 303 DISALLOW_COPY_AND_ASSIGN(RemoveChildTransaction); | |
| 304 }; | |
| 305 | |
| 306 class ReorderNodeTransaction : public ViewManagerTransaction { | |
| 307 public: | |
| 308 ReorderNodeTransaction(Id node_id, | |
| 309 Id relative_id, | |
| 310 OrderDirection direction, | |
| 311 ViewManagerClientImpl* client) | |
| 312 : ViewManagerTransaction(client), | |
| 313 node_id_(node_id), | |
| 314 relative_id_(relative_id), | |
| 315 direction_(direction) {} | |
| 316 virtual ~ReorderNodeTransaction() {} | |
| 317 | |
| 318 private: | |
| 319 // Overridden from ViewManagerTransaction: | |
| 320 virtual void DoCommit() OVERRIDE { | |
| 321 service()->ReorderNode(node_id_, | |
| 322 relative_id_, | |
| 323 direction_, | |
| 324 ActionCompletedCallback()); | |
| 325 } | |
| 326 | |
| 327 virtual void DoActionCompleted(bool success) OVERRIDE { | |
| 328 // TODO(beng): recovery? | |
| 329 DCHECK(success); | |
| 330 } | |
| 331 | |
| 332 const Id node_id_; | |
| 333 const Id relative_id_; | |
| 334 const OrderDirection direction_; | |
| 335 | |
| 336 DISALLOW_COPY_AND_ASSIGN(ReorderNodeTransaction); | |
| 337 }; | |
| 338 | |
| 339 class SetActiveViewTransaction : public ViewManagerTransaction { | |
| 340 public: | |
| 341 SetActiveViewTransaction(Id node_id, | |
| 342 Id view_id, | |
| 343 ViewManagerClientImpl* client) | |
| 344 : ViewManagerTransaction(client), | |
| 345 node_id_(node_id), | |
| 346 view_id_(view_id) {} | |
| 347 virtual ~SetActiveViewTransaction() {} | |
| 348 | |
| 349 private: | |
| 350 // Overridden from ViewManagerTransaction: | |
| 351 virtual void DoCommit() OVERRIDE { | |
| 352 service()->SetView(node_id_, view_id_, ActionCompletedCallback()); | |
| 353 } | |
| 354 virtual void DoActionCompleted(bool success) OVERRIDE { | |
| 355 // TODO(beng): recovery? | |
| 356 } | |
| 357 | |
| 358 const Id node_id_; | |
| 359 const Id view_id_; | |
| 360 | |
| 361 DISALLOW_COPY_AND_ASSIGN(SetActiveViewTransaction); | |
| 362 }; | |
| 363 | |
| 364 class SetBoundsTransaction : public ViewManagerTransaction { | |
| 365 public: | |
| 366 SetBoundsTransaction(Id node_id, | |
| 367 const gfx::Rect& bounds, | |
| 368 ViewManagerClientImpl* client) | |
| 369 : ViewManagerTransaction(client), | |
| 370 node_id_(node_id), | |
| 371 bounds_(bounds) {} | |
| 372 virtual ~SetBoundsTransaction() {} | |
| 373 | |
| 374 private: | |
| 375 // Overridden from ViewManagerTransaction: | |
| 376 virtual void DoCommit() OVERRIDE { | |
| 377 service()->SetNodeBounds( | |
| 378 node_id_, Rect::From(bounds_), ActionCompletedCallback()); | |
| 379 } | |
| 380 virtual void DoActionCompleted(bool success) OVERRIDE { | |
| 381 // TODO(beng): recovery? | |
| 382 } | |
| 383 | |
| 384 const Id node_id_; | |
| 385 const gfx::Rect bounds_; | |
| 386 | |
| 387 DISALLOW_COPY_AND_ASSIGN(SetBoundsTransaction); | |
| 388 }; | |
| 389 | |
| 390 class SetViewContentsTransaction : public ViewManagerTransaction { | |
| 391 public: | |
| 392 SetViewContentsTransaction(Id view_id, | |
| 393 const SkBitmap& contents, | |
| 394 ViewManagerClientImpl* client) | |
| 395 : ViewManagerTransaction(client), | |
| 396 view_id_(view_id), | |
| 397 contents_(contents) {} | |
| 398 virtual ~SetViewContentsTransaction() {} | |
| 399 | |
| 400 private: | |
| 401 // Overridden from ViewManagerTransaction: | |
| 402 virtual void DoCommit() OVERRIDE { | |
| 403 std::vector<unsigned char> data; | |
| 404 gfx::PNGCodec::EncodeBGRASkBitmap(contents_, false, &data); | |
| 405 | |
| 406 void* memory = NULL; | |
| 407 ScopedSharedBufferHandle duped; | |
| 408 bool result = CreateMapAndDupSharedBuffer(data.size(), | |
| 409 &memory, | |
| 410 &shared_state_handle_, | |
| 411 &duped); | |
| 412 if (!result) | |
| 413 return; | |
| 414 | |
| 415 memcpy(memory, &data[0], data.size()); | |
| 416 | |
| 417 service()->SetViewContents(view_id_, duped.Pass(), | |
| 418 static_cast<uint32_t>(data.size()), | |
| 419 ActionCompletedCallback()); | |
| 420 } | |
| 421 virtual void DoActionCompleted(bool success) OVERRIDE { | |
| 422 // TODO(beng): recovery? | |
| 423 } | |
| 424 | |
| 425 bool CreateMapAndDupSharedBuffer(size_t size, | |
| 426 void** memory, | |
| 427 ScopedSharedBufferHandle* handle, | |
| 428 ScopedSharedBufferHandle* duped) { | |
| 429 MojoResult result = CreateSharedBuffer(NULL, size, handle); | |
| 430 if (result != MOJO_RESULT_OK) | |
| 431 return false; | |
| 432 DCHECK(handle->is_valid()); | |
| 433 | |
| 434 result = DuplicateBuffer(handle->get(), NULL, duped); | |
| 435 if (result != MOJO_RESULT_OK) | |
| 436 return false; | |
| 437 DCHECK(duped->is_valid()); | |
| 438 | |
| 439 result = MapBuffer( | |
| 440 handle->get(), 0, size, memory, MOJO_MAP_BUFFER_FLAG_NONE); | |
| 441 if (result != MOJO_RESULT_OK) | |
| 442 return false; | |
| 443 DCHECK(*memory); | |
| 444 | |
| 445 return true; | |
| 446 } | |
| 447 | |
| 448 const Id view_id_; | |
| 449 const SkBitmap contents_; | |
| 450 ScopedSharedBufferHandle shared_state_handle_; | |
| 451 | |
| 452 DISALLOW_COPY_AND_ASSIGN(SetViewContentsTransaction); | |
| 453 }; | |
| 454 | |
| 455 class EmbedTransaction : public ViewManagerTransaction { | |
| 456 public: | |
| 457 EmbedTransaction(const String& url, | |
| 458 Id node_id, | |
| 459 ViewManagerClientImpl* client) | |
| 460 : ViewManagerTransaction(client), | |
| 461 url_(url), | |
| 462 node_id_(node_id) {} | |
| 463 virtual ~EmbedTransaction() {} | |
| 464 | |
| 465 private: | |
| 466 // Overridden from ViewManagerTransaction: | |
| 467 virtual void DoCommit() OVERRIDE { | |
| 468 service()->Embed(url_, node_id_, ActionCompletedCallback()); | |
| 469 } | |
| 470 virtual void DoActionCompleted(bool success) OVERRIDE { | |
| 471 // TODO(beng): recovery? | |
| 472 } | |
| 473 | |
| 474 const String url_; | |
| 475 const Id node_id_; | |
| 476 | |
| 477 DISALLOW_COPY_AND_ASSIGN(EmbedTransaction); | |
| 478 }; | |
| 479 | |
| 480 class SetFocusTransaction : public ViewManagerTransaction { | |
| 481 public: | |
| 482 SetFocusTransaction(Id node_id, ViewManagerClientImpl* client) | |
| 483 : ViewManagerTransaction(client), | |
| 484 node_id_(node_id) {} | |
| 485 virtual ~SetFocusTransaction() {} | |
| 486 | |
| 487 private: | |
| 488 // Overridden from ViewManagerTransaction: | |
| 489 virtual void DoCommit() OVERRIDE { | |
| 490 service()->SetFocus(node_id_, ActionCompletedCallback()); | |
| 491 } | |
| 492 virtual void DoActionCompleted(bool success) OVERRIDE { | |
| 493 // TODO(beng): recovery? | |
| 494 } | |
| 495 | |
| 496 const Id node_id_; | |
| 497 | |
| 498 DISALLOW_COPY_AND_ASSIGN(SetFocusTransaction); | |
| 499 }; | |
| 500 | |
| 501 class SetVisibleTransaction : public ViewManagerTransaction { | |
| 502 public: | |
| 503 SetVisibleTransaction(Id node_id, | |
| 504 bool visible, | |
| 505 ViewManagerClientImpl* client) | |
| 506 : ViewManagerTransaction(client), | |
| 507 node_id_(node_id), | |
| 508 visible_(visible) {} | |
| 509 virtual ~SetVisibleTransaction() {} | |
| 510 | |
| 511 private: | |
| 512 // Overridden from ViewManagerTransaction: | |
| 513 virtual void DoCommit() OVERRIDE { | |
| 514 service()->SetNodeVisibility(node_id_, visible_, ActionCompletedCallback()); | |
| 515 } | |
| 516 virtual void DoActionCompleted(bool success) OVERRIDE { | |
| 517 // TODO(beng): recovery? | |
| 518 } | |
| 519 | |
| 520 const Id node_id_; | |
| 521 const bool visible_; | |
| 522 | |
| 523 DISALLOW_COPY_AND_ASSIGN(SetVisibleTransaction); | |
| 524 }; | |
| 525 | 131 |
| 526 ViewManagerClientImpl::ViewManagerClientImpl(ViewManagerDelegate* delegate) | 132 ViewManagerClientImpl::ViewManagerClientImpl(ViewManagerDelegate* delegate) |
| 527 : connected_(false), | 133 : connected_(false), |
| 528 connection_id_(0), | 134 connection_id_(0), |
| 529 next_id_(1), | 135 next_id_(1), |
| 530 delegate_(delegate), | 136 delegate_(delegate), |
| 531 window_manager_delegate_(NULL) { | 137 window_manager_delegate_(NULL) { |
| 532 } | 138 } |
| 533 | 139 |
| 534 ViewManagerClientImpl::~ViewManagerClientImpl() { | 140 ViewManagerClientImpl::~ViewManagerClientImpl() { |
| 535 while (!nodes_.empty()) { | 141 while (!nodes_.empty()) { |
| 536 IdToNodeMap::iterator it = nodes_.begin(); | 142 IdToNodeMap::iterator it = nodes_.begin(); |
| 537 if (OwnsNode(it->second->id())) | 143 if (OwnsNode(it->second->id())) |
| 538 it->second->Destroy(); | 144 it->second->Destroy(); |
| 539 else | 145 else |
| 540 nodes_.erase(it); | 146 nodes_.erase(it); |
| 541 } | 147 } |
| 542 while (!views_.empty()) { | 148 while (!views_.empty()) { |
| 543 IdToViewMap::iterator it = views_.begin(); | 149 IdToViewMap::iterator it = views_.begin(); |
| 544 if (OwnsView(it->second->id())) | 150 if (OwnsView(it->second->id())) |
| 545 it->second->Destroy(); | 151 it->second->Destroy(); |
| 546 else | 152 else |
| 547 views_.erase(it); | 153 views_.erase(it); |
| 548 } | 154 } |
| 549 delegate_->OnViewManagerDisconnected(this); | 155 delegate_->OnViewManagerDisconnected(this); |
| 550 } | 156 } |
| 551 | 157 |
| 552 Id ViewManagerClientImpl::CreateNode() { | 158 Id ViewManagerClientImpl::CreateNode() { |
| 553 DCHECK(connected_); | 159 DCHECK(connected_); |
| 554 const Id node_id(MakeTransportId(connection_id_, ++next_id_)); | 160 const Id node_id = MakeTransportId(connection_id_, ++next_id_); |
| 555 pending_transactions_.push_back(new CreateNodeTransaction(node_id, this)); | 161 service_->CreateNode(node_id, ActionCompletedCallbackWithErrorCode()); |
| 556 Sync(); | |
| 557 return node_id; | 162 return node_id; |
| 558 } | 163 } |
| 559 | 164 |
| 560 void ViewManagerClientImpl::DestroyNode(Id node_id) { | 165 void ViewManagerClientImpl::DestroyNode(Id node_id) { |
| 561 DCHECK(connected_); | 166 DCHECK(connected_); |
| 562 pending_transactions_.push_back(new DestroyNodeTransaction(node_id, this)); | 167 service_->DeleteNode(node_id, ActionCompletedCallback()); |
| 563 Sync(); | |
| 564 } | 168 } |
| 565 | 169 |
| 566 Id ViewManagerClientImpl::CreateView() { | 170 Id ViewManagerClientImpl::CreateView() { |
| 567 DCHECK(connected_); | 171 DCHECK(connected_); |
| 568 const Id view_id(MakeTransportId(connection_id_, ++next_id_)); | 172 const Id view_id = MakeTransportId(connection_id_, ++next_id_); |
| 569 pending_transactions_.push_back(new CreateViewTransaction(view_id, this)); | 173 service_->CreateView(view_id, ActionCompletedCallback()); |
| 570 Sync(); | |
| 571 return view_id; | 174 return view_id; |
| 572 } | 175 } |
| 573 | 176 |
| 574 void ViewManagerClientImpl::DestroyView(Id view_id) { | 177 void ViewManagerClientImpl::DestroyView(Id view_id) { |
| 575 DCHECK(connected_); | 178 DCHECK(connected_); |
| 576 pending_transactions_.push_back(new DestroyViewTransaction(view_id, this)); | 179 service_->DeleteView(view_id, ActionCompletedCallback()); |
| 577 Sync(); | |
| 578 } | 180 } |
| 579 | 181 |
| 580 void ViewManagerClientImpl::AddChild(Id child_id, | 182 void ViewManagerClientImpl::AddChild(Id child_id, Id parent_id) { |
| 581 Id parent_id) { | |
| 582 DCHECK(connected_); | 183 DCHECK(connected_); |
| 583 pending_transactions_.push_back( | 184 service_->AddNode(parent_id, child_id, ActionCompletedCallback()); |
| 584 new AddChildTransaction(child_id, parent_id, this)); | |
| 585 Sync(); | |
| 586 } | 185 } |
| 587 | 186 |
| 588 void ViewManagerClientImpl::RemoveChild(Id child_id, Id parent_id) { | 187 void ViewManagerClientImpl::RemoveChild(Id child_id, Id parent_id) { |
| 589 DCHECK(connected_); | 188 DCHECK(connected_); |
| 590 pending_transactions_.push_back(new RemoveChildTransaction(child_id, this)); | 189 service_->RemoveNodeFromParent(child_id, ActionCompletedCallback()); |
| 591 Sync(); | |
| 592 } | 190 } |
| 593 | 191 |
| 594 void ViewManagerClientImpl::Reorder( | 192 void ViewManagerClientImpl::Reorder( |
| 595 Id node_id, | 193 Id node_id, |
| 596 Id relative_node_id, | 194 Id relative_node_id, |
| 597 OrderDirection direction) { | 195 OrderDirection direction) { |
| 598 DCHECK(connected_); | 196 DCHECK(connected_); |
| 599 pending_transactions_.push_back( | 197 service_->ReorderNode(node_id, relative_node_id, direction, |
| 600 new ReorderNodeTransaction(node_id, relative_node_id, direction, this)); | 198 ActionCompletedCallback()); |
| 601 Sync(); | |
| 602 } | 199 } |
| 603 | 200 |
| 604 bool ViewManagerClientImpl::OwnsNode(Id id) const { | 201 bool ViewManagerClientImpl::OwnsNode(Id id) const { |
| 605 return HiWord(id) == connection_id_; | 202 return HiWord(id) == connection_id_; |
| 606 } | 203 } |
| 607 | 204 |
| 608 bool ViewManagerClientImpl::OwnsView(Id id) const { | 205 bool ViewManagerClientImpl::OwnsView(Id id) const { |
| 609 return HiWord(id) == connection_id_; | 206 return HiWord(id) == connection_id_; |
| 610 } | 207 } |
| 611 | 208 |
| 612 void ViewManagerClientImpl::SetActiveView(Id node_id, Id view_id) { | 209 void ViewManagerClientImpl::SetActiveView(Id node_id, Id view_id) { |
| 613 DCHECK(connected_); | 210 DCHECK(connected_); |
| 614 pending_transactions_.push_back( | 211 service_->SetView(node_id, view_id, ActionCompletedCallback()); |
| 615 new SetActiveViewTransaction(node_id, view_id, this)); | |
| 616 Sync(); | |
| 617 } | 212 } |
| 618 | 213 |
| 619 void ViewManagerClientImpl::SetBounds(Id node_id, const gfx::Rect& bounds) { | 214 void ViewManagerClientImpl::SetBounds(Id node_id, const gfx::Rect& bounds) { |
| 620 DCHECK(connected_); | 215 DCHECK(connected_); |
| 621 pending_transactions_.push_back( | 216 service_->SetNodeBounds(node_id, Rect::From(bounds), |
| 622 new SetBoundsTransaction(node_id, bounds, this)); | 217 ActionCompletedCallback()); |
| 623 Sync(); | |
| 624 } | 218 } |
| 625 | 219 |
| 626 void ViewManagerClientImpl::SetViewContents(Id view_id, | 220 void ViewManagerClientImpl::SetViewContents(Id view_id, |
| 627 const SkBitmap& contents) { | 221 const SkBitmap& contents) { |
| 628 DCHECK(connected_); | 222 DCHECK(connected_); |
| 629 pending_transactions_.push_back( | 223 std::vector<unsigned char> data; |
| 630 new SetViewContentsTransaction(view_id, contents, this)); | 224 gfx::PNGCodec::EncodeBGRASkBitmap(contents, false, &data); |
| 631 Sync(); | 225 |
| 226 void* memory = NULL; |
| 227 ScopedSharedBufferHandle duped, shared_state_handle; |
| 228 bool result = CreateMapAndDupSharedBuffer(data.size(), |
| 229 &memory, |
| 230 &shared_state_handle, |
| 231 &duped); |
| 232 if (!result) |
| 233 return; |
| 234 |
| 235 memcpy(memory, &data[0], data.size()); |
| 236 |
| 237 service_->SetViewContents(view_id, duped.Pass(), |
| 238 static_cast<uint32_t>(data.size()), |
| 239 ActionCompletedCallback()); |
| 632 } | 240 } |
| 633 | 241 |
| 634 void ViewManagerClientImpl::SetFocus(Id node_id) { | 242 void ViewManagerClientImpl::SetFocus(Id node_id) { |
| 635 DCHECK(connected_); | 243 DCHECK(connected_); |
| 636 pending_transactions_.push_back(new SetFocusTransaction(node_id, this)); | 244 service_->SetFocus(node_id, ActionCompletedCallback()); |
| 637 Sync(); | |
| 638 } | 245 } |
| 639 | 246 |
| 640 void ViewManagerClientImpl::SetVisible(Id node_id, bool visible) { | 247 void ViewManagerClientImpl::SetVisible(Id node_id, bool visible) { |
| 641 DCHECK(connected_); | 248 DCHECK(connected_); |
| 642 pending_transactions_.push_back( | 249 service_->SetNodeVisibility(node_id, visible, ActionCompletedCallback()); |
| 643 new SetVisibleTransaction(node_id, visible, this)); | |
| 644 Sync(); | |
| 645 } | 250 } |
| 646 | 251 |
| 647 void ViewManagerClientImpl::Embed(const String& url, Id node_id) { | 252 void ViewManagerClientImpl::Embed(const String& url, Id node_id) { |
| 648 DCHECK(connected_); | 253 DCHECK(connected_); |
| 649 pending_transactions_.push_back( | 254 service_->Embed(url, node_id, ActionCompletedCallback()); |
| 650 new EmbedTransaction(url, node_id, this)); | |
| 651 Sync(); | |
| 652 } | 255 } |
| 653 | 256 |
| 654 void ViewManagerClientImpl::AddNode(Node* node) { | 257 void ViewManagerClientImpl::AddNode(Node* node) { |
| 655 DCHECK(nodes_.find(node->id()) == nodes_.end()); | 258 DCHECK(nodes_.find(node->id()) == nodes_.end()); |
| 656 nodes_[node->id()] = node; | 259 nodes_[node->id()] = node; |
| 657 } | 260 } |
| 658 | 261 |
| 659 void ViewManagerClientImpl::RemoveNode(Id node_id) { | 262 void ViewManagerClientImpl::RemoveNode(Id node_id) { |
| 660 IdToNodeMap::iterator it = nodes_.find(node_id); | 263 IdToNodeMap::iterator it = nodes_.find(node_id); |
| 661 if (it != nodes_.end()) | 264 if (it != nodes_.end()) |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 715 //////////////////////////////////////////////////////////////////////////////// | 318 //////////////////////////////////////////////////////////////////////////////// |
| 716 // ViewManagerClientImpl, ViewManagerClient implementation: | 319 // ViewManagerClientImpl, ViewManagerClient implementation: |
| 717 | 320 |
| 718 void ViewManagerClientImpl::OnViewManagerConnectionEstablished( | 321 void ViewManagerClientImpl::OnViewManagerConnectionEstablished( |
| 719 ConnectionSpecificId connection_id, | 322 ConnectionSpecificId connection_id, |
| 720 const String& creator_url, | 323 const String& creator_url, |
| 721 Array<NodeDataPtr> nodes) { | 324 Array<NodeDataPtr> nodes) { |
| 722 connected_ = true; | 325 connected_ = true; |
| 723 connection_id_ = connection_id; | 326 connection_id_ = connection_id; |
| 724 creator_url_ = TypeConverter<String, std::string>::ConvertFrom(creator_url); | 327 creator_url_ = TypeConverter<String, std::string>::ConvertFrom(creator_url); |
| 725 | |
| 726 DCHECK(pending_transactions_.empty()); | |
| 727 AddRoot(BuildNodeTree(this, nodes)); | 328 AddRoot(BuildNodeTree(this, nodes)); |
| 728 } | 329 } |
| 729 | 330 |
| 730 void ViewManagerClientImpl::OnRootAdded(Array<NodeDataPtr> nodes) { | 331 void ViewManagerClientImpl::OnRootAdded(Array<NodeDataPtr> nodes) { |
| 731 AddRoot(BuildNodeTree(this, nodes)); | 332 AddRoot(BuildNodeTree(this, nodes)); |
| 732 } | 333 } |
| 733 | 334 |
| 734 void ViewManagerClientImpl::OnNodeBoundsChanged(Id node_id, | 335 void ViewManagerClientImpl::OnNodeBoundsChanged(Id node_id, |
| 735 RectPtr old_bounds, | 336 RectPtr old_bounds, |
| 736 RectPtr new_bounds) { | 337 RectPtr new_bounds) { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 829 } | 430 } |
| 830 | 431 |
| 831 void ViewManagerClientImpl::DispatchOnViewInputEvent(Id view_id, | 432 void ViewManagerClientImpl::DispatchOnViewInputEvent(Id view_id, |
| 832 EventPtr event) { | 433 EventPtr event) { |
| 833 window_manager_delegate_->DispatchEvent(GetViewById(view_id), event.Pass()); | 434 window_manager_delegate_->DispatchEvent(GetViewById(view_id), event.Pass()); |
| 834 } | 435 } |
| 835 | 436 |
| 836 //////////////////////////////////////////////////////////////////////////////// | 437 //////////////////////////////////////////////////////////////////////////////// |
| 837 // ViewManagerClientImpl, private: | 438 // ViewManagerClientImpl, private: |
| 838 | 439 |
| 839 void ViewManagerClientImpl::Sync() { | |
| 840 // The service connection may not be set up yet. OnConnectionEstablished() | |
| 841 // will schedule another sync when it is. | |
| 842 if (!connected_) | |
| 843 return; | |
| 844 | |
| 845 Transactions::const_iterator it = pending_transactions_.begin(); | |
| 846 for (; it != pending_transactions_.end(); ++it) { | |
| 847 if (!(*it)->committed()) | |
| 848 (*it)->Commit(); | |
| 849 } | |
| 850 } | |
| 851 | |
| 852 void ViewManagerClientImpl::RemoveFromPendingQueue( | |
| 853 ViewManagerTransaction* transaction) { | |
| 854 DCHECK_EQ(transaction, pending_transactions_.front()); | |
| 855 pending_transactions_.erase(pending_transactions_.begin()); | |
| 856 if (pending_transactions_.empty() && !changes_acked_callback_.is_null()) | |
| 857 changes_acked_callback_.Run(); | |
| 858 } | |
| 859 | |
| 860 void ViewManagerClientImpl::AddRoot(Node* root) { | 440 void ViewManagerClientImpl::AddRoot(Node* root) { |
| 861 // A new root must not already exist as a root or be contained by an existing | 441 // A new root must not already exist as a root or be contained by an existing |
| 862 // hierarchy visible to this view manager. | 442 // hierarchy visible to this view manager. |
| 863 std::vector<Node*>::const_iterator it = roots_.begin(); | 443 std::vector<Node*>::const_iterator it = roots_.begin(); |
| 864 for (; it != roots_.end(); ++it) { | 444 for (; it != roots_.end(); ++it) { |
| 865 if (*it == root || (*it)->Contains(root)) | 445 if (*it == root || (*it)->Contains(root)) |
| 866 return; | 446 return; |
| 867 } | 447 } |
| 868 roots_.push_back(root); | 448 roots_.push_back(root); |
| 869 root->AddObserver(new RootObserver(root)); | 449 root->AddObserver(new RootObserver(root)); |
| 870 delegate_->OnRootAdded(this, root); | 450 delegate_->OnRootAdded(this, root); |
| 871 } | 451 } |
| 872 | 452 |
| 873 void ViewManagerClientImpl::RemoveRoot(Node* root) { | 453 void ViewManagerClientImpl::RemoveRoot(Node* root) { |
| 874 std::vector<Node*>::iterator it = | 454 std::vector<Node*>::iterator it = |
| 875 std::find(roots_.begin(), roots_.end(), root); | 455 std::find(roots_.begin(), roots_.end(), root); |
| 876 if (it != roots_.end()) | 456 if (it != roots_.end()) |
| 877 roots_.erase(it); | 457 roots_.erase(it); |
| 878 } | 458 } |
| 879 | 459 |
| 460 void ViewManagerClientImpl::OnActionCompleted(bool success) { |
| 461 if (!change_acked_callback_.is_null()) |
| 462 change_acked_callback_.Run(); |
| 463 } |
| 464 |
| 465 void ViewManagerClientImpl::OnActionCompletedWithErrorCode(ErrorCode code) { |
| 466 OnActionCompleted(code == ERROR_CODE_NONE); |
| 467 } |
| 468 |
| 469 base::Callback<void(bool)> ViewManagerClientImpl::ActionCompletedCallback() { |
| 470 return base::Bind(&ViewManagerClientImpl::OnActionCompleted, |
| 471 base::Unretained(this)); |
| 472 } |
| 473 |
| 474 base::Callback<void(ErrorCode)> |
| 475 ViewManagerClientImpl::ActionCompletedCallbackWithErrorCode() { |
| 476 return base::Bind(&ViewManagerClientImpl::OnActionCompletedWithErrorCode, |
| 477 base::Unretained(this)); |
| 478 } |
| 479 |
| 880 } // namespace view_manager | 480 } // namespace view_manager |
| 881 } // namespace mojo | 481 } // namespace mojo |
| OLD | NEW |