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) const; | |
41 | |
42 // Callback function of the request for all gcm related infos. | |
43 void RequestGCMStatisticsFinished( | |
44 const gcm::GCMClient::GCMStatistics& args) const; | |
45 | |
46 DISALLOW_COPY_AND_ASSIGN(GcmInternalsUIMessageHandler); | |
47 }; | |
48 | |
49 GcmInternalsUIMessageHandler::GcmInternalsUIMessageHandler() {} | |
50 | |
51 GcmInternalsUIMessageHandler::~GcmInternalsUIMessageHandler() {} | |
52 | |
53 void GcmInternalsUIMessageHandler::ReturnResults( | |
54 Profile* profile, | |
55 gcm::GCMProfileService* profile_service, | |
56 const gcm::GCMClient::GCMStatistics* stats) const { | |
57 base::DictionaryValue results; | |
58 base::DictionaryValue* device_info = new base::DictionaryValue(); | |
59 results.Set("deviceInfo", device_info); | |
60 | |
61 device_info->SetBoolean("userProfileExists", profile != NULL); | |
jianli
2014/03/04 23:47:39
Do we really need this? I think profile always exi
juyik
2014/03/05 05:08:08
Done.
| |
62 device_info->SetBoolean("profileServiceCreated", profile_service != NULL); | |
63 if (profile) { | |
64 device_info->SetString("gcmEnabledState", | |
65 gcm::GCMProfileService::GetGCMEnabledStateString( | |
66 gcm::GCMProfileService::GetGCMEnabledState(profile))); | |
67 } | |
68 if (profile_service) { | |
69 device_info->SetBoolean("userSignedIn", profile_service->IsSignedIn()); | |
70 device_info->SetBoolean("gcmClientReady", | |
71 profile_service->IsGCMClientReady()); | |
72 } | |
73 if (stats) { | |
74 device_info->SetBoolean("gcmClientCreated", stats->gcm_client_created); | |
75 device_info->SetString("gcmClientState", stats->gcm_client_state); | |
76 device_info->SetBoolean("connectionClientCreated", | |
77 stats->connection_client_created); | |
78 if (stats->connection_client_created) | |
79 device_info->SetString("connectionState", stats->connection_state); | |
80 } | |
81 web_ui()->CallJavascriptFunction("gcm_internals.setGcmInternalsInfo", | |
82 results); | |
83 } | |
84 | |
85 void GcmInternalsUIMessageHandler::RequestAllInfo( | |
86 const base::ListValue* args) const { | |
87 Profile* profile = Profile::FromWebUI(web_ui()); | |
88 gcm::GCMProfileService* profile_service = NULL; | |
89 if (profile) | |
jianli
2014/03/04 23:47:39
Can profile be NULL?
juyik
2014/03/05 05:08:08
Done.
| |
90 profile_service = gcm::GCMProfileServiceFactory::GetForProfile(profile); | |
jianli
2014/03/04 23:47:39
This will always create GCMProfileService instance
juyik
2014/03/05 05:08:08
For incognito window it is null. I tested this on
| |
91 | |
92 if (!profile_service) { | |
93 ReturnResults(profile, NULL, NULL); | |
94 } else { | |
95 profile_service->RequestGCMStatistics(base::Bind( | |
96 &GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished, | |
97 base::Unretained(this))); | |
jianli
2014/03/04 23:47:39
Is this safe? What if the internal page is closed
juyik
2014/03/05 05:08:08
Done.
| |
98 } | |
99 } | |
100 | |
101 void GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished( | |
102 const gcm::GCMClient::GCMStatistics& stats) const { | |
103 Profile* profile = Profile::FromWebUI(web_ui()); | |
104 DCHECK(profile); | |
105 gcm::GCMProfileService* profile_service = | |
106 gcm::GCMProfileServiceFactory::GetForProfile(profile); | |
107 DCHECK(profile_service); | |
108 ReturnResults(profile, profile_service, &stats); | |
109 } | |
110 | |
111 void GcmInternalsUIMessageHandler::RegisterMessages() { | |
112 web_ui()->RegisterMessageCallback( | |
113 "getGcmInternalsInfo", | |
114 base::Bind(&GcmInternalsUIMessageHandler::RequestAllInfo, | |
115 base::Unretained(this))); | |
116 } | |
117 | |
118 } // namespace | |
119 | |
12 GCMInternalsUI::GCMInternalsUI(content::WebUI* web_ui) | 120 GCMInternalsUI::GCMInternalsUI(content::WebUI* web_ui) |
13 : content::WebUIController(web_ui) { | 121 : content::WebUIController(web_ui) { |
14 // Set up the chrome://gcm-internals source. | 122 // Set up the chrome://gcm-internals source. |
15 content::WebUIDataSource* html_source = | 123 content::WebUIDataSource* html_source = |
16 content::WebUIDataSource::Create(chrome::kChromeUIGCMInternalsHost); | 124 content::WebUIDataSource::Create(chrome::kChromeUIGCMInternalsHost); |
17 html_source->SetUseJsonJSFormatV2(); | 125 html_source->SetUseJsonJSFormatV2(); |
18 | 126 |
19 html_source->SetJsonPath("strings.js"); | 127 html_source->SetJsonPath("strings.js"); |
20 | 128 |
21 // Add required resources. | 129 // Add required resources. |
22 html_source->AddResourcePath("gcm_internals.css", IDR_GCM_INTERNALS_CSS); | 130 html_source->AddResourcePath("gcm_internals.css", IDR_GCM_INTERNALS_CSS); |
23 html_source->AddResourcePath("gcm_internals.js", IDR_GCM_INTERNALS_JS); | 131 html_source->AddResourcePath("gcm_internals.js", IDR_GCM_INTERNALS_JS); |
24 html_source->SetDefaultResource(IDR_GCM_INTERNALS_HTML); | 132 html_source->SetDefaultResource(IDR_GCM_INTERNALS_HTML); |
25 | 133 |
26 Profile* profile = Profile::FromWebUI(web_ui); | 134 Profile* profile = Profile::FromWebUI(web_ui); |
27 content::WebUIDataSource::Add(profile, html_source); | 135 content::WebUIDataSource::Add(profile, html_source); |
136 | |
137 web_ui->AddMessageHandler(new GcmInternalsUIMessageHandler()); | |
28 } | 138 } |
29 | 139 |
30 GCMInternalsUI::~GCMInternalsUI() {} | 140 GCMInternalsUI::~GCMInternalsUI() {} |
OLD | NEW |