OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/browser/ui/webui/workers_ui.h" |
| 6 |
| 7 #include "base/json/json_writer.h" |
| 8 #include "base/memory/ref_counted_memory.h" |
| 9 #include "base/string_util.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/webui/chrome_url_data_manager_backend.h" |
| 13 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
| 14 #include "chrome/common/url_constants.h" |
| 15 #include "content/browser/tab_contents/tab_contents.h" |
| 16 #include "content/browser/worker_host/worker_process_host.h" |
| 17 #include "grit/generated_resources.h" |
| 18 #include "grit/workers_resources.h" |
| 19 #include "ui/base/resource/resource_bundle.h" |
| 20 |
| 21 static const char kWorkersDataFile[] = "workers_data.json"; |
| 22 static const char kIndexHtmlFile[] = "index.html"; |
| 23 |
| 24 namespace { |
| 25 |
| 26 class WorkersUIHTMLSource : public ChromeURLDataManager::DataSource { |
| 27 public: |
| 28 WorkersUIHTMLSource(); |
| 29 |
| 30 virtual void StartDataRequest(const std::string& path, |
| 31 bool is_incognito, |
| 32 int request_id); |
| 33 |
| 34 virtual std::string GetMimeType(const std::string&) const; |
| 35 |
| 36 private: |
| 37 ~WorkersUIHTMLSource() {} |
| 38 |
| 39 void SendSharedWorkersData(int request_id); |
| 40 |
| 41 DISALLOW_COPY_AND_ASSIGN(WorkersUIHTMLSource); |
| 42 }; |
| 43 |
| 44 WorkersUIHTMLSource::WorkersUIHTMLSource() |
| 45 : DataSource(chrome::kChromeUIWorkersHost, NULL) { |
| 46 } |
| 47 |
| 48 void WorkersUIHTMLSource::StartDataRequest(const std::string& path, |
| 49 bool is_incognito, |
| 50 int request_id) { |
| 51 if (path == kWorkersDataFile) { |
| 52 SendSharedWorkersData(request_id); |
| 53 } else { |
| 54 int idr = IDR_WORKERS_INDEX_HTML; |
| 55 scoped_refptr<RefCountedStaticMemory> response( |
| 56 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(idr)); |
| 57 SendResponse(request_id, response); |
| 58 } |
| 59 } |
| 60 |
| 61 std::string WorkersUIHTMLSource::GetMimeType(const std::string& path) const { |
| 62 if (EndsWith(path, ".css", false)) |
| 63 return "text/css"; |
| 64 if (EndsWith(path, ".js", false)) |
| 65 return "application/javascript"; |
| 66 if (EndsWith(path, ".json", false)) |
| 67 return "plain/text"; |
| 68 return "text/html"; |
| 69 } |
| 70 |
| 71 void WorkersUIHTMLSource::SendSharedWorkersData(int request_id) { |
| 72 ListValue workers_list; |
| 73 BrowserChildProcessHost::Iterator iter(ChildProcessInfo::WORKER_PROCESS); |
| 74 for (; !iter.Done(); ++iter) { |
| 75 WorkerProcessHost* worker = static_cast<WorkerProcessHost*>(*iter); |
| 76 const WorkerProcessHost::Instances& instances = worker->instances(); |
| 77 for (WorkerProcessHost::Instances::const_iterator i = instances.begin(); |
| 78 i != instances.end(); ++i) { |
| 79 if (!i->shared()) |
| 80 continue; |
| 81 DictionaryValue* worker_data = new DictionaryValue(); |
| 82 worker_data->SetInteger("id", i->worker_route_id()); |
| 83 worker_data->SetString("url", i->url().spec()); |
| 84 worker_data->SetString("name", i->name()); |
| 85 worker_data->SetInteger("pid", worker->pid()); |
| 86 workers_list.Append(worker_data); |
| 87 } |
| 88 } |
| 89 |
| 90 std::string json_string; |
| 91 base::JSONWriter::Write(&workers_list, false, &json_string); |
| 92 |
| 93 scoped_refptr<RefCountedBytes> json_bytes(new RefCountedBytes()); |
| 94 json_bytes->data.resize(json_string.size()); |
| 95 std::copy(json_string.begin(), json_string.end(), json_bytes->data.begin()); |
| 96 |
| 97 SendResponse(request_id, json_bytes); |
| 98 } |
| 99 |
| 100 } // namespace |
| 101 |
| 102 WorkersUI::WorkersUI(TabContents* contents) : ChromeWebUI(contents) { |
| 103 WorkersUIHTMLSource* html_source = new WorkersUIHTMLSource(); |
| 104 |
| 105 // Set up the chrome://workers/ source. |
| 106 contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source); |
| 107 } |
OLD | NEW |