1 // Copyright 2018 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 "chromecast/browser/cast_navigation_ui_data.h"
6 
7 #include "chromecast/browser/cast_session_id_map.h"
8 #include "content/public/browser/web_contents.h"
9 
10 namespace chromecast {
11 namespace shell {
12 namespace {
13 
14 const char kUserDataKey[] = "chromecast.shell.SessionIdUserData key";
15 
16 class SessionIdUserData : public base::SupportsUserData::Data {
17  public:
SessionIdUserData(const std::string & session_id)18   explicit SessionIdUserData(const std::string& session_id)
19       : session_id_(session_id) {}
20 
session_id() const21   const std::string& session_id() const { return session_id_; }
22 
23  private:
24   std::string session_id_;
25 };
26 
27 }  // namespace
28 
29 // static
SetSessionIdForWebContents(content::WebContents * web_contents,const std::string & session_id)30 void CastNavigationUIData::SetSessionIdForWebContents(
31     content::WebContents* web_contents,
32     const std::string& session_id) {
33   DCHECK(web_contents);
34   web_contents->SetUserData(kUserDataKey,
35                             std::make_unique<SessionIdUserData>(session_id));
36   CastSessionIdMap::SetSessionId(session_id, web_contents);
37 }
38 
39 // static
GetSessionIdForWebContents(content::WebContents * web_contents)40 std::string CastNavigationUIData::GetSessionIdForWebContents(
41     content::WebContents* web_contents) {
42   DCHECK(web_contents);
43   SessionIdUserData* data =
44       static_cast<SessionIdUserData*>(web_contents->GetUserData(kUserDataKey));
45   return data ? data->session_id() : "";
46 }
47 
CastNavigationUIData(const std::string & session_id)48 CastNavigationUIData::CastNavigationUIData(const std::string& session_id)
49     : session_id_(session_id) {}
50 
Clone()51 std::unique_ptr<content::NavigationUIData> CastNavigationUIData::Clone() {
52   return std::make_unique<CastNavigationUIData>(session_id_);
53 }
54 
55 }  // namespace shell
56 }  // namespace chromecast
57