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/ui/webui/gcm_internals_ui.h" | 5 #include "chrome/browser/ui/webui/gcm_internals_ui.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/format_macros.h" |
| 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/values.h" |
7 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
| 15 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
8 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
| 17 #include "content/public/browser/web_ui.h" |
| 18 #include "content/public/browser/web_ui_controller.h" |
9 #include "content/public/browser/web_ui_data_source.h" | 19 #include "content/public/browser/web_ui_data_source.h" |
| 20 #include "content/public/browser/web_ui_message_handler.h" |
| 21 #include "google_apis/gcm/gcm_client.h" |
10 #include "grit/browser_resources.h" | 22 #include "grit/browser_resources.h" |
11 | 23 |
| 24 namespace { |
| 25 |
| 26 // Class acting as a controller of the chrome://gcm-internals WebUI. |
| 27 class GcmInternalsUIMessageHandler : public content::WebUIMessageHandler { |
| 28 public: |
| 29 GcmInternalsUIMessageHandler(); |
| 30 virtual ~GcmInternalsUIMessageHandler(); |
| 31 |
| 32 // WebUIMessageHandler implementation. |
| 33 virtual void RegisterMessages() OVERRIDE; |
| 34 |
| 35 private: |
| 36 // Return all of the GCM related infos to the gcm-internals page by calling |
| 37 // Javascript callback function |
| 38 // |gcm-internals.returnInfo()|. |
| 39 void ReturnResults(Profile* profile, gcm::GCMProfileService* profile_service, |
| 40 const gcm::GCMClient::GCMStatistics* stats) const; |
| 41 |
| 42 // Request all of the GCM related infos through gcm profile service. |
| 43 void RequestAllInfo(const base::ListValue* args); |
| 44 |
| 45 // Callback function of the request for all gcm related infos. |
| 46 void RequestGCMStatisticsFinished( |
| 47 const gcm::GCMClient::GCMStatistics& args) const; |
| 48 |
| 49 // Factory for creating references in callbacks. |
| 50 base::WeakPtrFactory<GcmInternalsUIMessageHandler> weak_ptr_factory_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(GcmInternalsUIMessageHandler); |
| 53 }; |
| 54 |
| 55 GcmInternalsUIMessageHandler::GcmInternalsUIMessageHandler() |
| 56 : weak_ptr_factory_(this) {} |
| 57 |
| 58 GcmInternalsUIMessageHandler::~GcmInternalsUIMessageHandler() {} |
| 59 |
| 60 void GcmInternalsUIMessageHandler::ReturnResults( |
| 61 Profile* profile, |
| 62 gcm::GCMProfileService* profile_service, |
| 63 const gcm::GCMClient::GCMStatistics* stats) const { |
| 64 base::DictionaryValue results; |
| 65 base::DictionaryValue* device_info = new base::DictionaryValue(); |
| 66 results.Set("deviceInfo", device_info); |
| 67 |
| 68 device_info->SetBoolean("profileServiceCreated", profile_service != NULL); |
| 69 device_info->SetString("gcmEnabledState", |
| 70 gcm::GCMProfileService::GetGCMEnabledStateString( |
| 71 gcm::GCMProfileService::GetGCMEnabledState(profile))); |
| 72 if (profile_service) { |
| 73 device_info->SetString("signedInUserName", |
| 74 profile_service->SignedInUserName()); |
| 75 device_info->SetBoolean("gcmClientReady", |
| 76 profile_service->IsGCMClientReady()); |
| 77 } |
| 78 if (stats) { |
| 79 device_info->SetBoolean("gcmClientCreated", stats->gcm_client_created); |
| 80 device_info->SetString("gcmClientState", stats->gcm_client_state); |
| 81 device_info->SetBoolean("connectionClientCreated", |
| 82 stats->connection_client_created); |
| 83 if (stats->connection_client_created) |
| 84 device_info->SetString("connectionState", stats->connection_state); |
| 85 if (stats->android_id > 0) { |
| 86 device_info->SetString("androidId", |
| 87 base::StringPrintf("0x%" PRIx64, stats->android_id)); |
| 88 } |
| 89 } |
| 90 web_ui()->CallJavascriptFunction("gcmInternals.setGcmInternalsInfo", |
| 91 results); |
| 92 } |
| 93 |
| 94 void GcmInternalsUIMessageHandler::RequestAllInfo( |
| 95 const base::ListValue* args) { |
| 96 Profile* profile = Profile::FromWebUI(web_ui()); |
| 97 gcm::GCMProfileService* profile_service = |
| 98 gcm::GCMProfileServiceFactory::GetForProfile(profile); |
| 99 |
| 100 if (!profile_service) { |
| 101 ReturnResults(profile, NULL, NULL); |
| 102 } else { |
| 103 profile_service->RequestGCMStatistics(base::Bind( |
| 104 &GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished, |
| 105 weak_ptr_factory_.GetWeakPtr())); |
| 106 } |
| 107 } |
| 108 |
| 109 void GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished( |
| 110 const gcm::GCMClient::GCMStatistics& stats) const { |
| 111 Profile* profile = Profile::FromWebUI(web_ui()); |
| 112 DCHECK(profile); |
| 113 gcm::GCMProfileService* profile_service = |
| 114 gcm::GCMProfileServiceFactory::GetForProfile(profile); |
| 115 DCHECK(profile_service); |
| 116 ReturnResults(profile, profile_service, &stats); |
| 117 } |
| 118 |
| 119 void GcmInternalsUIMessageHandler::RegisterMessages() { |
| 120 web_ui()->RegisterMessageCallback( |
| 121 "getGcmInternalsInfo", |
| 122 base::Bind(&GcmInternalsUIMessageHandler::RequestAllInfo, |
| 123 base::Unretained(this))); |
| 124 } |
| 125 |
| 126 } // namespace |
| 127 |
12 GCMInternalsUI::GCMInternalsUI(content::WebUI* web_ui) | 128 GCMInternalsUI::GCMInternalsUI(content::WebUI* web_ui) |
13 : content::WebUIController(web_ui) { | 129 : content::WebUIController(web_ui) { |
14 // Set up the chrome://gcm-internals source. | 130 // Set up the chrome://gcm-internals source. |
15 content::WebUIDataSource* html_source = | 131 content::WebUIDataSource* html_source = |
16 content::WebUIDataSource::Create(chrome::kChromeUIGCMInternalsHost); | 132 content::WebUIDataSource::Create(chrome::kChromeUIGCMInternalsHost); |
17 html_source->SetUseJsonJSFormatV2(); | 133 html_source->SetUseJsonJSFormatV2(); |
18 | 134 |
19 html_source->SetJsonPath("strings.js"); | 135 html_source->SetJsonPath("strings.js"); |
20 | 136 |
21 // Add required resources. | 137 // Add required resources. |
22 html_source->AddResourcePath("gcm_internals.css", IDR_GCM_INTERNALS_CSS); | 138 html_source->AddResourcePath("gcm_internals.css", IDR_GCM_INTERNALS_CSS); |
23 html_source->AddResourcePath("gcm_internals.js", IDR_GCM_INTERNALS_JS); | 139 html_source->AddResourcePath("gcm_internals.js", IDR_GCM_INTERNALS_JS); |
24 html_source->SetDefaultResource(IDR_GCM_INTERNALS_HTML); | 140 html_source->SetDefaultResource(IDR_GCM_INTERNALS_HTML); |
25 | 141 |
26 Profile* profile = Profile::FromWebUI(web_ui); | 142 Profile* profile = Profile::FromWebUI(web_ui); |
27 content::WebUIDataSource::Add(profile, html_source); | 143 content::WebUIDataSource::Add(profile, html_source); |
| 144 |
| 145 web_ui->AddMessageHandler(new GcmInternalsUIMessageHandler()); |
28 } | 146 } |
29 | 147 |
30 GCMInternalsUI::~GCMInternalsUI() {} | 148 GCMInternalsUI::~GCMInternalsUI() {} |
OLD | NEW |