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/strings/stringprintf.h" | |
10 #include "base/values.h" | |
7 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/services/gcm/gcm_profile_service.h" | |
13 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | |
8 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
15 #include "content/public/browser/web_ui.h" | |
16 #include "content/public/browser/web_ui_controller.h" | |
9 #include "content/public/browser/web_ui_data_source.h" | 17 #include "content/public/browser/web_ui_data_source.h" |
18 #include "content/public/browser/web_ui_message_handler.h" | |
19 #include "google_apis/gcm/gcm_client.h" | |
10 #include "grit/browser_resources.h" | 20 #include "grit/browser_resources.h" |
11 | 21 |
22 namespace { | |
23 | |
24 // Class acting as a controller of the chrome://gcm-internals WebUI. | |
25 class GcmInternalsUIMessageHandler : public content::WebUIMessageHandler { | |
26 public: | |
27 GcmInternalsUIMessageHandler(); | |
28 virtual ~GcmInternalsUIMessageHandler(); | |
29 | |
30 // WebUIMessageHandler implementation. | |
31 virtual void RegisterMessages() OVERRIDE; | |
32 | |
33 private: | |
34 // Return all of the GCM related infos to the gcm-internals page by calling | |
35 // Javascript callback function | |
36 // |gcm-internals.returnInfo()|. | |
37 void ReturnResults(Profile* profile, gcm::GCMProfileService* profile_service, | |
38 const gcm::GCMClient::GCMStatistics* stats) const; | |
39 | |
40 // Request all of the GCM related infos through gcm profile service. | |
41 void RequestAllInfo(const base::ListValue* args); | |
42 | |
43 // Callback function of the request for all gcm related infos. | |
44 void RequestGCMStatisticsFinished( | |
45 const gcm::GCMClient::GCMStatistics& args) const; | |
46 | |
47 // Factory for creating references in callbacks. | |
48 base::WeakPtrFactory<GcmInternalsUIMessageHandler> weak_ptr_factory_; | |
jianli
2014/03/06 01:55:21
nit: please include header file for this
juyik
2014/03/06 02:23:59
Done.
| |
49 | |
50 DISALLOW_COPY_AND_ASSIGN(GcmInternalsUIMessageHandler); | |
51 }; | |
52 | |
53 GcmInternalsUIMessageHandler::GcmInternalsUIMessageHandler() | |
54 : weak_ptr_factory_(this) {} | |
55 | |
56 GcmInternalsUIMessageHandler::~GcmInternalsUIMessageHandler() {} | |
57 | |
58 void GcmInternalsUIMessageHandler::ReturnResults( | |
59 Profile* profile, | |
60 gcm::GCMProfileService* profile_service, | |
61 const gcm::GCMClient::GCMStatistics* stats) const { | |
62 base::DictionaryValue results; | |
63 base::DictionaryValue* device_info = new base::DictionaryValue(); | |
64 results.Set("deviceInfo", device_info); | |
65 | |
66 device_info->SetString("userProfileName", profile->GetProfileName()); | |
jianli
2014/03/06 01:55:21
Personally I think this info is not needed.
juyik
2014/03/06 02:23:59
Done.
| |
67 device_info->SetBoolean("profileServiceCreated", profile_service != NULL); | |
68 if (profile) { | |
jianli
2014/03/06 01:55:21
profile cannot be null, right?
juyik
2014/03/06 02:23:59
Done.
| |
69 device_info->SetString("gcmEnabledState", | |
70 gcm::GCMProfileService::GetGCMEnabledStateString( | |
71 gcm::GCMProfileService::GetGCMEnabledState(profile))); | |
72 } | |
73 if (profile_service) { | |
74 device_info->SetBoolean("userSignedIn", profile_service->IsSignedIn()); | |
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%lx", 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 |