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