OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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/extensions/api/tab_capture/tab_capture_event_router.h" | |
6 | |
7 #include "base/string_number_conversions.h" | |
8 #include "base/string_piece.h" | |
9 #include "chrome/browser/profiles/profile.h" | |
10 #include "chrome/browser/profiles/profile_dependency_manager.h" | |
11 #include "chrome/browser/media/media_internals.h" | |
12 #include "chrome/browser/extensions/event_names.h" | |
13 #include "chrome/browser/extensions/event_router.h" | |
Alpha Left Google
2012/10/04 20:52:01
Reorder includes.
justinlin
2012/10/08 09:58:31
Done.
| |
14 #include "content/public/browser/browser_thread.h" | |
15 | |
16 namespace events = extensions::event_names; | |
17 using content::BrowserThread; | |
18 | |
19 namespace extensions { | |
20 | |
21 void RegisterAsMediaObserver(TabCaptureEventRouter* tabCaptureRouter) { | |
Aaron Boodman
2012/10/04 07:21:43
unix_hacker_style
Aaron Boodman
2012/10/04 07:21:43
Free module-local functions should be defined in a
justinlin
2012/10/08 09:58:31
Done.
justinlin
2012/10/08 09:58:31
Done.
| |
22 DCHECK(MediaInternals::GetInstance()); | |
23 MediaInternals::GetInstance()->AddObserver(tabCaptureRouter); | |
24 } | |
25 | |
26 // TODO(justinlin): This should maybe be filtered/partitioned by extension. | |
27 TabCaptureEventRouter::TabCaptureEventRouter(Profile* profile) | |
28 : profile_(profile) { | |
29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
30 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
31 base::Bind(&RegisterAsMediaObserver, this)); | |
Alpha Left Google
2012/10/04 20:52:01
indent by 4 spaces when you wrap a line
justinlin
2012/10/08 09:58:31
Done.
| |
32 } | |
33 | |
34 void UnRegisterAsMediaObserver(TabCaptureEventRouter* tabCaptureRouter) { | |
Alpha Left Google
2012/10/04 20:52:01
like aa@ suggested, hacker_style.
justinlin
2012/10/08 09:58:31
Done.
| |
35 if (MediaInternals::GetInstance()) | |
36 MediaInternals::GetInstance()->RemoveObserver(tabCaptureRouter); | |
37 } | |
38 | |
39 TabCaptureEventRouter::~TabCaptureEventRouter() { | |
40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
41 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
42 base::Bind(&UnRegisterAsMediaObserver, this)); | |
Aaron Boodman
2012/10/04 07:21:43
Are you sure about the lifetime issues here? It se
Alpha Left Google
2012/10/04 20:52:01
indent by 4 spaces when you wrap a line
justinlin
2012/10/08 09:58:31
Done.
justinlin
2012/10/11 07:30:14
Done, from what I can tell. Used a proxy class.
| |
43 } | |
44 | |
45 void TabCaptureEventRouter::OnRequestUpdate( | |
46 const content::MediaStreamDevice& device, | |
47 const content::MediaStreamRequest::RequestState new_state) { | |
48 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
49 base::Bind(&TabCaptureEventRouter::HandleRequestUpdate, | |
Alpha Left Google
2012/10/04 20:52:01
indent by 4 spaces when you wrap a line
justinlin
2012/10/08 09:58:31
Done.
| |
50 base::Unretained(this), device, new_state)); | |
Alpha Left Google
2012/10/04 20:52:01
indent by 4 spaces when you wrap a line
justinlin
2012/10/08 09:58:31
Done.
| |
51 } | |
52 | |
53 void TabCaptureEventRouter::HandleRequestUpdate( | |
54 const content::MediaStreamDevice& device, | |
55 const content::MediaStreamRequest::RequestState new_state) { | |
56 EventRouter* router = profile_ ? profile_->GetExtensionEventRouter() : NULL; | |
57 if (!router) | |
58 return; | |
59 | |
60 if (device.type != content::MEDIA_TAB_VIDEO_CAPTURE && | |
61 device.type != content::MEDIA_TAB_AUDIO_CAPTURE) | |
62 return; | |
63 | |
64 // Parse out the tab_id. | |
65 const size_t sep_pos = device.device_id.find(':'); | |
66 if (sep_pos == std::string::npos) | |
67 return; | |
68 | |
69 int tabId = -1; | |
Aaron Boodman
2012/10/04 07:21:43
unix_hacker_style!
justinlin
2012/10/08 09:58:31
Done.
| |
70 if (!base::StringToInt( | |
71 base::StringPiece(device.device_id.data(), sep_pos), &tabId)) | |
Alpha Left Google
2012/10/04 20:52:01
indentation is wrong
justinlin
2012/10/08 09:58:31
Done.
| |
72 return; | |
73 | |
74 tab_capture::TabCaptureState state = | |
75 tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_NONE; | |
76 switch (new_state) { | |
77 case content::MediaStreamRequest::STATE_REQUESTED: | |
78 state = tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_REQUESTED; | |
79 break; | |
80 case content::MediaStreamRequest::STATE_PENDING_APPROVAL: | |
81 state = tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_PENDING; | |
82 break; | |
83 case content::MediaStreamRequest::STATE_OPENING: | |
84 state = tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_ACTIVE; | |
85 break; | |
86 case content::MediaStreamRequest::STATE_DONE: | |
87 state = tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_STOPPED; | |
88 break; | |
89 case content::MediaStreamRequest::STATE_ERROR: | |
90 state = tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_ERROR; | |
91 break; | |
92 default: | |
93 // TODO(justinlin): Implement muted, cancelled state notification. | |
94 break; | |
95 } | |
96 | |
97 if (state == tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_NONE) { | |
98 VLOG(1) << "Observed an unhandled capture state."; | |
99 return; | |
100 } | |
101 | |
102 scoped_ptr<tab_capture::CaptureInfo> info(new tab_capture::CaptureInfo()); | |
103 info->tab_id = tabId; | |
104 info->status = state; | |
105 | |
106 scoped_ptr<base::ListValue> args(new ListValue()); | |
107 args->Append(info->ToValue().release()); | |
108 router->DispatchEventToRenderers(events::kOnTabCaptured, | |
109 args.Pass(), | |
110 profile_, | |
111 GURL()); | |
112 | |
113 if (state != tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_ERROR) | |
114 requests_[tabId] = state; | |
115 else | |
116 requests_.erase(tabId); | |
117 } | |
118 | |
119 std::map<int, tab_capture::TabCaptureState> | |
Alpha Left Google
2012/10/04 20:52:01
Use a typedef it will be look better.
justinlin
2012/10/08 09:58:31
Done.
| |
120 TabCaptureEventRouter::GetCapturedTabs() { | |
121 return requests_; | |
122 } | |
123 | |
124 } // namespace extensions | |
OLD | NEW |