Chromium Code Reviews| 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 "chrome/browser/devtools/devtools_ui_bindings.h" | 5 #include "chrome/browser/devtools/devtools_ui_bindings.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 | 72 |
| 73 static const char kDevToolsActionTakenHistogram[] = "DevTools.ActionTaken"; | 73 static const char kDevToolsActionTakenHistogram[] = "DevTools.ActionTaken"; |
| 74 static const int kDevToolsActionTakenBoundary = 100; | 74 static const int kDevToolsActionTakenBoundary = 100; |
| 75 static const char kDevToolsPanelShownHistogram[] = "DevTools.PanelShown"; | 75 static const char kDevToolsPanelShownHistogram[] = "DevTools.PanelShown"; |
| 76 static const int kDevToolsPanelShownBoundary = 20; | 76 static const int kDevToolsPanelShownBoundary = 20; |
| 77 | 77 |
| 78 // This constant should be in sync with | 78 // This constant should be in sync with |
| 79 // the constant at shell_devtools_frontend.cc. | 79 // the constant at shell_devtools_frontend.cc. |
| 80 const size_t kMaxMessageChunkSize = IPC::Channel::kMaximumMessageSize / 4; | 80 const size_t kMaxMessageChunkSize = IPC::Channel::kMaximumMessageSize / 4; |
| 81 | 81 |
| 82 typedef std::vector<DevToolsUIBindings*> DevToolsUIBindingsList; | 82 using DevToolsUIBindingsList = std::vector<DevToolsUIBindings*>; |
| 83 base::LazyInstance<DevToolsUIBindingsList>::Leaky g_instances = | 83 base::LazyInstance<DevToolsUIBindingsList>::Leaky g_instances = |
| 84 LAZY_INSTANCE_INITIALIZER; | 84 LAZY_INSTANCE_INITIALIZER; |
| 85 | 85 |
| 86 using StreamOwners = std::map<int, DevToolsUIBindings*>; | |
| 87 base::LazyInstance<StreamOwners>::Leaky g_stream_owners = | |
| 88 LAZY_INSTANCE_INITIALIZER; | |
| 89 | |
| 86 std::string SkColorToRGBAString(SkColor color) { | 90 std::string SkColorToRGBAString(SkColor color) { |
| 87 // We avoid StringPrintf because it will use locale specific formatters for | 91 // We avoid StringPrintf because it will use locale specific formatters for |
| 88 // the double (e.g. ',' instead of '.' in German). | 92 // the double (e.g. ',' instead of '.' in German). |
| 89 return "rgba(" + base::IntToString(SkColorGetR(color)) + "," + | 93 return "rgba(" + base::IntToString(SkColorGetR(color)) + "," + |
| 90 base::IntToString(SkColorGetG(color)) + "," + | 94 base::IntToString(SkColorGetG(color)) + "," + |
| 91 base::IntToString(SkColorGetB(color)) + "," + | 95 base::IntToString(SkColorGetB(color)) + "," + |
| 92 base::DoubleToString(SkColorGetA(color) / 255.0) + ")"; | 96 base::DoubleToString(SkColorGetA(color) / 255.0) + ")"; |
| 93 } | 97 } |
| 94 | 98 |
| 95 base::DictionaryValue* CreateFileSystemValue( | 99 base::DictionaryValue* CreateFileSystemValue( |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 } | 239 } |
| 236 | 240 |
| 237 InfoBarService* DefaultBindingsDelegate::GetInfoBarService() { | 241 InfoBarService* DefaultBindingsDelegate::GetInfoBarService() { |
| 238 return InfoBarService::FromWebContents(web_contents_); | 242 return InfoBarService::FromWebContents(web_contents_); |
| 239 } | 243 } |
| 240 | 244 |
| 241 // ResponseWriter ------------------------------------------------------------- | 245 // ResponseWriter ------------------------------------------------------------- |
| 242 | 246 |
| 243 class ResponseWriter : public net::URLFetcherResponseWriter { | 247 class ResponseWriter : public net::URLFetcherResponseWriter { |
| 244 public: | 248 public: |
| 245 ResponseWriter(DevToolsUIBindings* bindings, int stream_id); | 249 explicit ResponseWriter(int stream_id); |
| 246 ~ResponseWriter() override; | 250 ~ResponseWriter() override; |
| 247 | 251 |
| 248 // URLFetcherResponseWriter overrides: | 252 // URLFetcherResponseWriter overrides: |
| 249 int Initialize(const net::CompletionCallback& callback) override; | 253 int Initialize(const net::CompletionCallback& callback) override; |
| 250 int Write(net::IOBuffer* buffer, | 254 int Write(net::IOBuffer* buffer, |
| 251 int num_bytes, | 255 int num_bytes, |
| 252 const net::CompletionCallback& callback) override; | 256 const net::CompletionCallback& callback) override; |
| 253 int Finish(const net::CompletionCallback& callback) override; | 257 int Finish(const net::CompletionCallback& callback) override; |
| 254 | 258 |
| 255 private: | 259 private: |
| 256 DevToolsUIBindings* bindings_; | |
| 257 int stream_id_; | 260 int stream_id_; |
| 258 | 261 |
| 259 DISALLOW_COPY_AND_ASSIGN(ResponseWriter); | 262 DISALLOW_COPY_AND_ASSIGN(ResponseWriter); |
| 260 }; | 263 }; |
| 261 | 264 |
| 262 ResponseWriter::ResponseWriter(DevToolsUIBindings* bindings, | 265 ResponseWriter::ResponseWriter(int stream_id) |
| 263 int stream_id) | 266 : stream_id_(stream_id) { |
| 264 : bindings_(bindings), | |
| 265 stream_id_(stream_id) { | |
| 266 } | 267 } |
| 267 | 268 |
| 268 ResponseWriter::~ResponseWriter() { | 269 ResponseWriter::~ResponseWriter() { |
| 269 } | 270 } |
| 270 | 271 |
| 271 int ResponseWriter::Initialize(const net::CompletionCallback& callback) { | 272 int ResponseWriter::Initialize(const net::CompletionCallback& callback) { |
| 272 return net::OK; | 273 return net::OK; |
| 273 } | 274 } |
| 274 | 275 |
| 276 static void WriteResponseOnUI(int stream_id, const std::string& chunk) { | |
| 277 auto it = g_stream_owners.Get().find(stream_id); | |
| 278 if (it != g_stream_owners.Get().end()) { | |
| 279 base::FundamentalValue id(stream_id); | |
| 280 base::StringValue value(chunk); | |
| 281 it->second->CallClientFunction( | |
| 282 "DevToolsAPI.streamWrite", &id, &value, nullptr); | |
| 283 } | |
| 284 } | |
| 285 | |
| 275 int ResponseWriter::Write(net::IOBuffer* buffer, | 286 int ResponseWriter::Write(net::IOBuffer* buffer, |
| 276 int num_bytes, | 287 int num_bytes, |
| 277 const net::CompletionCallback& callback) { | 288 const net::CompletionCallback& callback) { |
| 278 base::FundamentalValue id(stream_id_); | 289 content::BrowserThread::PostTask( |
| 279 base::StringValue chunk(std::string(buffer->data(), num_bytes)); | 290 content::BrowserThread::UI, FROM_HERE, |
| 280 bindings_->CallClientFunction( | 291 base::Bind(&WriteResponseOnUI, stream_id_, |
| 281 "DevToolsAPI.streamWrite", &id, &chunk, nullptr); | 292 std::string(buffer->data(), num_bytes))); |
| 282 return num_bytes; | 293 return num_bytes; |
| 283 } | 294 } |
| 284 | 295 |
| 296 static void FinishResponseOnUI(int stream_id) { | |
| 297 auto it = g_stream_owners.Get().find(stream_id); | |
| 298 if (it != g_stream_owners.Get().end()) { | |
| 299 base::FundamentalValue id(stream_id); | |
| 300 it->second->CallClientFunction( | |
| 301 "DevToolsAPI.streamFinish", &id, nullptr, nullptr); | |
| 302 } | |
| 303 } | |
| 304 | |
| 285 int ResponseWriter::Finish(const net::CompletionCallback& callback) { | 305 int ResponseWriter::Finish(const net::CompletionCallback& callback) { |
| 286 base::FundamentalValue id(stream_id_); | 306 content::BrowserThread::PostTask( |
| 287 bindings_->CallClientFunction( | 307 content::BrowserThread::UI, FROM_HERE, |
| 288 "DevToolsAPI.streamFinish", &id, nullptr, nullptr); | 308 base::Bind(&FinishResponseOnUI, stream_id_)); |
| 289 return net::OK; | 309 return net::OK; |
| 290 } | 310 } |
| 291 | 311 |
| 292 } // namespace | 312 } // namespace |
| 293 | 313 |
| 294 // DevToolsUIBindings::FrontendWebContentsObserver ---------------------------- | 314 // DevToolsUIBindings::FrontendWebContentsObserver ---------------------------- |
| 295 | 315 |
| 296 class DevToolsUIBindings::FrontendWebContentsObserver | 316 class DevToolsUIBindings::FrontendWebContentsObserver |
| 297 : public content::WebContentsObserver { | 317 : public content::WebContentsObserver { |
| 298 public: | 318 public: |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 435 DevToolsEmbedderMessageDispatcher::CreateForDevToolsFrontend(this)); | 455 DevToolsEmbedderMessageDispatcher::CreateForDevToolsFrontend(this)); |
| 436 | 456 |
| 437 frontend_host_.reset(content::DevToolsFrontendHost::Create( | 457 frontend_host_.reset(content::DevToolsFrontendHost::Create( |
| 438 web_contents_->GetMainFrame(), this)); | 458 web_contents_->GetMainFrame(), this)); |
| 439 } | 459 } |
| 440 | 460 |
| 441 DevToolsUIBindings::~DevToolsUIBindings() { | 461 DevToolsUIBindings::~DevToolsUIBindings() { |
| 442 for (const auto& pair : pending_requests_) | 462 for (const auto& pair : pending_requests_) |
| 443 delete pair.first; | 463 delete pair.first; |
| 444 | 464 |
| 465 for (auto it = g_stream_owners.Get().begin(); | |
| 466 it != g_stream_owners.Get().end(); ) { | |
| 467 if (it->second == this) | |
| 468 g_stream_owners.Get().erase(it); | |
| 469 else | |
| 470 ++it; | |
| 471 } | |
| 472 | |
| 445 if (agent_host_.get()) | 473 if (agent_host_.get()) |
| 446 agent_host_->DetachClient(); | 474 agent_host_->DetachClient(); |
| 447 | 475 |
| 448 for (IndexingJobsMap::const_iterator jobs_it(indexing_jobs_.begin()); | 476 for (IndexingJobsMap::const_iterator jobs_it(indexing_jobs_.begin()); |
| 449 jobs_it != indexing_jobs_.end(); ++jobs_it) { | 477 jobs_it != indexing_jobs_.end(); ++jobs_it) { |
| 450 jobs_it->second->Stop(); | 478 jobs_it->second->Stop(); |
| 451 } | 479 } |
| 452 indexing_jobs_.clear(); | 480 indexing_jobs_.clear(); |
| 453 SetDeviceCountUpdatesEnabled(0, false); | 481 SetDeviceCountUpdatesEnabled(0, false); |
| 454 SetDevicesUpdatesEnabled(0, false); | 482 SetDevicesUpdatesEnabled(0, false); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 574 const std::string& headers, | 602 const std::string& headers, |
| 575 int stream_id) { | 603 int stream_id) { |
| 576 GURL gurl(url); | 604 GURL gurl(url); |
| 577 if (!gurl.is_valid()) { | 605 if (!gurl.is_valid()) { |
| 578 base::DictionaryValue response; | 606 base::DictionaryValue response; |
| 579 response.SetInteger("statusCode", 404); | 607 response.SetInteger("statusCode", 404); |
| 580 SendMessageAck(request_id, &response); | 608 SendMessageAck(request_id, &response); |
| 581 return; | 609 return; |
| 582 } | 610 } |
| 583 | 611 |
| 612 g_stream_owners.Get()[stream_id] = this; | |
|
dgozman
2015/02/28 16:06:13
This will make streams from different DevTools cla
| |
| 613 | |
| 584 net::URLFetcher* fetcher = | 614 net::URLFetcher* fetcher = |
| 585 net::URLFetcher::Create(gurl, net::URLFetcher::GET, this); | 615 net::URLFetcher::Create(gurl, net::URLFetcher::GET, this); |
| 586 pending_requests_[fetcher] = request_id; | 616 pending_requests_[fetcher] = std::make_pair(request_id, stream_id); |
| 587 fetcher->SetRequestContext(profile_->GetRequestContext()); | 617 fetcher->SetRequestContext(profile_->GetRequestContext()); |
| 588 fetcher->SetExtraRequestHeaders(headers); | 618 fetcher->SetExtraRequestHeaders(headers); |
| 589 fetcher->SaveResponseWithWriter(scoped_ptr<net::URLFetcherResponseWriter>( | 619 fetcher->SaveResponseWithWriter(scoped_ptr<net::URLFetcherResponseWriter>( |
| 590 new ResponseWriter(this, stream_id))); | 620 new ResponseWriter(stream_id))); |
| 591 fetcher->Start(); | 621 fetcher->Start(); |
| 592 } | 622 } |
| 593 | 623 |
| 594 void DevToolsUIBindings::OpenInNewTab(int request_id, const std::string& url) { | 624 void DevToolsUIBindings::OpenInNewTab(int request_id, const std::string& url) { |
| 595 delegate_->OpenInNewTab(url); | 625 delegate_->OpenInNewTab(url); |
| 596 } | 626 } |
| 597 | 627 |
| 598 void DevToolsUIBindings::SaveToFile(int request_id, | 628 void DevToolsUIBindings::SaveToFile(int request_id, |
| 599 const std::string& url, | 629 const std::string& url, |
| 600 const std::string& content, | 630 const std::string& content, |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 796 net::HttpResponseHeaders* rh = source->GetResponseHeaders(); | 826 net::HttpResponseHeaders* rh = source->GetResponseHeaders(); |
| 797 response.SetInteger("statusCode", rh ? rh->response_code() : 200); | 827 response.SetInteger("statusCode", rh ? rh->response_code() : 200); |
| 798 response.Set("headers", headers); | 828 response.Set("headers", headers); |
| 799 | 829 |
| 800 void* iterator = NULL; | 830 void* iterator = NULL; |
| 801 std::string name; | 831 std::string name; |
| 802 std::string value; | 832 std::string value; |
| 803 while (rh && rh->EnumerateHeaderLines(&iterator, &name, &value)) | 833 while (rh && rh->EnumerateHeaderLines(&iterator, &name, &value)) |
| 804 headers->SetString(name, value); | 834 headers->SetString(name, value); |
| 805 | 835 |
| 806 SendMessageAck(it->second, &response); | 836 int request_id = it->second.first; |
| 837 int stream_id = it->second.second; | |
| 838 SendMessageAck(request_id, &response); | |
| 839 g_stream_owners.Get().erase(stream_id); | |
| 807 pending_requests_.erase(it); | 840 pending_requests_.erase(it); |
| 808 delete source; | 841 delete source; |
| 809 } | 842 } |
| 810 | 843 |
| 811 void DevToolsUIBindings::DeviceCountChanged(int count) { | 844 void DevToolsUIBindings::DeviceCountChanged(int count) { |
| 812 base::FundamentalValue value(count); | 845 base::FundamentalValue value(count); |
| 813 CallClientFunction("DevToolsAPI.deviceCountUpdated", &value, NULL, | 846 CallClientFunction("DevToolsAPI.deviceCountUpdated", &value, NULL, |
| 814 NULL); | 847 NULL); |
| 815 } | 848 } |
| 816 | 849 |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1023 if (frontend_loaded_) | 1056 if (frontend_loaded_) |
| 1024 return; | 1057 return; |
| 1025 frontend_loaded_ = true; | 1058 frontend_loaded_ = true; |
| 1026 | 1059 |
| 1027 // Call delegate first - it seeds importants bit of information. | 1060 // Call delegate first - it seeds importants bit of information. |
| 1028 delegate_->OnLoadCompleted(); | 1061 delegate_->OnLoadCompleted(); |
| 1029 | 1062 |
| 1030 UpdateTheme(); | 1063 UpdateTheme(); |
| 1031 AddDevToolsExtensionsToClient(); | 1064 AddDevToolsExtensionsToClient(); |
| 1032 } | 1065 } |
| OLD | NEW |